Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.

Slides:



Advertisements
Similar presentations
IS Programming Fundamentals 1 (Lec) Date: September 14, Array.
Advertisements

Java Control Statements
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Iterations for loop. Outcome Introduction to for loop The use of for loop instead of while loop Nested for loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Programming Methodology (1). Implementing Methods main.
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Picture It Very Basic Game Picture Pepper. Original Game import java.util.Scanner; public class Game { public static void main() { Scanner scan=new Scanner(System.in);
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Building Java Programs
Computer Programming Lab 8.
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Soal latihan java. Soal 1 public class MyClass { public static void main() { int arrmul [] [] = new int [4] [5]; int k = 0; for (int i=0;i
EGR 141 Computer Problem Solving in Engineering and Computer Science
Arrays part 2 Applications & such. Returning an array from a method A method can return an array, just like it can return any other kind of variable;
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
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.
Building Java Programs Chapter 5
A Java API Package java.security  The Java Security Package contains classes and interfaces that are required by many Java programs.  This package is.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Fall 2008ACS-1903 for Loop Reading files String conversions Random class.
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.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
The game of Craps Rules of play: 1. Played with two dice (six faces to a die – numbers 1-6 per face) 2. Sequence of betting rounds (or just rounds) 3.
Nesting Loops (That’s loops inside of loops. Why not?)
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Random numbers. 2 The Random class A Random object generates pseudo-random numbers. –Class Random is found in the java.util package. import java.util.*;
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
CS 108 Computing Fundamentals Notes for Tuesday, February 24, 2015.
Chapter 6 Methods: A Deeper Look. Objectives In this chapter you will learn: How static methods and fields are associated with an entire class rather.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 16 – Craps Game Application Introducing Random-Number.
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Objective of the lesson Use Blockly to make a dice for Snakes and Ladders All of you will: – Make an image which displays when you press a button Most.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Building Java Programs
Building Java Programs
Building Java Programs
null, true, and false are also reserved.
LRobot Game.
محاضرة 1: مقدمة للمسـاق و مراجعـة للأساسيـات
Building Java Programs
AP Java Warm-up Boolean Array.
Building Java Programs
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
Building Java Programs
Building Java Programs
Indefinite loop variations
Fun… Tree Diagrams… Probability.
Presentation transcript:

Craps

/* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate a single game of craps (dice). * desc. : program to simulate a single game of craps (dice). * * Here's how the game is played: * Here's how the game is played: * In craps you roll two dice: * In craps you roll two dice: * 1. If the first roll is a 7 or 11, you win. * 1. If the first roll is a 7 or 11, you win. * 2. If the first roll is a 2, 3, or 12, you lose. * 2. If the first roll is a 2, 3, or 12, you lose. * 3. If the first roll is a 4, 5, 6, 8, 9, or 10, * 3. If the first roll is a 4, 5, 6, 8, 9, or 10, * you keep on rolling the dice until you either: * you keep on rolling the dice until you either: * a) win by repeating the first number you roll * a) win by repeating the first number you roll * (you "make your point"), * (you "make your point"), * or * or * b) lose by rolling a 7 (you "crap out"). * b) lose by rolling a 7 (you "crap out"). */ */ How to play craps.

Algorithm 1. Declare and initialize variables. 2. Roll the dice (the first time). 1.Win (7 or 11). 2.Lose (2, 3, or 12). 3.Keep rolling.

Algorithm //1. Declare and initialize variables.

Algorithm Random d1 = new Random(); //for die 1 Random d2 = new Random(); //for die 2

Algorithm //2. Roll the dice (the first time).

Algorithm int firstRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; System.out.println( "Your first roll is " + firstRoll + "." ); switch (firstRoll) { //Win (7 or 11). case 7: case 11: System.out.println( "You win!" ); break; //Lose (2, 3, or 12). //Lose (2, 3, or 12). //Keep rolling. //Keep rolling.} Note: d1.nextInt( k ) returns a random number in [0..k) or [0..k-1].

//2. Roll the dice (the first time). int firstRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; System.out.println( "Your first roll is " + firstRoll + "." ); switch (firstRoll) { //Win (7 or 11). case 7: case 11: System.out.println( "You win!" ); break; //Lose (2, 3, or 12). //Lose (2, 3, or 12). case 2: case 2: case 3: case 3: case 12: case 12: System.out.println( "You lose!" ); break; //Keep rolling. //Keep rolling.}

//2. Roll the dice (the first time). int firstRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; System.out.println( "Your first roll is " + firstRoll + "." ); switch (firstRoll) { //Win (7 or 11). case 7: case 11: System.out.println( "You win!" ); break; //Lose (2, 3, or 12). //Lose (2, 3, or 12). case 2: case 2: case 3: case 3: case 12: case 12: System.out.println( "You lose!" ); break; //Keep rolling. //Keep rolling. default: default:…break;}

//Keep rolling. default: boolean timeToQuit = false; while (!timeToQuit) { //same as while (timeToQuit==false) int nextRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; System.out.println( "You rolled " + nextRoll + "." ); if (nextRoll == firstRoll) { System.out.println( "You made your point. You win!" ); timeToQuit = true; } else if (nextRoll == 7) { System.out.println( "You crapped out. You lose!" ); timeToQuit = true; } } //end while break;

/* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate a single game of craps (dice). * desc. : program to simulate a single game of craps (dice). * * Here's how the game is played: * Here's how the game is played: * In craps you roll two dice: * In craps you roll two dice: * 1. If the first roll is a 7 or 11, you win. * 1. If the first roll is a 7 or 11, you win. * 2. If the first roll is a 2, 3, or 12, you lose. * 2. If the first roll is a 2, 3, or 12, you lose. * 3. If the first roll is a 4, 5, 6, 8, 9, or 10, * 3. If the first roll is a 4, 5, 6, 8, 9, or 10, * you keep on rolling the dice until you either: * you keep on rolling the dice until you either: * a) win by repeating the first number you roll * a) win by repeating the first number you roll * (you "make your point"), * (you "make your point"), * or * or * b) lose by rolling a 7 (you "crap out"). * b) lose by rolling a 7 (you "crap out"). */ */ import java.util.Random; class Craps { public static void main ( String args[] ) { public static void main ( String args[] ) { //1. Declare and initialize variables. //1. Declare and initialize variables. Random d1 = new Random(); //for die 1 Random d1 = new Random(); //for die 1 Random d2 = new Random(); //for die 2 Random d2 = new Random(); //for die 2 //2. Roll the dice (the first time). //2. Roll the dice (the first time). int firstRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; int firstRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; System.out.println( "Your first roll is " + firstRoll + "." ); System.out.println( "Your first roll is " + firstRoll + "." ); switch (firstRoll) { switch (firstRoll) { //Win (7 or 11). //Win (7 or 11). case 7: case 7: case 11: case 11: System.out.println( "You win!" ); System.out.println( "You win!" ); break; break; //Lose (2, 3, or 12). //Lose (2, 3, or 12). case 2: case 2: case 3: case 3: case 12: case 12: System.out.println( "You lose!" ); System.out.println( "You lose!" ); break; break; //Keep rolling. //Keep rolling. default: default: boolean timeToQuit = false; boolean timeToQuit = false; while (!timeToQuit) { //same as while (timeToQuit==false) while (!timeToQuit) { //same as while (timeToQuit==false) int nextRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; int nextRoll = d1.nextInt(6)+1 + d2.nextInt(6)+1; System.out.println( "You rolled " + nextRoll + "." ); System.out.println( "You rolled " + nextRoll + "." ); if (nextRoll == firstRoll) { if (nextRoll == firstRoll) { System.out.println( "You made your point. You win!" ); System.out.println( "You made your point. You win!" ); timeToQuit = true; timeToQuit = true; } else if (nextRoll == 7) { } else if (nextRoll == 7) { System.out.println( "You crapped out. You lose!" ); System.out.println( "You crapped out. You lose!" ); timeToQuit = true; timeToQuit = true; } } //end while } //end while break; break; } //end switch } //end switch System.out.println( "Please play again!" ); System.out.println( "Please play again!" ); } //end main } //end main } //end class Complete program.