/// <summary> /// Update all the dunamic values on the form. /// </summary> /// <param name="who"></param> private void UpdateDynamicValues(int who) { if (who == PLAYER) { int playerPointsTotal = TwentyOneGame.CalculateHandTotal(PLAYER); int acesWithValueOne = TwentyOneGame.GetNumOfUserAcesWithValueOne(); playerPointsLabel.Text = (playerPointsTotal).ToString(); acesWithValueOneTextbox.Text = (acesWithValueOne).ToString(); DisplayGuiHand(TwentyOneGame.GetHand(PLAYER), playerTableLayoutPanel); } else if (who == DEALER) { int dealerPointsTotal = TwentyOneGame.CalculateHandTotal(DEALER); dealerPointsLabel.Text = (dealerPointsTotal).ToString(); DisplayGuiHand(TwentyOneGame.GetHand(DEALER), dealerTableLayoutPanel); } else if (who == PLAYER_AND_DEALER) { int playerPointsTotal = TwentyOneGame.CalculateHandTotal(PLAYER); int acesWithValueOne = TwentyOneGame.GetNumOfUserAcesWithValueOne(); int dealerPointsTotal = TwentyOneGame.CalculateHandTotal(DEALER); playerPointsLabel.Text = (playerPointsTotal).ToString(); acesWithValueOneTextbox.Text = (acesWithValueOne).ToString(); dealerPointsLabel.Text = (dealerPointsTotal).ToString(); DisplayGuiHand(TwentyOneGame.GetHand(PLAYER), playerTableLayoutPanel); DisplayGuiHand(TwentyOneGame.GetHand(DEALER), dealerTableLayoutPanel); } }
/// <summary> /// Check to see if the player is going to exceed 9 cards in their hand and prevent them from doing so. /// </summary> /// <param name="who"></param> private void checkExceedMaximumCards(int who) { if (who == PLAYER) { int amountOfPlayerCardsInHand = TwentyOneGame.GetHand(PLAYER).GetCount(); if (amountOfPlayerCardsInHand >= MAX_NUMBER_OF_CARDS_ALLOWED) { hitButton.Enabled = false; } } else if (who == DEALER) { int amountOfDealerCardsInHand = TwentyOneGame.GetHand(DEALER).GetCount(); if (amountOfDealerCardsInHand >= MAX_NUMBER_OF_CARDS_ALLOWED) { promptNextGame(); } } }
} // End DisplayGuiHand /// <summary> /// Copies data from logic class to display /// </summary> /// <param name="firstRun">Prevent exceptions resulting from no game being played prior</param> private void UpdateDisplay(bool firstRun) { // Show number of games won this.gamesWonLabels[TwentyOneGame.PLAYER_USER].Text = TwentyOneGame.GetNumOfGamesWon(TwentyOneGame.PLAYER_USER).ToString(); this.gamesWonLabels[TwentyOneGame.PLAYER_CPU].Text = TwentyOneGame.GetNumOfGamesWon(TwentyOneGame.PLAYER_CPU).ToString(); // Show how many aces the user has that have a value of 1 this.lblOneAces.Text = TwentyOneGame.GetNumOfUserAcesWithValueOne().ToString(); if (!firstRun) { // Show the hands this.DisplayGuiHand(TwentyOneGame.GetHand(TwentyOneGame.PLAYER_USER), this.tableLayoutPanels[TwentyOneGame.PLAYER_USER]); this.DisplayGuiHand(TwentyOneGame.GetHand(TwentyOneGame.PLAYER_CPU), this.tableLayoutPanels[TwentyOneGame.PLAYER_CPU]); int pointsPlayer = TwentyOneGame.CalculateHandTotal(TwentyOneGame.PLAYER_USER); int pointsCpu = TwentyOneGame.CalculateHandTotal(TwentyOneGame.PLAYER_CPU); // Show whether a player is busted this.bustedLabels[TwentyOneGame.PLAYER_USER].Visible = !this.gameCurrent && pointsPlayer > TwentyOneGame.MAX_POINTS; this.bustedLabels[TwentyOneGame.PLAYER_CPU].Visible = !this.gameCurrent && pointsCpu > TwentyOneGame.MAX_POINTS; // Only show if they have more than 0 points this.pointsLabels[TwentyOneGame.PLAYER_USER].Visible = pointsPlayer > 0; this.pointsLabels[TwentyOneGame.PLAYER_CPU].Visible = pointsCpu > 0; // Show points this.pointsLabels[TwentyOneGame.PLAYER_USER].Text = pointsPlayer.ToString(); this.pointsLabels[TwentyOneGame.PLAYER_CPU].Text = pointsCpu.ToString(); if (!this.gameCurrent && pointsPlayer >= TwentyOneGame.MAX_POINTS) { btnDeal.Enabled = true; btnHit.Enabled = false; btnStand.Enabled = false; } } // Force visual update this.Update(); }
} //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
} //end of HitButton_Click private void StandButton_Click(object sender, EventArgs e) { //While Dealer's points are under 17.. while (TwentyOneGame.GetTotalPoints(0) < 17) { //Dealer plays. TwentyOneGame.PlayForDealer(); //Updates points. PointsLabel1.Text = TwentyOneGame.GetTotalPoints(0).ToString(); //Updates GUI. DisplayGuiHand(TwentyOneGame.GetHand(0), LayoutPanel1); } //Disables Stand and Hit button and enables DealButton. StandButton.Enabled = false; HitButton.Enabled = false; DealButton.Enabled = true; //If Dealer's points are over 21.. if (TwentyOneGame.GetTotalPoints(0) > 21) { //Show BUSTED Label BustedLabel1.Visible = true; //Updates Games won of player. Number2.Text = TwentyOneGame.GetNumOfGamesWon(1).ToString(); } else { //Otherwise dealer plays TwentyOneGame.PlayForDealer(); //Updates Games won. Number1.Text = TwentyOneGame.GetNumOfGamesWon(0).ToString(); Number2.Text = TwentyOneGame.GetNumOfGamesWon(1).ToString(); } //end of if statement } //end of StandButton_Click
}//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