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