-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathConnection.java
179 lines (167 loc) · 4.88 KB
/
Connection.java
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
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
public class Connection {
public int turns = 0;
public int rows = 0;
public int cols = 0;
public int loadtime = 0;
public int turntime = 0;
public int viewradius2 = 0;
public int attackradius2 = 0;
public int spawnradius2 = 0;
public long player_seed = 0;
public int turn = 0;
public Tile[][] map;
public int numWaterTiles = 0;
public LinkedList<Ant> ants = new LinkedList<Ant>();
public LinkedList<Tile> hills = new LinkedList<Tile>();
public LinkedList<Tile> foods = new LinkedList<Tile>();
public void setup(List<String> data) {
for (String line : data) {
String tokens[] = line.toLowerCase().split(" ");
if (tokens[0].equals("cols")) {
this.cols = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("rows")) {
this.rows = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("turns")) {
this.turns = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("loadtime")) {
this.loadtime = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("turntime")) {
this.turntime = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("viewradius2")) {
this.viewradius2 = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("attackradius2")) {
this.attackradius2 = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("spawnradius2")) {
this.spawnradius2 = Integer.parseInt(tokens[1]);
} else if (tokens[0].equals("player_seed")) {
this.player_seed = Long.parseLong(tokens[1]);
}
}
this.map = new Tile[this.rows][this.cols];
for (int row = 0; row < rows; row++) {
map[row] = new Tile[this.cols];
for (int col = 0; col < cols; col++) {
map[row][col] = new Tile(row, col);
}
}
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
Tile tile = map[row][col];
Tile n = map[(row+rows-1)%rows][col];
Tile e = map[row][(col+1)%cols];
Tile s = map[(row+1)%rows][col];
Tile w = map[row][(col+cols-1)%cols];
if (((col + row) & 1) == 1) {
tile.neighbors[0] = n;
tile.neighbors[1] = s;
tile.neighbors[2] = e;
tile.neighbors[3] = w;
} else {
tile.neighbors[0] = e;
tile.neighbors[1] = w;
tile.neighbors[2] = n;
tile.neighbors[3] = s;
}
}
}
}
private boolean update(List<String> data) {
// clear
for (Ant ant : this.ants) {
ant.tile.type = Type.LAND;
ant.tile.ant = null;
}
this.ants.clear();
for (Tile food : this.foods) food.type = Type.LAND;
this.foods.clear();
for (Tile hill : this.hills) {
hill.type = Type.LAND;
hill.isHill = false;
}
this.hills.clear();
// get new data
for (String line : data) {
String tokens[] = line.split(" ");
if (tokens.length > 2) {
int row = Integer.parseInt(tokens[1]);
int col = Integer.parseInt(tokens[2]);
Tile tile = map[row][col];
if (tokens[0].equals("w")) {
tile.type = Type.WATER;
for (Tile n : tile.neighbors) n.removeNeighbor(tile);
tile.neighbors = null;
numWaterTiles++;
} else if (tokens[0].equals("a")) {
tile.type = Type.fromId(Integer.parseInt(tokens[3]));
tile.ant = new Ant(tile);
this.ants.add(tile.ant);
} else if (tokens[0].equals("h")) {
//tile.type = Type.fromId(Integer.parseInt(tokens[3]));
tile.isHill = true;
tile.hillPlayer = Type.fromId(Integer.parseInt(tokens[3]));
this.hills.add(tile);
} else if (tokens[0].equals("f")) {
tile.type = Type.FOOD;
this.foods.add(tile);
} else if (tokens[0].equals("d")) {
// dead ant - do nothing
}
}
}
return true;
}
public void issueOrder(int row, int col, Direction direction) {
System.out.println("o " + row + " " + col + " " + direction.symbol);
System.out.flush();
}
public void finishTurn() {
System.out.println("go");
System.out.flush();
this.turn++;
}
public static void run(Strategy bot) {
Connection ants = new Connection();
StringBuffer line = new StringBuffer();
LinkedList<String> data = new LinkedList<String>();
int c;
try {
while ((c = System.in.read()) >= 0) {
switch (c) {
case '\n':
case '\r':
if (line.length() > 0) {
String full_line = line.toString();
if (full_line.equals("ready")) {
ants.setup(data);
bot.init(ants);
ants.finishTurn();
data.clear();
} else if (full_line.equals("go")) {
bot.startTime = System.currentTimeMillis();
try {
ants.update(data);
bot.doTurn(ants);
} catch (Exception e) {
//if (!Strategy.DEBUG)
e.printStackTrace();
}
ants.finishTurn();
data.clear();
}
else if (line.length() > 0) data.add(full_line);
line = new StringBuffer();
}
break;
default:
line.append((char)c);
break;
}
}
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
}