simulate the card game war in csharp

Here's an example implementation of the card game War in C#:

main.cs
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Initialize the deck of cards
        List<Card> deck = new List<Card>();
        foreach (Suit suit in Enum.GetValues(typeof(Suit)))
        {
            foreach (Rank rank in Enum.GetValues(typeof(Rank)))
            {
                deck.Add(new Card(rank, suit));
            }
        }

        // Shuffle the deck
        Shuffle(deck);

        // Divide the deck between two players
        Queue<Card> player1Deck = new Queue<Card>(deck.GetRange(0, 26));
        Queue<Card> player2Deck = new Queue<Card>(deck.GetRange(26, 26));

        // Play the game
        int round = 1;
        while (player1Deck.Count > 0 && player2Deck.Count > 0)
        {
            Console.WriteLine("Round {0}:", round);
            Console.WriteLine("Player 1 cards: {0}", player1Deck.Count);
            Console.WriteLine("Player 2 cards: {0}", player2Deck.Count);

            // Draw a card from each player's deck
            Card player1Card = player1Deck.Dequeue();
            Card player2Card = player2Deck.Dequeue();

            Console.WriteLine("Player 1 plays: {0}", player1Card);
            Console.WriteLine("Player 2 plays: {0}", player2Card);

            // Compare the cards
            int result = player1Card.CompareTo(player2Card);
            if (result > 0)
            {
                // Player 1 wins the round
                Console.WriteLine("Player 1 wins the round!");
                player1Deck.Enqueue(player1Card);
                player1Deck.Enqueue(player2Card);
            }
            else if (result < 0)
            {
                // Player 2 wins the round
                Console.WriteLine("Player 2 wins the round!");
                player2Deck.Enqueue(player2Card);
                player2Deck.Enqueue(player1Card);
            }
            else
            {
                // War!
                Console.WriteLine("War!");

                // Draw three cards from each player's deck
                List<Card> player1WarCards = new List<Card>();
                List<Card> player2WarCards = new List<Card>();
                for (int i = 0; i < 3; i++)
                {
                    if (player1Deck.Count > 0)
                    {
                        player1WarCards.Add(player1Deck.Dequeue());
                    }
                    if (player2Deck.Count > 0)
                    {
                        player2WarCards.Add(player2Deck.Dequeue());
                    }
                }

                // Skip the round if a player doesn't have enough cards for war
                if (player1WarCards.Count < 3 || player2WarCards.Count < 3)
                {
                    Console.WriteLine("Not enough cards for war!");
                    continue;
                }

                // Add the face-down card and the face-up card to each player's war cards
                player1WarCards.Insert(0, player1Card);
                player2WarCards.Insert(0, player2Card);

                // Compare the war cards
                result = player1WarCards[3].CompareTo(player2WarCards[3]);
                if (result > 0)
                {
                    // Player 1 wins the war
                    Console.WriteLine("Player 1 wins the war!");
                    foreach (Card card in player1WarCards)
                    {
                        player1Deck.Enqueue(card);
                    }
                    foreach (Card card in player2WarCards)
                    {
                        player1Deck.Enqueue(card);
                    }
                }
                else if (result < 0)
                {
                    // Player 2 wins the war
                    Console.WriteLine("Player 2 wins the war!");
                    foreach (Card card in player2WarCards)
                    {
                        player2Deck.Enqueue(card);
                    }
                    foreach (Card card in player1WarCards)
                    {
                        player2Deck.Enqueue(card);
                    }
                }
                else
                {
                    // Another war!
                    Console.WriteLine("Another war!");
                    player1Deck.Enqueue(player1Card);
                    player2Deck.Enqueue(player2Card);
                    foreach (Card card in player1WarCards)
                    {
                        player1Deck.Enqueue(card);
                    }
                    foreach (Card card in player2WarCards)
                    {
                        player2Deck.Enqueue(card);
                    }
                }
            }

            round++;
        }

        // Determine the winner of the game
        if (player1Deck.Count == 0)
        {
            Console.WriteLine("Player 2 wins the game!");
        }
        else
        {
            Console.WriteLine("Player 1 wins the game!");
        }
    }

    static void Shuffle<T>(IList<T> list)
    {
        Random rng = new Random();
        int n = list.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            T value = list[k];
            list[k] = list[n];
            list[n] = value;
        }
    }
}

enum Suit
{
    Club,
    Diamond,
    Heart,
    Spade
}

enum Rank
{
    Ace = 1,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King
}

class Card : IComparable<Card>
{
    public Rank Rank { get; }
    public Suit Suit { get; }

    public Card(Rank rank, Suit suit)
    {
        Rank = rank;
        Suit = suit;
    }

    public override string ToString()
    {
        return string.Format("{0} of {1}s", Rank, Suit);
    }

    public int CompareTo(Card other)
    {
        int rankComparison = Rank.CompareTo(other.Rank);
        if (rankComparison != 0)
        {
            return rankComparison;
        }
        else
        {
            return Suit.CompareTo(other.Suit);
        }
    }
}
6107 chars
215 lines

This implementation uses object-oriented programming to represent Cards and Suits as enums, and the game logic is implemented in a while-loop that iteratively draws cards from each player's deck and compares them. In the event of a tie, a "war" occurs where three cards are drawn from each player's deck and the fourth card drawn determines the winner.

gistlibby LogSnag