using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class CardGame { static void Main(string[] args) { Deck d = new Deck(); //total cards in the deck d.display(); //creating two players Player p1 = new Player(); Player p2 = new Player(); //shuffling the cards in the deck, we can do any number of shuffles d.shuffle(); //displaying the cards again after shuffling d.display(); //distributing the cards among hte two players for (int i = 0; i < d.cards.Count; i++) { if ((i % 2) == 0) { p1.addCard(d.cards[i]); } else { p2.addCard(d.cards[i]); } } Console.WriteLine("*************************cards from player 1***********************\n"); p1.showCards(); Console.WriteLine("*************************cards from player 2***********************\n"); p2.showCards(); Console.ReadLine(); Console.WriteLine("************************Game starts now********************\n"); int c = p1.getCount(); List cardsPlayed = new List(); List cardstoRemoveP1 = new List(); List cardstoRemoveP2 = new List(); for (int i = 0; i < c; i++) { Console.WriteLine("Card of player one :" + p1.cards[i].Value + ", " + p1.cards[i].suit); cardsPlayed.Add(p1.cards[i]); cardstoRemoveP1.Add(p1.cards[i]); if (cardsPlayed.Count > 1) { if (cardsPlayed[cardsPlayed.Count - 1].Value == cardsPlayed[cardsPlayed.Count - 2].Value) { for (int j = 0; j < cardsPlayed.Count; j++) { p1.addCard(cardsPlayed[j]); } break; } } Console.WriteLine("Card of player two :" + p2.cards[i].Value + ", " + p2.cards[i].suit); cardsPlayed.Add(p2.cards[i]); cardstoRemoveP2.Add(p2.cards[i]); if (cardsPlayed.Count > 1) { if (cardsPlayed[cardsPlayed.Count - 1].Value == cardsPlayed[cardsPlayed.Count - 2].Value) { for (int j = 0; j < cardsPlayed.Count; j++) { p2.addCard(cardsPlayed[j]); } break; } } } for (int i = 0; i < cardstoRemoveP1.Count; i++) { p1.cards.Remove(cardstoRemoveP1[i]); } for (int i = 0; i < cardstoRemoveP2.Count; i++) { p2.cards.Remove(cardstoRemoveP2[i]); } Console.WriteLine("no of cards for player 1 at the end " + p1.getCount()); Console.WriteLine("no of cards for player 2 at the end " + p2.getCount()); Console.ReadLine(); } } class PlayingCard { private string cardValue; public string Value { get { return cardValue; } set { cardValue = value; } } public string suit; public enum suitEnum { SPADES, CLUBS, HEARTS, DIAMONDS } public int Count { get { return Count; } set { Count = value; } } public PlayingCard(string cValue, string cSuit) { cardValue = cValue; suit = cSuit; } } class Deck { public List cards = new List(); public Deck() { string rank = "", suit = ""; for (int i = 0; i < 4; i++) { switch (i) { case 0: suit = "Spades"; break; case 1: suit = "Hearts"; break; case 2: suit = "Diamonds"; break; case 3: suit = "Clubs"; break; } for (int j = 0; j < 13; j++) { switch (j) { case 0: rank = "1"; break; case 1: rank = "2"; break; case 2: rank = "3"; break; case 3: rank = "4"; break; case 4: rank = "5"; break; case 5: rank = "6"; break; case 6: rank = "7"; break; case 7: rank = "8"; break; case 8: rank = "9"; break; case 9: rank = "10"; break; case 10: rank = "11"; break; case 11: rank = "12"; break; case 12: rank = "13"; break; } cards.Add(new PlayingCard(rank, suit)); } } } public void shuffle() { List shufCards = new List(52); Random rand = new Random(); for (int i = 0; i < cards.Count; i++) { int c = rand.Next(cards.Count); shufCards.Add(cards[c]); cards.RemoveAt(c); } cards = shufCards; } public void display() { foreach (PlayingCard c in cards) { Console.WriteLine(c.Value + ", " + c.suit + " \n"); } } } class Player { public List cards = new List(); public Player() { } public void addCard(PlayingCard pc) { cards.Add(pc); } public int getCount() { int count = 0; foreach (PlayingCard c in cards) { count++; } return count; } public void showCards() { string playerCards = ""; foreach (PlayingCard c in cards) playerCards += c.suit + ", " + c.Value + "\n"; Console.WriteLine(playerCards); } public void remove(PlayingCard c) { cards.Remove(c); } public void clear() { cards.Clear(); } } }