private void PlayHand(Player player) { var play = player.Play(player.Hand, _dealer.Hand.First()); if (play == PlayAction.Stay) { return; } if (play == PlayAction.Hit) { var c = Deal(); var bust = player.TakeCard(c); if (!bust) { PlayHand(player); } } else if (play == PlayAction.Double) { if (player.Hand.Count() != 2) { throw new Exception("only double on first"); } player.TakeCard(Deal()); } }
bool PlayHand(Player player, Card dealerTopCard, Shoe shoe) { PlayAction play = player.Play(player.Hand, dealerTopCard); if (play == PlayAction.Stay) { return true; } if (play == PlayAction.Hit) { Card card = shoe.GetNextCard(); bool bust = player.TakeCard(card); if (bust) return false; return PlayHand(player, dealerTopCard, shoe); } if (play == PlayAction.Double) { if (player.Hand.Count() != 2) throw new Exception("only double on first"); var bust = player.TakeCard(shoe.GetNextCard()); if (bust) return false; return true; } throw new InvalidOperationException("shouldn't be here"); }
static void Main(string[] args) { Boolean playAgain = true; IParticipant player = new Player(); Dealer dealer = new Dealer(); Console.Write("Enter Player name: "); player.Name = Console.ReadLine(); Console.WriteLine(); while (playAgain) { Boolean bust = false; dealer.DealerShow(); Console.WriteLine(); int playerSum = player.Play(); Console.WriteLine(); int dealerSum = 0; if (playerSum <= 21) { dealerSum = dealer.Play(); Console.WriteLine(); } else { Console.WriteLine("Bust! You lose."); bust = true; } if (!bust) { if (dealerSum < playerSum || dealerSum > 21) { if (playerSum == 21) { Console.WriteLine("Blackjack!"); } Console.WriteLine("Congrats! You beat the dealer."); } else if (dealerSum == playerSum) { Console.WriteLine("You neither win nor lose."); } else { Console.WriteLine("Sorry, you lose."); } } Console.WriteLine(); Console.WriteLine("Hit y to play again. Any other key to exit."); String play = Console.ReadLine(); if (play.Equals("y") || play.Equals("Y")) playAgain = true; else playAgain = false; Console.Clear(); } }