I have this card war game; the game starts with each player flipping over 1 card and whomever has the highest card wins. If there is a tie, the next three cards in the deck are played face down and the 4th card dealt face up, whomever has the highest card wins. The game is played until 1 player has all of the cards; however, I haven’t been able to finish it correctly, it crashes every time that I hit enter after the statement "lets flipping cards," and I haven’t been able to make the players to get the 4th card because when using a third variable r3
, it crashes likewise.
using System;
using System.Collections.Generic;
namespace PTCB12WARGAME2
{
class Card
{
public string Name;
public int Value;
public string Suit;
public override string ToString() { return string.Format("{0} of {1}", Name, Suit); }
}
class DeckOfCards
{
static void Main(string[] args)
{
List<Card> DeckOfCards = new List<Card>();
var names = new[] { "Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
var suits = new[] { "Hearts", "Clubs", "Diamonds", "Spades" };
var cards = new List<Card>();
// Populate our deck of cards with two nested loops
foreach (var suit in suits)
{
// We use a 'for' loop here instead of 'foreach' so we can use the index to
// populate the Value (which is always one more than the index of the name)
for (int i = 0; i < names.Length; i++)
{
cards.Add(new Card { Name = names[i], Value = i + 1, Suit = suit });
}
}
//Display deck of 52 cards
Console.WriteLine("Each player is flipping over 1 card and whomever has the highest card wins");
Console.ReadLine();
for (int i = 0; i < DeckOfCards.Count; i++)
{
Console.WriteLine(DeckOfCards[i]);
}
Console.WriteLine();
// create just ONCE the random object
Random random = new Random();
List<Card> deckOfPlayer1 = new List<Card>();
List<Card> deckOfPlayer2 = new List<Card>();
do
{
Console.WriteLine("Let's flip cards");
string userAnswer = Console.ReadLine();
if (userAnswer == "quit")
break;
// pick a random card for player 1
int r1;
r1 = random.Next(0, DeckOfCards.Count);
Card c1 = DeckOfCards[r1];
deckOfPlayer1.Add(c1);
// pick a random card for player 2
int r2;
r2 = random.Next(0, DeckOfCards.Count);
Card c2 = DeckOfCards[r2];
deckOfPlayer2.Add(c2);
Console.WriteLine("Player1 has: {0}", c1);
Console.WriteLine("Player2 has: {0}", c2);
// now compare the cards
if (c1.Value == c2.Value )
{
Console.WriteLine("Its a tie! Next three cards in deck are played face down, " +
"4th card face up, whoever has the highest card wins");
int r3;
r3 = random.Next(0, DeckOfCards.Count);
Card c3 = DeckOfCards[3];
deckOfPlayer1.Add(c3);
Console.WriteLine(deckOfPlayer1[3]);
Console.WriteLine(deckOfPlayer2[3]);
if (c1.Value > c2.Value && c2.Value != 1)
{
deckOfPlayer1.Add(c2);
deckOfPlayer2.Remove(c2);
Console.WriteLine("Player1 wins");
}
else
{
Console.WriteLine("Player2 wins");
deckOfPlayer2.Add(c1);
deckOfPlayer1.Remove(c1);
}
}
else if (c1.Value == 1 || (c1.Value > c2.Value && c2.Value != 1))
{
deckOfPlayer1.Add(c2);
deckOfPlayer2.Remove(c2);
Console.WriteLine("Player1 wins");
}
else
{
Console.WriteLine("Player2 wins");
deckOfPlayer2.Add(c1);
deckOfPlayer1.Remove(c1);
}
} while (deckOfPlayer1.Count < 52 || (deckOfPlayer2.Count < 52));
}
}
}