Repository navigation
Expand file tree
/
Copy pathCraps.java
More file actions
125 lines (109 loc) · 4.03 KB
/
Copy pathCraps.java
File metadata and controls
125 lines (109 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.Random;
import java.util.Scanner;
/**
* Port of Craps from BASIC to Java 17.
*/
public class Craps {
public static final Random random = new Random();
public static void main(String[] args) {
System.out.println("""
CRAPS
CREATIVE COMPUTING MORRISTOWN, NEW JERSEY
2,3,12 ARE LOSERS; 4,5,6,8,9,10 ARE POINTS; 7,11 ARE NATURAL WINNERS.
""");
double winnings = 0.0;
do {
winnings = playCraps(winnings);
} while (stillInterested(winnings));
winningsReport(winnings);
}
public static double playCraps(double winnings) {
double wager = getWager();
System.out.println("I WILL NOW THROW THE DICE");
int roll = rollDice();
double payout = switch (roll) {
case 7, 11 -> naturalWin(roll, wager);
case 2, 3, 12 -> lose(roll, wager);
default -> setPoint(roll, wager);
};
return winnings + payout;
}
public static int rollDice() {
return random.nextInt(1, 7) + random.nextInt(1, 7);
}
private static double setPoint(int point, double wager) {
System.out.printf("%1$ d IS THE POINT. I WILL ROLL AGAIN%n",point);
return makePoint(point, wager);
}
private static double makePoint(int point, double wager) {
int roll = rollDice();
if (roll == 7)
return lose(roll, wager);
if (roll == point)
return win(roll, wager);
System.out.printf("%1$ d - NO POINT. I WILL ROLL AGAIN%n", roll);
return makePoint(point, wager); // recursive
}
private static double win(int roll, double wager) {
double payout = 2 * wager;
System.out.printf("%1$ d - A WINNER.........CONGRATS!!!!!!!!%n", roll);
System.out.printf("%1$ d AT 2 TO 1 ODDS PAYS YOU...LET ME SEE...$%2$3.2f%n",
roll, payout);
return payout;
}
private static double lose(int roll, double wager) {
String msg = roll == 2 ? "SNAKE EYES.":"CRAPS";
System.out.printf("%1$ d - %2$s...YOU LOSE.%n", roll, msg);
System.out.printf("YOU LOSE $%3.2f%n", wager);
return -wager;
}
public static double naturalWin(int roll, double wager) {
System.out.printf("%1$ d - NATURAL....A WINNER!!!!%n", roll);
System.out.printf("%1$ d PAYS EVEN MONEY, YOU WIN $%2$3.2f%n", roll, wager);
return wager;
}
public static void winningsUpdate(double winnings) {
System.out.println(switch ((int) Math.signum(winnings)) {
case 1 -> "YOU ARE NOW AHEAD $%3.2f".formatted(winnings);
case 0 -> "YOU ARE NOW EVEN AT 0";
default -> "YOU ARE NOW UNDER $%3.2f".formatted(-winnings);
});
}
public static void winningsReport(double winnings) {
System.out.println(
switch ((int) Math.signum(winnings)) {
case 1 -> "CONGRATULATIONS---YOU CAME OUT A WINNER. COME AGAIN!";
case 0 -> "CONGRATULATIONS---YOU CAME OUT EVEN, NOT BAD FOR AN AMATEUR";
default -> "TOO BAD, YOU ARE IN THE HOLE. COME AGAIN.";
}
);
}
public static boolean stillInterested(double winnings) {
System.out.print(" IF YOU WANT TO PLAY AGAIN PRINT 5 IF NOT PRINT 2 ");
int fiveOrTwo = (int)getInput();
winningsUpdate(winnings);
return fiveOrTwo == 5;
}
public static double getWager() {
System.out.print("INPUT THE AMOUNT OF YOUR WAGER. ");
return getInput();
}
public static double getInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("> ");
while (true) {
try {
return scanner.nextDouble();
} catch (Exception ex) {
try {
scanner.nextLine(); // flush whatever this non number stuff is.
} catch (Exception ns_ex) { // received EOF (ctrl-d or ctrl-z if windows)
System.out.println("END OF INPUT, STOPPING PROGRAM.");
System.exit(1);
}
}
System.out.println("!NUMBER EXPECTED - RETRY INPUT LINE");
System.out.print("> ");
}
}
}