-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjava.java
377 lines (306 loc) · 11.9 KB
/
java.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
class PossibleMove {
public Cell cell;
public Game.ACTION action;
public PossibleMove(Cell cell, Game.ACTION action) {
this.cell = cell;
this.action = action;
}
}
class MinimaxScore {
public int score;
public Game.ACTION action;
public int newThorPosition;
public MinimaxScore(int score) {
this.score = score;
this.action = Game.ACTION.WAIT;
}
public MinimaxScore(int score, Game.ACTION action) {
this.score = score;
this.action = action;
}
public MinimaxScore(int score, Game.ACTION action, int newThorPosition) {
this.score = score;
this.action = action;
this.newThorPosition = newThorPosition;
}
}
class Cell {
public int pos;
public boolean hasGiant = false;
public boolean hasThor = false;
public Cell(int pos) {
this.pos = pos;
}
public Cell(Cell copy) {
this.pos = copy.pos;
this.hasGiant = copy.hasGiant;
this.hasThor = copy.hasThor;
}
}
class Game {
public int width;
public int height;
public Cell[] cells;
public int thorLightningsLeft;
public int thorPos;
public ArrayList<Integer> giantsPos = new ArrayList<>();
public Game(int width, int height) {
this.cells = new Cell[width * height];
this.width = width;
this.height = height;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pos = Game.coordinatesToPos(this.width, j, i);
this.cells[pos] = new Cell(pos);
}
}
}
// public ArrayList<Cell> getNeighbors(int pos) {
// ArrayList<Cell> neighbors = new ArrayList<>();
// if (pos % this.width != 0) // left
// neighbors.add(this.cells[pos - 1]);
// if ((pos + 1) % this.width != 0) // right
// neighbors.add(this.cells[pos + 1]);
// if (pos - this.width >= 0) // top
// neighbors.add(this.cells[pos - this.width]);
// if (pos + this.width < this.width * this.height) // bottom
// neighbors.add(this.cells[pos + this.width]);
// if (pos - this.width - 1 >= 0 && pos % this.width != 0) // top-left
// neighbors.add(this.cells[pos - this.width - 1]);
// if (pos - this.width + 1 >= 0 && (pos - this.width + 1) % this.width != 0) //
// top-right
// neighbors.add(this.cells[pos - this.width + 1]);
// if (pos + this.width - 1 < this.width * this.height && pos % this.width != 0)
// // bottom-left
// neighbors.add(this.cells[pos + this.width - 1]);
// if (pos + this.width + 1 < this.width * this.height && (pos + this.width + 1)
// % this.width != 0) // bottom-right
// neighbors.add(this.cells[pos + this.width + 1]);
// return neighbors;
// }
public void clearGiantsPos() {
for (int pos : this.giantsPos)
this.cells[pos].hasGiant = false;
this.giantsPos.clear();
}
public void markGiantPos(int x, int y) {
int pos = Game.coordinatesToPos(this.width, x, y);
this.cells[pos].hasGiant = true;
this.giantsPos.add(pos);
}
public void setThorPos(int x, int y) {
this.setThorPos(Game.coordinatesToPos(this.width, x, y));
}
public void setThorPos(int pos) {
this.cells[this.thorPos].hasThor = false;
this.cells[pos].hasThor = true;
this.thorPos = pos;
}
public int getGiantsCentroidPos() {
double centroidX = 0, centroidY = 0;
for (int aGiantPos : this.giantsPos) {
centroidX += Game.posToX(this.width, aGiantPos);
centroidY += Game.posToY(this.width, aGiantPos);
}
centroidX /= this.giantsPos.size();
centroidY /= this.giantsPos.size();
return Game.coordinatesToPos(this.width, (int) Math.round(centroidX), (int) Math.round(centroidY));
}
public static int coordinatesToPos(int width, int x, int y) {
return y * width + x;
}
public static int posToX(int width, int pos) {
return pos % width;
}
public static int posToY(int width, int pos) {
return pos / width;
}
public static int manhattanDistance(int width, int aPos, int bPos) {
return Math.abs(Game.posToX(width, bPos) - Game.posToX(width, aPos))
+ Math.abs(Game.posToY(width, bPos) - Game.posToY(width, aPos));
}
@Override
public String toString() {
StringBuilder str = new StringBuilder();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pos = Game.coordinatesToPos(this.width, j, i);
Cell cell = this.cells[pos];
if (cell.hasThor) {
str.append("T");
System.err.println("THOR : " + j + " " + i + " " + pos);
} else if (cell.hasGiant) {
str.append("G");
System.err.println("GIANT : " + j + " " + i + " " + pos);
} else
str.append("·");
}
str.append("\n");
}
return str.toString();
}
@Override
public Game clone() {
Game gameCopy = new Game(this.width, this.height);
gameCopy.cells = new Cell[this.cells.length];
for (int i = 0; i < gameCopy.cells.length; i++)
gameCopy.cells[i] = new Cell(this.cells[i]);
gameCopy.giantsPos = new ArrayList<>();
for (Integer aGiantPos : this.giantsPos)
gameCopy.giantsPos.add(aGiantPos);
gameCopy.thorLightningsLeft = this.thorLightningsLeft;
gameCopy.thorPos = this.thorPos;
return gameCopy;
}
public static enum ACTION {
W, E, N, S, NW, NE, SW, SE, WAIT, STRIKE
};
public ArrayList<PossibleMove> getPossibleMoves(int pos) {
ArrayList<PossibleMove> neighbors = new ArrayList<>();
if (pos % this.width != 0) // left
neighbors.add(new PossibleMove(this.cells[pos - 1], Game.ACTION.W));
if ((pos + 1) % this.width != 0) // right
neighbors.add(new PossibleMove(this.cells[pos + 1], Game.ACTION.E));
if (pos - this.width >= 0) // top
neighbors.add(new PossibleMove(this.cells[pos - this.width], Game.ACTION.N));
if (pos + this.width < this.width * this.height) // bottom
neighbors.add(new PossibleMove(this.cells[pos + this.width], Game.ACTION.S));
if (pos - this.width - 1 >= 0 && pos % this.width != 0) // top-left
neighbors.add(new PossibleMove(this.cells[pos - this.width - 1], Game.ACTION.NW));
if (pos - this.width + 1 >= 0 && (pos - this.width + 1) % this.width != 0) // top-right
neighbors.add(new PossibleMove(this.cells[pos - this.width + 1], Game.ACTION.NE));
if (pos + this.width - 1 < this.width * this.height && pos % this.width != 0) // bottom-left
neighbors.add(new PossibleMove(this.cells[pos + this.width - 1], Game.ACTION.SW));
if (pos + this.width + 1 < this.width * this.height && (pos + this.width + 1) % this.width != 0) // bottom-right
neighbors.add(new PossibleMove(this.cells[pos + this.width + 1], Game.ACTION.SE));
return neighbors;
}
public static MinimaxScore gameScore(Game game) {
// No more giants left (win)
if (game.giantsPos.size() == 0)
return new MinimaxScore(Integer.MAX_VALUE);
// Some giants are left but no more strikes left (lose)
if (game.giantsPos.size() > 0 && game.thorLightningsLeft == 0)
return new MinimaxScore(Integer.MIN_VALUE);
// Giants kill thor (lose)
for (int aGiantPos : game.giantsPos)
if (aGiantPos == game.thorPos)
return new MinimaxScore(Integer.MIN_VALUE);
return null;
}
public static MinimaxScore minimax(Game game, int depth, boolean isMaximizing, int alpha, int beta) {
MinimaxScore score = Game.gameScore(game);
if (score != null)
return score;
// Distance from centroid
if (depth == 0)
return new MinimaxScore(-Game.manhattanDistance(game.width, game.thorPos, game.getGiantsCentroidPos()));
if (isMaximizing) {
// Thor turn
MinimaxScore bestScore = new MinimaxScore(Integer.MIN_VALUE, Game.ACTION.WAIT, game.thorPos);
// Try to make Thor wait
MinimaxScore waitScore = minimax(game, depth - 1, false, alpha, beta);
if (waitScore.score > bestScore.score)
bestScore = new MinimaxScore(waitScore.score, Game.ACTION.WAIT, game.thorPos);
// Try to make Thor strike
if (game.thorLightningsLeft > 0) {
int thorLightingsLeft = game.thorLightningsLeft;
ArrayList<Integer> giantsPos = new ArrayList<>(game.giantsPos);
game.thorLightningsLeft--;
// Remove all giants with a manhattan distance of <= 9
game.giantsPos = (ArrayList<Integer>) game.giantsPos.stream()
.filter(b -> Game.manhattanDistance(game.width, game.thorPos, b) >= 5/* 9 */).collect(Collectors.toList());
MinimaxScore strikeScore = minimax(game, depth - 1, false, alpha, beta);
game.thorLightningsLeft = thorLightingsLeft;
game.giantsPos = giantsPos;
if (strikeScore.score > bestScore.score)
bestScore = new MinimaxScore(strikeScore.score, Game.ACTION.STRIKE, game.thorPos);
}
// Try every Thor move
ArrayList<PossibleMove> possibleMove = game.getPossibleMoves(game.thorPos);
for (PossibleMove aPossibleMove : possibleMove) {
int thorPos = game.thorPos;
game.thorPos = aPossibleMove.cell.pos;
MinimaxScore moveScore = minimax(game, depth - 1, false, alpha, beta);
if (moveScore.score > bestScore.score)
bestScore = new MinimaxScore(moveScore.score, aPossibleMove.action, aPossibleMove.cell.pos);
game.thorPos = thorPos;
}
// Alpha-beta pruning
if (bestScore.score >= beta)
return bestScore;
if (bestScore.score > alpha)
alpha = bestScore.score;
return bestScore;
} else {
// Giants turn
MinimaxScore bestScore = new MinimaxScore(Integer.MAX_VALUE);
ArrayList<Integer> giantsPosBackup = new ArrayList<>(game.giantsPos);
ArrayList<Integer> newGiantsPos = new ArrayList<>(game.giantsPos);
for (int aGiantPos : game.giantsPos) {
ArrayList<PossibleMove> aGiantMoves = game.getPossibleMoves(aGiantPos);
int closestToThorDistance = Integer.MAX_VALUE;
int closestToThorPos = 0;
// For all possible moves of this giant, find the closest to Thor
for (PossibleMove aGiantMove : aGiantMoves) {
int distanceFromThor = Game.manhattanDistance(game.width, game.thorPos, aGiantMove.cell.pos);
if (distanceFromThor < closestToThorDistance) {
closestToThorDistance = distanceFromThor;
closestToThorPos = aGiantMove.cell.pos;
}
}
newGiantsPos.add(closestToThorPos);
}
game.giantsPos = newGiantsPos;
// Compute score
MinimaxScore giantsScore = minimax(game, depth - 1, true, alpha, beta);
if (giantsScore.score < bestScore.score)
bestScore = new MinimaxScore(giantsScore.score);
// Restore initial giants positions
game.giantsPos = giantsPosBackup;
// Alpha-beta pruning
if (bestScore.score <= alpha)
return bestScore;
if (bestScore.score < beta)
beta = bestScore.score;
return giantsScore;
}
}
}
class Player {
public static void main(String args[]) {
Game game = new Game(40, 18);
Scanner in = new Scanner(System.in);
int tx = in.nextInt();
int ty = in.nextInt();
game.setThorPos(tx, ty);
// game loop
while (true) {
// the remaining number of hammer strikes.
int h = in.nextInt();
game.thorLightningsLeft = h;
// the number of giants which are still present on the map.
int n = in.nextInt();
for (int i = 0; i < n; i++) {
int x = in.nextInt();
int y = in.nextInt();
// System.err.println(x + " " + y);
game.markGiantPos(x, y);
}
System.err.println(game);
MinimaxScore move = Game.minimax(game, 6, true, Integer.MIN_VALUE, Integer.MAX_VALUE);
game.setThorPos(move.newThorPosition);
System.out.println(move.action);
int centroid = game.getGiantsCentroidPos();
System.err.println("Centroid: " + Game.posToX(game.width, centroid) + " " + Game.posToY(game.width, centroid));
// The movement or action to be carried out: WAIT STRIKE N NE E SE S SW W or N
// System.out.println("WAIT");
game.clearGiantsPos();
}
}
}