Download presentation

Presentation is loading. Please wait.

Published byCarley Cockroft Modified over 3 years ago

1
Craps

2
/* * 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.

3
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.

4
Algorithm //1. Declare and initialize variables.

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

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

7
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].

8
//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.}

9
//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;}

10
//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;

11
/* * 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.

Similar presentations

Presentation is loading. Please wait....

OK

Building Java Programs Chapter 5

Building Java Programs Chapter 5

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google