/// <summary> /// Utility method to deal a card, also handles aces and first run of cards /// </summary> /// <param name="who">Which player</param> /// <param name="firstRun">Prevent message box and assign default value of 1 for aces</param> /// <returns></returns> private Card DealCardTo(int who, bool firstRun) { Card card = TwentyOneGame.DealOneCardTo(who); // Is it an ace if (!firstRun && who == TwentyOneGame.PLAYER_USER && card.GetFaceValue() == FaceValue.Ace) { if (MessageBox.Show("Count ace as 1?", this.Text, MessageBoxButtons.YesNo) == DialogResult.Yes) { TwentyOneGame.IncrementNumOfUserAcesWithValueOne(); } } this.UpdateDisplay(false); return(card); }
/// <summary> /// Play the game on click of the hit button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void hitButton_Click(object sender, EventArgs e) { Card playerDeltCard = TwentyOneGame.DealOneCardTo(PLAYER); FaceValue faceValueOfDeltCard = playerDeltCard.GetFaceValue(); if (faceValueOfDeltCard == FaceValue.Ace) { DialogResult messageBoxResult = ClassAssignment.Program.showYesNoMessageBox("Count Ace as 1?", "You got an Ace!"); if (messageBoxResult == DialogResult.Yes) { TwentyOneGame.IncrementNumOfUserAcesWithValueOne(); } } checkPlayerBusted(PLAYER); UpdateDynamicValues(PLAYER); checkExceedMaximumCards(PLAYER); }
} //end of DealButton_Click private void HitButton_Click(object sender, EventArgs e) { //Ask player if they want to use Ace as 1. if (TwentyOneGame.DealOneCardTo(1).GetFaceValue() == FaceValue.Ace) { DialogResult choice = MessageBox.Show("Count Ace as 1?", "You got an Ace!", MessageBoxButtons.YesNo, MessageBoxIcon.Question); //If choice is Yes, Ace card is given a 1 value. if (choice == DialogResult.Yes) { TwentyOneGame.IncrementNumOfUserAcesWithValueOne(); AmountOfAcesAsOne.Text = TwentyOneGame.GetNumOfUserAcesWithValueOne().ToString(); } //end of if statement } //end of if statement //Calculates totals. TwentyOneGame.CalculateHandTotal(1); //Updates the points text. PointsLabel2.Text = TwentyOneGame.GetTotalPoints(1).ToString(); //Updates GUI panels. DisplayGuiHand(TwentyOneGame.GetHand(1), LayoutPanel2); //If players points go over 21.. if (TwentyOneGame.GetTotalPoints(1) > 21) { //Show BUSTED label. BustedLabel2.Visible = true; //Updates games won. Number1.Text = TwentyOneGame.GetNumOfGamesWon(0).ToString(); //Enables DealButton again for another Game, and disables Hit and stand buttons. DealButton.Enabled = true; HitButton.Enabled = false; StandButton.Enabled = false; } //end of if statement } //end of HitButton_Click
private void btnDeal_Click(object sender, EventArgs e) { this.btnDeal.Enabled = false; this.btnHit.Enabled = true; this.btnStand.Enabled = true; this.gameCurrent = true; TwentyOneGame.SetUpGame(); // Deal initial cards this.DealCardTo(TwentyOneGame.PLAYER_CPU, true); this.DealCardTo(TwentyOneGame.PLAYER_CPU, true); Card card1 = this.DealCardTo(TwentyOneGame.PLAYER_USER, true); Card card2 = this.DealCardTo(TwentyOneGame.PLAYER_USER, true); // If two aces are dealt, both have a value of one if (card1.GetFaceValue() == FaceValue.Ace || card2.GetFaceValue() == FaceValue.Ace) { // Handle two aces being dealt if (card1.GetFaceValue() == FaceValue.Ace && card2.GetFaceValue() == FaceValue.Ace) { TwentyOneGame.IncrementNumOfUserAcesWithValueOne(); TwentyOneGame.IncrementNumOfUserAcesWithValueOne(); } else // Or just one { if (MessageBox.Show("Count ace as 1?", this.Text, MessageBoxButtons.YesNo) == DialogResult.Yes) { TwentyOneGame.IncrementNumOfUserAcesWithValueOne(); } } } this.UpdateDisplay(false); }
}//end of DisplayGuiHand private void DealButton_Click(object sender, EventArgs e) { //Hides the BUSTED labels. BustedLabel1.Visible = false; BustedLabel2.Visible = false; //Resets Totals. TwentyOneGame.ResetTotals(); //Updates the Points text. PointsLabel2.Text = TwentyOneGame.GetTotalPoints(1).ToString(); PointsLabel1.Text = TwentyOneGame.GetTotalPoints(0).ToString(); //Updates the Amount of Aces that are considered as one. AmountOfAcesAsOne.Text = TwentyOneGame.GetNumOfUserAcesWithValueOne().ToString(); //Gets the cards from the player's hand. Hand players_hand = TwentyOneGame.GetHand(1); //Updates GUI panels. DisplayGuiHand(TwentyOneGame.GetHand(0), LayoutPanel1); DisplayGuiHand(TwentyOneGame.GetHand(1), LayoutPanel2); //Find the Aces in the cards. foreach (Card card in players_hand) { if (card.GetFaceValue() == FaceValue.Ace) { DialogResult choice = MessageBox.Show("Count Ace as 1?", "You got an Ace!", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (choice == DialogResult.Yes) { TwentyOneGame.IncrementNumOfUserAcesWithValueOne(); AmountOfAcesAsOne.Text = TwentyOneGame.GetNumOfUserAcesWithValueOne().ToString(); } //end of if statement } //end of if statement } //end of foreach loop //Calculates totals. TwentyOneGame.CalculateHandTotal(0); TwentyOneGame.CalculateHandTotal(1); //Updates Totals. PointsLabel2.Text = TwentyOneGame.GetTotalPoints(1).ToString(); PointsLabel1.Text = TwentyOneGame.GetTotalPoints(0).ToString(); //If dealer goes over 21 points.. if (TwentyOneGame.GetTotalPoints(0) > 21) { //Show BUSTED label. BustedLabel1.Visible = true; //Updates number of games won. Number2.Text = TwentyOneGame.GetNumOfGamesWon(1).ToString(); //If player goes over 21 points.. } else if (TwentyOneGame.GetTotalPoints(1) > 21) { //Show BUSTED label. BustedLabel2.Visible = true; //Updates number of games won. Number1.Text = TwentyOneGame.GetNumOfGamesWon(0).ToString(); } else { //Disable DealButton when already dealt and enable Hit and Stand buttons. DealButton.Enabled = false; HitButton.Enabled = true; StandButton.Enabled = true; } //end of if statement } //end of DealButton_Click