Example #1
0
        /// <summary>
        /// Begins the game.
        /// </summary>
        public override void Start()
        {
            // The eventual winner
            WhistPlayer winner = this.Players[0];

            do
            {
                // Reset player scores
                foreach (WhistPlayer player in this.Players)
                {
                    player.Score = 0;
                }

                // Play a single hand of Whist
                Whist.Whist hand = new Whist.Whist();
                hand.AddPlayer(this.Players);
                hand.CardsInHand = this.CardsInHand;
                hand.Start();

                // Detect all losers
                List <WhistPlayer> losers = this.DetectLosers(hand.Players);

                if (losers.Count == 1)
                {
                    // If there is a clear loser, remove him
                    this.RemoveLoser(losers[0]);
                }
                else if (losers.Count > 1)
                {
                    // Else pick one at random
                    Random rand  = new Random();
                    int    index = rand.Next(losers.Count);
                    this.RemoveLoser(losers[index]);
                }

                // Check for winners
                if (this.CardsInHand == 1 && this.Players.Count > 1)
                {
                    // If this was the last round, pick the winner
                    winner = hand.Winner;
                    break;
                }
                else if (this.Players.Count == 1)
                {
                    // If all other players have been eliminated, break with the winner
                    winner = this.Players[0];
                    break;
                }

                // Else continue with the next round
                // Decrement the number of cards
                this.CardsInHand--;
            }while (this.Players.Count > 1);

            // Assign the winner
            this.Winner = winner;
        }
Example #2
0
        /// <summary>
        /// Runs a game of Whist
        /// </summary>
        /// <param name="players"> The amount of players in the game. </param>
        public static void RunWhist(int players)
        {
            Whist whist = new Whist();
            List <WhistPlayer> whistPlayers = new List <WhistPlayer>();

            for (int i = 0; i < players; i++)
            {
                whistPlayers.Add(new ConsolePlayer());
                whist.AddPlayer(whistPlayers[i]);
            }

            whist.Start();
        }
Example #3
0
        public void DealingTest()
        {
            // arrange
            Whist.Whist game = new Whist.Whist();
            for (int i = 0; i < 5; i++)
            {
                game.AddPlayer(new ConsolePlayer());
            }

            var expected = new Card(Value.Nine, Suit.Spades);  // The last Players Last Card

            // act
            game.Deal(false, 7); // The deck is not shuffled so we can track where the cards go

            // assert
            Assert.AreEqual(expected, game.Players[4].Hand[6]);
        }
Example #4
0
        public void DealingTest()
        {
            // arrange
            Whist.Whist game = new Whist.Whist();
            for (int i = 0; i < 5; i++)
            {
                game.AddPlayer(new ConsolePlayer());
            }

            var expected = new Card(Value.Nine, Suit.Spades);  // The last Players Last Card

            // act
            game.Deal(false, 7); // The deck is not shuffled so we can track where the cards go

            // assert
            Assert.AreEqual(expected, game.Players[4].Hand[6]);
        }