-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest_move2.cpp
337 lines (296 loc) · 10 KB
/
best_move2.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
#include <fstream>
#include <vector>
#include <cmath> //////////////modified all functions to not use files anymore, but nodes
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
ofstream g("output.txt");
struct node {
int board[4][4];
int root_move;
int score;
};
void move_up(bool &ok, int a[4][4]);
void move_right(bool &ok, int a[4][4]);
void move_down(bool &ok, int a[4][4]);
void move_left(bool &ok, int a[4][4]);
int make_tree(string b, int depth, int width);
int evaluate(int board[4][4]);
bool next_board(int o_board[4][4], int i_board[4][4], int move);
void generate_2(int board[4][4]);
int make_generations(node out[], node in[], int width);
int make_children(node out[], node in[], int width);
bool gen_cmp(node a, node b){
return rand()%2;
}
bool child_cmp(node a, node b) {
return (a.score > b.score);
}
void debug(int board[4][4])
{
for (int i=0; i<4; ++i) {
for (int j=0; j<4; ++j)
cout<<board[i][j]<<" ";
cout<<endl;
}
}
int main(){
srand(time(0)); //for the random function
int tree_depth=10, tree_width=100;
//here I should create the tree using two vectors with min max beam search and then choose the children using the eval function
int result = make_tree("Z:/GitHub/2048-environment/tree.txt",tree_depth,tree_width);
cout<<to_string(result);
return 1;
}
//reads a state from a file and gives the next best move
int make_tree(string b, int depth, int width) {
//2 rows for list of current states of the board and for list of board with the 2s generated on them
node children[width*5];
node generations[width*17];
int x;
int child_nr = 0; //to count how many children of the big 4 are valid
ifstream brd(b);
int board[4][4];
for (int i = 0; i < 4; ++i) //read board from file
for (int j = 0; j < 4; ++j) {
brd>>x;
if (x)
board[i][j]=static_cast<int>(log2(x));//since all values are powers of 2, save the log
else board[i][j]=0;
}
for(int i=0; i<4; ++i) {
x = next_board(children[child_nr].board,board,i); //will write in 'tree[0][child_nr].board', the board for the very first node in the first row of the tree, the modified 'board' after move '0' and x will verify if the fct outputed true or false
if (x){
children[child_nr].root_move = i; //goes ot the next node of the first row of the tree
children[child_nr].score = evaluate(children[child_nr].board);
child_nr++;
}
}
sort(children, children + child_nr + 1, child_cmp);
int best_move = children[0].root_move;
//child_nr will keep the number of valid root_moves, ex 3 means 3 valid root_moves
int number_of_generations = 0;
for (int r=0; r<depth; ++r) { //every row
number_of_generations = make_generations(generations, children, min(child_nr, width/20)); //generated all possible generations
// sort the generations increasingly
sort(generations, generations + number_of_generations+1, child_cmp);
// generate all possible moves
child_nr = make_children(children, generations, min(number_of_generations, width));
if(!child_nr) break;
// sort all moves decreasingly
sort(children, children + child_nr, gen_cmp);
best_move=children[0].root_move;
}
brd.close();
return best_move;
//return best found move
}
int make_generations(node out[], node in[], int width){
int index_in=0, index_out=0;
for( ; index_in < width; ++index_in){
for(int i=0; i<4; ++i)
for(int j=0; j<4; ++j) {
if (in[index_in].board[i][j] == 0) {
for(int ii=0; ii<4; ++ii)
for(int jj=0; jj<4; ++jj)
out[index_out].board[ii][jj]=in[index_in].board[ii][jj];
out[index_out].board[i][j]=1;
out[index_out].root_move = in[index_in].root_move;
out[index_out].score=evaluate(out[index_out].board);
if(out[index_out].score < 5) continue;
index_out++;
}
}
}
return index_out;
}
int make_children(node out[], node in[], int width) {
int index_in=0, index_out=0;
for( ; index_in < width; ++index_in) {
if(in[index_in].score==0) return index_out;
for(int i=0; i<4; ++i) {
int ok=0;
ok = next_board(out[index_out].board, in[index_in].board, i);
if (ok) {
out[index_out].score = evaluate(out[index_out].board);
out[index_out].root_move = in[index_in].root_move;
index_out++;
}
}
}
return index_out;
}
//generates a random 2 in the empty spaces of the board
void generate_2(int board[4][4]){
//parse the board, find all places which are empty, choose randomly between them, insert a 2 in said place
int free_places[16], nr_places=0, counter=0;
for (int i=0; i<4; ++i)
for (int j=0; j<4; ++j){
if(board[i][j]==0){
free_places[nr_places] = counter; //keep in a vector the indices of the free spaces, as if marking each square of the matrix ascendigly
nr_places++;//count free spaces found
}
counter++;//count at which square we are
}
int random = rand() % nr_places; //will give a random nr in range of the empty spaces
board[free_places[random]/4][free_places[random]%4] = 2; //said square will become 2
}
//evaluates a state/board
int evaluate(int board[4][4]){
long score=0;
bool ok=0;
int aux[4][4];
for (int i=0; i<4; i++){
bool canMove=next_board(aux, board, i);
if (canMove) ok=1;
}
if(!ok) return 1;
int w_matrix[4][4]= { //snake-like pattern for the values, aims to have the highest number in the down left corner
{0, 0, 0, 0},
{1, 2, 3, 4},
{8, 7, 6, 5},
{9, 10, 11, 12}
};
for (int i=0; i<4; ++i)
for (int j=0; j<4; ++j)
score += board[i][j] * w_matrix[i][j];
// reward: ajacent conscutive numbers (snake shaped), many blank spaces, possible merges, punish: not enough blanks
return score;
}
//writes in the output board how the next board based on the input board will look like, returning 1 if valid and 0 if not
bool next_board(int o_board[4][4], int i_board[4][4], int move){
for (int i = 0; i < 4; ++i) //copy input board in a
for (int j = 0; j < 4; ++j)
o_board[i][j]=i_board[i][j];
bool legal_move=0;
switch(move) {
case 0: move_up(legal_move, o_board); break; //legal move will stay 0 if the move is invalid and 1 if valid, and a will change accordingly
case 1: move_right(legal_move, o_board); break;
case 2: move_down(legal_move, o_board); break;
case 3: move_left(legal_move, o_board); break;
}
return legal_move;
}
//move functions
void move_left(bool &ok, int a[4][4]){
for(int i=0; i<4; i++){
int n=0;
int prev=0;
for (int j=0; j<4; j++)
{
if (n==a[i][j] && n!=0){ ok=1;
a[i][prev] = n+1;
a[i][j] = 0;
n = 0;
continue;
}
if (a[i][j]!=0){
n = a[i][j];
prev = j;
}
}
}
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int k=0; k<3; k++){
if(a[i][k]==0 && a[i][k+1]!=0){ ok=1;
a[i][k]=a[i][k]^a[i][k+1];
a[i][k+1]=a[i][k]^a[i][k+1];
a[i][k]=a[i][k]^a[i][k+1];
}
}
}
}
}
void move_right(bool &ok, int a[4][4]){
for(int i=0; i<4; i++){
int n=0;
int prev=0;
for (int j=3; j>=0; j--)
{
if (n==a[i][j] && n!=0){ ok=1;
a[i][prev] = 1+n;
a[i][j] = 0;
n = 0;
continue;
}
if (a[i][j]!=0){
n = a[i][j];
prev = j;
}
}
}
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int k=3; k>0; k--){
if(a[i][k]==0 && a[i][k-1]!=0){ ok=1;
a[i][k]=a[i][k]^a[i][k-1];
a[i][k-1]=a[i][k]^a[i][k-1];
a[i][k]=a[i][k]^a[i][k-1];
}
}
}
}
}
void move_up(bool &ok, int a[4][4]){
for(int i=0; i<4; i++){
int n=0;
int prev=0;
for (int j=0; j<4; j++)
{
if (n==a[j][i] && n!=0){ ok=1;
a[prev][i] = 1+n;
a[j][i] = 0;
n = 0;
continue;
}
if (a[j][i]!=0){
n = a[j][i];
prev = j;
}
}
}
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int k=0; k<3; k++){
if(a[k][i]==0 && a[k+1][i]!=0){ ok=1;
a[k][i]=a[k][i]^a[k+1][i];
a[k+1][i]=a[k][i]^a[k+1][i];
a[k][i]=a[k][i]^a[k+1][i];
}
}
}
}
}
void move_down(bool &ok, int a[4][4]){
for(int i=0; i<4; i++){
int n=0;
int prev=0;
for (int j=3; j>=0; j--)
{
if (n==a[j][i] && n!=0){ ok=1;
a[prev][i] = 1+n;
a[j][i] = 0;
n = 0;
continue;
}
if (a[j][i]!=0){
n = a[j][i];
prev = j;
}
}
}
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int k=3; k>0; k--){
if(a[k][i]==0 && a[k-1][i]!=0){ ok=1;
a[k][i]=a[k][i]^a[k-1][i];
a[k-1][i]=a[k][i]^a[k-1][i];
a[k][i]=a[k][i]^a[k-1][i];
}
}
}
}
}