© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random Number Generation and Enumerations Outline 12.1 Test-Driving the Craps Game Application 12.2 Random Number Generation 12.3 Using an enum in the Craps Game Application 12.4 Using Random Numbers in the Craps Game Application 12.5 Wrap-Up
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this tutorial, you will learn to: –Use simulation techniques that employ random-number generation. –Use the rand math library function to generate random numbers. –Use enumerations to enhance code readability.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12.1Test Driving the Craps Game Application
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12.1Test Driving the Craps Game Application (Cont.) Figure 12.1 Example of an initial appearance of Craps Game application. Figure 12.2 Player winning on the first roll by rolling an 11.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12.1Test Driving the Craps Game Application (Cont.) Figure 12.3 Player losing on the first roll by rolling a 3. Figure 12.6 Player losing by rolling a 7 before matching the point value.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12.1Test Driving the Craps Game Application (Cont.) Figure 12.5 Player winning the game by matching the point value before rolling a 7. Figure 12.6 Player losing by rolling a 7 before matching the point value.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. c standard utility library header file cstdlib is part of the C++ Standard Library. The C++ Standard Library includes 18 header files from the C Standard Library. The cstdlib include/header file contains the function declarations for commonly used library functions which either don't fit somewhere else, or, cannot be declared in the normal place for other reasons. cstdlib includes several general purpose functions, including dynamic memory management, random number generation, communication with the environment, integer arithmetic, searching, sorting and converting. Function prototypes declared in cstdlib are defined within the std namespace.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. c standard utility library header file (Cont.) Each C++ header file has the same name as the C language version but with a "c" prefix and no extension. In this case, the C++ equivalent for the C language header file is. If you look inside the cstlib file you will find the following preprocessor directive. #include
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. c standard utility library header file (Cont.) Figure 12.9 Including the and standard library header files so that the application can produce random numbers. Including the standard library header file
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Random Number Generation rand function :Generates a pseudorandom number between 0 and RAND_MAX RAND_MAX is defined in stdlib.h as… /* Maximum value that can be returned by the rand function. */ #define RAND_MAX 0x7fff #define directive substitutes token-string for all subsequent occurrences of an identifier in the source file. 0x7fff = 32,767 10
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Random Number Generation Seed the rand function with srand The value used to seed rand determines the sequence of pseudorandom numbers it generates Use srand(time(0)) to make rand generate a different sequence of pseudorandom numbers each time the application runs –time returns the current calendar time in seconds since 1/1/ :00:00. The function prototype for time is in the C++ Standard Library header file Figure Using the time and srand functions to seed the rand function. Seeding the rand function
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Random Number Generation (Cont.) Using the modulus operator to generate random numbers within a certain range
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Random Number Generation rollDice Example rollDice function simulates rolling two dice –Returns the value of the sum of the two dice Figure Declaring the rollDice function prototype. Declaring the rollDice function prototype
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Random Number Generation rollDice Example Figure Defining the rollDice function. Defining local variables to store die values Getting two random numbers and storing their sum
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Random Number Generation rollDice Example Using the rollDice function –“rolls” two dice, displays their values and returns their sum Figure “Rolling” the dice. “Rolling” the dice
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Using an enum in the Craps Game Application In this example, user-defined type Status can only be one of three values –CONTINUE, WON or LOST Figure Using the enum keyword to declare an enumeration. Declaring an enumeration In C and C++, enum types can be used to set up collections of user-defined (or programmer-defined) named integer constants. (The keyword enum is short for ''enumerated''.)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Using an enum in the Craps Game Application (Cont.) Enumeration constants start at 0, unless specified otherwise, and increment by 1 enum FooSize {SMALL=10, MEDIUM=100, LARGE=1000}; Attempting to assign another value to an enumeration constant after it has been defined is an error.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Using an enum in the Craps Game Application (Cont.) Figure Defining a variable to store values declared in the Status enumeration. Defining a variable of type Status
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Using an enum in the Craps Game Application (Cont.) A common syntax error is to assign the integer equivalent of an enumeration constant to a variable of the enumeration type. Figure default case in main. User must match the point Displaying the point Assigning an enumeration constant to a variable of the enumeration type.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Roll the two dice using random numbers Calculate and display the sum of the two dice Switch based on the sum of the two dice: Case where the sum is 7 or 11 Display the winning message Case where the sum is 2, 3 or 12 Display the losing message Default case Set the value of the point to the sum of the dice and display the value While the player has not won or lost Roll the two dice using random numbers Calculate and display the sum of the two dice If the player rolled the point Display the winning message If the player rolls a 7 Display the losing message Figure 12.8 Pseudocode for the Craps Game application. Using an enum in the Craps Game Application (Cont.)
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Using an enum in the Craps Game Application (Cont.) Figure Defining constants in the Craps Game application. Defining constants
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Using an enum in the Craps Game Application (Cont.) Figure switch statement in main. Winning on the first roll Losing on the first roll
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CrapsGame.cpp (1 of 6) Include the standard library header file Declare the rollDice function prototype
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CrapsGame.cpp (2 of 6) Define constants Declare an enumeration Define a variable of the Status type
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CrapsGame.cpp (3 of 6) Seed the rand function “Roll” the dice Win on the first roll Lose on the first roll
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CrapsGame.cpp (4 of 6) User must match the point Repeat until user has won or lost “Roll” the dice again Determine if user won
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CrapsGame.cpp (5 of 6) Determine if user lost Display game outcome
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. CrapsGame.cpp (6 of 6) Generate random numbers and calculate their sum Display the die values and their sum