コード例 #1
0
        public void BlackJackGameBothBlackJackPushNoOneWins()
        {
            var deckOfCards = new CardGameLibrary.DeckOfCards(CardGameLibrary.Constants.BLACKJACK);

            deckOfCards.Shuffle(CardGameLibrary.Constants.NCARDS);
            var blackJackGame = new CardGames.BlackJackGame(deckOfCards);

            var playerOfCard = new CardGameLibrary.PlayerOfCard();

            playerOfCard.PlayerNumber = 1;
            var blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Spades, 1);

            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Hearts, 10);
            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackGame.PlayerOfCardsInGame.Add(playerOfCard);

            playerOfCard = new CardGameLibrary.PlayerOfCard();
            playerOfCard.PlayerNumber = 2;
            playerOfCard.IsDealer     = true;
            blackJackCard             = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Clubs, 1);
            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Diamonds, 10);
            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackGame.PlayerOfCardsInGame.Add(playerOfCard);

            var evaluatedWinners = blackJackGame.EvaluateWinners();

            Assert.AreEqual(evaluatedWinners.Count, 1);
            Assert.AreEqual(evaluatedWinners[0].playerNumber, 1);
            Assert.AreEqual(evaluatedWinners[0].whoWon, CardGameLibrary.DealerOrPlayerOrNone.None);
        }
コード例 #2
0
 //i.Multiple players are dealt a single card from a shuffled set of card
 public void Play(int numPlayers)
 {
     _deckOfCards.Shuffle(CardGameLibrary.Constants.NCARDS);
     for (int i = 0; i < numPlayers; i++)
     {
         CardGameLibrary.PlayerOfCard playerOfCards = new CardGameLibrary.PlayerOfCard();
         playerOfCards.PlayerNumber = i + 1;
         playerOfCards.PlayerCards.Add(GetCard());
         PlayerOfCardsInGame.Add(playerOfCards);
     }
 }
コード例 #3
0
        public void HighCardGameTwoPlayersWinner()
        {
            var deckOfCards = new CardGameLibrary.DeckOfCards(CardGameLibrary.Constants.HIGHCARD);

            deckOfCards.Shuffle(CardGameLibrary.Constants.NCARDS);
            var highCardGame = new CardGames.HighCardGame(deckOfCards);

            var playerOfCard1 = new CardGameLibrary.PlayerOfCard();

            playerOfCard1.PlayerNumber = 1;
            playerOfCard1.PlayerCards.Add(highCardGame.GetCard());
            highCardGame.PlayerOfCardsInGame.Add(playerOfCard1);

            var playerOfCard2 = new CardGameLibrary.PlayerOfCard();

            playerOfCard2.PlayerNumber = 2;
            playerOfCard2.PlayerCards.Add(highCardGame.GetCard());
            highCardGame.PlayerOfCardsInGame.Add(playerOfCard2);

            var evaluatedWinners = highCardGame.EvaluateWinners();

            Assert.IsTrue(evaluatedWinners.Count <= 2);

            for (int i = 0; i < highCardGame.PlayerOfCardsInGame.Count; i++)
            {
                var player = highCardGame.PlayerOfCardsInGame[i];
                Assert.AreEqual(player.PlayerCards.Count, 1);
                if (i > 0)
                {
                    var previousPlayer = highCardGame.PlayerOfCardsInGame[i - 1];
                    if (previousPlayer.PlayerCards[0].GetRank() > player.PlayerCards[0].GetRank())
                    {
                        Assert.AreEqual(evaluatedWinners.Count, 1);
                        Assert.AreEqual(evaluatedWinners[0], 1);
                    }
                    else if (previousPlayer.PlayerCards[0].GetRank() < player.PlayerCards[0].GetRank())
                    {
                        Assert.AreEqual(evaluatedWinners.Count, 1);
                        Assert.AreEqual(evaluatedWinners[0], 2);
                    }
                    else
                    {
                        Assert.AreEqual(evaluatedWinners.Count, 2);
                        Assert.IsTrue(evaluatedWinners.Exists(e => e == 1));
                        Assert.IsTrue(evaluatedWinners.Exists(e => e == 2));
                    }
                }
            }
        }
