-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayerSkeleton.java
More file actions
438 lines (362 loc) · 10.7 KB
/
PlayerSkeleton.java
File metadata and controls
438 lines (362 loc) · 10.7 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
class Moves extends State {
public static int[][][] allMoves = legalMoves;
public static int getNumMoves(int piece) {
return allMoves[piece].length;
}
public static int[][] getLegalMoves(int piece) {
return allMoves[piece];
}
}
class Weights {
public double numHoles;
public double maxHeight;
public double rowsCleared;
public double colHeights;
public double adjColHeightDiffs;
public double rowTrans;
public double colTrans;
public double totalWells;
public Weights() {}
public double[] toArray() {
double[] arr = new double[8];
int wi = 0;
arr[wi++] = numHoles;
arr[wi++] = maxHeight;
arr[wi++] = rowsCleared;
arr[wi++] = colHeights;
arr[wi++] = adjColHeightDiffs;
arr[wi++] = rowTrans;
arr[wi++] = colTrans;
arr[wi++] = totalWells;
return arr;
}
public static Weights fromArray(double[] arr) {
Weights w = new Weights();
int wi = 0;
w.numHoles = arr[wi++];
w.maxHeight = arr[wi++];
w.rowsCleared = arr[wi++];
w.colHeights = arr[wi++];
w.adjColHeightDiffs = arr[wi++];
w.rowTrans = arr[wi++];
w.colTrans = arr[wi++];
w.totalWells = arr[wi++];
return w;
}
public static Weights someWeights() {
Weights w = new Weights(); // [10.978][4.024][-0.432][0.002][1.680][0.011][0.925][5.396]
w.numHoles = 10.978;
w.maxHeight = 4.024;
w.rowsCleared = -0.432;
w.colHeights = 0.002;
w.adjColHeightDiffs = 1.680;
w.rowTrans = 0.011;
w.colTrans = 0.925;
w.totalWells = 5.396;
return w;
}
public static Weights randomWeights() {
Weights w = new Weights();
w.numHoles = getRandom();
w.maxHeight = getRandom();
w.rowsCleared = getRandom();
w.colHeights = getRandom();
w.adjColHeightDiffs = getRandom();
w.rowTrans = getRandom();
w.colTrans = getRandom();
w.totalWells = getRandom();
return w;
}
public static double getRandom() {
java.util.Random r = new java.util.Random();
return r.nextDouble() * 10 - 5;
}
}
class Simulator
{
// Handy pointers to have locally
private static int[][][] legalMoves = Moves.allMoves;
private static int[][][] pBottom = State.getpBottom();
private static int[][][] pTop = State.getpTop();
private static int[][] pWidth = State.getpWidth();
private static int[][] pHeight = State.getpHeight();
// Simulator state
public int[][] field = new int[State.ROWS][State.COLS];
public int[] top = new int[State.COLS];
public int turn, maxHeight, rowsCleared, rows, cols;
public Weights weights;
// For quick heuristics, simMove keeps this field updated for:
// - Max height
// - Column Heights
// - Holes
// - Cleared
public double heuristic;
public Simulator(Simulator sim) {
this(sim.rows, sim.cols, sim.weights);
}
public Simulator(int rows, int cols, Weights w) {
this.weights = w;
this.rows = rows;
this.cols = cols;
this.field = new int[rows][cols];
this.top = new int[cols];
}
public void revertTo(Simulator sim) {
System.arraycopy(sim.top, 0, top, 0, top.length);
for (int i = 0; i < field.length; i++)
System.arraycopy(sim.field[i], 0, field[i], 0, field[i].length);
turn = sim.turn;
rowsCleared = sim.rowsCleared;
heuristic = sim.heuristic;
maxHeight = sim.maxHeight;
}
public double getHeuristic() {
double sum = heuristic;
for(int i = 0; i < top.length - 1; i++)
sum += Math.abs(top[i] - top[i+1]) * weights.adjColHeightDiffs;
int columnTransitions = 0;
int rowTransitions = 0;
int wells = 0;
int a, b, c, d;
// column Transitions
for(int col = 0; col < cols; col++) {
for(int row = 0; row < top[col]; row++) {
a = field[row][col];
b = field[row+1][col];
if( a!=0 && b==0 ) {
columnTransitions++;
} else if(a==0 && b != 0) {
columnTransitions++;
}
}
}
// row transitions and wells (inner)
for(int row = 0; row < maxHeight; row++) {
for(int col = 1; col < cols-1; col++) {
a=field[row][col-1];
b=field[row][col];
c=field[row][col+1];
if( a!=0 && b==0 && c!=0 ) {
wells++;
}
if( b!=0 && c==0 ) {
rowTransitions++;
} else if(b==0 && c != 0) {
rowTransitions++;
}
}
// edges;
a = field[row][0];
b = field[row][1];
c = field[row][cols - 2];
d = field[row][cols - 1];
if(a == 0 && b != 0) {
wells++;
}
if(c != 0 && d == 0) {
wells++;
}
}
sum += columnTransitions * weights.colTrans;
sum += rowTransitions * weights.rowTrans;
sum += wells * weights.totalWells;
return sum;
}
public boolean simMove(int move, int piece) {
int orient = legalMoves[piece][move][State.ORIENT];
int slot = legalMoves[piece][move][State.SLOT];
turn++;
// height if the first column makes contact
int height = top[slot] - pBottom[piece][orient][0];
// for each column beyond the first in the piece
for(int col = 1; col < pWidth[piece][orient]; col++)
height = Math.max(height, top[slot + col] - pBottom[piece][orient][col]);
// Check if game ended
if(height + pHeight[piece][orient] >= this.rows)
return false;
placePiece(piece, orient, slot, height);
clearRows(piece, orient, height);
return true;
}
private void placePiece(int piece, int orient, int slot, int height) {
// For each column in the piece
for (int col = 0; col < pWidth[piece][orient]; col++) {
int colBottom = height + pBottom[piece][orient][col];
int colTop = height + pTop[piece][orient][col];
// Adjust top and max height heuristic
top[slot + col] = colTop;
if (colTop > maxHeight) {
heuristic += weights.maxHeight * (colTop - maxHeight);
maxHeight = colTop;
}
// For each field in piece-column - bottom to top
for (int row = colBottom; row < colTop; row++) {
field[row][col + slot] = turn;
heuristic += weights.colHeights;
}
// Adjust holes heuristic by looking for new holes under the col
while (--colBottom > 0 && field[colBottom][col + slot] == 0)
heuristic += weights.numHoles;
}
}
private void clearRows(int piece, int orient, int height) {
// Check for full rows - starting at the top of the piece
for (int row = height + pHeight[piece][orient] - 1; row >= height; row--) {
boolean full = true;
// Is this row full?
for (int col = 0; col < this.cols; col++) {
if (field[row][col] == 0) {
full = false;
break;
}
}
if (full)
removeRow(row);
}
}
private void removeRow(int row) {
int newMaxHeight = 0;
rowsCleared++;
// For each column in row
for (int col = 0; col < this.cols; col++) {
// Slide down all bricks
for (int r = row; r < top[col]; r++)
field[r][col] = field[r + 1][col];
// Lower the top
top[col]--;
heuristic -= weights.colHeights;
// If a hole opened up, adjust top and heuristic
while (top[col] > 0 && field[top[col] - 1][col] == 0) {
heuristic -= weights.colHeights;
heuristic -= weights.numHoles;
top[col]--;
}
if(top[col] > newMaxHeight) {
newMaxHeight = top[col];
}
}
heuristic += weights.rowsCleared;
heuristic -= weights.maxHeight * (maxHeight - newMaxHeight);
maxHeight = newMaxHeight;
}
}
public class PlayerSkeleton {
private Simulator gameSim;
public PlayerSkeleton(Weights w, int rows,int cols) {
gameSim = new Simulator(rows,cols,w);
}
public int playAndReturnScore() {
int piece = randomPiece();
while(gameSim.simMove(pickMove(Moves.getLegalMoves(piece), piece), piece))
piece = randomPiece();
return gameSim.rowsCleared;
}
private void forwardLookAvg(Simulator s, int maxdepth, double[] hues, int hIndex) {
double average = 0;
Simulator sim = new Simulator(s);
// For all possible pieces
for (int piece = 0; piece < State.N_PIECES; piece++) {
int numMoves = Moves.getNumMoves(piece);
double pieceBestHeu = Double.MAX_VALUE;
// Try all possible moves for piece
for (int move = 0; move < numMoves; move++) {
sim.revertTo(s);
if (!sim.simMove(move, piece))
continue;
//if (maxdepth != 1)
// heu += forwardLookAvg(sim, maxdepth - 1);
//else
double heu = sim.getHeuristic();
if (heu < pieceBestHeu)
pieceBestHeu = heu;
}
average += pieceBestHeu;
}
average /= State.N_PIECES;
hues[hIndex] = average;
}
// implement this function to have a working system
public int pickMove(int[][] legalMoves, int piece) {
Simulator sim = new Simulator(gameSim);
int bestMove = 0;
double bestHeuristic = Double.POSITIVE_INFINITY;
final double[] hues = new double[legalMoves.length];
ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); // for playing
// ExecutorService executor = Executors.newFixedThreadPool(1); // for training
for (int move = 0; move < legalMoves.length; move++) {
sim.revertTo(gameSim);
if (!sim.simMove(move, piece)) {
continue;
}
final Simulator sn = new Simulator(sim);
sn.revertTo(sim);
hues[move] = 0;
final int hMove = move;
Runnable aRunnable = new Runnable(){
@Override
public void run() {
forwardLookAvg(sn, 1, hues, hMove);
}
};
executor.execute(aRunnable);
// double heu = sim.getHeuristic();
// if (heu < bestHeuristic) {
// bestMove = move;
// bestHeuristic = heu;
// }
}
executor.shutdown();
try {
while (!executor.awaitTermination(50L, TimeUnit.DAYS)) {}
} catch (InterruptedException e) {
}
for (int move = 0; move < legalMoves.length; move++) {
if(hues[move] < bestHeuristic) {
bestHeuristic = hues[move];
bestMove = move;
}
}
return bestMove;
}
public int randomPiece() {
return randomWithRange(0,6);
}
public static int randomWithRange(int min, int max) {
int range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}
public static void main(String[] args) {
State s = new State();
// TFrame tFrame = new TFrame(s);
// Training
//Genetic gen = new Genetic(100, State.ROWS-10, State.COLS);
//Weights w = gen.train(25); // Number of generations
// Playing
Weights w = Weights.someWeights();
s = new State();
PlayerSkeleton p = new PlayerSkeleton(w, State.ROWS, State.COLS);
while(!s.hasLost()) {
int move = p.pickMove(s.legalMoves(), s.getNextPiece());
p.gameSim.simMove(move, s.getNextPiece());
s.makeMove(move);
// s.draw();
int x = s.getRowsCleared();
if(x % 10000 == 0 && x > 0)
System.out.println(x+" rows.");
// tFrame.setScoreLabel(s.getRowsCleared());
// s.drawNext(0,0);
// try {
// Thread.sleep(00);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
}
//System.out.println("You have completed "+p.playAndReturnScore()+" rows.");
System.out.println("You have completed "+s.getRowsCleared()+" rows.");
System.exit(0);
}
}