/* Take another card or stay on the current value. This is also * called if the player refuses a double down to ensure that the * main game loop can advance properly */ protected bool HitOrStay(CardList hand, int handIndex) { Console.WriteLine("(H)it or (S)tay on hand " + (handIndex + 1).ToString() + "?"); char userInput = Console.ReadKey(true).KeyChar.ToString().ToUpper()[0]; //Stores the next key pressed as an uppercase char switch (userInput) { case 'H': Console.WriteLine("Hit!"); hand.AddCard(Deck.DealCard()); //Adds a new card to the current hand if (CheckHandScore(hand) >= 21) { frozenHands[handIndex] = true; } return(true); case 'S': Console.WriteLine("Stay."); frozenHands[handIndex] = true; //Makes sure the hand will be skipped on future iterations return(true); default: break; } return(false); }
/* This method adds the values in the hand and returns it as an int. * If the hand goes over 21 it checks to see if there is an ace present * to adjust the score before returning it. */ protected int CheckHandScore(CardList hand) { int handScore = 0; int acesPresent = 0; //Counts the aces present to adjust for busted hands foreach (Card card in hand.Cards) { if (card.Value == 1) { acesPresent++; handScore += 11; } else if (card.Value > 1 && card.Value < 10) { handScore += card.Value; } else { handScore += 10; } } //Adjusts the score as necessary when there are one or more aces present while (handScore > 21 && acesPresent > 0) { handScore -= 10; acesPresent--; } return(handScore); }
protected List <float> playerBets; //A list of the bets tied to each individual hand if the player ends up splitting them //The BlackJackRules are initialized alongside the BlackJackTable public BlackJackRules() { dealerHand = new CardList(); //Initializes the empty dealer hand playerHands = new List <CardList>(); //Initializes an empty list of player hands frozenHands = new List <bool>(); playerBets = new List <float>(); }
//Returns true if the hand is a blackjack otherwise returns false protected bool CheckBlackJack(CardList hand, int handIndex) { if (hand.Cards.Count == 2 && CheckHandScore(hand) == 21) { frozenHands[handIndex] = true; return(true); } return(false); }
/* This returns true if the hand is exactly 2 cards with paired * face values */ protected bool CheckSplit(CardList hand) { if (hand.Cards[0].Value == hand.Cards[1].Value) { return(true); } else { return(false); } }
//This is called by DoubleDownPrompt() when the user chooses to double down private void DoubleDown(CardList hand, int handIndex) { if (playerBets[handIndex] <= HumanPlayer.Bank) { Console.WriteLine("Double down bet placed.\n"); playerBets[handIndex] += HumanPlayer.PlaceBet(playerBets[handIndex]); } else { Console.WriteLine("Not enough funds, bet " + HumanPlayer.Bank.ToString() + ".00 instead.\n"); //Goes all-in if the player has insufficient funds playerBets[handIndex] += HumanPlayer.PlaceBet(HumanPlayer.Bank); } playerHands[handIndex].AddCard(Deck.DealCard()); frozenHands[handIndex] = true; }
/* This method initializes the double down process. Any hand of exactly * 2 cards can Double Down. Returning true signifies proper user input. * Incorrect input will display an error before forcing the game to loop * back to here. The split prompt calls this immediately if the player * chooses not to split to avoid a never-ending loop of split prompts*/ protected bool DoubleDownPrompt(CardList hand, int handIndex) { Console.Write("Double down on hand " + (handIndex + 1).ToString() + "?\n(Y/N): "); char userInput = Console.ReadKey(true).KeyChar.ToString().ToUpper()[0]; //Stores the next key pressed as an uppercase char if (userInput == 'Y') { DoubleDown(hand, handIndex); //Adds a card to the hand return(true); } else if (userInput == 'N') { Console.WriteLine("Passed on double down\n"); HitOrStay(hand, handIndex); //To avoid a never-ending loop of double down offers return(true); } return(false); }
/* This prompt is displayed to the user if their hand is valid to be * split into two*/ protected bool SplitHandPrompt(CardList hand, int handIndex) { Console.Write("Split hand " + (handIndex + 1).ToString() + "?\n(Y/N): "); char userInput = Console.ReadKey(true).KeyChar.ToString().ToUpper()[0]; //Stores the next key pressed as an uppercase char if (userInput == 'Y') { SplitHand(hand, handIndex); //Splits the paired cards into two hands return(true); } else if (userInput == 'N') { Console.WriteLine("Hand not split.\n"); DoubleDownPrompt(hand, handIndex); //To avoid a never-ending loop of hand split offers return(true); } else { return(false); } }
//This performs the actual action of splitting the hand if the player so chooses. private void SplitHand(CardList hand, int handIndex) { if (playerBets[handIndex] <= HumanPlayer.Bank) { Console.WriteLine("Split bet placed.\n"); playerBets.Add(playerBets[handIndex]); } else { Console.WriteLine("Not enough funds, bet " + HumanPlayer.Bank.ToString() + ".00 on split hand\n"); playerBets.Add(HumanPlayer.Bank); } playerHands.Add(new CardList()); frozenHands.Add(false); int newHandIndex = playerHands.Count - 1; //Finds the newly created hand playerHands[newHandIndex].AddCard(playerHands[handIndex].Cards[1]); //Copies one of the original cards playerHands[handIndex].RemoveCard(1); //Removes the copied card //Fill both hands playerHands[handIndex].AddCard(Deck.DealCard()); playerHands[newHandIndex].AddCard(Deck.DealCard()); }