-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLine.java
More file actions
298 lines (265 loc) · 10.1 KB
/
Copy pathCommandLine.java
File metadata and controls
298 lines (265 loc) · 10.1 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package edu.kit.informatik;
import java.util.Stack;
/**
* The interactive CommandLine to control the program flow.
*
* @author John Dederer
* @version 1.0
*/
public final class CommandLine {
/**
* The select command.
* Allows the player to take a token from the bag.
* Accepts only one argument.
* Prints out "OK" if draw was successful. Otherwise throws error.
*/
public static final String SELECT = "select";
/**
* The place command.
* Allows the player to place token if select command has been issued before.
* If draw is not successful, token will be put back into the bag.
* Accepts two arguments.
* Prints out "OK" if successful, "draw" if no token left and draw, "P1 wins" if P1 wins and for P2 the same.
*/
public static final String PLACE = "place";
/**
* The bag command.
* Prints out the available tokens in the bag.
* Does not accept any arguments.
*/
public static final String BAG = "bag";
/**
* The rowprint command.
* Prints out a row of the board.
* Accepts only one argument.
*/
public static final String ROWPRINT = "rowprint";
/**
* The colprint command.
* Prints out a column of the board.
* Accepts only one argument.
*/
public static final String COLPRINT = "colprint";
/**
* The quit command.
* Terminates the program without output.
* Does not accept any arguments.
*/
public static final String QUIT = "quit";
private static int draw = 0;
private static Stack<Token> tokenStack = new Stack<>();
private static Stack<String> stringStack = new Stack<>();
private static Stack<String> playerStack = new Stack<>();
private static Stack<String> state = new Stack<>();
/**
* Private constructor to avoid object generation.
*/
private CommandLine() {
}
/**
* Helper method which checks the number of given to the number of required arguments.
* Idea for this method came from example solutions of the Übungsaufgaben. Credits go to : Martin Löper.
*
* @param given the number of arguments given
* @param required the number of arguments required
*/
private static int checkParameterNumber(int given, int required) {
try {
if (given != required && required == 0) {
Terminal.printLine("Error, you provided " + given + " parameters but none are allowed.");
return 0;
} else if (given != required && required == 1) {
Terminal.printLine("Error, you provided " + given + " parameters but only one is allowed.");
return 0;
} else if (given != required) {
Terminal.printLine("Error, you provided " + given + " parameters but only two is allowed.");
return 0;
}
} catch (NumberFormatException e) {
Terminal.printLine("Error, " + e);
}
return 1;
}
/**
* Reads commands from command-line and executes them if they are valid.
* Method runs as long until quit command is issued.
* Idea for this method came from example solutions of the Übungsaufgaben. Credits go to : Martin Löper.
*
* @param pitch the pitch to execute the commands on
* @param bag the bag containing all tokens
*/
public static void startInteractiveSequence(Pitch pitch, Bag bag) {
while (true) {
String command = Terminal.readLine();
if (command == null) {
quit();
}
String[] split = command.replaceAll("\\s", ";").split(";");
switch (split[0]) {
case CommandLine.BAG:
if (checkParameterNumber(split.length - 1, 0) != 0) {
CommandLine.bag(bag);
}
break;
case CommandLine.SELECT:
if (checkParameterNumber(split.length - 1, 1) != 0) {
CommandLine.select(split[1], bag, pitch);
}
break;
case CommandLine.PLACE:
if (checkParameterNumber(split.length - 1, 2) != 0) {
CommandLine.place(split[1], split[2], bag, pitch);
}
break;
case CommandLine.ROWPRINT:
try {
if (checkParameterNumber(split.length - 1, 1) != 0) {
CommandLine.rowPrint(pitch, Integer.parseInt(split[1]));
}
} catch (NumberFormatException e) {
Terminal.printLine("Error, row number has to be an integer value.");
}
break;
case CommandLine.COLPRINT:
try {
if (checkParameterNumber(split.length - 1, 1) != 0) {
CommandLine.colPrint(pitch, Integer.parseInt(split[1]));
}
} catch (NumberFormatException e) {
Terminal.printLine("Error, column number has to be an integer value.");
}
break;
case CommandLine.QUIT:
checkParameterNumber(split.length - 1, 0);
CommandLine.quit();
break;
default:
if (command.trim().length() == 0) {
Terminal.printLine("Error, please enter a command.");
} else {
Terminal.printLine("Error, command \"" + split[0] + "\" not found. "
+ "Only the following are valid: " + QUIT + ", " + BAG + ", "
+ SELECT + ", " + PLACE + ", " + ROWPRINT + ", " + COLPRINT + ".");
}
}
}
}
/**
* The select command.
*
* @see CommandLine#SELECT
*/
private static void select(String token, Bag bag, Pitch pitch) {
if (!state.isEmpty() && state.peek().contentEquals("done")) {
Terminal.printLine("Error, game is finished. This command is not available now.");
} else {
try {
if (stringStack.isEmpty()) {
Token temp = pitch.select(Integer.parseInt(token), bag);
if (temp != null) {
tokenStack.push(temp);
stringStack.push("select");
}
if (playerStack.isEmpty()) {
playerStack.push("P1");
}
if (playerStack.peek().contentEquals("P2")) {
playerStack.push("P1");
}
if (playerStack.peek().contentEquals("P1")) {
playerStack.push("P2");
}
} else {
Terminal.printLine("Error, \"select\" command is not allowed.");
}
} catch (NumberFormatException e) {
Terminal.printLine("Error, you have to provide a valid integer number.");
}
}
}
/**
* The place command.
*
* @see CommandLine#PLACE
*/
private static void place(String row, String column, Bag bag, Pitch pitch) {
int drawTemp = draw;
if (!state.isEmpty() && state.peek().contentEquals("done")) {
Terminal.printLine("Error, game is finished. This command is not available now.");
} else {
try {
if (!stringStack.isEmpty()) {
if (!stringStack.isEmpty() && playerStack.peek().contentEquals("P1")) {
playerStack.push("P2");
}
if (!stringStack.isEmpty() && playerStack.peek().contentEquals("P2")) {
playerStack.push("P1");
}
if (pitch.place(Integer.parseInt(row), Integer.parseInt(column), bag, tokenStack.peek())) {
stringStack.clear();
tokenStack.clear();
draw++;
if (!pitch.checkWin().contentEquals("win") && !bag.getBag().isEmpty()) {
Terminal.printLine("OK");
}
}
if (!pitch.checkWin().contentEquals("win") && bag.getBag().isEmpty()) {
Terminal.printLine("draw");
state.push("done");
}
if (pitch.checkWin().contentEquals("win")) {
Terminal.printLine(playerStack.peek() + " wins");
Terminal.printLine(String.valueOf(draw - 1));
state.push("done");
}
if (drawTemp == draw) {
bag.addToken(tokenStack.peek());
stringStack.clear();
tokenStack.clear();
}
} else {
Terminal.printLine("Error, \"place\" command is not allowed.");
}
} catch (NumberFormatException e) {
Terminal.printLine("Error, no valid integer number has been provided.");
}
}
}
/**
* The bag command.
*
* @see CommandLine#BAG
*/
private static void bag(Bag bag) {
String output = "";
for (Token token : bag.getBag()) {
String temp = token.getNumber() + " ";
output = output + temp;
}
Terminal.printLine(output);
}
/**
* The rowprint command.
*
* @see CommandLine#ROWPRINT
*/
private static void rowPrint(Pitch pitch, int row) {
pitch.rowPrint(row);
}
/**
* The colprint command.
*
* @see CommandLine#COLPRINT
*/
private static void colPrint(Pitch pitch, int column) {
pitch.colPrint(column);
}
/**
* The quit command.
*
* @see #QUIT
*/
private static void quit() {
System.exit(0);
}
}