-
Notifications
You must be signed in to change notification settings - Fork 3
/
ai.cpp
300 lines (261 loc) · 7.8 KB
/
ai.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
#include <bits/stdc++.h>
using namespace std;
struct board {
int arr[4][4];
};
double weight[4][4] = {
{13.5, 12.1, 10.2, 9.9},
{9.9, 8.8, 7.6, 7.2},
{6.0, 5.6, 3.7, 1.6},
{1.2, 0.9, 0.5, 0.3}
};
int values[2] = {1, 2};
double probs[2] = {0.9, 0.1};
bool inline isSame(board grid1, board grid2) {
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
if(grid1.arr[i][j] != grid2.arr[i][j])
return false;
return true;
}
void inline move_line(int cells[4]) {
int target = 0;
for(int i = 1; i < 4; i++) {
int targetVal = cells[target];
int currVal = cells[i];
if(currVal) { //currVal != 0
if(!targetVal) //targetVal == 0
cells[target] = currVal, cells[i] = 0;
else {
if(targetVal == currVal)
cells[i] = 0, cells[target]++;
else
cells[i] = 0, cells[target + 1] = currVal;
target++;
}
}
}
}
board inline move_left(board grid) {
board grid1;
for(int i = 0; i < 4; i++) {
int temp[4];
for(int j = 0; j < 4; j++)
temp[j] = grid.arr[i][j];
move_line(temp);
for(int j = 0; j < 4; j++)
grid1.arr[i][j] = temp[j];
}
return grid1;
}
board inline move_right(board grid) {
board grid1;
for(int i = 0; i < 4; i++) {
int temp[4];
for(int j = 0; j < 4; j++)
temp[j] = grid.arr[i][3 - j];
move_line(temp);
for(int j = 0; j < 4; j++)
grid1.arr[i][3 - j] = temp[j];
}
return grid1;
}
board inline move_up(board grid) {
board grid1;
for(int j = 0; j < 4; j++) {
int temp[4];
for(int i = 0; i < 4; i++)
temp[i] = grid.arr[i][j];
move_line(temp);
for(int i = 0; i < 4; i++)
grid1.arr[i][j] = temp[i];
}
return grid1;
}
board inline move_down(board grid) {
board grid1;
for(int j = 0; j < 4; j++) {
int temp[4];
for(int i = 0; i < 4; i++)
temp[i] = grid.arr[3 - i][j];
move_line(temp);
for(int i = 0; i < 4; i++)
grid1.arr[3 - i][j] = temp[i];
}
return grid1;
}
board inline moveGrid(board grid, int ind) {
if(ind == 0)
return move_left(grid);
else if(ind == 1)
return move_right(grid);
else if(ind == 2)
return move_up(grid);
else if(ind == 3)
return move_down(grid);
}
double inline currScore(board grid) {
double tot = -1e9;
double s0 = 0, s1 = 0, s2 = 0, s3 = 0;
double sc0 = 0, sc1 = 0, sc2 = 0, sc3 = 0;
double grid1[4][4];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++) {
if(!grid.arr[i][j])
grid1[i][j] = 0;
else
grid1[i][j] = pow(2, grid.arr[i][j]);
}
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[i][j]);
tot = max(tot, s0);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[j][i]);
tot = max(tot, sc0);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[i][3 - j]);
tot = max(tot, s1);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[j][3 - i]);
tot = max(tot, sc1);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[3 - i][j]);
tot = max(tot, s2);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[j][3 - i]);
tot = max(tot, sc2);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[3 - i][3 - j]);
tot = max(tot, s3);
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
s0 += (grid1[i][j] * weight[3 - j][3 - i]);
tot = max(tot, sc3);
return tot;
}
double expectiMax(board grid, int depth) {
if(depth == 0)
return currScore(grid);
double score = 0;
double tot_prob = 0;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(!grid.arr[i][j]) {
for(int ind = 0; ind < 2; ind++) {
board grid1 = grid;
grid1.arr[i][j] = values[ind];
double best = -1e9;
int direction = -1;
for(int dir = 0; dir < 4; dir++) {
board grid2 = moveGrid(grid1, dir);
if(!isSame(grid2, grid1)) {
double score1 = expectiMax(grid2, depth - 1);
if(score1 > best)
best = score1, direction = dir;
}
}
if(direction != -1)
score += (probs[ind] * best);
else
score += (probs[ind] * currScore(grid1));
tot_prob += probs[ind];
}
}
}
}
return (score / tot_prob);
}
int inline blankNum(board grid) {
int num = 0;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
if(!grid.arr[i][j])
num++;
return num;
}
int inline bestMove(board grid, int depth) {
int move = 0;
double highestScore = -1e9;
for(int i = 0; i < 4; i++) {
board grid1 = moveGrid(grid, i);
if(!isSame(grid, grid1)) {
double score1 = expectiMax(grid1, depth);
if(score1 > highestScore)
highestScore = score1, move = i;
//cout << "Score1 " << i << ": " << score1 << endl;
}
}
return move;
}
bool isOver(board grid) {
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
if(grid.arr[i][j] == 0)
return false;
return true;
}
int main(int argc ,char * argv[]) {
srand(time(NULL));
//cout << argv[1] << endl;
vector <int> vec;
for(int i = 0; i < 16; i++) {
int a = argv[1][2 * i] - 48;
int b = argv[1][2 * i + 1] - 48;
int num = (10 * a) + b;
//cout << num << endl;
vec.push_back(num);
}
board matrix;
int ind = 0;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
matrix.arr[i][j] = vec[ind++];
int depth = 2;
int soFar = 1;
int move = bestMove(matrix, depth); //move to be printed
printf("%d", move);
/*while(true) {
int move = bestMove(matrix, depth); //move to be printed
cout << "Move: " << move << endl;
cout << "Moves so far: " << soFar++ << endl;
cout << endl;
matrix = moveGrid(matrix, move);
int count1 = 0;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
if(!matrix.arr[i][j])
count1++, cout << "0 ";
else
cout << pow(2, matrix.arr[i][j]) << " ";
}
cout << endl;
}
if(isOver(matrix))
break;
int pos1 = rand() % count1;
int val = rand() % 10 + 1, val1;
if(val == 10)
val1 = 2;
else
val1 = 1;
int ind = 0;
cout << "Random: ";
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++) {
if(ind == pos1 && !matrix.arr[i][j]) {
matrix.arr[i][j] = val1, ind++;
cout << val1 << " (" << i << ", " << j << ")" << endl;
}
if(!matrix.arr[i][j])
ind++;
}
}*/
return 0;
}