-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractiveLeverGame.java
More file actions
187 lines (168 loc) · 5.48 KB
/
InteractiveLeverGame.java
File metadata and controls
187 lines (168 loc) · 5.48 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package org.collins.kent.leverage;
import java.util.Collections;
import java.util.Scanner;
import java.util.stream.Collectors;
public class InteractiveLeverGame extends LeverMachine {
private Scanner s = new Scanner(System.in);
private long jackpot = 0;
private long earnings = 0;
private boolean inProgress = true;
public static final String QUIT = "q";
public static final String RAISE = "r";
public static final String LOWER = "l";
public static final String STATUS = "s";
public static final String CLAIM = "c";
public static final String BITS = "b";
public static final String MODIFY = "m";
public static final String HINT = "h";
public InteractiveLeverGame() {
System.out.println(this.buildGameInstructionString());
setRandomJackpot();
System.out.println(this.buildStatusString());
while (inProgress) {
play();
}
System.out.println(this.buildFinalResultString());
}
public void play() {
printCommandString();
String command = s.next();
if (QUIT.equalsIgnoreCase(command)) {
inProgress = false;
} else if (STATUS.equalsIgnoreCase(command)) {
System.out.println(this.buildStatusString());
} else if (RAISE.equalsIgnoreCase(command)) {
System.out.println("Which lever to raise?");
int lever;
try {
lever = s.nextInt();
super.raiseLever(lever);
} catch (NonExistentLeverException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(
"Not a lever. " + super.getDescription());
}
} else if (LOWER.equalsIgnoreCase(command)) {
System.out.println("Which lever to lower?");
int lever;
try {
lever = s.nextInt();
super.lowerLever(lever);
} catch (NonExistentLeverException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(
"Not a lever. " + super.getDescription());
}
} else if (MODIFY.equalsIgnoreCase(command)) {
System.out.println(
"How many levers should this machine have?");
int numLevers = s.nextInt();
this.setNumLevers(numLevers);
} else if (BITS.equalsIgnoreCase(command)) {
System.out.println(this.toString());
} else if (CLAIM.equalsIgnoreCase(command)) {
long value = super.getCurrentMachineValue();
if (value == jackpot) {
earnings += jackpot;
System.out.println(this.buildCorrectJackpotString());
setRandomJackpot();
System.out.println(this.buildJackpotString());
this.getRaisedLevers().clear();
System.out.println(
"All levers have been reset to their lowered positions.");
} else {
System.out.println(
this.buildWrongJackpotString(value));
}
}
else if (HINT.equalsIgnoreCase(command)) {
System.out.println(this.buildHintString());
}
}
private void printCommandString() {
System.out.println(
"(R)aise a lever, (L)ower a lever, (S)tatus report, \n(C)laim the jackpot, request a (H)int, or (Q)uit?: ");
}
private void setRandomJackpot() {
jackpot = (long) (Math.random()
* Math.pow(2, getNumLevers() - 1));
}
private int getNumLeversNeeded() {
String jackpotString = Long.toBinaryString(jackpot);
int numLevers = 0;
for (char c : jackpotString.toCharArray()) {
numLevers += c == '1' ? 1 : 0;
}
return numLevers;
}
private String buildGameInstructionString() {
StringBuilder sb = new StringBuilder("");
sb.append("======== INSTRUCTIONS ========\n");
sb.append("Welcome to the Magical Mystery Machine. \n");
sb.append(
"Raise and lower levers to adjust the machine's secret value.\n");
sb.append(
"When you think the machine's value matches the jackpot value, claim the jackpot.");
return sb.toString();
}
private String buildJackpotString() {
return "The jackpot is set to $" + jackpot;
}
private String buildWrongJackpotString(long incorrectAmount) {
StringBuilder sb = new StringBuilder();
boolean plural = this.getRaisedLevers().size() != 1;
if (plural) {
sb.append(
"Sorry -- the raised levers produce a value of ");
} else {
sb.append(
"Sorry -- the raised lever produces a value of ");
}
sb.append(incorrectAmount + " but the jackpot amount is "
+ jackpot + "\n");
if (incorrectAmount > jackpot) {
sb.append("You need to lower one or more levers.");
} else {
sb.append("You need to raise some more levers.");
}
return sb.toString();
}
private String buildCorrectJackpotString() {
return "WINNER! You won the jackpot of $" + jackpot
+ " bringing your total winnings to $" + earnings;
}
private String buildFinalResultString() {
return "You won a total of $" + earnings;
}
private String buildStatusString() {
int numLifted = super.getRaisedLevers().size();
StringBuilder sb = new StringBuilder();
boolean plural = super.getRaisedLevers().size() != 1;
sb.append("======== CURRENT STATUS\n");
sb.append(this.getDescription());
sb.append(".\nThe following ");
sb.append(plural ? "levers are" : "lever is");
sb.append(" raised: ");
sb.append(numLifted == 0 ? "none"
: super.getRaisedLevers().stream()
.sorted(Collections.reverseOrder())
.collect(Collectors.toList()));
sb.append("\nThe jackpot is set to $");
sb.append(jackpot);
return sb.toString();
}
private String buildHintString() {
StringBuilder sb = new StringBuilder("For a jackpot of ");
sb.append("$");
sb.append(jackpot);
sb.append(" you will need to raise exactly ");
int num = getNumLeversNeeded();
sb.append(num == 1 ? "one lever." : num + " levers.");
return sb.toString();
}
public static void main(String[] args) {
InteractiveLeverGame game = new InteractiveLeverGame();
}
}