Presentation is loading. Please wait.

Presentation is loading. Please wait.

T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 16 Craps Game Application Introducing Random-Number Generation and Enum.

Similar presentations


Presentation on theme: "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 16 Craps Game Application Introducing Random-Number Generation and Enum."— Presentation transcript:

1 T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 16 Craps Game Application Introducing Random-Number Generation and Enum

2  2009 Pearson Education, Inc. All rights reserved. 2 Outline 16.1 Test-Driving the Craps Game Application 16.2 Random-Number Generation 16.3 Constructing the Craps Game Application 16.4 Using Random Numbers in the Craps Game Application

3  2009 Pearson Education, Inc. All rights reserved. 3 In this tutorial you will learn: ■Code simulation techniques that employ random-number generation. ■Use class Random methods to generate random numbers. ■Use enumerations to enhance code readability. ■Read images from files. Objectives

4 Application Requirements  2009 Pearson Education, Inc. All rights reserved. 4 16.1 Test-Driving the Craps Game Application Create an application that simulates playing the game of craps. In this game, a player rolls two dice. Each die has six faces. After the dice have come to rest, the sum of the spots on the two top faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2, 3 or 12 on the first throw (called “craps”), the player loses. If the sum is 4, 5, 6, 8, 9 or 10 on the first throw, that sum becomes the player’s “point.” To win, a player must continue rolling the dice until the point value is rolled. The player loses by rolling a 7 before rolling the point.

5  2009 Pearson Education, Inc. All rights reserved. 5 ■Run the completed application (Fig. 16.1). Figure 16.1 | Craps Game application’s initial appearance. Test-Driving the Craps Game Application

6  2009 Pearson Education, Inc. All rights reserved. 6 Test-Driving the Craps Game Application (Cont.) ■There are three possible outcomes for clicking the Play Button. –The player wins by rolling 7 or 11 (Fig. 16.2). Figure 16.2 | Player wins on first roll by rolling 7 or 11.

7  2009 Pearson Education, Inc. All rights reserved. 7 Test-Driving the Craps Game Application (Cont.) –The player loses by rolling 2, 3 or 12 (Fig. 16.3). Figure 16.3 | Player loses on first roll by rolling 2, 3 or 12.

8  2009 Pearson Education, Inc. All rights reserved. 8 Test-Driving the Craps Game Application (Cont.) –Otherwise, the roll becomes the player’s point (4, 5, 6, 8, 9 or 10), which is then displayed (Fig. 16.4). Figure 16.4 | First roll sets the point that the player must match to win.

9  2009 Pearson Education, Inc. All rights reserved. 9 ■Click the Roll Button repeatedly until either you win by matching your point value (Fig. 16.5) or you lose by rolling a 7 (Fig. 16.6). Figure 16.5 | Winning the game by matching your point. Test-Driving the Craps Game Application Figure 16.6 | Losing by rolling a 7 before matching your point.

10  2009 Pearson Education, Inc. All rights reserved. 10 ■An object of class Random introduces chance into your applications. ■Consider the following statements: Dim randomObject As New Random() Dim randomNumber As Integer = randomObject.Next() –The first statement declares randomObject as a reference of type Random and assigns it a New Random object. –The second statement declares Integer variable randomNumber and assigns it the value returned by calling Random ’ s Next method on r andomObject. 16.2 Random-Number Generation

11  2009 Pearson Education, Inc. All rights reserved. 11 ■The Next method generates a positive Integer value between zero and the largest possible Integer. ■You can also use NextDouble method to generate random values of type Double. The NextDouble method returns a positive Double value from 0.0 up to, but not including, 1.0. ■The values returned by Next are actually pseudorandom numbers produced by a complex mathematical calculation. 16.2 Random-Number Generation (Cont.)

12  2009 Pearson Education, Inc. All rights reserved. 12 ■The range of values produced by Next often is different from the range needed in a particular application. –An application that simulates coin tossing might require only 0 for heads and 1 for tails. –An application that simulates the rolling of a six-sided die would require random Integer s from 1 to 6. ■By passing an argument to the Next method you can produce integers in the range from 1 to 6. value = 1 + randomObject.Next(6) 16.2 Random-Number Generation (Cont.)

13  2009 Pearson Education, Inc. All rights reserved. 13 ■You may also pass two arguments to Next to produce a range of numbers. value = randomObject.Next(1, 7) ' from 1 up to, but not including, 7 ■As with method Next, the range of values produced by method NextDouble is also usually different from the range needed in a particular application. ■By multiplying the value returned from method NextDouble you can produce Double values in the range from 0.0 to 6.0 (not including 6.0) value = 6 * randomObject.NextDouble() 16.2 Random-Number Generation (Cont.)

