コード例 #1
0
        /// <summary>
        /// Draw a card from each player's pile, determine who wins the round
        /// </summary>
        public void Deal()
        {
            TotalRounds++;

            // each player puts a card onto the table, face up
            Card player1Card = Player1.Deal(true);

            if (player1Card != null) // should only happen when user is out of cards
            {
                GameTable.AddCard(1, player1Card);
            }
            Card player2Card = Player2.Deal(true);

            if (player2Card != null)
            {
                GameTable.AddCard(2, player2Card);
            }

            // which player's card wins?
            switch (Rank(player1Card, player2Card))
            {
            case CardRanking.Equal:

                // if one player has won (i.e. out of cards), end game now, otherwise go to war
                if (!CheckWinner())
                {
                    this._Report(player1Card.ToString() + " and " + player2Card.ToString() + ". THIS MEANS WAR!");
                    TotalWars++;

                    //  If one player only has one card left, don't draw it, save until next round
                    if (Player1.Hand.Count > 1)
                    {
                        player1Card = Player1.Deal(false);     // draw face-down card
                        GameTable.AddCard(1, player1Card);
                    }
                    if (Player2.Hand.Count > 1)
                    {
                        player2Card = Player2.Deal(false);
                        GameTable.AddCard(2, player2Card);
                    }

                    Deal();
                }
                break;

            case CardRanking.More:
                this._Report(player1Card.ToString() + " beats " + player2Card.ToString() + "." + Environment.NewLine + Player1.Name + " wins this round.");
                GameTable.Award(Player1);
                CheckWinner();
                break;

            case CardRanking.Less:
                this._Report(player2Card.ToString() + " beats " + player1Card.ToString() + "." + Environment.NewLine + Player2.Name + " wins this round.");
                GameTable.Award(Player2);
                CheckWinner();
                break;
            }

            // periodically re-shuffle each players' hands so we don't get caught in infinite loops
            _reshuffleCounter++;
            if (_reshuffleCounter >= ReShuffleLimit)
            {
                Player1.Hand      = CardDeck.Shuffle(Player1.Hand);
                Player2.Hand      = CardDeck.Shuffle(Player2.Hand);
                _reshuffleCounter = 0;
            }
        }