コード例 #1
0
        static void Main(string[] args)
        {
            PlayerGroup playerGroup = new PlayerGroup();
            Dealer      dealer      = new Dealer();
            House       house       = new House();
            bool        gameWon     = false;
            bool        keepPlaying = true;
            int         numberOfPlayers;
            string      userInput = "";

            Console.WriteLine("Welcome to Blackjack!" + "\r\n\r\n\r\n");

            // Get number of players.
            numberOfPlayers = playerGroup.SetNumberOfPlayers();

            // Set players names.
            playerGroup.SetPlayersNames(numberOfPlayers);

            while (keepPlaying == true)
            {
                while (gameWon == false)
                {
                    // Create and shuffle deck.
                    Deck deck = new Deck();
                    deck.CardDeck = deck.BuildCardDeck();
                    deck.CardDeck = deck.ShuffleDeck(deck.CardDeck);

                    Console.Write("\r\n\r\n" + "Press enter to deal hands...");
                    Console.ReadLine();

                    // Deal first round of cards.
                    dealer.DealFirstRound(deck, playerGroup, house);
                    house.HandTotal = house.CountCards();
                    house.SetHandAsString();

                    // Print the houses hand.
                    Console.WriteLine("\r\n\r\n" + house.HandAsString);

                    // Print each of the players hands.
                    foreach (Player player in playerGroup.PlayerList)
                    {
                        player.HandTotal = player.CountCards();
                        player.SetHandAsString();

                        Console.WriteLine("\r\n" + player.HandAsString);
                    }

                    Console.WriteLine();

                    // If any players have blackjack add them to the winning players list.
                    foreach (Player player in playerGroup.PlayerList)
                    {
                        if (player.HandTotal == 21)
                        {
                            playerGroup.WinningPlayers.Add(player);
                        }
                    }

                    // If anyone has blackjack.
                    if (playerGroup.WinningPlayers.Count > 0 || house.HandTotal == 21)
                    {
                        // Blackjack for player/s and house.
                        if (playerGroup.WinningPlayers.Count > 0 && house.HandTotal == 21)
                        {
                            foreach (Player player in playerGroup.WinningPlayers)
                            {
                                Console.WriteLine("\r\n" + player.Name + " Tie...");
                            }

                            Console.WriteLine("\r\n" + house.Name + " Tie...");
                            gameWon = true;
                        }

                        // Blackjack for house.
                        if (playerGroup.WinningPlayers.Count == 0 && house.HandTotal == 21)
                        {
                            Console.WriteLine("\r\n" + house.Name + " Wins - Blackjack");
                            gameWon = true;
                        }

                        // Blackjack for player/s.
                        if (playerGroup.WinningPlayers.Count > 0 && house.HandTotal != 21)
                        {
                            if (playerGroup.WinningPlayers.Count > 1)
                            {
                                foreach (Player player in playerGroup.WinningPlayers)
                                {
                                    Console.WriteLine("\r\n" + player.Name + " Tie...");
                                }
                            }
                            else
                            {
                                Console.WriteLine("\r\n" + playerGroup.WinningPlayers[0].Name + " Wins - Blackjack!");
                            }

                            gameWon = true;
                        }
                    }

                    // If someone hasnt got blackjack then continue - necessary as not exiting while loop
                    if (gameWon == false)
                    {
                        // Loop through the list of players.
                        foreach (Player player in playerGroup.PlayerList)
                        {
                            // Count current players hand.
                            player.HandTotal = player.CountCards();
                            player.SetHandAsString();

                            // If player hasnt bust.
                            if (player.Bust == false)
                            {
                                Console.WriteLine("\r\n" + player.HandAsString);

                                string playerHit = "Y";

                                // Loop while player hasnt won, bust or stayed.
                                while (playerHit != "N" && gameWon == false && player.Bust == false)
                                {
                                    Console.Write("Would you like another card (y/n)? ");
                                    playerHit = Console.ReadLine().ToUpper();

                                    // Validation for user input.
                                    while (playerHit != "Y" && playerHit != "N")
                                    {
                                        Console.WriteLine("Incorect input...");
                                        Console.Write("\r\n" + "Would you like another card(y/n)?: ");
                                        playerHit = Console.ReadLine().ToUpper();
                                    }

                                    // If player selects hit, deal them a card and remove it from the deck.
                                    if (playerHit == "Y")
                                    {
                                        dealer.DealPlayerCard(player, deck);
                                        player.HandTotal = player.CountCards();
                                        player.SetHandAsString();

                                        Console.WriteLine("\r\n" + player.HandAsString);
                                    }
                                }
                            }
                        }

                        // 'Flip' the houses first card.
                        house.firstCardFlipped = true;
                        house.SetHandAsString();
                        Console.WriteLine("\r\n" + house.HandAsString);

                        // Determine the highest score
                        playerGroup.GetHighestHand(playerGroup);

                        if (house.HandTotal > playerGroup.HighestHand)
                        {
                            Console.WriteLine("\r\n" + house.Name + " Wins!");
                            gameWon = true;
                        }
                        else
                        {
                            // Loop and keep hiting house hand if it hasnt won, bust or gone over 17.
                            while (gameWon == false && house.HandTotal <= 16 && house.HandTotal < playerGroup.HighestHand)
                            {
                                Random rnd = new Random();
                                int    randomCard;

                                Console.WriteLine("\r\n" + "Press enter to deal the House a card...");
                                Console.ReadLine();

                                randomCard = rnd.Next(deck.CardDeck.Count);
                                house.Hand.Add(deck.CardDeck[randomCard]);
                                deck.CardDeck.Remove(deck.CardDeck[randomCard]);

                                house.HandTotal = house.CountCards();
                                house.SetHandAsString();

                                Console.WriteLine("\r\n" + house.HandAsString);
                            }

                            // If house has bust all players who havent, win.
                            if (house.Bust == true)
                            {
                                foreach (Player player in playerGroup.PlayerList)
                                {
                                    if (player.Bust == false)
                                    {
                                        Console.WriteLine("\r\n" + player.Name + " Wins!");
                                    }
                                }

                                gameWon = true;
                            }
                            // Else house must have 17-21 so check and display win/draw
                            else
                            {
                                if (house.HandTotal > playerGroup.HighestHand)
                                {
                                    Console.WriteLine("\r\n" + house.Name + " Wins!");
                                    gameWon = true;
                                }
                                else if (house.HandTotal < playerGroup.HighestHand)
                                {
                                    foreach (Player player in playerGroup.PlayerList)
                                    {
                                        if (player.HandTotal > house.HandTotal && player.Bust == false)
                                        {
                                            Console.WriteLine("\r\n" + player.Name + " Wins!");
                                        }
                                    }

                                    gameWon = true;
                                }
                                else
                                {
                                    Console.WriteLine("\r\n" + house.Name + " Tie...");

                                    foreach (Player player in playerGroup.PlayerList)
                                    {
                                        if (player.HandTotal == house.HandTotal)
                                        {
                                            Console.WriteLine("\r\n" + player.Name + " Tie...");
                                        }
                                    }

                                    gameWon = true;
                                }
                            }
                        }
                    }

                    Console.Write("\r\n\r\n" + "Game over! Play again (y/n)? ");
                    userInput = Console.ReadLine().ToUpper();

                    // Validate user input.
                    while (userInput != "Y" && userInput != "N")
                    {
                        Console.Write("Incorect input...");
                        Console.Write("\r\n\r\n" + "Play again (y/n)? ");
                        userInput = Console.ReadLine().ToUpper();
                    }

                    if (userInput == "N")
                    {
                        keepPlaying = false;
                    }
                    else
                    {
                        house.ResetHand();

                        foreach (Player player in playerGroup.PlayerList)
                        {
                            player.ResetHand();
                        }

                        playerGroup.ResetPlayerGroup();

                        keepPlaying            = true;
                        gameWon                = false;
                        house.firstCardFlipped = false;
                    }
                }
            }
        }
