© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Advertisements

© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 13 – Salary Survey Application: Introducing.
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
© 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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 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.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
Chapter 7 Methods: A Deeper Look Visual C# 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
1 Lecture 3 Part 1 Functions with math and randomness.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Flag Quiz Application Introducing One-Dimensional Arrays and ComboBox es.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - Welcome Application: Introduction to C++
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 2 - HelloWorld Application: Introduction to.
 Monday 10/18/2010  Content: Week 1 – Week 6  Format:  Multiple choice questions  Matching questions  Determine what’s wrong  Determine the results.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Interest Calculator Application Introducing the For...Next Repetition Statements.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhancing the Wage Calculator Application Introducing Function Procedures and.
© 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
© 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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Ticket Information Application Introducing Sequential-Access Files.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Shipping Hub Application.
© 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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "___________________" in Java Purpose –Reuse code –Modularize.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
Playing with Dice 2/20/2016Copyright © 2010 … REMTECH, inc … All Rights Reserved1 ● Rolling a Single Die – 6 possible outcomes (1 – 6) ● Rolling Dice is.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Introduction Program Components in C++ Math Library Functions Functions.
Chapter 10 - JavaScript: Functions
Program Modules in C# Modules The .NET Framework Class Library (FCL)
More About Objects and Methods
Presentation transcript:

© 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