-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs.c
289 lines (262 loc) · 7.09 KB
/
dfs.c
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
//************************************
//"dfs.c"
//max-min deep search to play 2048 game
#include "gamestruct.h"
#define NGIF -9999
#define PGIF 9999
int game_board[SIZE][SIZE];
int initialboard[SIZE][SIZE];
//*********************//
//max-min tree strcut
typedef struct MaxNode
{
int dir;
int value;
int alpha;
int beta;
struct MinNode *next[SIZE];
}MaxNode;
typedef struct minnode
{
int value;
int alpha;
int beta;
struct MaxNode *next[2*SIZE*SIZE];
}MinNode;
//**********************//
//*********************************//
//Function:get_exp
// to get the experiment of a number
//Type:int
//Inlet parameter:int x (the number to calculate)
//Outlet parameter:count (the experiment of the number)
int get_exp(int x)
{
int count = 1,i = 2;
if(x==0)
count = 0;
else
{
while(i < x)
{
i *= 2;
count++;
}
}
return count;
}
//*********************************//
//Function:get_value
// access the value of current game board
//Type:int
//Inlet parameter:void,(global variable) board[SIZE][SIZE] (game board of 2048)
//Outlet parameter:value (the accessed value of the board)
int get_value(void)
{
int corner[SIZE] = {0,0,3,3}; //to find the corner near the max number
int xy_change[SIZE] = {1,1,-1,-1}; //change nearly x¡¢y to calculate the value
int a,b,x,y,i,j;
int value = 0,value1,value2,count = 0,max = -99;
for(i=0;i<SIZE;i++) //to find the (x,y) of the max number
{
for(j=0;j<SIZE;j++)
{
if(game_board[i][j] != 0)
count++;
if(game_board[i][j] > max)
{
x = i;
y = j;
max = game_board[i][j];
}
}
}
for(i=0;i<3;i++)
{
a = corner[x] + i * xy_change[x];
b = corner[y] + i * xy_change[y];
value += 2*(get_exp(game_board[a+i*xy_change[x]][b+i*xy_change[y]]) - get_exp(game_board[a][b]));
if(i==2 || i==3)
{
value1 = get_exp(game_board[a-xy_change[x]][b]) - get_exp(game_board[a][b]);
value2 = get_exp(game_board[a][b-xy_change[y]]) - get_exp(game_board[a][b]);
value += (value1 + value2);
}
for(j=0;j<3-i;j++)
{
value1 = get_exp(game_board[a+j*xy_change[x]][b]) - get_exp(game_board[a+(j+1)*xy_change[x]][b]);
value2 = get_exp(game_board[a][b+j*xy_change[y]]) - get_exp(game_board[a][b+(j+1)*xy_change[y]]);
value += (value1 + value2);
}
}
value = value *3 - count * 2;
return value;
}
//*********************************//
//Function:delete_maxnode
//Type:void
//Inlet parameter:MaxNode *T
//Outlet parameter:none
void delete_maxnode(MaxNode *T)
{
int i;
MaxNode *p = T;
MinNode *pnext = NULL;
if(p != NULL)
{
for(i=0;i<SIZE;i++)
{
if(p->next[i] != NULL)
{
pnext = p->next[i];
delete_minnode(pnext);
}
}
free(p);
}
}
//*********************************//
//Function:delete_minnode
//Type:void
//Inlet parameter:MinNode *T
//Outlet parameter:none
void delete_minnode(MinNode *T)
{
int i,j;
MinNode *p = T;
MaxNode *pnext = NULL;
if(p != NULL)
{
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
if(p->next[(i*4+j)*2] != NULL)
{
pnext = p->next[(i*4+j)*2];
delete_maxnode(pnext);
}
if(p->next[(i*4+j)*2+1])
{
pnext = p->next[(i*4+j)*2+1];
delete_maxnode(pnext);
}
}
}
free(p);
}
}
//*********************************//
//Function:create_maxnode
//Type:int
//Inlet parameter:MaxNode *T,int *dir,int depth,int alpha,int beta
//Outlet parameter:value
int create_maxnode(MaxNode *T,int *dir,int depth,int alpha,int beta)
{
int i;
if(depth==0)
{
alpha = get_value();
}
else
{
depth--;
memcpy(initialboard,game_board,16*sizeof(int));
if(!(T = (MaxNode*)malloc(sizeof(MaxNode))))
{
printf("FAILED TO CREATE MAX NODE!\n");
exit(0);
}
for(i=0;i<4;i++)
{
if(move_game(i,game_board) != -1)
{
T->value = create_minnode(T->next[i],depth,alpha,beta);
if(T->value > alpha)
{
*dir = i;
alpha = T->value;
}
else
//delete_maxnode(T->next[i]);
T->next[i] = NULL;
memcpy(game_board,initialboard,SIZE*SIZE*sizeof(int));
}
else
{
T->next[i] = NULL;
}
}
T->value = alpha;
}
return alpha;
}
//*********************************//
//Function:create_minnode
//Type:int
//Inlet parameter:MinNode *T,int depth,int alpha,int beta
//Outlet parameter:value
int create_minnode(MinNode *T,int depth,int alpha,int beta)
{
int i,j;
int initialboard[SIZE][SIZE];
if(depth==0)
{
beta = get_value();
}
else
{
depth--;
if(!(T = (MinNode*)malloc(sizeof(MinNode))))
{
printf("FAILED TO CREATE MIN NODE!\n");
exit(0);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(game_board[i][j]==0)
{
game_board[i][j] = 2;
T->value = create_maxnode(T->next[(i*4+j)*2],&T->next[(i*4+j)*2]->dir,depth,alpha,beta);
if(T->value < beta)
beta = T->value;
else
//delete_maxnode(T->next[(i*4+j)*2]);
T->next[(i*4+j)*2] = NULL;
game_board[i][j] = 4;
T->value = create_maxnode(T->next[(i*4+j)*2+1],&T->next[(i*4+j)*2+1]->dir,depth,alpha,beta);
if(T->value < beta)
beta = T->value;
else
//delete_maxnode(T->next[(i*4+j)*2+1]);
T->next[(i*4+j)*2+1] = NULL;
game_board[i][j] = 0;
}
else
{
T->next[(i*4+j)*2] = NULL;
T->next[(i*4+j)*2+1] = NULL;
}
}
}
T->value = beta;
}
return beta;
}
//*********************************//
//Function:DFS
// deep first search to find a choose of next step in game 2048
//Type:int
//Inlet parameter:int board[SIZE][SIZE]
//Outlet parameter:code[SearchTree.dir] (scan code of up|right|down|left on keyboard)
int DFS(int board[SIZE][SIZE])
{
int code[4] = {72,77,80,75};
int value;
MaxNode SearchTree;
memcpy(game_board,board,SIZE*SIZE*sizeof(int));
value = create_maxnode(&SearchTree,&SearchTree.dir,2,NGIF,PGIF);
return code[SearchTree.dir];
}