Ejemplo n.º 1
0
        public void GetHandScoreTest()
        {
            List <Card> testCards = new List <Card>();

            testCards.Add(new Card(CardType.Diamond, new CardWorth(new KeyValuePair <string, int?>("Five", 5)), true));
            testCards.Add(new Card(CardType.Club, new CardWorth(new KeyValuePair <string, int?>("Six", 6)), true));
            player1.Hand.AddRange(testCards);
            Assert.IsTrue(DeckService.GetHandScore(player1) == 11);
            Assert.IsTrue(player1.CurrentScore == 11);
        }
Ejemplo n.º 2
0
        public void AceAsElevenTest()
        {
            List <Card> testCards = new List <Card>();

            testCards.Add(new Card(CardType.Diamond, new CardWorth(new KeyValuePair <string, int?>("King", 10)), true));
            testCards.Add(new Card(CardType.Club, new CardWorth(new KeyValuePair <string, int?>("Ace", null)), true));
            player1.Hand.AddRange(testCards);
            int result = DeckService.GetHandScore(player1);

            Assert.IsTrue(result == 21);
            Assert.IsTrue(testCards.Any(c => c.CardWorth.CardValue.Value.Value == 11));
            //Asserting when our Ace should be an eleven and not a one.
        }
Ejemplo n.º 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);
        }