コード例 #2
0
        // Deal first round of cards.
        public void DealFirstRound(Deck deck, PlayerGroup playerGroup, House house)
        {
            // Give each player a card and remove card from deck.
            foreach (Player player in playerGroup.PlayerList)
            {
                randomCard = random.Next(deck.CardDeck.Count);
                player.Hand.Add(deck.CardDeck[randomCard]);
                deck.CardDeck.Remove(deck.CardDeck[randomCard]);
            }

            // Give player second card.
            foreach (Player player in playerGroup.PlayerList)
            {
                randomCard = random.Next(deck.CardDeck.Count);

                // If player has been delt two Aces.
                if (player.Hand[0].Name == "A" && deck.CardDeck[randomCard].Name == "A")
                {
                    Console.Write("\r\n\r\n" + player.Name + ": You have been delt two Aces. Would you like the first to count as a 1 or 11? ");
                    userInput = Console.ReadLine();

                    while (userInput != "1" && userInput != "11")
                    {
                        Console.Write("\r\n" + "Incorect input. Enter 1 or 11: ");
                        userInput = Console.ReadLine();
                    }

                    player.Hand[0].Rank = int.Parse(userInput);

                    Console.Write("\r\n\r\n" + player.Name + ": Would you like the second Ace to count as a 1 or 11? ");
                    userInput = Console.ReadLine();

                    while (userInput != "1" && userInput != "11")
                    {
                        Console.Write("\r\n" + "Incorect input. Enter 1 or 11: ");
                        userInput = Console.ReadLine();
                    }

                    deck.CardDeck[randomCard].Rank = int.Parse(userInput);
                    player.Hand.Add(deck.CardDeck[randomCard]);
                    deck.CardDeck.Remove(deck.CardDeck[randomCard]);
                }
                // If the player has been delt an Ace for thier first card and they dont have blackjack.
                else if (player.Hand[0].Name == "A" && deck.CardDeck[randomCard].Name != "A" && deck.CardDeck[randomCard].Rank != 10)
                {
                    Console.Write("\r\n\r\n" + player.Name + ": You have been delt an ace and a " + deck.CardDeck[randomCard].Name +
                                  ". Would you like the ace to count as a 1 or 11? ");
                    userInput = Console.ReadLine();

                    while (userInput != "1" && userInput != "11")
                    {
                        Console.Write("\r\n" + "Incorect input. Enter 1 or 11: ");
                        userInput = Console.ReadLine();
                    }

                    player.Hand[0].Rank = int.Parse(userInput);
                    player.Hand.Add(deck.CardDeck[randomCard]);
                    deck.CardDeck.Remove(deck.CardDeck[randomCard]);
                }
                // If the player has been delt an Ace for thier second card and they dont have blackjack.
                else if (player.Hand[0].Name != "A" && player.Hand[0].Rank != 10 && deck.CardDeck[randomCard].Name == "A")
                {
                    Console.Write("\r\n\r\n" + player.Name + ": You have been delt a " + player.Hand[0].Name +
                                  " and an Ace. Would you like the ace to count as a 1 or 11? ");
                    userInput = Console.ReadLine();

                    while (userInput != "1" && userInput != "11")
                    {
                        Console.Write("\r\n" + "Incorect input. Enter 1 or 11: ");
                        userInput = Console.ReadLine();
                    }

                    deck.CardDeck[randomCard].Rank = int.Parse(userInput);
                    player.Hand.Add(deck.CardDeck[randomCard]);
                    deck.CardDeck.Remove(deck.CardDeck[randomCard]);
                }
                else
                {
                    player.Hand.Add(deck.CardDeck[randomCard]);
                    deck.CardDeck.Remove(deck.CardDeck[randomCard]);
                }
            }

            // Deal the house two cards.
            for (int i = 0; i < 2; i++)
            {
                randomCard = random.Next(deck.CardDeck.Count);
                house.Hand.Add(deck.CardDeck[randomCard]);
                deck.CardDeck.Remove(deck.CardDeck[randomCard]);
            }
        }