© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.

Slides:



Advertisements
Similar presentations
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
Page 1 of 26 Javascript/Jscript Ch 7,8,9,10 Vadim Parizher Computer Science Department California State University, Northridge Spring 2003 Slides from.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2007 Pearson Education, Inc. All rights reserved C Functions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Lab Session-9 CSIT-121 Fall 2003 w Random Number Generation w Designing a Game.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
1 Lecture 3 Part 1 Functions with math and randomness.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
 Monday 10/18/2010  Content: Week 1 – Week 6  Format:  Multiple choice questions  Matching questions  Determine what’s wrong  Determine the results.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Beginning C++ Through Game Programming, Second Edition
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Part II © Copyright by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
CMSC 1041 Functions II Functions that return a value.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Header files and Library Functions) Outline.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
EPSII 59:006 Spring Call-by-value example #include void increment(int); //prototype for increment function int main(void) { int a=1; printf("Value.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Switch Case, Enums, and Random Numbers CS 242 Tarik Booker California State University, Los Angeles.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
Chapter 9: Value-Returning Functions
Functions Course conducted by: Md.Raihan ul Masood
CISC181 Introduction to Computer Science Dr
Functions and an Introduction to Recursion
Deitel- C:How to Program (5ed)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 6 - Functions Outline 5.1 Introduction
6 Functions.
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

© 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