forked from CodinGame/ghost-in-the-cell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefereeRunner.java
More file actions
83 lines (75 loc) · 3.31 KB
/
RefereeRunner.java
File metadata and controls
83 lines (75 loc) · 3.31 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
import java.io.*;
import java.util.Properties;
import java.util.Scanner;
class RefereeRunner {
final static int playerCount = 2;
static Process[] ai = new Process[playerCount];
static Scanner[] in = new Scanner[playerCount];
static Scanner[] err = new Scanner[playerCount];
static BufferedWriter[] out = new BufferedWriter[playerCount];
public static void main(String[] args) {
Referee ref = null;
try {
for (int player = 0; player < playerCount; player++) {
ProcessBuilder pb = new ProcessBuilder();
pb.command(args[player].split(" "));
Process pr = pb.start();
ai[player] = pr;
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
out[player] = writer;
if (args.length >= 3 && "--use-stderr".equals(args[2])) {
err[player] = new Scanner(pr.getInputStream());
in[player] = new Scanner(pr.getErrorStream());
} else {
in[player] = new Scanner(pr.getInputStream());
err[player] = new Scanner(pr.getErrorStream());
}
}
ref = new Referee(System.in, System.out, System.err);
Properties prop = new Properties();
ref.initReferee(playerCount, prop);
String[][] initialToPlayer = new String[playerCount][];
for (int player = 0; player < playerCount; player++) {
initialToPlayer[player] = ref.getInitInputForPlayer(player);
}
sendToPlayers(initialToPlayer);
for (int round = 0; round < ref.getMaxRoundCount(playerCount); round++) {
// System.out.println(round);
String[][] toPlayer = new String[playerCount][];
for (int player = 0; player < playerCount; player++) {
toPlayer[player] = ref.getInputForPlayer(round, player);
}
sendToPlayers(toPlayer);
String[][] fromPlayer = new String[playerCount][];
for (int player = 0; player < playerCount; player++) {
// System.out.println(ai[player].isAlive());
String line = in[player].nextLine();
// System.out.println(line);
fromPlayer[player] = new String[]{line};
}
for (int player = 0; player < playerCount; player++) {
ref.handlePlayerOutput(0, round, player, fromPlayer[player]);
}
ref.updateGame(round);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(ref != null) {
for (int player = 0; player < playerCount; player++) {
System.out.println(ref.getScore(player));
}
}
}
}
private static void sendToPlayers(String[][] dataToPlayer) throws IOException {
for (int player = 0; player < playerCount; player++) {
String toSend = String.join("\n", dataToPlayer[player]) + "\n";
if (player == 0) {
// System.out.print(toSend);
}
out[player].write(toSend);
out[player].flush();
}
}
}