//A helper method to split DisplayTable into smaller parts private void DisplayDealerHand(bool holeCardRevealed) { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Dealer Hand"); //The dealer's hand label is displayed in blue on black CasinoDoor.RestoreDefaultColors(); int cardsInHand = dealerHand.Cards.Count; if (cardsInHand > 0) { for (int card = 0; card < cardsInHand; card++) { if (!holeCardRevealed && card == 1) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write("?"); Console.ForegroundColor = ConsoleColor.Black; Console.Write("? "); CasinoDoor.RestoreDefaultColors(); } else { dealerHand.Cards[card].PrintCardInfo(); } } } Console.WriteLine(); }
/* This resolves any hand in which the player has beat the dealer. A player * beats the dealer if their hand score is larger than that of the dealers * without going over 21*/ private void ResolveWinningHand(int handIndex) { float payout; if (playerHands[handIndex].Cards.Count == 2 && CheckHandScore(playerHands[handIndex]) == 21) //This signifies a blackjack { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write("BLACK"); //Black is written with red text on black Console.BackgroundColor = ConsoleColor.DarkRed; Console.ForegroundColor = ConsoleColor.Black; Console.Write("JACK"); //Jack is written with black text on red CasinoDoor.RestoreDefaultColors(); payout = ((playerBets[handIndex] * 1.5f) + playerBets[handIndex]); //A blackjack pays out at a 3:2 ratio } else { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; Console.Write("WIN"); //A standard winning hand is signified by green text on black payout = playerBets[handIndex] * 2; //A blackjack pays out at a 3:2 ratio } Console.WriteLine(" pays $" + payout + ".00"); HumanPlayer.AddToBank(payout); }
/* This method displays the final score for the dealer hand, then displays * the result of each player hand one at a time. This can never be called * if all of a player's hands have been busted thus it only applies to */ protected void ResolvePlayerHands() { ShowDealerScore(); CasinoDoor.WaitForDisplay(); //A short delay before displaying the player's hands int dealerScore = CheckHandScore(dealerHand); //Stores the dealer's hand score //Player hands are resolved one at a time for (int handIndex = 0; handIndex < playerHands.Count; handIndex++) { int handScore = CheckHandScore(playerHands[handIndex]); Console.Write("Hand " + (handIndex + 1).ToString() + ": "); if ((handScore > dealerScore) || (handScore < dealerScore && dealerScore > 21)) //A player always wins these hands { ResolveWinningHand(handIndex); } else if (handScore < dealerScore && dealerScore <= 21) //A player always loses this hand { ResolveLosingHand(handIndex); } else //If the player's hand and the dealer both have the same hand value { ResolveTiedHand(handIndex); //A player wins if this hand is a blackjack and the dealer's isn't, otherwise it is a push } CasinoDoor.RestoreDefaultColors(); //The default colors are restored after each hand as each hand resolution may have unique coloring CasinoDoor.WaitForDisplay(); //Each hand is displayed with a slight delay before the next reveal } }
//If the player makes an incorrect selection the error is displayed before they are re-prompted public void DisplayError(string error) { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine(error); CasinoDoor.RestoreDefaultColors(); CasinoDoor.WaitForDisplay(); }
/* PlayHand initializes the loop for a single hand of BlackJack. Returns * true or false based on whether or not the player wants to keep playing * at the end of a hand*/ private bool PlayHand() { bool handInProgress = true; //This variable tracks whether a hand is complete bool dealerHandRevealed = false; while (handInProgress) { DisplayTable(dealerHandRevealed); if (playerHands.Count == 0) //If the player is not currently holding any cards { GetWagerAndDeal(); } else //If the player has at least one hand { Console.WriteLine(); if (AllHandsFrozen() == true) //If all the hands are busted, doubled down, or stayed { if (AllHandsBusted() == false) { dealerHandRevealed = true; DisplayTable(dealerHandRevealed); if (CheckHandScore(dealerHand) < 17) { CasinoDoor.WaitForDisplay(); DealerHit(); } else { dealerHandRevealed = true; ResolvePlayerHands(); handInProgress = false; } } else { handInProgress = false; } Console.WriteLine(); //Spacing for readability CasinoDoor.WaitForDisplay(); } else { OfferPlayerOptions(); CasinoDoor.WaitForDisplay(); } } } if (HumanPlayer.Bank > 0) { return(PlayAnotherHand(true)); } else { Console.WriteLine("You went bankrupt! Time to go home."); return(false); } }
//A helper method that informs the player that they have hit a blackjack public void AnnounceBlackJack(int handIndex) { Console.Write("Hand " + (handIndex + 1) + ": "); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write("Black"); Console.BackgroundColor = ConsoleColor.DarkRed; Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine("Jack\n"); CasinoDoor.RestoreDefaultColors(); }
//This is called by PlayGame when the player has no cards private void GetWagerAndDeal() { if (InitialWagerPrompt() == true) { Console.WriteLine(); DealHands(); CasinoDoor.WaitForDisplay(); } else { DisplayError("Invalid wager, try again"); } }
/* Shuffles the deck and deals the initial 4 cards, 2 to the dealer and 2 to the * human player.*/ public void DealHands() { Console.WriteLine("Shuffling deck..."); Deck.ShuffleDeck(); CasinoDoor.WaitForDisplay(); Console.WriteLine("Dealing hands..."); for (int startingCards = 0; startingCards < 2; startingCards++) { if (playerHands.Count == 0) { playerHands.Add(new CardList()); frozenHands.Add(false); } playerHands[0].AddCard(Deck.DealCard()); dealerHand.AddCard(Deck.DealCard()); } }
/* Resolves hands where the dealer and player have the same hand score. Players * can still win a tied hand if they have a blackjack*/ private void ResolveTiedHand(int handIndex) { if (CheckHandScore(dealerHand) == 21) { if (playerHands[handIndex].Cards.Count == 2 && dealerHand.Cards.Count > 2) { ResolveWinningHand(handIndex); //A player blackjack beats a non-blackjack dealer 21 } else { ResolvePush(handIndex); } } else { ResolvePush(handIndex); } CasinoDoor.RestoreDefaultColors(); }
//Draws the score and the hands of cards public void DisplayTable(bool allHandsRevealed) { Console.Clear(); //Clears the console before re-drawing all game elements //The name of the current game is displayed in a stylized format Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write("Console"); Console.BackgroundColor = ConsoleColor.DarkRed; Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine("Jack\n"); //Bank and wager values are displayed in green text on black Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Remaining Funds: " + HumanPlayer.Bank.ToString("c2")); DisplayBets(); CasinoDoor.RestoreDefaultColors(); DisplayDealerHand(allHandsRevealed); DisplayPlayerHands(); }
/* This is the pre-bet wager. Players choose the amount they wish to wager * at the beginning of each hand with opportunities to increase it via other * means later */ protected bool InitialWagerPrompt() { float wagerAmount; //This is used to store the player input from the console Console.Write("How much would you like to wager?\n(Min = $1.00, Max = " + HumanPlayer.Bank.ToString("c2") + ")\n"); //Prompts the player to wager an appropriate amount if (float.TryParse(Console.ReadLine(), out wagerAmount)) { if (wagerAmount > 0 && wagerAmount <= HumanPlayer.Bank) //The wager has to be between 1 and 100 dollars { Console.Write("\nYour wager was "); Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(wagerAmount.ToString("c2")); //The wager is confirmed CasinoDoor.RestoreDefaultColors(); HumanPlayer.PlaceBet(wagerAmount); //The wager amount is removed from the player's bank playerBets.Add(wagerAmount); //Adds the initial wager to the list of wagers return(true); } } return(false); }
/* This is a helper method for the resolution of player hands. It displays * the dealer's score in one of 3 ways based on whether it us less than, * equal to, or greater than 21*/ private void ShowDealerScore() { int dealerScore = CheckHandScore(dealerHand); //Stores the hand score for use Console.Write("\nDealer hand: "); //A label to identify that this is the dealer's hand if (dealerScore > 21) { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("BUSTED"); //Busted hands are identified with red text on black background } else if (dealerScore == 21) //A dealer score of 21 has special rendering considerations { if (dealerHand.Cards.Count == 2) //Renders a dealer blackjack { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.DarkRed; Console.Write("BLACK"); //The first half of blackjack is displayed with red text on black Console.BackgroundColor = ConsoleColor.DarkRed; Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine("JACK"); //The second half of blackjack is displayed with black text on red } else //Renders a non-blackjack 21 { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Score = 21"); //A non-blackjack 21 hand is displayed with green text on black } } else { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Score = " + dealerScore.ToString()); //A non-21 dealer hand is displayed with blue text on black } Console.WriteLine(); CasinoDoor.RestoreDefaultColors(); //Default colors need to be restored after displaying the dealer's score }
//A helper method to split DisplayTable into smaller parts private void DisplayPlayerHands() { Console.BackgroundColor = ConsoleColor.Black; Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("Player Hand(s)"); //The player's hand label is displayed in blue on black CasinoDoor.RestoreDefaultColors(); int numberofHands = playerHands.Count; if (numberofHands > 0) { for (int handIndex = 0; handIndex < numberofHands; handIndex++) { Console.Write((handIndex + 1).ToString() + ": "); if (CheckHandScore(playerHands[handIndex]) > 21) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("BUST"); Console.ForegroundColor = ConsoleColor.White; } else { for (int cardIndex = 0; cardIndex < playerHands[handIndex].Cards.Count; cardIndex++) { playerHands[handIndex].Cards[cardIndex].PrintCardInfo(); } Console.WriteLine("= " + CheckHandScore(playerHands[handIndex]).ToString()); } } } else { Console.WriteLine(); //Spacing for readability } }
/* Prints the information for this card directly to the console. * Uses UTF-8 to produce suit values. Some fonts can't render * the suit values correctly. */ public void PrintCardInfo() { string cardInfo = ""; switch (Value) { case 1: cardInfo += "A"; //Aces use an A break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: cardInfo += Value.ToString(); //2-9 use an integer break; case 10: cardInfo += "X"; //10's use an X to maintain single-character formatting break; case 11: cardInfo += "J"; //Jacks use a J break; case 12: cardInfo += "Q"; //Queens use a Q break; case 13: cardInfo += "K"; //Kings use a K break; } switch (Suit) { case 'C': cardInfo += "\u2663"; //The utf value for clubs Console.ForegroundColor = ConsoleColor.Black; break; case 'D': cardInfo += "\u2666"; //The utf value for diamonds Console.ForegroundColor = ConsoleColor.DarkRed; break; case 'S': cardInfo += "\u2660"; //The utf value for spades Console.ForegroundColor = ConsoleColor.Black; break; case 'H': cardInfo += "\u2665"; //The utf value for hearts Console.ForegroundColor = ConsoleColor.DarkRed; break; } Console.Write(cardInfo + " "); CasinoDoor.RestoreDefaultColors(); }
//This is called by PlayGame when there is one or more hands needing attention from the user private void OfferPlayerOptions() { for (int handIndex = 0; handIndex < playerHands.Count; handIndex++) //Iterates over the currently present hands { if (frozenHands[handIndex] == false) //Checks to see if this hand has been frozen { int cardsInHand = playerHands[handIndex].Cards.Count; if (cardsInHand == 2 && HumanPlayer.Bank > 0) //If the player's hand has 2 cards and the player has funds remaining { if (CheckBlackJack(playerHands[handIndex], handIndex) == true) //Checks for a blackjack { AnnounceBlackJack(handIndex); } else if (CheckSplit(playerHands[handIndex]) == true) //Checks to see if the cards are a pair { if (SplitHandPrompt(playerHands[handIndex], handIndex) == true) //Prompts the player to split the hand and validates input { CasinoDoor.WaitForDisplay(); break; } else //If the user presses a wrong key when attempting to split a hand { DisplayError("Input for split attempt invalid, please try again."); //Occurs when the player's input is invalid break; } } else { if (DoubleDownPrompt(playerHands[handIndex], handIndex) == true) //Prompts the player to double down and verifies input { CasinoDoor.WaitForDisplay(); break; } else //If the user presses an incorrect key when attempting to double down { DisplayError("Input for double down attempt invalid, please try again."); //Occurs when the player's input is invalid break; } } } else if (cardsInHand == 2 && HumanPlayer.Bank <= 0) //If the player has 2 cards but does not have any funds remaining { if (CheckBlackJack(playerHands[handIndex], handIndex) == true) //Checks for a blackjack { AnnounceBlackJack(handIndex); } else if (CheckSplit(playerHands[handIndex]) == true) //If the hand is not a blackjack but is a face-value pair { Console.WriteLine("Insufficient funds to split hand."); CasinoDoor.WaitForDisplay(); } else { Console.WriteLine("Insufficient funds to double down on hand."); //If the hand is neither blackjack nor a pair CasinoDoor.WaitForDisplay(); HitOrStay(playerHands[handIndex], handIndex); //This ensures that the split/double down checks won't loop endlessly } } else //If this hand has more than 2 cards in it { if (CheckHandScore(playerHands[handIndex]) < 21) { HitOrStay(playerHands[handIndex], handIndex); //Prompts the player to hit or stay } break; } } } }