author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Thu, 28 May 2009 16:51:26 +0200 | |
changeset 95 | d2a071bd1a60 |
parent 87 | b2f1756c17ca |
child 96 | 810acedf60d8 |
permissions | -rw-r--r-- |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
1 |
/** |
77 | 2 |
* alpha_beta.c |
9
c3fecc82ade6
standard tags for git projects
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
8
diff
changeset
|
3 |
* Copyright (C) 2008 Markus Broeker |
0 | 4 |
*/ |
5 |
||
6 |
#include <stdio.h> |
|
7 |
#include <stdlib.h> |
|
8 |
#include <limits.h> |
|
9 |
||
10 |
#define BOARD_SIZE 9 |
|
11 |
#define EMPTY 32 |
|
12 |
||
13 |
struct Move { |
|
14 |
int target; |
|
15 |
int value; |
|
16 |
}; |
|
17 |
||
18 |
typedef struct Move Move; |
|
19 |
||
20 |
struct Node { |
|
21 |
Move *data; |
|
22 |
struct Node *prev; |
|
23 |
}; |
|
24 |
||
25 |
typedef struct Node Node; |
|
26 |
||
27 |
Move *bestMove; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
28 |
|
0 | 29 |
Node *stack_end; |
30 |
||
31 |
char board[BOARD_SIZE] = { |
|
32 |
EMPTY, EMPTY, EMPTY, |
|
33 |
EMPTY, EMPTY, EMPTY, |
|
34 |
EMPTY, EMPTY, EMPTY, |
|
35 |
}; |
|
36 |
||
37 |
int desired_depth = -1; |
|
38 |
||
39 |
int estimateFunction (); |
|
40 |
int max_alpha_beta (int, int, int); |
|
41 |
int min_alpha_beta (int, int, int); |
|
42 |
void print (void); |
|
43 |
||
44 |
/** |
|
45 |
* push: push a move onto the stack |
|
46 |
*/ |
|
47 |
void push (Move * move) |
|
48 |
{ |
|
49 |
Node *actual; |
|
50 |
||
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
51 |
if ((actual = malloc (sizeof (Node))) == NULL) { |
0 | 52 |
perror ("MALLOC"); |
53 |
exit (EXIT_FAILURE); |
|
54 |
} |
|
55 |
||
56 |
actual->data = move; |
|
57 |
actual->prev = stack_end; |
|
58 |
||
59 |
stack_end = actual; |
|
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
* undo: revert the last move on the stack |
|
64 |
*/ |
|
65 |
void undo () |
|
66 |
{ |
|
67 |
Node *actual; |
|
68 |
||
69 |
if (stack_end == NULL) { |
|
70 |
printf ("Stack underrun\n"); |
|
71 |
return; |
|
72 |
} |
|
73 |
||
74 |
board[stack_end->data->target] = EMPTY; |
|
75 |
||
76 |
actual = stack_end->prev; |
|
87
b2f1756c17ca
[alpha_beta] Fix memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
77 |
free (stack_end->data); |
0 | 78 |
free (stack_end); |
79 |
||
80 |
stack_end = actual; |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* validMove returns true(1) when the inspected field is EMPTY |
|
85 |
*/ |
|
86 |
int validMove (int t) |
|
87 |
{ |
|
88 |
if (board[t] == EMPTY) |
|
89 |
return 1; |
|
90 |
return 0; |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Returns a Score of +-50 for 3 in a Row |
|
95 |
*/ |
|
96 |
int estimateFunction () |
|
97 |
{ |
|
98 |
if (board[0] == 'X' && board[1] == 'X' && board[2] == 'X') |
|
99 |
return -50; |
|
100 |
if (board[0] == 'O' && board[1] == 'O' && board[2] == 'O') |
|
101 |
return 50; |
|
102 |
||
103 |
if (board[3] == 'X' && board[4] == 'X' && board[5] == 'X') |
|
104 |
return -50; |
|
105 |
if (board[3] == 'O' && board[4] == 'O' && board[5] == 'O') |
|
106 |
return 50; |
|
107 |
||
108 |
if (board[6] == 'X' && board[7] == 'X' && board[8] == 'X') |
|
109 |
return -50; |
|
110 |
if (board[6] == 'O' && board[7] == 'O' && board[8] == 'O') |
|
111 |
return 50; |
|
112 |
||
113 |
if (board[0] == 'X' && board[3] == 'X' && board[6] == 'X') |
|
114 |
return -50; |
|
115 |
if (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') |
|
116 |
return 50; |
|
117 |
||
118 |
if (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') |
|
119 |
return -50; |
|
120 |
if (board[1] == 'O' && board[4] == 'O' && board[7] == 'O') |
|
121 |
return 50; |
|
122 |
||
123 |
if (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') |
|
124 |
return -50; |
|
125 |
if (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') |
|
126 |
return 50; |
|
127 |
||
128 |
if (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') |
|
129 |
return -50; |
|
130 |
if (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') |
|
131 |
return 50; |
|
132 |
||
133 |
if (board[2] == 'X' && board[4] == 'X' && board[6] == 'X') |
|
134 |
return -50; |
|
135 |
if (board[2] == 'O' && board[4] == 'O' && board[6] == 'O') |
|
136 |
return 50; |
|
137 |
||
138 |
return 0; |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* this function visualizes the board |
|
143 |
*/ |
|
144 |
void print () |
|
145 |
{ |
|
146 |
int i; |
|
147 |
||
148 |
printf (" 1 2 3 \n +---+---+---+\n"); |
|
149 |
for (i = 0; i < 3; i++) { |
|
150 |
printf (" %d | ", i + 1); |
|
151 |
printf ("%c", board[3 * i]); |
|
152 |
printf (" | "); |
|
153 |
printf ("%c", board[3 * i + 1]); |
|
154 |
printf (" | "); |
|
155 |
printf ("%c", board[3 * i + 2]); |
|
156 |
printf (" | \n"); |
|
157 |
if (i != 2) { |
|
158 |
printf (" +---+---+---+\n"); |
|
159 |
} else { |
|
160 |
printf (" +---+---+---+\n"); |
|
161 |
} |
|
162 |
} |
|
163 |
if (stack_end->data != NULL) |
|
164 |
printf ("Score: %3d\n", stack_end->data->value); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* simulates and returns the next possible move for a Player or null, when no more moves are possible. |
|
169 |
*/ |
|
170 |
Move *simulate (int t, int player) |
|
171 |
{ |
|
172 |
Move *move; |
|
173 |
||
174 |
while (t < BOARD_SIZE) { |
|
175 |
if (!validMove (t)) { |
|
176 |
t++; |
|
177 |
continue; |
|
178 |
} |
|
179 |
||
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
180 |
if ((move = malloc (sizeof (Move))) == NULL) { |
0 | 181 |
perror ("MALLOC"); |
182 |
exit (EXIT_FAILURE); |
|
183 |
} |
|
184 |
||
185 |
if (player == 0) |
|
186 |
board[t] = 'O'; |
|
187 |
else |
|
188 |
board[t] = 'X'; |
|
189 |
||
190 |
move->target = t; |
|
191 |
move->value = estimateFunction (); |
|
192 |
||
193 |
push (move); |
|
194 |
||
195 |
return move; |
|
196 |
} |
|
197 |
return NULL; |
|
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* AlphaBeta-Algorithm: the Maximizer |
|
202 |
*/ |
|
203 |
int max_alpha_beta (int depth, int alpha, int beta) |
|
204 |
{ |
|
205 |
int moveValue; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
206 |
|
0 | 207 |
int t; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
208 |
|
0 | 209 |
Move *move; |
210 |
||
211 |
if (desired_depth == -1) |
|
212 |
desired_depth = depth; |
|
213 |
||
214 |
t = 0; |
|
215 |
while ((move = simulate (t, 0)) != NULL) { |
|
216 |
/* |
|
217 |
* particular move from black |
|
218 |
*/ |
|
219 |
t = move->target; |
|
220 |
||
221 |
if (depth == 0 || (estimateFunction () * estimateFunction ()) == 2500) |
|
222 |
moveValue = move->value; |
|
223 |
else /* best next move */ |
|
224 |
moveValue = min_alpha_beta (depth - 1, alpha, beta); |
|
225 |
||
226 |
print (); |
|
227 |
undo (); |
|
228 |
t++; |
|
229 |
||
230 |
if (moveValue >= beta) |
|
231 |
return beta; |
|
232 |
||
233 |
if (moveValue > alpha) { |
|
234 |
alpha = moveValue; |
|
235 |
if (depth == desired_depth) |
|
236 |
bestMove = move; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
return alpha; |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* AlphaBeta-Algorithm: the Minimizer |
|
245 |
*/ |
|
246 |
int min_alpha_beta (int depth, int alpha, int beta) |
|
247 |
{ |
|
248 |
int moveValue; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
249 |
|
0 | 250 |
int t; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
251 |
|
0 | 252 |
Move *move; |
253 |
||
254 |
t = 0; |
|
255 |
while ((move = simulate (t, 1)) != NULL) { |
|
256 |
/* |
|
257 |
* particular move from white |
|
258 |
*/ |
|
259 |
t = move->target; |
|
260 |
||
261 |
if (depth == 0 || (estimateFunction () * estimateFunction ()) == 2500) |
|
262 |
moveValue = move->value; |
|
263 |
else /* best next move */ |
|
264 |
moveValue = max_alpha_beta (depth - 1, alpha, beta); |
|
265 |
||
266 |
undo (); |
|
267 |
t++; |
|
268 |
||
269 |
if (moveValue <= alpha) |
|
270 |
return alpha; |
|
271 |
||
272 |
if (moveValue < beta) |
|
273 |
beta = moveValue; |
|
274 |
} |
|
275 |
||
276 |
return beta; |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* returns true when no more fields are empty |
|
281 |
*/ |
|
282 |
int game_over () |
|
283 |
{ |
|
284 |
int i; |
|
285 |
||
286 |
for (i = 0; i < BOARD_SIZE; i++) { |
|
287 |
if (board[i] == EMPTY) |
|
288 |
return 0; |
|
289 |
} |
|
290 |
||
291 |
return 1; |
|
292 |
} |
|
293 |
||
294 |
int main (int argc, char **argv) |
|
295 |
{ |
|
296 |
Node *actual; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
297 |
|
0 | 298 |
int i, t, value; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
299 |
|
0 | 300 |
int depth; |
301 |
||
302 |
if (argc == 2) |
|
303 |
depth = atoi (argv[1]); |
|
304 |
else |
|
305 |
depth = 1; |
|
306 |
||
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
307 |
if ((actual = malloc (sizeof (Node))) == NULL) { |
0 | 308 |
perror ("MALLOC"); |
309 |
return EXIT_FAILURE; |
|
310 |
} |
|
311 |
||
312 |
actual->data = NULL; |
|
313 |
actual->prev = NULL; |
|
314 |
||
315 |
stack_end = actual; |
|
316 |
||
317 |
printf ("\tTic Tac Toe\n"); |
|
318 |
||
319 |
for (i = 0; i < 5; i++) { |
|
320 |
printf ("Enter a number: "); |
|
321 |
while (scanf ("%d", &t) < 1) { |
|
322 |
printf ("Enter a number: "); |
|
323 |
t = getchar (); |
|
324 |
} |
|
325 |
||
326 |
if (!validMove (--t)) { |
|
327 |
i--; |
|
328 |
continue; |
|
329 |
} |
|
330 |
||
331 |
board[t] = 'X'; |
|
332 |
||
333 |
bestMove = NULL; |
|
334 |
value = max_alpha_beta (depth, SHRT_MIN, SHRT_MAX); |
|
335 |
||
336 |
if (bestMove == NULL) { |
|
337 |
break; |
|
338 |
} |
|
339 |
||
340 |
board[bestMove->target] = 'O'; |
|
341 |
print (); |
|
342 |
||
343 |
if ((estimateFunction () * estimateFunction ()) == 2500) { |
|
87
b2f1756c17ca
[alpha_beta] Fix memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
344 |
free (actual); |
0 | 345 |
return EXIT_SUCCESS; |
346 |
} |
|
347 |
||
348 |
if (game_over ()) |
|
349 |
break; |
|
350 |
} |
|
351 |
||
352 |
print (); |
|
87
b2f1756c17ca
[alpha_beta] Fix memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
353 |
free (actual); |
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
354 |
|
0 | 355 |
return EXIT_SUCCESS; |
356 |
} |