forked from nneonneo/2048-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.cpp
488 lines (388 loc) · 12.8 KB
/
2048.cpp
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <map>
#include "2048.h"
/* We can perform state lookups one row at a time by using arrays with 65536 entries. */
/* Move tables. Each row or compressed column is mapped to (oldrow^newrow) assuming row/col 0.
*
* Thus, the value is 0 if there is no move, and otherwise equals a value that can easily be
* xor'ed into the current board state to update the board. */
static board_t row_left_table[65536];
static board_t row_right_table[65536];
static board_t col_up_table[65536];
static board_t col_down_table[65536];
void init_move_tables(void) {
unsigned row;
memset(row_left_table, 0, sizeof(row_left_table));
memset(row_right_table, 0, sizeof(row_right_table));
memset(col_up_table, 0, sizeof(col_up_table));
memset(col_down_table, 0, sizeof(col_down_table));
for(row = 0; row < 65536; row++) {
unsigned int line[4] = {row & 0xf, (row >> 4) & 0xf, (row >> 8) & 0xf, (row >> 12) & 0xf};
row_t result;
int i, j;
/* execute a move to the left */
for(i=0; i<3; i++) {
for(j=i+1; j<4; j++) {
if(line[j] != 0)
break;
}
if(j == 4)
break; // no more tiles to the right
if(line[i] == 0) {
line[i] = line[j];
line[j] = 0;
i--; // retry this entry
} else if(line[i] == line[j] && line[i] != 0xf) {
line[i]++;
line[j] = 0;
}
}
result = (line[0]) | (line[1] << 4) | (line[2] << 8) | (line[3] << 12);
row_left_table[row] = row ^ result;
row_right_table[reverse_row(row)] = reverse_row(row) ^ reverse_row(result);
col_up_table[row] = unpack_col(row) ^ unpack_col(result);
col_down_table[reverse_row(row)] = unpack_col(reverse_row(row)) ^ unpack_col(reverse_row(result));
}
}
#define DO_LINE(tbl,i,lookup,xv) do { \
tmp = tbl[lookup]; \
ret ^= xv; \
} while(0)
#define DO_ROW(tbl,i) DO_LINE(tbl,i, (board >> (16*i)) & ROW_MASK, tmp << (16*i))
#define DO_COL(tbl,i) DO_LINE(tbl,i, pack_col((board >> (4*i)) & COL_MASK), tmp << (4*i))
static inline board_t execute_move_0(board_t board) {
board_t tmp;
board_t ret = board;
DO_COL(col_up_table, 0);
DO_COL(col_up_table, 1);
DO_COL(col_up_table, 2);
DO_COL(col_up_table, 3);
return ret;
}
static inline board_t execute_move_1(board_t board) {
board_t tmp;
board_t ret = board;
DO_COL(col_down_table, 0);
DO_COL(col_down_table, 1);
DO_COL(col_down_table, 2);
DO_COL(col_down_table, 3);
return ret;
}
static inline board_t execute_move_2(board_t board) {
board_t tmp;
board_t ret = board;
DO_ROW(row_left_table, 0);
DO_ROW(row_left_table, 1);
DO_ROW(row_left_table, 2);
DO_ROW(row_left_table, 3);
return ret;
}
static inline board_t execute_move_3(board_t board) {
board_t tmp;
board_t ret = board;
DO_ROW(row_right_table, 0);
DO_ROW(row_right_table, 1);
DO_ROW(row_right_table, 2);
DO_ROW(row_right_table, 3);
return ret;
}
#undef DO_ROW
#undef DO_COL
#undef DO_LINE
/* Execute a move. */
static inline board_t execute_move(int move, board_t board) {
switch(move) {
case 0: // up
return execute_move_0(board);
case 1: // down
return execute_move_1(board);
case 2: // left
return execute_move_2(board);
case 3: // right
return execute_move_3(board);
default:
return ~0ULL;
}
}
static inline int get_max_rank(board_t board) {
int maxrank = 0;
while(board) {
int k = board & 0xf;
if(k > maxrank) maxrank = k;
board >>= 4;
}
return maxrank;
}
/* Optimizing the game */
static float line_heur_score_table[65536];
static float row_score_table[65536];
struct eval_state {
typedef std::map<board_t, float> trans_table_t;
trans_table_t trans_table; // transposition table, to cache previously-seen moves
float cprob_thresh;
int maxdepth;
int curdepth;
int cachehits;
int moves_evaled;
eval_state() : cprob_thresh(0), maxdepth(0), curdepth(0), cachehits(0), moves_evaled(0) {
}
};
// score a single board heuristically
static float score_heur_board(board_t board);
// score a single board actually (adding in the score from spawned 4 tiles)
static float score_board(board_t board);
// score over all possible moves
static float score_move_node(eval_state &state, board_t board, float cprob);
// score over all possible tile choices and placements
static float score_tilechoose_node(eval_state &state, board_t board, float cprob);
void init_score_tables(void) {
unsigned row;
memset(line_heur_score_table, 0, sizeof(line_heur_score_table));
memset(row_score_table, 0, sizeof(row_score_table));
for(row = 0; row < 65536; row++) {
unsigned int line[4] = {row & 0xf, (row >> 4) & 0xf, (row >> 8) & 0xf, (row >> 12) & 0xf};
int i;
float heur_score = 0;
float score = 0;
for(i=0; i<4; i++) {
int rank = line[i];
if(rank == 0) {
heur_score += 10000;
} else if(rank >= 2) {
// the score is the total sum of the tile and all intermediate merged tiles
score += (rank-1) * powf(2, rank);
}
}
int maxi = 0;
int maxrank = 0;
for(i=0; i<4; i++) {
int rank = line[i];
if(rank > maxrank) {
maxrank = rank;
maxi = i;
}
}
if(maxi == 0 || maxi == 3)
heur_score += 20000;
// Check if maxis are close to eachother, and of diff ranks (eg 128 256)
for(i=1; i<4; i++) {
if ((line[i] == line[i-1] + 1) || (line[i] == line[i-1] - 1)) {
heur_score += 1000;
}
}
// Check if the values are ordered:
if ((line[0] < line[1]) && (line[1] < line[2]) && (line[2] < line[3])) heur_score += 10000;
if ((line[0] > line[1]) && (line[1] > line[2]) && (line[2] > line[3])) heur_score += 10000;
row_score_table[row] = score;
line_heur_score_table[row] = heur_score;
}
}
#define SCORE_BOARD(board,tbl) ((tbl)[(board) & ROW_MASK] + \
(tbl)[((board) >> 16) & ROW_MASK] + \
(tbl)[((board) >> 32) & ROW_MASK] + \
(tbl)[((board) >> 48) & ROW_MASK])
#define SCORE_COL_BOARD(board,tbl) ((tbl)[pack_col((board) & COL_MASK)] + \
(tbl)[pack_col(((board) >> 4) & COL_MASK)] + \
(tbl)[pack_col(((board) >> 8) & COL_MASK)] + \
(tbl)[pack_col(((board) >> 12) & COL_MASK)])
static float score_heur_board(board_t board) {
return SCORE_BOARD(board, line_heur_score_table) + SCORE_COL_BOARD(board, line_heur_score_table) + 100000;
}
static float score_board(board_t board) {
return SCORE_BOARD(board, row_score_table);
}
static float score_tilechoose_node(eval_state &state, board_t board, float cprob) {
float res = 0;
int num_open = 0;
for(int i=0; i<16; i++) {
if(((board >> (4*i)) & 0xf) == 0)
num_open++;
}
cprob /= num_open;
for(int i=0; i<16; i++) {
if(((board >> (4*i)) & 0xf) == 0) {
res += score_move_node(state, board | (((board_t)1) << (4*i)), cprob * 0.9f) * 0.9f;
res += score_move_node(state, board | (((board_t)2) << (4*i)), cprob * 0.1f) * 0.1f;
}
}
return res / num_open;
}
/* Statistics and controls */
// cprob: cumulative probability
/* don't recurse into a node with a cprob less than this threshold */
#define CPROB_THRESH_BASE (0.0001f)
#define CACHE_DEPTH_LIMIT 6
#define SEARCH_DEPTH_LIMIT 7
static float score_move_node(eval_state &state, board_t board, float cprob) {
if(cprob < state.cprob_thresh || state.curdepth >= SEARCH_DEPTH_LIMIT) {
if(state.curdepth > state.maxdepth)
state.maxdepth = state.curdepth;
return score_heur_board(board);
}
if(state.curdepth < CACHE_DEPTH_LIMIT) {
const eval_state::trans_table_t::iterator &i = state.trans_table.find(board);
if(i != state.trans_table.end()) {
state.cachehits++;
return i->second;
}
}
int move;
float best = 0;
state.curdepth++;
for(move=0; move<4; move++) {
board_t newboard = execute_move(move, board);
state.moves_evaled++;
if(board == newboard)
continue;
float res = score_tilechoose_node(state, newboard, cprob);
if(res > best)
best = res;
}
state.curdepth--;
if(state.curdepth < CACHE_DEPTH_LIMIT) {
state.trans_table[board] = best;
}
return best;
}
static float _score_toplevel_move(eval_state &state, board_t board, int move) {
//int maxrank = get_max_rank(board);
board_t newboard = execute_move(move, board);
if(board == newboard)
return 0;
state.cprob_thresh = CPROB_THRESH_BASE;
return score_tilechoose_node(state, newboard, 1.0f);
}
float score_toplevel_move(board_t board, int move) {
float res;
struct timeval start, finish;
double elapsed;
eval_state state;
gettimeofday(&start, NULL);
res = _score_toplevel_move(state, board, move);
gettimeofday(&finish, NULL);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_usec - start.tv_usec) / 1000000.0;
printf("Move %d: result %f: eval'd %d moves (%d cache hits, %zd cache size) in %.2f seconds (maxdepth=%d)\n", move, res,
state.moves_evaled, state.cachehits, state.trans_table.size(), elapsed, state.maxdepth);
return res;
}
/* Find the best move for a given board. */
int find_best_move(board_t board) {
int move;
float best = 0;
int bestmove = -1;
print_board(board);
printf("Current scores: heur %.0f, actual %.0f\n", score_heur_board(board), score_board(board));
for(move=0; move<4; move++) {
float res = score_toplevel_move(board, move);
if(res > best) {
best = res;
bestmove = move;
}
}
return bestmove;
}
int ask_for_move(board_t board) {
int move;
char validstr[5];
char *validpos = validstr;
print_board(board);
for(move=0; move<4; move++) {
if(execute_move(move, board) != board)
*validpos++ = "UDLR"[move];
}
*validpos = 0;
if(validpos == validstr)
return -1;
while(1) {
char movestr[64];
const char *allmoves = "UDLR";
printf("Move [%s]? ", validstr);
if(!fgets(movestr, sizeof(movestr)-1, stdin))
return -1;
if(!strchr(validstr, toupper(movestr[0]))) {
printf("Invalid move.\n");
continue;
}
return strchr(allmoves, toupper(movestr[0])) - allmoves;
}
}
/* Playing the game */
static int draw_tile() {
return (unif_random(10) < 9) ? 1 : 2;
}
static board_t insert_tile_rand(board_t board, int tile) {
int num_open = 0;
for(int i=0; i<16; i++) {
if(((board >> (4*i)) & 0xf) == 0)
num_open++;
}
if(num_open == 0) {
printf("insert_tile_rand: no open spots!\n");
return board;
}
int index = unif_random(num_open);
for(int i=0; i<16; i++) {
if(((board >> (4*i)) & 0xf) != 0)
continue;
if(index == 0) {
board |= ((board_t)tile) << (4*i);
break;
}
index--;
}
return board;
}
static board_t initial_board() {
board_t board = 0;
/* Draw initial values */
for(int i=0; i<2; i++) {
board = insert_tile_rand(board, draw_tile());
}
return board;
}
void play_game(get_move_func_t get_move) {
board_t board = initial_board();
int moveno = 0;
int scorepenalty = 0; // "penalty" for obtaining free 4 tiles
while(1) {
int move;
board_t newboard;
for(move = 0; move < 4; move++) {
if(execute_move(move, board) != board)
break;
}
if(move == 4)
break; // no legal moves
printf("\nMove #%d, current score=%.0f\n", ++moveno, score_board(board) - scorepenalty);
move = get_move(board);
if(move < 0)
break;
newboard = execute_move(move, board);
if(newboard == board) {
printf("Illegal move!\n");
moveno--;
continue;
}
int tile = draw_tile();
if(tile == 2)
scorepenalty += 4;
board = insert_tile_rand(newboard, tile);
}
print_board(board);
printf("\nGame over. Your score is %.0f. The highest rank you achieved was %d.\n", score_board(board) - scorepenalty, get_max_rank(board));
}
int main(int argc, char **argv) {
(void)argc;
(void)argv;
init_move_tables();
init_score_tables();
play_game(find_best_move);
return 0;
}