Пример #1
0
        /// <summary>
        /// Present the user with the results when they decide to cancel the game and close the window.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cancelButton_Click(object sender, EventArgs e)
        {
            int dealerGamesWon = TwentyOneGame.GetNumOfGamesWon(DEALER);
            int playerGamesWon = TwentyOneGame.GetNumOfGamesWon(PLAYER);

            if (dealerGamesWon > playerGamesWon)
            {
                Program.showOKMessageBox("House won. Better luck next time.");
            }
            else if (playerGamesWon > dealerGamesWon)
            {
                Program.showOKMessageBox("You won! Well done.");
            }
            else
            {
                Program.showOKMessageBox("It was a draw!");
            }
            TwentyOneGame.ResetTotals();
            this.Close();
        }
Пример #2
0
        } // 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();
        }
Пример #3
0
        }     //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
Пример #4
0
        }     //end of StandButton_Click

        private void CancelButton_Click(object sender, EventArgs e)
        {
            //Reset totals.
            TwentyOneGame.ResetTotals();

            //If player and dealer has the same amount of games won.
            if (TwentyOneGame.GetNumOfGamesWon(0) == TwentyOneGame.GetNumOfGamesWon(1))
            {
                MessageBox.Show("It was a draw!");
            }
            //If dealer has less games won than players show player won message.
            else if (TwentyOneGame.GetNumOfGamesWon(0) > TwentyOneGame.GetNumOfGamesWon(1))
            {
                MessageBox.Show("House won! Better luck next time!");
            }
            //Otherwise show house won message.
            else
            {
                MessageBox.Show("You won! Well done.");
            }//end if
            //Close window.
            this.Close();
        }//end of CancelButton_Click
Пример #5
0
        }     //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
Пример #6
0
 /// <summary>
 /// Display the number of games won at the end of a game and reset a new round.
 /// </summary>
 private void promptNextGame()
 {
     playerGamesWonTextbox.Text = (TwentyOneGame.GetNumOfGamesWon(PLAYER)).ToString();
     dealerGamesWonTextBox.Text = (TwentyOneGame.GetNumOfGamesWon(DEALER)).ToString();
     dealButton.Enabled         = true;
 }
Пример #7
0
        }//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