public bool PlayHand(DeckOfCards deck, bool busted) { string answer; HandValue = CalculateHandValue(Hand, NumOfCards); while (busted == false) { Console.WriteLine("\nHand:"); ShowHand(); answer = ""; bool validInput = false; while (validInput == false) { try { while (answer != "yes" && answer != "y" && answer != "no" && answer != "n") { Console.Write("\nWould you like to take a hit?\n(Currently sitting at {0})\n(y/n): ", HandValue); answer = Console.ReadLine(); } validInput = true; } catch (FormatException e) { Console.WriteLine(e.Message + "\nInvalid input, please try again.\n"); } catch (Exception e) { Console.WriteLine(e.Message + "\nUnknown Error.\n"); } } if (answer == "yes" || answer == "y" || answer == "Yes") { Hand[NumOfCards] = deck.DealCard(); NumOfCards++; HandValue = CalculateHandValue(Hand, NumOfCards); if (HandValue > 21) busted = true; } else break; } if (busted == true) { Console.WriteLine("\nHand:"); ShowHand(); Console.WriteLine("Total is: {0}\nBusted.", HandValue); return true; } else return false; }
public void Play() { _discardPile = new List<Card>(); // create a player _player = new Player(); // create a dealer _dealer = new Dealer(); // create some cards in a deck of cards _deck = new DeckOfCards(); _deck.Shuffle(); while (true) { List<Card> discards; discards = _player.ClearYourHand(); _discardPile.AddRange(discards); discards = _dealer.ClearYourHand(); _discardPile.AddRange(discards); // deal the cards Card cardToDeal; cardToDeal = GetTopCardFromDeck(); _player.PutInHand(cardToDeal); cardToDeal = GetTopCardFromDeck(); _dealer.PutInHand(cardToDeal); cardToDeal = GetTopCardFromDeck(); _player.PutInHand(cardToDeal); cardToDeal = GetTopCardFromDeck(); _dealer.PutInHand(cardToDeal); // print the state of the game for the user PrintTheGame(); // hit the player until they don't wanna be hit no more while (_player.HasNotGoneOver21() && _player.WantsAnotherCard()) { cardToDeal = GetTopCardFromDeck(); _player.PutInHand(cardToDeal); PrintTheGame(); } // stop if the player busts if (_player.HasGoneOver21()) { Console.WriteLine("You have lost, human."); Console.WriteLine("It is a sad day for your species."); } else { _dealer.ShowAllOfYourCards(); // hit the dealer until the computer is over 17 while (_dealer.HasNotGoneOver21() && _dealer.WantsAnotherCard()) { cardToDeal = GetTopCardFromDeck(); _dealer.PutInHand(cardToDeal); } PrintTheGame(); // figure out the winner if (_dealer.HasGoneOver21()) { Console.WriteLine("You win, this time, human."); } else if (_player.TotalOfHand() >= _dealer.TotalOfHand()) { Console.WriteLine("You win, this time, human."); } else { Console.WriteLine("You have lost, puny human."); Console.WriteLine("Your species will never win."); } } // see if the player wants to go another hand Console.WriteLine("Press any key but N and Enter to play another hand."); string input = Console.ReadLine().ToLower(); if (input == "n") { break; } } }
// methods public bool PlayHand(DeckOfCards deck, bool busted) { HandValue = CalculateHandValue(Hand, NumOfCards); ShowHand(); Console.WriteLine("Currently at: {0}", HandValue); while (busted == false && HandValue < 17) { Console.WriteLine("Must take a hit until 17 or greater.\n"); Hand[NumOfCards] = deck.DealCard(); NumOfCards++; HandValue = CalculateHandValue(Hand, NumOfCards); if (HandValue > 21) busted = true; ShowHand(); Console.WriteLine("\nCurrently at: {0}", HandValue); } if (busted == true) { Console.WriteLine("\nHand:"); ShowHand(); Console.WriteLine("Total is: {0}\nBusted.", HandValue); return true; } else return false; }