-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_search.c
321 lines (298 loc) · 8.23 KB
/
ai_search.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
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
#include "ai_header.h"
void ai_search_function(int **map)
{
struct tree_node *root = NULL;
//Make full tree untill depth 3
root = make_full_tree(map);
min_max_function(root); //min_max_function internally includes evaluation_function.
select_map(map, pick_next_map(root));
free_tree(root);
return;
}
struct tree_node* make_full_tree(int **map)
{
//root = max
//1 = min
//2 = max
//3 = min (leaf if depth3)
//4 = max (leaf if depth4)
int i, k, j, z, w;
struct tree_node *temp = NULL, *temp2 = NULL, *temp3 = NULL, *temp4 = NULL, *temp5 = NULL;
struct tree_node *root = create_node(map, TRUE, FALSE, MAX);
insert_child(root, MIN);
temp = root->child1;
for (i = 0; i < 7; i++)
{
insert_child(temp, MAX);
temp2 = temp->child1;
for (k = 0; k < 7; k++)
{
#ifdef DEPTH3
insert_child(temp2, LEAF);
temp2 = temp2->next_sibling;
#endif // DEPTH3
#ifdef DEPTH4
insert_child(temp2, MIN);
temp3 = temp2->child1;
for (j = 0; j < 7; j++)
{
insert_child(temp3, LEAF);
temp3 = temp3->next_sibling;
}
temp2 = temp2->next_sibling;
#endif // DEPTH4
#ifdef DEPTH5
insert_child(temp2, MIN);
temp3 = temp2->child1;
for (j = 0; j < 7; j++)
{
insert_child(temp3, MAX);
temp4 = temp3->child1;
for (z = 0; z < 7; z++)
{
insert_child(temp4, LEAF);
temp4 = temp4->next_sibling;
}
temp3 = temp3->next_sibling;
}
temp2 = temp2->next_sibling;
#endif // DEPTH5
#ifdef DEPTH6
insert_child(temp2, MIN);
temp3 = temp2->child1;
for (j = 0; j < 7; j++)
{
insert_child(temp3, MAX);
temp4 = temp3->child1;
for (z = 0; z < 7; z++)
{
insert_child(temp4, MIN);
temp5 = temp4->child1;
for (w = 0; w < 7; w++)
{
insert_child(temp5, LEAF);
temp5 = temp5->next_sibling;
}
temp4 = temp4->next_sibling;
}
temp3 = temp3->next_sibling;
}
temp2 = temp2->next_sibling;
#endif // DEPTH6
}
temp = temp->next_sibling;
}
return root;
}
//count most winning line
void evaluation_function_version_1(struct tree_node *node)
{
node->evaluation_value = 0;
int i, k;
int **map = node->map;
if (decide_win_or_lose_or_continue(map) == USERWIN)
{
node->evaluation_value = -9999;
return;
}
else if (decide_win_or_lose_or_continue(map) == AIWIN)
{
node->evaluation_value = 9999;
return;
}
else
{
//2 point for 2 consecutive stones
for (i = 0; i < 6; i++)
{
for (k = 0; k < 5; k++)
{
if (map[i][k] == 0 && map[i][k + 1] == 1 && map[i][k + 2] == 1)
node->evaluation_value -= 2;
if (map[i][k] == 1 && map[i][k + 1] == 1 && map[i][k + 2] == 0)
node->evaluation_value -= 2;
if (map[i][k] == 0 && map[i][k + 1] == 2 && map[i][k + 2] == 2)
node->evaluation_value += 2;
if (map[i][k] == 2 && map[i][k + 1] == 2 && map[i][k + 2] == 0)
node->evaluation_value += 2;
}
}
for (i = 0; i < 4; i++)
{
for (k = 0; k < 7; k++)
{
if (map[i][k] == 1 && map[i + 1][k] == 1 && map[i + 2][k] == 0)
node->evaluation_value -= 2;
if (map[i][k] == 0 && map[i + 1][k] == 1 && map[i + 2][k] == 1)
node->evaluation_value -= 2;
if (map[i][k] == 2 && map[i + 1][k] == 2 && map[i + 2][k] == 0)
node->evaluation_value += 2;
if (map[i][k] == 0 && map[i + 1][k] == 2 && map[i + 2][k] == 2)
node->evaluation_value += 2;
}
}
for (i = 0; i < 4; i++)
{
for (k = 0; k < 5; k++)
{
if (map[i][k] == 1 && map[i + 1][k + 1] == 1 && map[i + 2][k + 2] == 0)
node->evaluation_value -= 2;
if (map[i][k] == 0 && map[i + 1][k + 1] == 1 && map[i + 2][k + 2] == 1)
node->evaluation_value -= 2;
if (map[i][k] == 2 && map[i + 1][k + 1] == 2 && map[i + 2][k + 2] == 0)
node->evaluation_value += 2;
if (map[i][k] == 0 && map[i + 1][k + 1] == 2 && map[i + 2][k + 2] == 2)
node->evaluation_value += 2;
if (map[i + 2][k] == 1 && map[i + 1][k + 1] == 1 && map[i][k + 2] == 0)
node->evaluation_value -= 2;
if (map[i + 2][k] == 0 && map[i + 1][k + 1] == 1 && map[i][k + 2] == 1)
node->evaluation_value -= 2;
if (map[i + 2][k] == 2 && map[i + 1][k + 1] == 2 && map[i][k + 2] == 0)
node->evaluation_value += 2;
if (map[i + 2][k] == 0 && map[i + 1][k + 1] == 2 && map[i][k + 2] == 2)
node->evaluation_value += 2;
}
}
//4 point for 3 consecutive stones
for (i = 0; i < 6; i++)
{
for (k = 0; k < 4; k++)
{
if (map[i][k] == 1 && map[i][k + 1] == 1 && map[i][k + 2] == 1 && map[i][k + 3] == 0)
node->evaluation_value -= 4;
if (map[i][k] == 0 && map[i][k + 1] == 1 && map[i][k + 2] == 1 && map[i][k + 3] == 1)
node->evaluation_value -= 4;
if (map[i][k] == 2 && map[i][k + 1] == 2 && map[i][k + 2] == 2 && map[i][k + 3] == 0)
node->evaluation_value += 4;
if (map[i][k] == 0 && map[i][k + 1] == 2 && map[i][k + 2] == 2 && map[i][k + 3] == 2)
node->evaluation_value += 4;
}
}
for (i = 0; i < 3; i++)
{
for (k = 0; k < 7; k++)
{
if (map[i][k] == 1 && map[i + 1][k] == 1 && map[i + 2][k] == 1 && map[i + 3][k] == 0)
node->evaluation_value -= 4;
if (map[i][k] == 0 && map[i + 1][k] == 1 && map[i + 2][k] == 1 && map[i + 3][k] == 1)
node->evaluation_value -= 4;
if (map[i][k] == 2 && map[i + 1][k] == 2 && map[i + 2][k] == 2 && map[i + 3][k] == 0)
node->evaluation_value += 4;
if (map[i][k] == 0 && map[i + 1][k] == 2 && map[i + 2][k] == 2 && map[i + 3][k] == 2)
node->evaluation_value += 4;
}
}
for (i = 0; i < 3; i++)
{
for (k = 0; k < 4; k++)
{
if (map[i][k] == 1 && map[i + 1][k + 1] == 1 && map[i + 2][k + 1] == 1 && map[i + 3][k + 3] == 0)
node->evaluation_value -= 4;
if (map[i][k] == 0 && map[i + 1][k + 1] == 1 && map[i + 2][k + 1] == 1 && map[i + 3][k + 3] == 1)
node->evaluation_value -= 4;
if (map[i][k] == 2 && map[i + 1][k + 1] == 2 && map[i + 2][k + 1] == 2 && map[i + 3][k + 3] == 0)
node->evaluation_value += 4;
if (map[i][k] == 0 && map[i + 1][k + 1] == 2 && map[i + 2][k + 1] == 2 && map[i + 3][k + 3] == 2)
node->evaluation_value += 4;
if (map[i + 3][k] == 1 && map[i + 2][k + 1] == 1 && map[i + 1][k + 2] == 1 && map[i][k + 3] == 0)
node->evaluation_value -= 4;
if (map[i + 3][k] == 0 && map[i + 2][k + 1] == 1 && map[i + 1][k + 2] == 1 && map[i][k + 3] == 1)
node->evaluation_value -= 4;
if (map[i + 3][k] == 2 && map[i + 2][k + 1] == 2 && map[i + 1][k + 2] == 2 && map[i][k + 3] == 0)
node->evaluation_value += 4;
if (map[i + 3][k] == 0 && map[i + 2][k + 1] == 2 && map[i + 1][k + 2] == 2 && map[i][k + 3] == 2)
node->evaluation_value += 4;
}
}
}
}
void min_max_function(struct tree_node *node)
{
if (node->min_or_max_or_leaf == LEAF&&node->is_in_tree == TRUE)
{
evaluation_function_version_1(node);
return;
}
if (node->is_in_tree == FALSE)
return;
min_max_function(node->child1);
min_max_function(node->child2);
min_max_function(node->child3);
min_max_function(node->child4);
min_max_function(node->child5);
min_max_function(node->child6);
min_max_function(node->child7);
struct tree_node *curr = node->child1;
if (node->min_or_max_or_leaf == MAX)
{
int i, max = -9999;
for (i = 0; i < 7; i++)
{
if (curr->is_in_tree == TRUE)
{
if (curr->evaluation_value > max)
max = curr->evaluation_value;
}
curr = curr->next_sibling;
}
node->evaluation_value = max;
return;
}
else if (node->min_or_max_or_leaf == MIN)
{
int i, min = 9999;
for (i = 0; i < 7; i++)
{
if (curr->is_in_tree == TRUE)
{
if (curr->evaluation_value < min)
min = curr->evaluation_value;
}
curr = curr->next_sibling;
}
node->evaluation_value = min;
return;
}
}
int pick_next_map(struct tree_node *root)
{
int i;
struct tree_node *curr = root->child1;
for (i = 0; i < 7; i++)
{
if (curr->is_in_tree == TRUE)
{
if (root->evaluation_value == curr->evaluation_value)
return i;
else
curr = curr->next_sibling;
}
else
curr = curr->next_sibling;
}
}
void select_map(int **map, int pick_next)
{
int i;
for (i = 0; i < 6; i++)
{
if (map[i][pick_next] == 0)
{
map[i][pick_next] = 2;
return;
}
}
}
void free_tree(struct tree_node *root)
{
int i;
struct tree_node *curr = root->child1;
struct tree_node *temp = NULL;
for (i = 0; i < 7; i++)
{
temp = curr->next_sibling;
mem_free_tree(curr);
curr = temp;
}
free(root);
}