7.16. Case Study: A Game of ChanceOne of the most popular games of chance is a dice game known as "craps." The rules of the game are straightforward:
The application in Fig. 7.17 simulates the game of craps. Figure 7.17. Craps game using class Random.
The player must roll two dice on the first and all subsequent rolls. When executing the application, click the Play Button to play the game. The form displays the results of each roll. The screen captures depict the execution of two games. Fig. 7.17(a) displays the Form before the game has begun, while Fig. 7.17(b) shows the application after the Play Button has been clicked. In this example, a 6 is rolled, which is made the point. We click Roll again and a 7 is rolled, causing us to lose the game (Fig. 7.17(c)). Fig. 7.17(d) shows another example game, where the user wins by rolling a 7 on the first roll. This program introduces the GroupBox control, used to display the user's point. A GroupBox is a container used to group related controls. Within the GroupBox pointDiceGroup, we add two PictureBoxes, which are controls that display images. A GroupBox can be dragged and dropped onto the Form the same way you would any other control. When adding other controls to a GroupBox (such as the two PictureBoxes in this example), drag and drop the controls within the bounds of the GroupBox on the Form. The GroupBox is named grpPoint. We use the prefix grp for GroupBoxes. We set the font size of the GroupBox to 9.75 and the Text property of the GroupBox to Point. This is the text that displays in the upper-left corner of the GroupBox. Note in the output of Fig. 7.17 that we will be modifying this text to display the user's point value. If you leave the Text property blank, the GroupBox will display as a simple border without any text. Enumerations and ConstantsBefore introducing any method declarations, the program includes several declarations, including our first enumeration in lines 714 and our first constant identifiers in lines 1718. Constant identifiers and Enumerations enhance program readability by providing descriptive identifiers for constant numbers or Strings. Constant identifiers and Enumerations help you ensure that values are consistent throughout a program. Recall that keyword Const creates a single constant identifier; Enumerations are used to define groups of related constants. In this case, we create Constant identifiers for the file names that are used throughout the program and create an Enumeration of descriptive names for the various dice combinations in craps (i.e., SNAKE_EYES, trEY, LUCKY_SEVEN, CRAPS, YO_LEVEN and BOX_CARS). Note that multiple enumeration members can have the same valuein this example, LUCKY_SEVEN and CRAPS both have the value 7. We use two identifiers for program clarity; on the first roll, a seven causes the player to win (LUCKY_SEVEN), and on subsequent rolls, a seven causes the player to lose (CRAPS). Constant identifiers must be assigned constant values and cannot be modified after they are declared. After the constant-identifier declarations and the declarations for several instance variables (lines 2023), method btnPlay_Click is declared (lines 2657). It is the event handler for btnPlay's Click event. When the user clicks the Play button, method btnPlay_Click sets up a new game by initializing several values (lines 3032). Note that line 32 sets our status Label's Text property to "". This is known as the empty string (i.e., a string that does not contain any characters). Nothing appears on the screen when an empty string is displayed. Setting the Image property of picPointDie1 and picPointDie2 to Nothing (lines 3536) causes the PictureBoxes to appear blank. Method btnPlay_Click executes the game's opening roll by calling RollDice (line 38). Internally, RollDice (lines 8696) generates two random numbers and calls method DisplayDie (lines 7883), which loads an appropriate die image in the PictureBox passed to it. The Select...Case statement (lines 4156) analyzes the roll returned by RollDice to determine how play should continue (i.e., by terminating the game with a win or loss, or by enabling subsequent rolls). If the user does not win or lose on the first roll, the GroupBox's text is set to display the point value (line 50) and the property images are displayed in the GroupBox's PictureBoxes (lines 5253). Depending on the value of the roll, Buttons Roll and Play become either enabled or disabled (lines 43, 46 and 5455). Disabling a Button means that no action will be performed when the Button is clicked. Buttons can be enabled and disabled by setting the Button's Enabled property to true or False, respectively. If button Roll is enabled, clicking it invokes method btnRoll_Click (lines 6075), which executes an additional roll of the dice. Method btnRoll_Click then analyzes the roll, letting users know whether they have won or lost. |