CS 1073 Introductory Programming
|
The above is the simplest form of street craps. For the rules of the more complicated casino craps see: Casino craps.
// Craps: play one game of craps
public class Craps {
public static void main(String[] args) {
int dice; // initial roll
dice = (int)(6.0*Math.random() + 1.0) +
(int)(6.0*Math.random() + 1.0);
if (dice == 2 || dice == 3 || dice == 12) {
System.out.println("Immediate loss with: " + dice);
}
else if (dice == 7 || dice == 11) {
System.out.println("Immediate win with: " + dice);
}
else {
int point = dice; // point: 4, 5, 6, 8, 9, or 10
System.out.println("Point: " + point);
while (true) { // keep rolling
dice = (int)(6.0*Math.random() + 1.0) +
(int)(6.0*Math.random() + 1.0);
System.out.println("\nNew roll: " + dice);
if (dice == point) {
System.out.println("Made point, won");
break; // break out of loop, a win
}
if (dice == 7) {
System.out.println("Lost with 7");
break; // break out of loop, a loss
}
else System.out.println("No help");
}
}
}
}
Here are results of a sequence of runs:
Point: 9 New roll: 12 No help New roll: 7 Lost with 7
Point: 4 New roll: 9 No help New roll: 6 No help New roll: 4 Made point, won
Immediate win with: 7
Immediate loss with: 3
Point: 4 New roll: 5 No help New roll: 5 No help New roll: 3 No help New roll: 11 No help New roll: 11 No help New roll: 8 No help New roll: 10 No help New roll: 5 No help New roll: 10 No help New roll: 5 No help New roll: 10 No help New roll: 8 No help New roll: 7 Lost with 7