-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
107 lines (90 loc) · 3.32 KB
/
Main.java
File metadata and controls
107 lines (90 loc) · 3.32 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
/**
* Starts the game for one client.
*
* Note:
* Use the verbose flag for outputting game information.
* Use the fast flag for using 100ms move deadline instead of 1000ms.
* Use the init flag if you want this client to initialise the game, that
* is, send a starting board without moving for the other client to move
* first.
*/
public class Main {
public static void main(String[] args) throws IOException {
/* Parse parameters */
boolean init = false;
boolean verbose = false;
boolean fast = false;
for (int i = 0; i < args.length; ++i) {
String param = args[i];
if (param.equals("init") || param.equals("i")) {
init = true;
} else if (param.equals("verbose") || param.equals("v")) {
verbose = true;
} else if (param.equals("fast") || param.equals("f")) {
fast = true;
} else {
System.err.println("Unknown parameter: '" + args[i] + "'");
return;
}
}
/**
* Start the game by sending the starting board without moves
* if the parameter "init" is given
*/
if (init) {
String message = new GameState().toMessage();
//System.err.println("Sending initial board: '" + message + "'");
System.out.println(message);
}
Player player = new Player();
String input_message;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while ((input_message = br.readLine()) != null) {
/* Deadline is one second from when we receive the message */
Deadline deadline = new Deadline(Deadline.getCpuTime() + (fast ? (long) 1e8 : (long) 1e9));
/* Get game state from standard input */
//System.err.println("Receiving: '" + input_message + "'");
GameState input_state = new GameState(input_message);
/* See if we would produce the same message */
if (!input_state.toMessage().equals(input_message)) {
System.err.println("*** ERROR! ***");
System.err.println("Interpreted: '" + input_message + "'");
System.err.println("As: '" + input_state.toMessage() + "'");
System.err.println(input_state.toString(input_state.getNextPlayer()));
assert(false);
}
/* Print the input state */
if (verbose) {
System.err.println(input_state.toMessage());
System.err.println(input_state.toString(input_state.getNextPlayer()));
}
/* Quit if this is end of game */
if (input_state.getMove().isEOG()) {
break;
}
/* Figure out the next move */
GameState output_state = player.play(input_state, deadline);
/* Crash if deadline has been exceeded */
if (deadline.timeUntil() < 0) {
System.exit(152);
}
/* Print the output state */
if (verbose) {
System.err.println(output_state.toMessage());
System.err.println(output_state.toString(input_state.getNextPlayer()));
}
/* Send the next move */
String output_message = output_state.toMessage();
//System.err.println("Sending: '" + output_message + "'");
System.out.println(output_message);
/* Quit if this is end of game */
if (output_state.getMove().isEOG()) {
break;
}
}
}
}