static void Main(string[] args) { //intro Console.WriteLine("Welcome to Blackjack where the goal is to get as close to 21 as possible."); Console.WriteLine("This game is meant to help teach basic stradegy"); Console.WriteLine("If a bad/ weird move is made it will tell you the recommended play"); Console.WriteLine("Good Luck and Have fun learning how to play Blackjack"); Console.WriteLine(); string response; //stops game if false Player player = new Player(); //create player Console.WriteLine("You will start off with {0} chips", player.ChipTotal); //player is sitting at table until he wants to end the session do { //create dealer and hand response = ""; player.Bet(); Hand H1 = new Hand(); player.AddHand(H1); Dealer dealer = new Dealer(); //dealer BlackJack if (dealer.BlackKJackCheck()) { player.Lose(); player.RemoveHands();// adjust player back to no hands and ends the round if (MoneyCheck(player.ChipTotal)) { break; } response = EndGameCheck(); continue; } for (int i = 0; i < player.handCount; i++) //iterate throught player hands { if (i > 0) { Console.WriteLine("Your second hand has {0}", player.hands[i].GetPlayerHandSum()); } //Player BlackJack if (player.hands[i].BlackJackCheck()) { player.BlackJack(i + 1); if (player.handCount == 1 || (player.handCount == 2 && i == 1 && player.hands[0].BlackJackCheck() && player.hands[1].BlackJackCheck())) { Console.WriteLine("You have {0} chips left", player.ChipTotal); response = EndGameCheck(); continue; } else { continue; } } string handNum = ""; if (player.handCount > 1) { if (i == 0) { handNum = "First hand ... "; } else if (i == 1) { handNum = "Second hand ... "; } } if (player.handCount > 1) { Console.WriteLine("{0}You can hit, double, split, stay", handNum); } else { Console.WriteLine("You can hit, double, split, stay, surrender"); } string action = Console.ReadLine(); Stradegy(player.hands[i].GetPlayerHandSum(), dealer.card2, action, player.hands[i].SoftCheck(), player.hands[i].CheckSplit()); player.hands[i].Action(action); if (action == "surrender") { player.Surrender(); Console.WriteLine("You have {0} chips left", player.ChipTotal); response = EndGameCheck(); continue; } //split hand if (action == "split") { Hand H2 = player.hands[i].Split(player.hands[i].CheckSplit()); player.AddHand(H2); i--; continue; } //double bet if (action == "double") { player.DoubleBet(i + 1); } //if player bust exit / continue if (player.handCount > 1 && player.hands[i].GetPlayerHandSum() == 0) { if (i == 0) { continue; } else if (i == 1) { break; } } else if (player.hands[i].GetPlayerHandSum() == 0) { player.Lose(); Console.WriteLine("You have {0} chips left", player.ChipTotal); response = EndGameCheck(); continue; } } // check to see if dealer needs to play if (response == "") { Console.WriteLine(); Console.WriteLine("The dealer was hiding a {0}", dealer.card1); Console.WriteLine("The dealer has {0}", dealer.GetDealerHandSum()); dealer.Hit(); Console.WriteLine(); // compare dealer and player hands for (int i = 0; i < player.handCount; i++) { if (dealer.GetDealerHandSum() > player.hands[i].GetPlayerHandSum()) { player.Lose(i + 1); } else if (dealer.GetDealerHandSum() < player.hands[i].GetPlayerHandSum()) { player.Win(i + 1); } else { player.Draw(i + 1); } } // player chips if (MoneyCheck(player.ChipTotal)) { break; } response = EndGameCheck(); } player.RemoveHands();// adjust player back to no hands and ends the round }while (response == "yes"); }
static void Main(string[] args) { var keepPlaying = true; while (keepPlaying) { Deck _deck = new Deck(); Player _player = new Player(); Dealer _dealer = new Dealer(); Hand playerHand = new Hand(); Hand dealerHand = new Hand(); playerHand.AddCard(_deck.DealCard()); dealerHand.AddCard(_deck.DealCard()); playerHand.AddCard(_deck.DealCard()); dealerHand.AddCard(_deck.DealCard()); _player.AddHand(playerHand); _dealer.AddHand(dealerHand); while (playerHand.TotalValue() <= 21) { Console.WriteLine(); Console.WriteLine("---------------------"); Console.WriteLine($"Hand Value: {playerHand.TotalValue()}"); playerHand.ShowHand(); Console.WriteLine("---------------------"); Console.WriteLine(); Console.Write("(H)it or (S)tand: "); var answer = Console.ReadLine(); if (answer == "H") { playerHand.AddCard(_deck.DealCard()); } else { break; } } Console.WriteLine(); Console.WriteLine("---------------------"); Console.WriteLine($"Hand Value: {playerHand.TotalValue()}"); playerHand.ShowHand(); Console.WriteLine("---------------------"); Console.WriteLine(); while (dealerHand.TotalValue() < 17) { dealerHand.AddCard(_deck.DealCard()); } Console.WriteLine(); Console.WriteLine("---------------------"); Console.WriteLine($"Dealer's Hand Value: {dealerHand.TotalValue()}"); dealerHand.ShowHand(); Console.WriteLine("---------------------"); Console.WriteLine(); if (playerHand.TotalValue() > 21) { Console.WriteLine("Dealer Wins!"); } else if (dealerHand.TotalValue() > 21) { Console.WriteLine("Player Wins!"); } else if (dealerHand.TotalValue() >= playerHand.TotalValue()) { Console.WriteLine("Dealer Wins!"); } else { Console.WriteLine("Player Wins!"); } Console.Write("Play again? (Y/N): "); var playAgainString = Console.ReadLine(); keepPlaying = (playAgainString == "Y" || playAgainString == "y"); } Console.WriteLine(); Console.WriteLine("Thanks for playing"); }