示例#1
0
        static void PlayerTest()
        {
            Console.WriteLine("Creating players...");
            List <Player> players = new List <Player>
            {
                new HumanPlayer("John"),
                new SimpleAIPlayer("Computron 386")
            };

            Console.WriteLine("Creating talon...");
            Talon talon = new Talon();

            const int CARDS_TO_DRAW = 6;

            Console.WriteLine("Populating hands...");
            for (int i = 0; i < CARDS_TO_DRAW; i++)
            {
                foreach (Player player in players)
                {
                    player.TakeCard(talon.Draw());
                }
            }

            // Display contents of hands
            foreach (Player player in players)
            {
                Console.WriteLine("\n------\n{0} is holding:", player.Name);
                foreach (Card card in player.Hand)
                {
                    Console.WriteLine("\t{0}", card.ToString());
                }
            }
        }
示例#2
0
        /// <summary>
        /// Test defense logic by having the player defend against an entire talon.
        /// </summary>
        static void DefenseTest()
        {
            List <Card> hand  = new List <Card>(); // Player's hand
            Talon       talon = new Talon();       // Talon to play against

            Console.WriteLine("----------");
            Console.WriteLine("Populating player's hand...");
            for (int i = 1; i <= 6; i++)
            {
                hand.Add(talon.Draw());
            }

            // Main game loop
            do
            {
                Card attack = talon.Draw();
                Card defense;

                Console.WriteLine("\n===\nDefending against: " + attack);
                Console.WriteLine("Trump suit is " + talon.Trump.Suit);

                Console.WriteLine("Your hand: ");
                for (int index = 0; index < hand.Count; index++)
                {
                    Console.WriteLine("\t" + index + ": " + hand[index]);
                }

                Console.Write("Select a card to defend with: ");

                // Player selects a card
                defense = hand[int.Parse(Console.ReadLine())];

                if (defense.CanDefendAgainst(attack, talon.Trump.Suit))
                {
                    Console.WriteLine("+ Defended successfully. Defense card has been shed from your hand.");
                    hand.Remove(defense);
                }
                else
                {
                    Console.WriteLine("- Defense failed. You must take the attack card.");
                    hand.Add(attack);
                }

                Console.WriteLine("Cards remaining in talon: " + talon.Size);
            } while (talon.Size > 0 && hand.Count > 0);

            Console.WriteLine("-----------");
            if (hand.Count == 0)
            {
                Console.WriteLine("\n...Oh, you won! Good job!");
            }
            else
            {
                Console.WriteLine("\nTalon empty; test concluded.");
            }
        }
示例#3
0
        /// <summary>
        /// Advances the primary game loop.
        /// </summary>
        public void Continue()
        {
            // If there's only one player remaining, then the game is over.
            if (ActivePlayers == 1)
            {
                // Assuming we haven't already figured out who the fool is (no need to do it twice!)
                if (Fool == null)
                {
                    // Iterate through the players to figure out which one was the fool.
                    foreach (Player player in Players)
                    {
                        if (player.IsActive)
                        {
                            Fool = player;
                        }
                    }
                }

                OnEnd(new GameLogEventArgs(string.Format("The game has ended! {0} is the fool!", Fool.Name)));
            }
            // Otherwise, the game continues.
            else
            {
                // If the current bout is still ongoing...
                if (CurrentBout.Winner == null)
                {
                    // Advance the bout
                    CurrentBout.Continue();
                }
                else
                {
                    // All players replenish their hands, starting with the previous bout's attacker
                    Player replenishingPlayer = CurrentBout.Attacker;
                    for (int i = 0; !Talon.IsEmpty && i < ActivePlayers; i++)
                    {
                        int cardsToDraw = MINIMUM_HAND_SIZE - replenishingPlayer.Hand.Count;
                        if (cardsToDraw > 0)
                        {
                            List <Card> cardsToAdd = new List <Card>();
                            for (int j = 0; !Talon.IsEmpty && j < cardsToDraw; j++)
                            {
                                cardsToAdd.Add(Talon.Draw());
                            }
                            replenishingPlayer.TakeCards(cardsToAdd, "the talon", "draws");
                        }

                        // Replenishing moves counterclockwise (opposite to turn order)
                        replenishingPlayer = PlayerBefore(replenishingPlayer);
                    }

                    // Move to the next bout
                    NextBout();
                }
            }
        }
示例#4
0
        /// <summary>
        /// Instantiates a new Durak game.
        /// </summary>
        /// <param name="deck">The deck to use as the talon.</param>
        /// <param name="players">The players involved in the game.</param>
        /// <exception cref="ArgumentException">There aren't enough cards in the deck for each player to draw their opening hand.</exception>
        public Game(Deck deck, List <Player> players)
        {
            // Throw an exception if the deck is too small
            if (deck.Size < (players.Count * MINIMUM_HAND_SIZE))
            {
                throw new ArgumentException(string.Format("Deck only contains {0} cards! A game with {1} players needs a deck with at least {2} cards.",
                                                          deck.Size, players.Count, (players.Count * MINIMUM_HAND_SIZE)));
            }
            this.Talon    = new Talon(deck);
            this.Players  = players;
            ActivePlayers = 0;
            // Count the active players, just in case we were passed an inactive player (for some reason)
            foreach (Player player in Players)
            {
                if (player.IsActive)
                {
                    ActivePlayers++;
                }
                // Subscribe to their HandEmpty event
                // (it really wasn't worth making a method for this)
                player.HandEmpty += delegate(object sender, GameLogEventArgs e) { ActivePlayers--; };
            }

            // Players draw their opening hands
            foreach (Player player in Players)
            {
                List <Card> openingHand = new List <Card>();
                for (int i = 0; i < MINIMUM_HAND_SIZE; i++)
                {
                    openingHand.Add(Talon.Draw());
                }
                player.TakeCards(openingHand, "the talon", "draws");
            }

            // Set up the initial bout.
            NextBout();
        }