-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
186 lines (170 loc) · 4.17 KB
/
main.cc
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "bot.h"
#include "util.h"
//#define LOG_MOVES
//#define REPLAY_MOVES
// globals
int flag_verbose;
FILE* log_fp = NULL;
// ---------------------------------------------------------
// Functions to implement game runner
int calculate_game_score(board_t b, int num_tile4) {
int score = 0;
// 4: 4
// 8: 8 + 4*2
// 16: 16 + 8*2 + 4*4
// k: k*(log(k)-1)
while (b) {
int rank = b & 0xf;
if (rank >= 2) {
score += (rank - 1) << rank;
}
b >>= 4;
}
return score - num_tile4 * 4;
}
board_t random_tile(board_t b, int* num_tile4) {
board_t tile = my_random() % 10 == 0 ? 2 : 1;
if (tile == 2)
(*num_tile4)++;
int blank = count_blank_ex(b);
if (blank == 0) blank = 16; // hack for "blank" overflow
int n = my_random() % blank;
board_t tmp = b;
while (1) {
while (tmp & 0xf) {
tmp >>= 4;
tile <<= 4;
}
if (n-- == 0)
break;
tmp >>= 4;
tile <<= 4;
}
return b | tile;
}
bool is_can_move(board_t b) {
board_t t = transpose_ex(b);
for (int m = 0; m < 4; m++) {
if (b != do_move_ex(b, t, m))
return true;
}
return false;
}
void main_loop() {
double t0 = now();
char log_filename[1024];
sprintf(log_filename, "log/%d.txt", my_random_seed);
board_t b = 0;
int num_tile4 = 0;
b = random_tile(b, &num_tile4);
b = random_tile(b, &num_tile4);
#if defined(REPLAY_MOVES)
log_fp = fopen(log_filename, "r");
#elif defined(LOG_MOVES)
log_fp = fopen(log_filename, "w");
#endif
int move_count = 0;
if (flag_verbose)
print_pretty_board(b);
while (is_can_move(b)) {
int m = 0;
#if defined(REPLAY_MOVES)
if (!log_fp || fscanf(log_fp, "move %d\n", &m) != 1) {
printf("bad replay\n");
break;
}
#elif defined(LOG_MOVES)
m = root_search_move(b);
if (log_fp) fprintf(log_fp, "move %d\n", m);
#else
m = root_search_move(b);
#endif
board_t b2 = do_move_ex(b, transpose_ex(b), m);
if (b == b2) {
printf("bad move\n");
break;
}
move_count++;
my_random_seed ^= murmur3_simplified_ex(b);
b = random_tile(b2, &num_tile4);
if (flag_verbose) {
printf("step %d, move %c, score=%d\n",
move_count, move_str[m],
calculate_game_score(b, num_tile4));
print_pretty_board(b);
}
#if 0
print_board(b);
printf("\n");
printf("\n");
#endif
}
double t1 = now();
printf("time=%f, final score=%d, moves=%d, max tile=%d\n",
t1-t0,
calculate_game_score(b, num_tile4),
move_count,
find_max_tile_ex(b));
#if defined(REPLAY_MOVES) || defined(LOG_MOVES)
fclose(log_fp);
#endif
#if 0
extern int64_t min_scores[17];
for (int i = 0; i <= 16; i++)
printf("min score %d: %ld\n", i, min_scores[i]);
#endif
}
int main(int argc, char* argv[]) {
int opt;
while ((opt = getopt(argc, argv, "vs:p:")) != -1) {
switch (opt) {
case 'v':
flag_verbose = 1;
break;
case 's':
my_random_seed = atoi(optarg);
break;
case 'p':
if (sscanf(optarg, "%d,%f,%d",
&max_lookahead,
&search_threshold,
&maybe_dead_threshold) == 3) {
} else if (sscanf(optarg, "reverse=%f,%f,%f,%f,%f",
¶_reverse_weight,
¶_reverse,
¶_reverse_4,
¶_reverse_8,
¶_reverse_12)==5) {
} else if (sscanf(optarg, "equal=%f",
¶_equal)==1) {
} else if (sscanf(optarg, "inc=%f,%f,%f,%f",
¶_inc_0,
¶_inc_1,
¶_inc_2,
¶_inc_3) == 4) {
} else if (sscanf(optarg, "smooth=%f,%f,%f,%f",
¶_smooth,
¶_smooth_4,
¶_smooth_8,
¶_smooth_12) == 4) {
} else if (sscanf(optarg, "blank=%f,%f,%f",
¶_blank_1,
¶_blank_2,
¶_blank_3) == 3) {
} else {
printf("bad arg -p %s", optarg);
return -1;
}
break;
default: /* '?' */
printf("unknown option '%c'\n", opt);
break;
}
}
init_bot();
main_loop();
return 0;
}