Example #1
0
        public void ShouldShuffledDeck()
        {
            // Arrange
            var deck = _deckService
                       .BuildStandardDeck()
                       .ConfigureAwait(false)
                       .GetAwaiter()
                       .GetResult();

            var shuffledDeck = _deckService
                               .BuildStandardDeck()
                               .ConfigureAwait(false)
                               .GetAwaiter()
                               .GetResult();

            // Act
            _deckService
            .ShuffleCards(shuffledDeck)
            .ConfigureAwait(false)
            .GetAwaiter()
            .GetResult();

            // Assert [It's possible that these cards maybe the same so this isn't a great test]
            Assert.NotEqual(deck.Cards[0], shuffledDeck.Cards[0]);
        }
Example #2
0
        public void ShuffleCardsTest()
        {
            List <Card> testCards = new List <Card>();

            testCards.Add(new Card(CardType.Diamond, new CardWorth(new KeyValuePair <string, int?>("Three", 3)), true));
            testCards.Add(new Card(CardType.Club, new CardWorth(new KeyValuePair <string, int?>("Four", 4)), true));
            testCards.Add(new Card(CardType.Spade, new CardWorth(new KeyValuePair <string, int?>("Five", 5)), true));
            testCards.Add(new Card(CardType.Heart, new CardWorth(new KeyValuePair <string, int?>("Six", 6)), true));
            testCards.Add(new Card(CardType.Spade, new CardWorth(new KeyValuePair <string, int?>("Seven", 7)), true));
            testCards.Add(new Card(CardType.Club, new CardWorth(new KeyValuePair <string, int?>("Eight", 8)), true));
            testCards.Add(new Card(CardType.Heart, new CardWorth(new KeyValuePair <string, int?>("Nine", 9)), true));
            testCards.Add(new Card(CardType.Diamond, new CardWorth(new KeyValuePair <string, int?>("Ten", 10)), true));
            testCards = DeckService.ShuffleCards(testCards);
            //There is a small chance this could fail as with any real shuffle could just return cards in original position.
            int matchCount = 0;

            if (testCards[0].CardType == CardType.Diamond && testCards[0].CardWorth.CardValue.Key == "Three")
            {
                matchCount++;
            }
            if (testCards[0].CardType == CardType.Club && testCards[0].CardWorth.CardValue.Key == "Four")
            {
                matchCount++;
            }
            if (testCards[0].CardType == CardType.Spade && testCards[0].CardWorth.CardValue.Key == "Five")
            {
                matchCount++;
            }
            if (testCards[0].CardType == CardType.Heart && testCards[0].CardWorth.CardValue.Key == "Six")
            {
                matchCount++;
            }
            if (testCards[0].CardType == CardType.Spade && testCards[0].CardWorth.CardValue.Key == "Seven")
            {
                matchCount++;
            }
            if (testCards[0].CardType == CardType.Club && testCards[0].CardWorth.CardValue.Key == "Eight")
            {
                matchCount++;
            }
            if (testCards[0].CardType == CardType.Heart && testCards[0].CardWorth.CardValue.Key == "Nine")
            {
                matchCount++;
            }
            if (testCards[0].CardType == CardType.Diamond && testCards[0].CardWorth.CardValue.Key == "Ten")
            {
                matchCount++;
            }
            //if we have 50% of the cards OR LESS in the same order, this is acceptable to me.
            Assert.IsTrue(matchCount <= 4);
        }
Example #3
0
        static void PlayGame(Dealer dealer, List <Player> players)
        {
            List <Card> cards = DeckService.GetCards();

            cards = DeckService.ShuffleCards(cards);
            //Dealer is dealt two cards, one is face up.
            Console.ForegroundColor = ConsoleColor.Cyan;
            DeckService.Deal(dealer, cards, 2, false); //Rule 3: Dealer dealt 2, one face down.
            string lineSeparator = "\n____________";

            Console.WriteLine($"{dealer.Name} score is: {DeckService.GetHandScore(dealer)}{lineSeparator}");
            bool isGameOver = false;

            while (!isGameOver)
            {
                int lowestTurnCount = Math.Min(dealer.TurnNumber, players.Min(p => p.TurnNumber));
                if (dealer.TurnNumber == lowestTurnCount && !dealer.IsBust && !dealer.IsStaying)
                {
                    //Dealer's turn
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    DeckService.PlayerTurnChange(dealer, cards, players.Max(p => p.CurrentScore));
                    Console.WriteLine($"{dealer.Name} score is: {DeckService.GetHandScore(dealer)}{lineSeparator}");
                }
                else if (players.Any(p => p.TurnNumber == lowestTurnCount && !p.IsStaying && !p.IsBust))
                {
                    //Player's turn
                    foreach (var player in players.Where(p => p.TurnNumber == lowestTurnCount && !p.IsStaying && !p.IsBust))
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        //One thing I wasn't sure about was the case where the dealer's total is lower but another players total is higher...
                        //I'm not sure if the player has to hit in that case.
                        DeckService.PlayerTurnChange(player, cards, dealer.CurrentScore); //Rule 4.
                        Console.WriteLine($"{player.Name} score is: {DeckService.GetHandScore(player)}{lineSeparator}");
                    }
                }
                else
                {
                    isGameOver = true;
                }
                if (!isGameOver && (dealer.IsBust) || (dealer.IsStaying && players.All(p => p.IsStaying)) || players.All(p => p.IsBust))
                {
                    isGameOver = true;
                }
            }
            TallyScores(dealer, players);
            PlayAgainPrompt(dealer, players);
        }