/// <summary> /// Print out the description of the card, marking Aces as Soft or Hard. /// </summary> public void WriteDescription() { if (Suit == Suit.Diamonds || Suit == Suit.Hearts) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.White; } if (Face == Ace) { if (Value == 11) { Console.WriteLine(Symbol + " Soft " + Face + " of " + Suit); } else { Console.WriteLine(Symbol + " Hard " + Face + " of " + Suit); } } else { Console.WriteLine(Symbol + " " + Face + " of " + Suit); } Casino.ResetColor(); }
/// <summary> /// Write player's hand to console. /// </summary> public void WriteHand() { // Write Bet, Chip, Win, Amount with color, and write Round # Console.Write("Bet: "); Console.ForegroundColor = ConsoleColor.Magenta; Console.Write(Bet + " "); Casino.ResetColor(); Console.Write("Chips: "); Console.ForegroundColor = ConsoleColor.Green; Console.Write(Chips + " "); Casino.ResetColor(); Console.Write("Wins: "); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(Wins); Casino.ResetColor(); Console.WriteLine("Round #" + HandsCompleted); Console.WriteLine(); Console.WriteLine("Your Hand (" + GetHandValue() + "):"); foreach (Card card in Hand) { card.WriteDescription(); } Console.WriteLine(); }
/// <summary> /// Take player's bet /// </summary> /// <returns>Was the bet valid</returns> static bool TakeBet() { Console.Write("Current Chip Count: "); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(player.Chips); Casino.ResetColor(); Console.Write("Minimum Bet: "); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(Casino.MinimumBet); Casino.ResetColor(); Console.Write("Enter bet to begin hand " + player.HandsCompleted + ": "); Console.ForegroundColor = ConsoleColor.Magenta; string s = Console.ReadLine(); Casino.ResetColor(); if (Int32.TryParse(s, out int bet) && bet >= Casino.MinimumBet && player.Chips >= bet) { player.AddBet(bet); return(true); } return(false); }
static void Main(string[] args) { // Console cannot render unicode characters without this line Console.OutputEncoding = Encoding.UTF8; Casino.ResetColor(); Console.Title = "♠♥♣♦ Blackjack"; Console.WriteLine("♠♥♣♦ Welcome to Blackjack v" + Casino.GetVersionCode()); Console.WriteLine("Press any key to play."); Console.ReadKey(); StartRound(); }
/// <summary> /// Write Dealer's RevealedCards to Console. /// </summary> public static void WriteHand() { Console.WriteLine("Dealer's Hand (" + GetHandValue() + "):"); foreach (Card card in RevealedCards) { card.WriteDescription(); } for (int i = 0; i < HiddenCards.Count; i++) { Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("<hidden>"); Casino.ResetColor(); } Console.WriteLine(); }
/// <summary> /// Handles everything for the round. /// </summary> static void StartRound() { Console.Clear(); if (!TakeBet()) { EndRound(RoundResult.INVALID_BET); return; } Console.Clear(); InitializeHands(); TakeActions(); Dealer.RevealCard(); Console.Clear(); player.WriteHand(); Dealer.WriteHand(); player.HandsCompleted++; if (player.Hand.Count == 0) { EndRound(RoundResult.SURRENDER); return; } else if (player.GetHandValue() > 21) { EndRound(RoundResult.PLAYER_BUST); return; } while (Dealer.GetHandValue() <= 16) { Thread.Sleep(1000); Dealer.RevealedCards.Add(deck.DrawCard()); Console.Clear(); player.WriteHand(); Dealer.WriteHand(); } if (player.GetHandValue() > Dealer.GetHandValue()) { player.Wins++; if (Casino.IsHandBlackjack(player.Hand)) { EndRound(RoundResult.PLAYER_BLACKJACK); } else { EndRound(RoundResult.PLAYER_WIN); } } else if (Dealer.GetHandValue() > 21) { player.Wins++; EndRound(RoundResult.PLAYER_WIN); } else if (Dealer.GetHandValue() > player.GetHandValue()) { EndRound(RoundResult.DEALER_WIN); } else { EndRound(RoundResult.PUSH); } }
/// <summary> /// Perform action based on result of round and start next round. /// </summary> /// <param name="result">The result of the round</param> static void EndRound(RoundResult result) { switch (result) { case RoundResult.PUSH: player.ReturnBet(); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Player and Dealer Push."); break; case RoundResult.PLAYER_WIN: Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Player Wins " + player.WinBet(false) + " chips"); break; case RoundResult.PLAYER_BUST: player.ClearBet(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Player Busts"); break; case RoundResult.PLAYER_BLACKJACK: Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Player Wins " + player.WinBet(true) + " chips with Blackjack."); break; case RoundResult.DEALER_WIN: player.ClearBet(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Dealer Wins."); break; case RoundResult.SURRENDER: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Player Surrenders " + (player.Bet / 2) + " chips"); player.Chips += player.Bet / 2; player.ClearBet(); break; case RoundResult.INVALID_BET: Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Invalid Bet."); break; } if (player.Chips <= 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(); Console.WriteLine("You ran out of Chips after " + (player.HandsCompleted - 1) + " rounds."); Console.WriteLine("500 Chips will be added and your statistics have been reset."); player = new Player(); } Casino.ResetColor(); Console.WriteLine("Press any key to continue"); Console.ReadKey(); StartRound(); }
/// <summary> /// Ask user for action and perform that action until they stay, double, or bust. /// </summary> static void TakeActions() { string action; do { Console.Clear(); player.WriteHand(); Dealer.WriteHand(); Console.Write("Enter Action (? for help): "); Console.ForegroundColor = ConsoleColor.Cyan; action = Console.ReadLine(); Casino.ResetColor(); switch (action.ToUpper()) { case "HIT": player.Hand.Add(deck.DrawCard()); break; case "STAY": break; case "SURRENDER": player.Hand.Clear(); break; case "DOUBLE": if (player.Chips <= player.Bet) { player.AddBet(player.Chips); } else { player.AddBet(player.Bet); } player.Hand.Add(deck.DrawCard()); break; default: Console.WriteLine("Valid Moves:"); Console.WriteLine("Hit, Stay, Surrender, Double"); Console.WriteLine("Press any key to continue."); Console.ReadKey(); break; } if (player.GetHandValue() > 21) { foreach (Card card in player.Hand) { if (card.Value == 11) // Only a soft ace can have a value of 11 { card.Value = 1; break; } } } } while (!action.ToUpper().Equals("STAY") && !action.ToUpper().Equals("DOUBLE") && !action.ToUpper().Equals("SURRENDER") && player.GetHandValue() <= 21); }