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