14  2009 Pearson Education, Inc. All rights reserved. 14 ■Figure 16.7 shows examples of the ranges returned by calls to methods Next and NextDouble. Figure 16.7 | Next and NextDouble method calls with corresponding ranges. 16.2 Random-Number Generation (Cont.)

15  2009 Pearson Education, Inc. All rights reserved. 15 When the player clicks the Play Button: Roll the dice using random numbers Display images corresponding to the numbers on the rolled dice Calculate the sum of both dice Select correct case based on the sum of the two dice: Case where first roll is 7 or 11 Display the winning message Case where first roll is 2, 3 or 12 Display the losing message 16.3 Constructing the Craps Game Application

16  2009 Pearson Education, Inc. All rights reserved. 16 Case where none of the preceding Cases are true Set the value of the point to the sum of the dice Display point value Display message to roll again Display images for user’s point Disable the Play Button Enable the Roll Button When the player clicks the Roll Button: Roll the dice using random numbers Display images corresponding to the numbers on the rolled dice Calculate the sum of both dice 16.3 Constructing the Craps Game Application (Cont.)

17  2009 Pearson Education, Inc. All rights reserved. 17 If the player rolls the same value as the point Display the winning message Disable the Roll Button Enable the Play Button Else If the player rolls a 7 Display the losing message Disable the Roll Button Enable the Play Button 16.3 Constructing the Craps Game Application (Cont.)

18  2009 Pearson Education, Inc. All rights reserved. 18 ■Use an ACE table to convert pseudocode into Visual Basic (Fig. 16.8). Figure 16.8 | ACE table for the Craps Game application. (Part 1 of 3.) Action/Control/Event (ACE) Table for the Craps Game Application

19  2009 Pearson Education, Inc. All rights reserved. 19 Figure 16.8 | ACE table for the Craps Game application. (Part 2 of 3.) Action/Control/Event (ACE) Table for the Craps Game Application (Cont.)

20  2009 Pearson Education, Inc. All rights reserved. 20 Figure 16.8 | ACE table for the Craps Game application. (Part 3 of 3.) Action/Control/Event (ACE) Table for the Craps Game Application (Cont.)

21  2009 Pearson Education, Inc. All rights reserved. 21 ■With enumerations, you can create constant identifiers. –In this application, enumerations are used to describe significant dice combinations. ■Enumerations: –describe their values –enhance program readability –ensure that numbers are consistent 16.3 Constructing the Craps Game Application (Cont.)

22  2009 Pearson Education, Inc. All rights reserved. 22 Figure 16.9 | Template Craps Game Form in Design view. Introducing Enumerations and Declaring Instance Variables ■Open the template application (Fig. 16.9).

23  2009 Pearson Education, Inc. All rights reserved. 23 Figure 16.10 | Enumeration DiceNames in the Craps Game application. Introducing Enumerations and Declaring Instance Variables (Cont.) ■Enumerations begin with the keyword Enum (line 3), and end with the keywords End Enum (line 10) (Fig. 16.10). Defining an enumeration

24  2009 Pearson Education, Inc. All rights reserved. 24 Good Programming Practice Use enumerations to group related constants and enhance code ­readability.

25  2009 Pearson Education, Inc. All rights reserved. 25 ■You can refer to the numbers using the enumeration name and the member-access operator. –For instance, use DiceNames.SNAKE_EYES for the number 2. –Note that you can assign the same value to multiple enumeration constants. –If no values are specified, the constants are automatically assigned consecutive values starting from 0. Introducing Enumerations and Declaring Instance Variables (Cont.)

26  2009 Pearson Education, Inc. All rights reserved. 26 Common Programming Error You can specify an enumeration’s type after its name by using the keyword As followed by Byte, SByte, Short, UShort, Integer, UInteger, Long, or ULong. If no type is specified, enumeration constants are of type Integer by default. Attempting to create enumerations of other types results in compilation errors.

27  2009 Pearson Education, Inc. All rights reserved. 27 Figure 16.11 | Instance variables added to the Craps Game application. Introducing Enumerations and Declaring Instance Variables (Cont.) ■Add constants representing the file names (Fig. 16.11). Declaring constants Declaring a variable to store point value Creating a Random object

28  2009 Pearson Education, Inc. All rights reserved. 28 ■The game of craps requires that you store the user ’ s point, once established on the first roll, for the game ’ s duration. –Variable myPoint stores the value of the dice on the first roll. –You use randomObject to “ roll ” the dice and generate those values. Introducing Enumerations and Declaring Instance Variables (Cont.)

29  2009 Pearson Education, Inc. All rights reserved. 29 Figure 16.12 | playButton_Click event handler definition. Coding the Play Button ’s Click Event Handler ■Double click the Play Button to generate the Play Button ’s Click event handler (Fig. 16.12). Initializing values for a new game “Rolling” the dice Removing images from PictureBoxes

30  2009 Pearson Education, Inc. All rights reserved. 30 ■Lines 30 – 31 remove any images from the PictureBox es used to display the point die. –Though there are no images when the application is first run, if the user chooses to continue playing after completing a game, the images from the previous game must be cleared. –Setting the Image property to keyword Nothing indicates that there is no image to display. Coding the Play Button ’s Click Event Handler (Cont.)

31  2009 Pearson Education, Inc. All rights reserved. 31 Figure 16.13 | Select Case statement in playButton_Click. ■Add a Select Case statement to respond to various die rolls (Fig. 16.13). Winning on the first roll Coding the Play Button ’s Click Event Handler (Cont.) Losing on the first roll

32  2009 Pearson Education, Inc. All rights reserved. 32 Figure 16.14 | Case Else statement in playButton_Click. ■If the player did not roll a 2, 3, 7, 11 or 12 ( Case Else ), then the value of the dice becomes the point and the player must roll again (Fig. 16.14). Coding the Play Button ’s Click Event Handler (Cont.) Player must match the point Display die images Allow player to roll again

33  2009 Pearson Education, Inc. All rights reserved. 33 Figure 16.15 | Rolling the dice in rollButton_Click. ■Generate the Roll Button ’s Click event handler (Fig. 16.15). Coding the Roll Button ’s Click Event Handler Rolling the dice

34  2009 Pearson Education, Inc. All rights reserved. 34 ■The If...Then statement (Fig. 16.16) determines whether the current roll matches the point. –If so, the program displays a winning message. Coding the Roll Button ’s Click Event Handler (Cont.) Figure 16.16 | Determining the outcome of a roll. Display winning message Display losing message

35  2009 Pearson Education, Inc. All rights reserved. 35 ■The ElseIf statement determines whether the sum of the dice in the current roll is 7. –If so, the application displays a message that the user has lost. –It then ends the game by disabling the Roll Button and enabling the Play Button. Coding the Roll Button ’s Click Event Handler (Cont.)

36  2009 Pearson Education, Inc. All rights reserved. 36 ■This application will roll and display dice many times as it executes. –Therefore, it is a good idea to create procedures to roll the dice and to display a die (Fig. 16.17). Using Random Numbers to Simulate Rolling Dice Figure 16.17 | RollDice procedure definition. Displaying die images Getting two random numbers Returning sum of dice

37  2009 Pearson Education, Inc. All rights reserved. 37 Figure 16.18 | DisplayDie procedure definition. ■Now define procedure DisplayDie to display the die images corresponding to the random numbers generated in procedure RollDice (Fig. 16.18). Using Random Numbers to Simulate Rolling Dice (Cont.) Displaying a die image

38  2009 Pearson Education, Inc. All rights reserved. 38 ■ Image.FromFile returns an Image object containing the image located at the path you specify. –To specify the location, pass a String representing the path to the FromFile method. –If the value of face is 1, the expression would represent the string images\ die1.png –The application searches for the specified file in the directory in which its executable file (.exe) is located. Using Random Numbers to Simulate Rolling Dice (Cont.)

39  2009 Pearson Education, Inc. All rights reserved. 39 Defining an enumeration ■Figure 16.19 presents the source code for the Craps Game application. Outline (1 of 5 )

40  2009 Pearson Education, Inc. All rights reserved. 40 Outline (2 of 5 ) Creating a Random object Removing the images from both point PictureBoxes

41  2009 Pearson Education, Inc. All rights reserved. 41 Outline (3 of 5 ) Using Enum constants

42  2009 Pearson Education, Inc. All rights reserved. 42 Outline (4 of 5 )

43  2009 Pearson Education, Inc. All rights reserved. 43 Outline (5 of 5 ) Generating random numbers Using code to display an image


Download ppt "T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 16 Craps Game Application Introducing Random-Number Generation and Enum."

Similar presentations


Ads by Google