コード例 #4
0
        //ii.Highest Card wins
        public Generic.List <int> EvaluateWinners()
        {
            Generic.List <int> winningPlayerNumbers = new Generic.List <int>();
            Generic.List <CardGameLibrary.PlayerOfCard> highestPlayersOfCards = new Generic.List <CardGameLibrary.PlayerOfCard>();
            CardGameLibrary.PlayerOfCard highestPlayerOfCards = null;

            //Initially, assign first player as highest card winner
            if (PlayerOfCardsInGame.Count > 0)
            {
                highestPlayerOfCards = PlayerOfCardsInGame[0];
                highestPlayersOfCards.Add(highestPlayerOfCards);
            }

            for (int i = 1; i < PlayerOfCardsInGame.Count; i++)
            {
                //If previous player(s) temporarily assigned
                //as highest card winner is outranked, remove
                //and add new player as currently
                //assigned highest card winner
                if (PlayerOfCardsInGame[i].PlayerScore >
                    highestPlayerOfCards.PlayerScore)
                {
                    highestPlayersOfCards.RemoveAll
                        (match => match.PlayerScore
                        == highestPlayerOfCards.PlayerScore);
                    highestPlayerOfCards = PlayerOfCardsInGame[i];
                    highestPlayersOfCards.Add(highestPlayerOfCards);
                }
                //Multiple players having cards of same rank can be co-winners
                else if (PlayerOfCardsInGame[i].PlayerScore ==
                         highestPlayerOfCards.PlayerScore)
                {
                    highestPlayerOfCards = PlayerOfCardsInGame[i];
                    highestPlayersOfCards.Add(highestPlayerOfCards);
                }
            }

            //This should be a list of at most Count == 4 for co-winner use-cases
            foreach (CardGameLibrary.PlayerOfCard playerOfCards in highestPlayersOfCards)
            {
                winningPlayerNumbers.Add(playerOfCards.PlayerNumber);
            }

            return(winningPlayerNumbers);
        }
コード例 #5
0
        public void BlackJackGamePlayerWithThreeAcesInHandNotBustWinner()
        {
            var deckOfCards = new CardGameLibrary.DeckOfCards(CardGameLibrary.Constants.BLACKJACK);

            deckOfCards.Shuffle(CardGameLibrary.Constants.NCARDS);
            var blackJackGame = new CardGames.BlackJackGame(deckOfCards);

            var playerOfCard = new CardGameLibrary.PlayerOfCard();

            playerOfCard.PlayerNumber = 1;
            var blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Spades, 1);

            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Hearts, 1);
            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Clubs, 1);
            playerOfCard.PlayerCards.Add(blackJackCard);
            playerOfCard.HasSoftScore = true;
            blackJackGame.PlayerOfCardsInGame.Add(playerOfCard);

            playerOfCard = new CardGameLibrary.PlayerOfCard();
            playerOfCard.PlayerNumber = 2;
            playerOfCard.IsDealer     = true;
            blackJackCard             = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Diamonds, 2);
            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Clubs, 8);
            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackCard = new CardGameLibrary.BlackJackCard(CardGameLibrary.Suit.Hearts, 2);
            playerOfCard.PlayerCards.Add(blackJackCard);
            blackJackGame.PlayerOfCardsInGame.Add(playerOfCard);

            var evaluatedWinners = blackJackGame.EvaluateWinners();

            Assert.AreEqual(evaluatedWinners.Count, 1);
            Assert.AreEqual(evaluatedWinners[0].playerNumber, 1);
            Assert.AreEqual(evaluatedWinners[0].whoWon, CardGameLibrary.DealerOrPlayerOrNone.Player);
        }
コード例 #6
0
        public void HighCardGameMultiplePlayersWinner()
        {
            var deckOfCards = new CardGameLibrary.DeckOfCards(CardGameLibrary.Constants.HIGHCARD);

            deckOfCards.Shuffle(CardGameLibrary.Constants.NCARDS);
            var highCardGame = new CardGames.HighCardGame(deckOfCards);

            for (int i = 1; i <= CardGameLibrary.Constants.NCARDS / 2; i++)
            {
                var playerOfCard = new CardGameLibrary.PlayerOfCard();
                playerOfCard.PlayerNumber = i;
                playerOfCard.PlayerCards.Add(highCardGame.GetCard());
                highCardGame.PlayerOfCardsInGame.Add(playerOfCard);
            }

            var evaluatedWinners = highCardGame.EvaluateWinners();

            Assert.IsTrue(evaluatedWinners.Count <= 4);

            var winners     = new System.Collections.Generic.List <int>();
            int winningRank = 0;

            for (int i = 0; i < highCardGame.PlayerOfCardsInGame.Count; i++)
            {
                var player = highCardGame.PlayerOfCardsInGame[i];
                Assert.AreEqual(player.PlayerCards.Count, 1);
                if (i > 0)
                {
                    var previousPlayer = highCardGame.PlayerOfCardsInGame[i - 1];
                    if (previousPlayer.PlayerCards[0].GetRank() > player.PlayerCards[0].GetRank() &&
                        winningRank < previousPlayer.PlayerCards[0].GetRank())
                    {
                        winningRank = previousPlayer.PlayerCards[0].GetRank();
                    }
                    else if (previousPlayer.PlayerCards[0].GetRank() < player.PlayerCards[0].GetRank() &&
                             winningRank < player.PlayerCards[0].GetRank())
                    {
                        winningRank = player.PlayerCards[0].GetRank();
                    }
                    else if (winningRank < player.PlayerCards[0].GetRank())
                    {
                        winningRank = player.PlayerCards[0].GetRank();
                    }
                }
            }

            foreach (var player in highCardGame.PlayerOfCardsInGame)
            {
                if (player.PlayerCards[0].GetRank() == winningRank)
                {
                    winners.Add(player.PlayerNumber);
                }
            }

            //CollectionAssert.AreEqual
            Assert.AreEqual(evaluatedWinners.Count, winners.Count);

            foreach (var winner in winners)
            {
                Assert.IsTrue(evaluatedWinners.Exists(e => e == winner));
            }
        }