public Game(Player player1, Player dealer, Deck deck) { // Shuffle the deck for a new game deck.Shuffle(); // Deal first cards to player and dealer player1.Draw(deck, true); dealer.Draw(deck, false); player1.Draw(deck, true); dealer.Draw(deck, true); // Display what each player has, except the dealer's face down card Console.WriteLine("Dealer's hand: "); dealer.ShowHand(); Console.WriteLine("-------------------------------"); Console.WriteLine("Player's hand: "); player1.ShowHand(); // If player already has 21, no reason to allow them to draw if (player1.Score == 21) { // if the dealer also has 21, the outcome is a push if (dealer.Score == 21) { Console.WriteLine("Push\n"); } else { Console.WriteLine("\n~ Blackjack ~ \n"); Console.WriteLine("You won!"); player1.wins++; } // Both players return their cards to the deck player1.ReturnCards(deck); dealer.ReturnCards(deck); return; } bool continueGame; bool draw; do { continueGame = true; while (true) { Console.Write("\nDraw? (Y/N) "); ConsoleKeyInfo response = Console.ReadKey(); Console.WriteLine("\n"); if (response.Key == ConsoleKey.Y) { draw = true; break; } else if (response.Key == ConsoleKey.N) { draw = false; break; } else { Console.WriteLine("Invalid selection."); } } if (draw) { player1.Draw(deck, true); Console.WriteLine("Player's hand:"); player1.ShowHand(); if (player1.Score > 21) { dealer.wins++; Console.WriteLine("\nYou went over. Dealer wins!\n"); continueGame = false; break; } else if (player1.Score == 21) { Console.WriteLine("~ 21 ~"); break; } } else { break; } } while (true); // Player 2 turn if (continueGame) { Console.WriteLine("-------------------------------\n"); if (dealer.Score == 21 || dealer.Score > player1.Score || (dealer.Score == player1.Score && dealer.hand.Count < player1.hand.Count)) { dealer.wins++; Console.WriteLine("Dealer wins!\n"); Console.WriteLine("Dealer's hand:"); dealer.ShowHand("*"); } else { do { dealer.Draw(deck, true); if (dealer.Score > 21) { player1.wins++; Console.WriteLine("Dealer went over. You won!\n"); Console.WriteLine("Dealer's hand:"); dealer.ShowHand("*"); break; } else if ((dealer.Score > player1.Score) || (dealer.Score == player1.Score && dealer.hand.Count < player1.hand.Count)) { dealer.wins++; Console.WriteLine("Dealer wins!\n"); Console.WriteLine("Dealer's hand:"); dealer.ShowHand("*"); break; } } while (dealer.Score <= player1.Score); } } player1.ReturnCards(deck); dealer.ReturnCards(deck); }
public static void PlayGame() { // Instantiate using optional argument constructor Dealer dealer = new Dealer(); // Instantiate with named argument for player Player player = new Player(name: "Player"); // Instantiate the deck Deck deck = new Deck(); // Get player name Console.Write("Enter your name: "); player.Name = Console.ReadLine(); bool playing = true; while (playing) { // Update games played and reset lost booleans ConsoleKey key; player.GamesPlayed++; player.Lost = false; dealer.Lost = false; // Deal initial cards player.Draw(deck); dealer.Draw(deck); player.Draw(deck); dealer.Draw(deck); Message(player.ToString()); Message(dealer.ToString()); // Allow player the choice to hit or stay bool playerChoice = true; while (playerChoice) { Message("1: Hit \n2: Stay"); // Console.ReadKey(true) hides the key press in console key = Console.ReadKey(true).Key; // Player choice if (key == ConsoleKey.D1) { player.Draw(deck); } else if (key == ConsoleKey.D2) { playerChoice = false; } // Check if player busted but has an ace player.CheckForAce(); // Check if player busts, inverted to reduce nesting if (player.SumHand() <= 21) { continue; } player.Bust(); playerChoice = false; } // Dealer draws until hand value < 17 // No need to do this is player busts if (!player.Lost) { while (dealer.SumHand() < 17) { dealer.Draw(deck); // Check if dealer busted but has an ace dealer.CheckForAce(); // Check if dealer busts if (dealer.SumHand() <= 21) { continue; } dealer.Bust(); } } Message(player.ToString()); Message(dealer.ToString()); // Win/Lose Conditions if (player.Lost) { dealer.Winner(); } else if (dealer.Lost) { player.Winner(); } else if (player.SumHand() == dealer.SumHand()) { Message("Tie."); } else if (player.SumHand() > dealer.SumHand()) { player.Winner(); } else { dealer.Winner(); } // Discard hands foreach (Card card in player.Hand) { deck.Discard(card); } foreach (Card card in dealer.Hand) { deck.Discard(card); } player.Hand = new List <Card>(); dealer.Hand = new List <Card>(); // Play again or exit Message("Press any key to play again or press 0 to exit. \n"); key = Console.ReadKey(true).Key; Console.Clear(); if (key == ConsoleKey.D0) { playing = false; } } }