Created
November 28, 2022 15:11
-
-
Save mrgonegamer/573612ae83d6aae37ec88ddc61966b5b to your computer and use it in GitHub Desktop.
c# War Card Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace WarCardGame | |
{ | |
public partial class Form1 : Form | |
{ | |
static Size size = new System.Drawing.Size(500, 500); | |
static Size size2 = new System.Drawing.Size(45, 75); | |
static Size size3 = new System.Drawing.Size(70, 75); | |
static Bitmap temp2 = new Bitmap("C:\\GameBoard\\board.jpg"); | |
static Bitmap backofcard = new Bitmap("C:\\PlayerCards\\backofcard.png"); | |
static Bitmap backofcardwar = new Bitmap("C:\\PlayerCards\\backofcardwar.png"); | |
Bitmap temp = new Bitmap(temp2, size); | |
Bitmap back = new Bitmap(backofcard, size2); | |
Bitmap war = new Bitmap(backofcardwar, size3); | |
Cards player; | |
Cards AI; | |
struct Cards | |
{ | |
public string suit; | |
public int cardValue; | |
public Bitmap card; | |
} | |
List<Cards> DeckOfCards = new List<Cards>(); | |
List<Cards> War = new List<Cards>(); | |
Stack<Cards> playerOne = new Stack<Cards>(); | |
Stack<Cards> playerAI = new Stack<Cards>(); | |
Stack<Cards> playerOneDiscard = new Stack<Cards>(); | |
Stack<Cards> playerAIDiscard = new Stack<Cards>(); | |
Stack<Cards> playerOneWar = new Stack<Cards>(); | |
Stack<Cards> playerAIWar = new Stack<Cards>(); | |
public Form1() | |
{ | |
InitializeComponent(); | |
changesize(515, 540); | |
loadCardImg(); | |
dealCards(); | |
pbtheirhand.Hide(); | |
pbyourhand.Hide(); | |
pbyourdiscard.Hide(); | |
pbtheirdiscard.Hide(); | |
pbyourwar.Hide(); | |
pbaiwar.Hide(); | |
} | |
private void changesize(int width, int height) | |
{ | |
this.Size = new Size(width, height); | |
} | |
private void Form1_Paint(object sender, PaintEventArgs e) | |
{ | |
textBox1.Text = (playerOne.Count.ToString()); | |
textBox1.Location = new Point(240, 465); | |
textBox2.Text = (playerAI.Count.ToString()); | |
textBox2.Location = new Point(240, 15); | |
Size pbsize = new Size(45, 75); | |
Size pbwar = new Size(70, 75); | |
Graphics graphics = e.Graphics; | |
graphics.DrawImage(temp, new Point(0, 0)); | |
pbyourhand.Image = player.card; | |
pbyourhand.Size = pbsize; | |
pbyourhand.Location = new Point(230, 275); | |
pbtheirhand.Image = AI.card; | |
pbtheirhand.Size = pbsize; | |
pbtheirhand.Location = new Point(230, 150); | |
if (playerAIDiscard.Count != 0) | |
{ | |
pbtheirdiscard.Image = playerAIDiscard.Peek().card; | |
pbtheirdiscard.Size = pbsize; | |
pbtheirdiscard.Location = new Point(330, 150); | |
} | |
if (playerOneDiscard.Count != 0) | |
{ | |
pbyourdiscard.Image = playerOneDiscard.Peek().card; | |
pbyourdiscard.Size = pbsize; | |
pbyourdiscard.Location = new Point(130, 275); | |
} | |
if (playerAIWar.Count != 0) | |
{ | |
pbaiwar.Image = war; | |
pbaiwar.Size = pbwar; | |
pbaiwar.Location = new Point(130, 150); | |
} | |
if (playerOneWar.Count != 0) | |
{ | |
pbyourwar.Image = war; | |
pbyourwar.Size = pbwar; | |
pbyourwar.Location = new Point(330, 275); | |
} | |
pb2.Image = back; | |
pb2.Size = pbsize; | |
pb2.Location = new Point(230, 50); | |
pb1.Image = back; | |
pb1.Size = pbsize; | |
pb1.Location = new Point(230, 375); | |
} | |
private void dealCards() | |
{ | |
shuffleCards(); | |
string one = "one"; | |
for (int i = 0; i <= DeckOfCards.Count - 1; i++) | |
{ | |
if (one == "one") | |
{ | |
playerOne.Push(DeckOfCards[i]); | |
one = "two"; | |
} | |
else if (one == "two") | |
{ | |
playerAI.Push(DeckOfCards[i]); | |
one = "one"; | |
} | |
} | |
} | |
private static Random rng = new Random(); | |
private void shuffleCards() | |
{ | |
int n = DeckOfCards.Count; | |
while (n > 1) | |
{ | |
n--; | |
int k = rng.Next(n + 1); | |
Cards value = DeckOfCards[k]; | |
DeckOfCards[k] = DeckOfCards[n]; | |
DeckOfCards[n] = value; | |
} | |
} | |
private void loadCardImg() | |
{ | |
StreamReader _reader = new StreamReader("cards.txt"); | |
while (true) | |
{ | |
Cards cards = new Cards(); | |
string sreader = _reader.ReadLine(); | |
if (sreader == null) | |
{ | |
break; | |
} | |
string[] namearray1 = sreader.Split(' '); | |
cards.card = new Bitmap((Bitmap)Image.FromFile(namearray1[2]), new Size(45, 75)); | |
cards.suit = namearray1[1]; | |
cards.cardValue = int.Parse(namearray1[0]); | |
DeckOfCards.Add(cards); | |
} | |
} | |
private void isOutOfCards() | |
{ | |
if (playerOne.Count == 0 && playerOneDiscard.Count == 0 && playerOneWar.Count == 0) | |
{ | |
MessageBox.Show("You Lost!"); | |
} | |
else if (playerAI.Count == 0 && playerAIDiscard.Count == 0 && playerAIWar.Count == 0) | |
{ | |
MessageBox.Show("You Won!"); | |
} | |
else if (playerOne.Count == 0 || playerAI.Count == 0) | |
{ | |
for (int i = 1; i <= playerOneDiscard.Count; i++) | |
{ | |
playerOne.Push(playerOneDiscard.Pop()); | |
i--; | |
} | |
for (int i = 1; i <= playerOneWar.Count; i++) | |
{ | |
playerOne.Push(playerOneWar.Pop()); | |
i--; | |
} | |
for (int i = 1; i <= playerAIDiscard.Count; i++) | |
{ | |
playerAI.Push(playerAIDiscard.Pop()); | |
i--; | |
} | |
for (int i = 1; i <= playerAIWar.Count; i++) | |
{ | |
playerAI.Push(playerAIWar.Pop()); | |
i--; | |
} | |
} | |
} | |
private void thisIsWar() | |
{ | |
bool war = true; | |
playerAIWar.Push(AI); | |
playerOneWar.Push(player); | |
MessageBox.Show("THIS MEANS WAR!!!!!"); | |
for (int i = 0; i <= 2; i++) | |
{ | |
playerOneWar.Push(playerOne.Pop()); | |
playerAIWar.Push(playerAI.Pop()); | |
} | |
pbaiwar.Show(); | |
pbyourwar.Show(); | |
isOutOfCards(); | |
player = playerOne.Pop(); | |
AI = playerAI.Pop(); | |
pbyourhand.Show(); | |
pbtheirhand.Show(); | |
if (player.cardValue > AI.cardValue) | |
{ | |
for (int i = 0; i <= 2; i++) | |
{ | |
isOutOfCards(); | |
playerOneDiscard.Push(playerAIWar.Pop()); | |
playerOneDiscard.Push(playerOneWar.Pop()); | |
} | |
pbyourdiscard.Show(); | |
} | |
else if (player.cardValue < AI.cardValue) | |
{ | |
for (int i = 0; i <= 2; i++) | |
{ | |
isOutOfCards(); | |
playerAIDiscard.Push(playerAIWar.Pop()); | |
playerAIDiscard.Push(playerOneWar.Pop()); | |
} | |
pbtheirdiscard.Show(); | |
} | |
pbaiwar.Hide(); | |
pbyourwar.Hide(); | |
} | |
private void pb1_Click(object sender, EventArgs e) | |
{ | |
if (player.cardValue > AI.cardValue) | |
{ | |
playerOneDiscard.Push(player); | |
playerOneDiscard.Push(AI); | |
pbyourdiscard.Show(); | |
} | |
else if (player.cardValue < AI.cardValue) | |
{ | |
playerAIDiscard.Push(AI); | |
playerAIDiscard.Push(player); | |
pbtheirdiscard.Show(); | |
} | |
if (player.cardValue == AI.cardValue && player.cardValue != 0) | |
{ | |
thisIsWar(); | |
} | |
player = playerOne.Pop(); | |
AI = playerAI.Pop(); | |
pbyourhand.Show(); | |
pbtheirhand.Show(); | |
if (playerOne.Count == 0) | |
{ | |
isOutOfCards(); | |
} | |
if (playerAI.Count == 0) | |
{ | |
isOutOfCards(); | |
} | |
this.Invalidate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment