Download presentation
Presentation is loading. Please wait.
Published byFrank Anthony Modified over 9 years ago
1
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number Generation Outline 16.1 Test-Driving the Craps Game Application 16.2 Random-Number Generation 16.3 Using Enumerations in the Craps Game Application 16.4 Using Random Numbers in the Craps Game Application 16.5 Wrap-Up
2
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Code simulation techniques that employ random-number generation. –Use class Random ’s methods. –Generate random numbers. –Use enumerations to enhance code readability.
3
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 16.1 Test-Driving the Craps Game Application
4
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 16.1 Test-Driving the Craps Game Application Figure 16.1 Craps Game application initial appearance. Opening the completed application –Debug > Start
5
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 16.1 Test-Driving the Craps Game Application Figure 16.2 Player wins on first roll by rolling 7 or 11.
6
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 16.1 Test-Driving the Craps Game Application Figure 16.3 Player loses on first roll by rolling 2, 3 or 12.
7
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 16.1 Test-Driving the Craps Game Application Figure 16.4 First roll sets the point that the player must match to win.
8
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 16.1 Test-Driving the Craps Game Application Figure 16.5 Winning the game by matching your point before rolling a 7.
9
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 16.1 Test-Driving the Craps Game Application Figure 16.6 Losing by rolling a 7 before matching your point.
10
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 16.2 Random-Number Generation Random-number generation introduces element of chance Pseudorandom numbers –Sequence of numbers generated by a mathematical formula –Simulates random number generation
11
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 16.2 Random-Number Generation Methods for generating random numbers –Next method Generates a random int between 0 and the largest possible int, Int32.MaxValue Can be called with 0, 1, or 2 arguments –NextDouble method Generates a positive double value greater than 0.0 and less than 1.0
12
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 16.2 Random-Number Generation Figure 16.7 Next and NextDouble method calls with corresponding ranges.
13
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 16.3 Using Enumerations in the Craps Game Application
14
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 16.3 Using Enumerations in the Craps Game Application
15
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 16.3 Using Enumerations in the Craps Game Application Figure 16.9 Template Craps Game Form in design view. Empty PictureBox es
16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 16.3 Using Enumerations in the Craps Game Application Namespaces –A group of related classes in the Framework Class Library (FCL) –Contain classes that provide methods for using files, graphics, multimedia and more –Must add System.IO namespace with a using directive to access member classes and methods used for accessing files and directories
17
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 16.3 Using Enumerations in the Craps Game Application Figure 16.10 Adding a using directive to the Craps Game application. Using the System.IO namespace
18
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 16.3 Using Enumerations in the Craps Game Application Enumerations –A group of related, named constants –Gives meaningful names for use in applications –Enhances program readability
19
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 16.3 Using Enumerations in the Craps Game Application Declaring an enumeration –Begins with keyword enum followed by enumeration’s name, opening and closing braces –You can assign the same value to multiple enumeration constants
20
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 16.3 Using Enumerations in the Craps Game Application Figure 16.11 Enumeration DiceNames in the Craps Game application. Defining an enumeration
21
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 16.3 Using Enumerations in the Craps Game Application Figure 16.12 Instance variables added to the Craps Game application. Declaring constantsDeclaring variable to store point value Creating a Random object
22
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 16.3 Using Enumerations in the Craps Game Application To build the Craps Game application, you will need: –To access images that display the six faces of a die string s used to store prefix and suffix of the file name –An int variable to store point value
23
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 16.4 Using Random Numbers in the Craps Game Application Coding the Play Button ’s Click event handler –Initialize values for a new game Set point value to 0 –Remove images from PictureBox es –Roll the dice
24
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 16.4 Using Random Numbers in the Craps Game Application Figure 16.13 btnPlay_Click event handler definition. Initializing values for a new game Removing images from PictureBox es “Rolling” the dice
25
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 16.4 Using Random Numbers in the Craps Game Application Using a switch statement to determine the result of the roll –First case statement selects 7 and 11 Result: “You win!!!” –Second case statement selects 2, 3, and 12 Result: “Sorry, you lose.” –Fix incorrect comment indentation
26
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 16.4 Using Random Numbers in the Craps Game Application Figure 16.14 The switch statement in btnPlay_Click. Winning on the first rollLosing on the first roll
27
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 16.4 Using Random Numbers in the Craps Game Application Using the default case statement –Continues the game Value of the dice becomes point Player rolls again
28
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 16.4 Using Random Numbers in the Craps Game Application Figure 16.15 The default statement in btnPlay_Click. Player must match the point Display die images Allow player to roll again
29
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 16.4 Using Random Numbers in the Craps Game Application Coding the Roll Button ’s Click event handler –User clicks Roll Button Roll the dice Display die images Store sum of the dice in variable intSum
30
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 16.4 Using Random Numbers in the Craps Game Application Figure 16.16 Rolling the dice in btnRoll_Click. “Rolling” the dice
31
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 16.4 Using Random Numbers in the Craps Game Application Determining the result when player clicks Roll Button –User matches point User wins and the game ends –User rolls a 7 ( DiceNames.CRAPS ) User loses and the game ends
32
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 16.4 Using Random Numbers in the Craps Game Application Figure 16.17 Determining the outcome of a roll. Display winning message Display losing message
33
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 16.4 Using Random Numbers in the Craps Game Application Using random numbers to simulate rolling dice –Two methods, one rolls dice, other displays dice –RollDice procedure Sets the values of intDie1 and intDie2 to random int values between 1 and 6 –m_objRandom.Next(1, 7) Makes two calls to DisplayDie Returns the sum of the values of the dice
34
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 16.4 Using Random Numbers in the Craps Game Application Figure 16.18 RollDice method definition. Getting two random numbers Displaying die images Returning sum of dice
35
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 16.4 Using Random Numbers in the Craps Game Application Displaying the dice images –Create Image object –Determine current directory Use Directory.GetCurrentDirectory method –Append m_strFILE_PREFIX & intFace & m_strFILE_SUFFIX to add the appropriate file name to location of the file
36
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 16.4 Using Random Numbers in the Craps Game Application Figure 16.19 DisplayDie procedure definition. Displaying a die image
37
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 CrapsGame.cs (1 of 8) Using the System.IO namespace
38
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 CrapsGame.cs (2 of 8) Declaring an enumeration
39
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 CrapsGame.cs (3 of 8) Creating a Random object
40
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 CrapsGame.cs (4 of 8)
41
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 CrapsGame.cs (5 of 8)
42
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 CrapsGame.cs (6 of 8)
43
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 CrapsGame.cs (7 of 8)
44
Outline © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 CrapsGame.cs (8 of 8) Using code to display an image Generating random numbers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.