I have this code. There are 52 cards in a deck. 2-10, Jack, Queen, King, Ace (Ace is the highest card) It is a 2 player game, each player starts with 26 cards (half the deck) 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 one of the players has all of the 52 cards.
However, I haven't been able to split the 26 cards to each one of the players at the beginning of the game as expected.
Another issue is to finish the game just when one of the players have all the 52 cards. Could some body grant a solution ,please?
Thanks
Alexander Sanchez
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>()
{
new Card { Name="Ace", Value=1, Suit="Heart"},
new Card{Name="two",Value=2,Suit="Heart"},
new Card{Name="three",Value=3,Suit="Heart"},
new Card{Name="four",Value=4,Suit="Heart"},
new Card { Name="five", Value=5, Suit="Heart"},
new Card{Name="six",Value=6,Suit="Heart"},
new Card{Name="seven",Value=7,Suit="Heart"},
new Card{Name="eight",Value=8,Suit="Heart"},
new Card { Name="Nine", Value=9, Suit="heart"},
new Card{Name="ten",Value=10,Suit="Heart"},
new Card{Name="jack",Value=11,Suit="Heart"},
new Card{Name="queen",Value=12,Suit="Heart"},
new Card { Name="king", Value=13, Suit="heart"},
new Card { Name="Ace", Value=1, Suit="Spades"},
new Card{Name="two",Value=2,Suit="Spades"},
new Card{Name="three",Value=3,Suit="Spades"},
new Card{Name="four",Value=4,Suit="Spades"},
new Card { Name="five", Value=5, Suit="Spades"},
new Card{Name="six",Value=6,Suit="Spades"},
new Card{Name="seven",Value=7,Suit="Spades"},
new Card{Name="eight",Value=8,Suit="Spades"},
new Card { Name="Nine", Value=9, Suit="Spades"},
new Card{Name="ten",Value=10,Suit="Spades"},
new Card{Name="jack",Value=11,Suit="Spades"},
new Card{Name="queen",Value=12,Suit="Spades"},
new Card { Name="king", Value=13, Suit="Spades"},
new Card { Name="Ace", Value=1, Suit="Diamonds"},
new Card{Name="two",Value=2,Suit="Diamonds"},
new Card{Name="three",Value=3,Suit="Diamonds"},
new Card{Name="four",Value=4,Suit="Diamonds"},
new Card { Name="five", Value=5, Suit="Diamonds"},
new Card{Name="six",Value=6,Suit="Diamonds"},
new Card{Name="seven",Value=7,Suit="Diamonds"},
new Card{Name="eight",Value=8,Suit="Diamonds"},
new Card { Name="Nine", Value=9, Suit="Diamonds"},
new Card{Name="ten",Value=10,Suit="Diamonds"},
new Card{Name="jack",Value=11,Suit="Diamonds"},
new Card{Name="queen",Value=12,Suit="Diamonds"},
new Card { Name="king", Value=13, Suit="Diamonds"},
new Card { Name="Ace", Value=1, Suit="Clubs"},
new Card{Name="two",Value=2,Suit="Clubs"},
new Card{Name="three",Value=3,Suit="Clubs"},
new Card{Name="four",Value=4,Suit="Clubs"},
new Card { Name="five", Value=5, Suit="Clubs"},
new Card{Name="six",Value=6,Suit="Clubs"},
new Card{Name="seven",Value=7,Suit="Clubs"},
new Card{Name="eight",Value=8,Suit="Clubs"},
new Card { Name="Nine", Value=9, Suit="Clubs"},
new Card{Name="ten",Value=10,Suit="Clubs"},
new Card{Name="jack",Value=11,Suit="Clubs"},
new Card{Name="queen",Value=12,Suit="Clubs"},
new Card { Name="king", Value=13, Suit="Clubs"},
};
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]);
}
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;
int r1;
r1 = random.Next(0, DeckOfCards.Count);
Card c1 = DeckOfCards[r1];
deckOfPlayer1.Add(c1);
DeckOfCards.RemoveAt(r1);
int r2;
r2 = random.Next(0, DeckOfCards.Count);
Card c2 = DeckOfCards[r2];
deckOfPlayer2.Add(c2);
DeckOfCards.RemoveAt(r2);
Console.WriteLine("Player1 has: {0}", c1);
Console.WriteLine("Player2 has: {0}", c2);
Console.WriteLine("Player1 has: {0}", r1);
Console.WriteLine("Player2 has: {0}", r2);
if (c1.Value == c2.Value && deckOfPlayer1.Count > 4 || (deckOfPlayer2.Count > 4))
{
Console.WriteLine("Its a tie! Next three cards in deck are played face down, " +
"4th card face up, whoever has the highest card wins");
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");
Console.WriteLine("Player1 has: {0}", r1);
Console.WriteLine("Player2 has: {0}", r2);
}
else
{
Console.WriteLine("Player2 wins");
deckOfPlayer2.Add(c1);
deckOfPlayer1.Remove(c1);
Console.WriteLine("Player1 has: {0}", r1);
Console.WriteLine("Player2 has: {0}", r2);
}
}
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));
}
}
}
What I have tried:
I have tried to shorten my code a lot by creating an array of names and an array of suits, as I can simply use nested loops to populate your deck of cards list, but it hasn't work, either. Thanks
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>();
foreach(var suit in suits)
{
for(int i = 0; i < names.Length; i++)
{
cards.Add(new Card { Name = names[i], Value = i + 1, Suit = suit });
}
}