/// <summary> /// Check if Can Split /// </summary> /// <returns>Return True if player Can split</returns> public static bool CanSplit() { BlackjackHand playerHandNum = PlayerHands[0]; if (PlayerHands[0].Count <= 2 && !playerHandNum.IsStanding) { if ((playerHandNum.GetCard(0).FaceValue == playerHandNum.GetCard(1).FaceValue) || ((int)playerHandNum.GetCard(0).FaceValue >= 10 && (int)playerHandNum.GetCard(1).FaceValue >= 10) && playerHandNum.IsStanding == false) { return(true); } else { return(false); } } else { return(false); } }
static bool PlayBlackjack() { var deck = new Deck(); deck.Shuffle(); var dealerHand = new BlackjackHand(); var userHand = new BlackjackHand(); dealerHand.AddCard(deck.DealCard()); dealerHand.AddCard(deck.DealCard()); Console.WriteLine("\nDealer's face card is " + dealerHand.GetCard(0)); userHand.AddCard(deck.DealCard()); userHand.AddCard(deck.DealCard()); Console.Write("\nUser has" + " " + userHand.GetBlackjackValue() + "\n"); userHand.DisplayCards(); if (userHand.GetBlackjackValue() == 21) { Console.WriteLine("\nCongratulations you win!"); userHand.Clear(); return(true); } else if (dealerHand.GetBlackjackValue() == 21) { Console.WriteLine("\n You Lose"); userHand.Clear(); return(false); } while (true) { if (userHand.GetBlackjackValue() < 21) { int choice = GetChoice(); switch (choice) { case 1: { userHand.AddCard(deck.DealCard()); userHand.DisplayCards(); break; } case 2: { Console.Write("\n" + userHand.GetBlackjackValue()); break; } default: { Console.WriteLine("Please enter a valid choice"); break; } } } else { Console.WriteLine("You lose!"); Console.WriteLine(dealerHand.GetBlackjackValue()); return(false); } // Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21"); while (dealerHand.GetBlackjackValue() <= 16) { dealerHand.AddCard(deck.DealCard()); // Console.Write("\nDealer has" + " " + dealerHand.GetCardCount() + " "); if (dealerHand.GetBlackjackValue() > 21) { Console.WriteLine("\nDealer busts... User wins!"); Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21"); return(true); } else if (dealerHand.GetBlackjackValue() >= userHand.GetBlackjackValue()) { Console.WriteLine("\nDealer Wins!"); Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21"); return(false); } else if (userHand.GetBlackjackValue() >= dealerHand.GetBlackjackValue()) { Console.WriteLine("\nUser wins!"); Console.WriteLine("\nDealer has " + dealerHand.GetBlackjackValue() + "/21"); return(true); } } } }
static bool PlayBlackjack() { Deck deck = new Deck(); deck.Shuffle(); //userHand and dealerHand BlackjackHand userHand = new BlackjackHand(); BlackjackHand dealerHand = new BlackjackHand(); userHand.AddCard(deck.DealCard()); userHand.AddCard(deck.DealCard()); dealerHand.AddCard(deck.DealCard()); dealerHand.AddCard(deck.DealCard()); if (dealerHand.GetBlackjackValue() == 21) { Console.WriteLine("Dealer's cards are: "); dealerHand.Display(); Console.WriteLine("Your cards are: "); userHand.Display(); Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose."); return(false); } if (userHand.GetBlackjackValue() == 21) { Console.WriteLine("Dealer's cards are: "); dealerHand.Display(); Console.WriteLine(" \nYour cards are: "); userHand.Display(); Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You win."); return(true); } string hitOrStand; while (true) { Console.WriteLine(" \nYour cards are: "); userHand.Display(); Console.WriteLine($"Your total is {userHand.GetBlackjackValue()}"); Console.WriteLine("\nDealer is showing: "); dealerHand.Display(dealerHand.GetCard(0)); Console.WriteLine(); hitOrStand = GetHitOrStand(); if (hitOrStand == "S" || hitOrStand == "s") { Console.WriteLine("\nUser stands."); break; } else { userHand.AddCard(deck.DealCard()); if (userHand.GetBlackjackValue() > 21) { Console.WriteLine("User lose! Your cards are: "); userHand.Display(); Console.WriteLine("Dealer's cards are: "); dealerHand.Display(); Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose."); return(false); } } } Console.WriteLine("Dealer's cards are: "); dealerHand.Display(); Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()}."); while (dealerHand.GetBlackjackValue() <= 16) { Console.WriteLine("Dealer hits and gets a card: "); dealerHand.AddCard(deck.DealCard()); dealerHand.Display(dealerHand.GetCard(dealerHand.GetCardCount() - 1)); if (dealerHand.GetBlackjackValue() > 21) { Console.WriteLine("Dealer busted by going over 21. You win."); return(true); } } if (dealerHand.GetBlackjackValue() >= userHand.GetBlackjackValue()) { Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You lose."); return(false); } else { Console.WriteLine($"Dealer's total is {dealerHand.GetBlackjackValue()} and your total is {userHand.GetBlackjackValue()}. You win."); return(true); } }