-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEngine.java
More file actions
358 lines (312 loc) · 11.2 KB
/
Engine.java
File metadata and controls
358 lines (312 loc) · 11.2 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2010 owners of the AI Challenge project
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at http://www.apache.org/licenses/LICENSE-2.0 . Unless
// required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
//
// Author: Jeff Cameron (jeff@jpcameron.com)
//
// Plays a game of Planet Wars between two computer programs.
// NOTICE: code has been modified to allow synchronized gameplay (one player
// per turn).
import java.io.*;
import java.util.*;
public class Engine {
public static int GAME_MODE_SERIAL = 1;
public static int GAME_MODE_PARALLEL = 2;
public static void KillClients(List<Process> clients) {
for (Process p : clients) {
if (p != null) {
p.destroy();
}
}
}
public static boolean AllTrue(boolean[] v) {
for (int i = 0; i < v.length; ++i) {
if (!v[i]) {
return false;
}
}
return true;
}
public static boolean clientsDone(boolean[] clientsDone, int gameMode, int playerId) {
if (gameMode == GAME_MODE_PARALLEL) {
return AllTrue(clientsDone);
} else {
return clientsDone[playerId];
}
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
// Check the command-line arguments.
if (args.length < 3 || args.length > 6) {
System.err.println("ERROR: wrong number of command-line "
+ "arguments.");
System.err
.println("USAGE: java -jar PlayGame.jar <map_file_name> "
+ "\"java <player_one>\" "
+ "\"java <player_two>\" [<game_mode>] [<max_num_turns>] [<max_turn_time>] ");
System.err.println("");
System.err.println("Explanation:");
System.err
.println("<map_file_name>: Location of .txt file of map to use for this game");
System.err
.println("\"java <player_one>\": Player1. Make sure to add quotes, and add the 'java' part before the bot name. Also make sure your bot is actually compiled (there should be a .class file of your bot file)");
System.err.println("\"java <player_two>\": Idem");
System.err.println("Optional:");
System.err
.println("<game_mode>: Game mode to run in. Options are: 'parallel' and 'serial'. Serial (used in week 1) means for each turn, first player1 makes a move, and then player2. Parallel (used in week2) means for every turn, each player makes a move at the same time. Default: serial");
System.err
.println("<max_num_turns>: Maximum number of turns this game may take. Default: 100");
System.err
.println("<max_turn_time>: Maximum number of time a bot is allowed to take per turn. Default: 1000");
System.exit(1);
}
// Initialize the game. Load the map.
String mapFilename = args[0];
int maxNumTurns = 100;
int maxTurnTime = 1000;
String logFilename = "log.txt";
int gameMode = GAME_MODE_SERIAL;
// optional arguments
if (args.length >= 4) {
if (args[3].equalsIgnoreCase(("parallel"))) {
gameMode = GAME_MODE_PARALLEL;
} else if (args[3].equalsIgnoreCase(("serial"))) {
gameMode = GAME_MODE_SERIAL;
} else {
System.err
.println("The 4th argument is unknown. This should either be 'parallel' or 'serial'");
System.exit(1);
}
}
if (args.length >= 5) {
maxNumTurns = Integer.parseInt(args[4]);
}
if (args.length >= 6) {
maxTurnTime = Integer.parseInt(args[5]);
}
Game game = new Game(mapFilename, maxNumTurns, 0, logFilename);
if (game.Init() == 0) {
System.err.println("ERROR: failed to start game. map: "
+ mapFilename);
}
// Start the client programs (players).
List<Process> clients = new ArrayList<Process>();
for (int i = 1; i <= 2; ++i) {
String command = args[i];
Process client = null;
try {
client = Runtime.getRuntime().exec(command);
} catch (Exception e) {
client = null;
}
if (client == null) {
KillClients(clients);
System.err.println("ERROR: failed to start client: " + command);
System.exit(1);
}
clients.add(client);
}
try {
Thread.currentThread().sleep(maxTurnTime);
} catch (InterruptedException e1) {
//pff, nothing
}
boolean[] isAlive = new boolean[clients.size()];
for (int i = 0; i < clients.size(); ++i) {
isAlive[i] = (clients.get(i) != null);
}
System.err.println("Engine entering main game loop. Mode '"+ (gameMode == GAME_MODE_PARALLEL? "parallel": "serial") + "'");
int numTurns = 0;
int ap = 0; // MODIFIED: active player, based on current numTurns
// Enter the main game loop.
while (game.Winner() < 0) {
// Send the game state to the clients.
//System.err.println("The game state :");
//System.err.print(game);
game.WriteLogMessage("Game state turn " + numTurns + ": \n" + game);
if (gameMode == GAME_MODE_SERIAL) {
// MODIFIED: send game state only to active player (ap)
if (clients.get(ap) == null ) {
break;
}
sendGameState(game, clients, ap);
} else {
for (int i = 0; i < clients.size(); ++i) {
if (clients.get(i) == null ) {
break;
}
sendGameState(game, clients, i);
}
}
// Get orders from the clients.
StringBuilder[] buffers = new StringBuilder[clients.size()];
boolean[] clientDone = new boolean[clients.size()];
for (int i = 0; i < clients.size(); ++i) {
buffers[i] = new StringBuilder();
clientDone[i] = false;
}
long startTime = System.currentTimeMillis();
while (!clientsDone(clientDone, gameMode, ap) ) {
if( System.currentTimeMillis() - startTime > maxTurnTime){
//check client done
for (int i = 0; i < clientDone.length; ++i) {
if (!clientDone[i]) {
System.err.println("Client " + (ap+1) + " timeout: you missed a turn! Consider to make your bot faster, or increase the maxTurnTime (argument of PlayGame.jar).");
}
}
break;
}
// int i;
// int end;
// if (gameMode == GAME_MODE_SERIAL) {
// // MODIFIED: one player per turn
// i = ap; // gets overridden in for loop
// end = ap + 1; // not correct
// } else {
// i = 0;
// end = clients.size();
// }
for (int i = 0; i < clients.size(); ++i) {
int j = (ap + i) % 2; // required to switch between whom to start first (not to favouritize player1)
if (!isAlive[j] || clientDone[j]) {
clientDone[j] = true;
continue;
}
// if serial read only the active player
if (gameMode == GAME_MODE_SERIAL) {
if (j != ap)
continue;
}
// if it's parallel read both
try {
InputStream inputStream = clients.get(j)
.getInputStream();
while (inputStream.available() > 0) {
char c = (char) inputStream.read();
if (c == '\n') {
String line = buffers[j].toString().trim();
// System.err.println("P" + (i+1) + ": " +
// line);
line = line.toLowerCase().trim();
game.WriteLogMessage("player" + (j + 1) + " > engine: " + line);
// System.err.println("player" + (j + 1)
// + " > engine: " + line);
// System.err.flush();
// Modified: only process 1 order
if (line.equals("go")) {
buffers[j] = new StringBuilder();
} else {
int result = game.IssueOrder(j + 1, line);
if (result == -1) {
System.err.println("== player" + (j + 1) + " skipped a turn. Check log file for info. ==");
}
clientDone[j] = true;
buffers[j] = new StringBuilder();
break;
}
} else {
buffers[j].append(c);
}
try {
printBotDebugOutput(clients, j, numTurns);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} catch (Exception e) {
System.err.println("WARNING: player " + (j + 1)
+ " crashed.");
clients.get(j).destroy();
game.DropPlayer(j + 1);
isAlive[j] = false;
}
}
}
// MODIFIED: update active player
ap = (ap + 1) % 2;
// for (int j = 0; j < clients.size(); ++j) {
// if (!isAlive[j] || !game.IsAlive(j + 1)) {
// continue;
// }
// if (clientDone[j]) {
// continue;
// }
// // Do NOT drop players at timeouts
// /*
// * System.err.println("WARNING: player " + (i+1) +
// * " timed out."); clients.get(i).destroy(); game.DropPlayer(i +
// * 1); isAlive[i] = false;
// */
// }
// Keep advancing turns, until there are no ships in flight.
// This way each player has complete knowledge of game state on
// start
while (game.getFleets().size() != 0) {
game.skipTimeStep();
}
++numTurns;
System.err.println("Turn " + numTurns);
System.out.print(game.FlushGamePlaybackString());
System.out.flush();
game.DoTimeStep();
}
KillClients(clients);
if (game.Winner() > 0) {
System.err.println("Player " + game.Winner() + " Wins!");
} else {
System.err.println("Draw!");
}
System.out.println(game.GamePlaybackString());
}
public void WriteLogMessage(String logFilename, String message) {
if (logFilename == null) {
return;
}
try {
BufferedWriter logFile = new BufferedWriter(new FileWriter(logFilename, true));
logFile.write(message);
logFile.newLine();
logFile.flush();
logFile.close();
} catch (Exception e) {
// whatev
}
}
private static void sendGameState(Game game, List<Process> clients, int clientId) {
String message = game.PovRepresentation(clientId + 1) + "go\n";
try {
OutputStream out = clients.get(clientId).getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out);
writer.write(message, 0, message.length());
writer.flush();
//System.err.println("engine > player" + (clientId + 1) +" : " + message);
game.WriteLogMessage("engine > player" + (clientId + 1) +": \n" + message);
} catch (Exception e) {
clients.set(clientId, null);
}
}
private static void printBotDebugOutput(List<Process> clients, int clientId, int turnNumber) throws IOException {
StringBuilder buf = new StringBuilder();
InputStream in = clients.get(clientId).getErrorStream();
BufferedInputStream stderr = new BufferedInputStream (in);
while (stderr.available() > 0){
char c = (char)stderr.read();
if (c == '\n') {
String ln = buf.toString();
System.err.println("Player " + (clientId+1) + ": " + ln );
System.err.flush();
buf = new StringBuilder();
}
else {
buf.append(c);
}
}
}
}