© Copyright 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
© Copyright 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Craps Game Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Craps Game Application Figure 16.1 Craps Game application initial appearance. Opening the completed application –Debug > Start
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Craps Game Application Figure 16.2 Player wins on first roll by rolling 7 or 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Craps Game Application Figure 16.3 Player loses on first roll by rolling 2, 3 or 12.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Craps Game Application Figure 16.4 First roll sets the point that the player must match to win.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Craps Game Application Figure 16.5 Winning the game by matching your point before rolling a 7.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Craps Game Application Figure 16.6 Losing by rolling a 7 before matching your point.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Random-Number Generation Random-number generation introduces element of chance Pseudorandom numbers –Sequence of numbers generated by a mathematical formula –Simulates random number generation
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Random-Number Generation Figure 16.7 Next and NextDouble method calls with corresponding ranges.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Enumerations in the Craps Game Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Enumerations in the Craps Game Application
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Enumerations in the Craps Game Application Figure 16.9 Template Craps Game Form in design view. Empty PictureBox es
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Enumerations in the Craps Game Application Figure Adding a using directive to the Craps Game application. Using the System.IO namespace
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Enumerations in the Craps Game Application Enumerations –A group of related, named constants –Gives meaningful names for use in applications –Enhances program readability
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Enumerations in the Craps Game Application Figure Enumeration DiceNames in the Craps Game application. Defining an enumeration
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Enumerations in the Craps Game Application Figure Instance variables added to the Craps Game application. Declaring constantsDeclaring variable to store point value Creating a Random object
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Random Numbers in the Craps Game Application Figure btnPlay_Click event handler definition. Initializing values for a new game Removing images from PictureBox es “Rolling” the dice
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Random Numbers in the Craps Game Application Figure The switch statement in btnPlay_Click. Winning on the first rollLosing on the first roll
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Random Numbers in the Craps Game Application Figure The default statement in btnPlay_Click. Player must match the point Display die images Allow player to roll again
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Random Numbers in the Craps Game Application Figure Rolling the dice in btnRoll_Click. “Rolling” the dice
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Random Numbers in the Craps Game Application Figure Determining the outcome of a roll. Display winning message Display losing message
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Random Numbers in the Craps Game Application Figure RollDice method definition. Getting two random numbers Displaying die images Returning sum of dice
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Using Random Numbers in the Craps Game Application Figure DisplayDie procedure definition. Displaying a die image
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 CrapsGame.cs (1 of 8) Using the System.IO namespace
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 CrapsGame.cs (2 of 8) Declaring an enumeration
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 CrapsGame.cs (3 of 8) Creating a Random object
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 CrapsGame.cs (4 of 8)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 CrapsGame.cs (5 of 8)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 CrapsGame.cs (6 of 8)
Outline © Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 CrapsGame.cs (7 of 8)
Outline © Copyright 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