author | Markus Brökers <mbroeker@largo.homelinux.org> |
Sun, 29 Aug 2010 16:36:19 +0200 | |
changeset 145 | 63a5269fb113 |
parent 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"); |
|
97
f883331a1bf2
alpha_beta ui: removed a redundant line
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
96
diff
changeset
|
156 |
printf (" +---+---+---+\n"); |
0 | 157 |
} |
158 |
if (stack_end->data != NULL) |
|
159 |
printf ("Score: %3d\n", stack_end->data->value); |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* simulates and returns the next possible move for a Player or null, when no more moves are possible. |
|
164 |
*/ |
|
165 |
Move *simulate (int t, int player) |
|
166 |
{ |
|
167 |
Move *move; |
|
168 |
||
169 |
while (t < BOARD_SIZE) { |
|
170 |
if (!validMove (t)) { |
|
171 |
t++; |
|
172 |
continue; |
|
173 |
} |
|
174 |
||
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
175 |
if ((move = malloc (sizeof (Move))) == NULL) { |
0 | 176 |
perror ("MALLOC"); |
177 |
exit (EXIT_FAILURE); |
|
178 |
} |
|
179 |
||
180 |
if (player == 0) |
|
181 |
board[t] = 'O'; |
|
182 |
else |
|
183 |
board[t] = 'X'; |
|
184 |
||
185 |
move->target = t; |
|
186 |
move->value = estimateFunction (); |
|
187 |
||
188 |
push (move); |
|
189 |
||
190 |
return move; |
|
191 |
} |
|
192 |
return NULL; |
|
193 |
} |
|
194 |
||
195 |
/** |
|
196 |
* AlphaBeta-Algorithm: the Maximizer |
|
197 |
*/ |
|
198 |
int max_alpha_beta (int depth, int alpha, int beta) |
|
199 |
{ |
|
200 |
int moveValue; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
201 |
|
0 | 202 |
int t; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
203 |
|
0 | 204 |
Move *move; |
205 |
||
206 |
if (desired_depth == -1) |
|
207 |
desired_depth = depth; |
|
208 |
||
209 |
t = 0; |
|
210 |
while ((move = simulate (t, 0)) != NULL) { |
|
211 |
/* |
|
212 |
* particular move from black |
|
213 |
*/ |
|
214 |
t = move->target; |
|
215 |
||
216 |
if (depth == 0 || (estimateFunction () * estimateFunction ()) == 2500) |
|
217 |
moveValue = move->value; |
|
218 |
else /* best next move */ |
|
219 |
moveValue = min_alpha_beta (depth - 1, alpha, beta); |
|
220 |
||
221 |
print (); |
|
222 |
undo (); |
|
223 |
t++; |
|
224 |
||
96
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
225 |
if (moveValue >= beta) { |
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
226 |
free (move); |
0 | 227 |
return beta; |
96
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
228 |
} |
0 | 229 |
|
230 |
if (moveValue > alpha) { |
|
231 |
alpha = moveValue; |
|
96
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
232 |
if (depth == desired_depth) { |
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
233 |
if (bestMove != NULL) |
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
234 |
free (bestMove); |
0 | 235 |
bestMove = move; |
96
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
236 |
continue; |
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
237 |
} |
0 | 238 |
} |
96
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
239 |
free (move); |
0 | 240 |
} |
241 |
||
242 |
return alpha; |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* AlphaBeta-Algorithm: the Minimizer |
|
247 |
*/ |
|
248 |
int min_alpha_beta (int depth, int alpha, int beta) |
|
249 |
{ |
|
250 |
int moveValue; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
251 |
|
0 | 252 |
int t; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
253 |
|
0 | 254 |
Move *move; |
255 |
||
256 |
t = 0; |
|
257 |
while ((move = simulate (t, 1)) != NULL) { |
|
258 |
/* |
|
259 |
* particular move from white |
|
260 |
*/ |
|
261 |
t = move->target; |
|
262 |
||
263 |
if (depth == 0 || (estimateFunction () * estimateFunction ()) == 2500) |
|
264 |
moveValue = move->value; |
|
265 |
else /* best next move */ |
|
266 |
moveValue = max_alpha_beta (depth - 1, alpha, beta); |
|
267 |
||
268 |
undo (); |
|
96
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
269 |
free (move); |
0 | 270 |
t++; |
271 |
||
272 |
if (moveValue <= alpha) |
|
273 |
return alpha; |
|
274 |
||
275 |
if (moveValue < beta) |
|
276 |
beta = moveValue; |
|
277 |
} |
|
278 |
||
279 |
return beta; |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* returns true when no more fields are empty |
|
284 |
*/ |
|
285 |
int game_over () |
|
286 |
{ |
|
287 |
int i; |
|
288 |
||
289 |
for (i = 0; i < BOARD_SIZE; i++) { |
|
290 |
if (board[i] == EMPTY) |
|
291 |
return 0; |
|
292 |
} |
|
293 |
||
294 |
return 1; |
|
295 |
} |
|
296 |
||
297 |
int main (int argc, char **argv) |
|
298 |
{ |
|
299 |
Node *actual; |
|
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
300 |
|
0 | 301 |
int i, t, value; |
27
81a574d60c15
typo in min2time format string
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
9
diff
changeset
|
302 |
|
0 | 303 |
int depth; |
304 |
||
305 |
if (argc == 2) |
|
306 |
depth = atoi (argv[1]); |
|
307 |
else |
|
308 |
depth = 1; |
|
309 |
||
48
b94d657a9acb
Policy Inonsistency on many files
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
29
diff
changeset
|
310 |
if ((actual = malloc (sizeof (Node))) == NULL) { |
0 | 311 |
perror ("MALLOC"); |
312 |
return EXIT_FAILURE; |
|
313 |
} |
|
314 |
||
315 |
actual->data = NULL; |
|
316 |
actual->prev = NULL; |
|
317 |
||
318 |
stack_end = actual; |
|
319 |
||
320 |
printf ("\tTic Tac Toe\n"); |
|
321 |
||
322 |
for (i = 0; i < 5; i++) { |
|
323 |
printf ("Enter a number: "); |
|
324 |
while (scanf ("%d", &t) < 1) { |
|
325 |
printf ("Enter a number: "); |
|
326 |
t = getchar (); |
|
327 |
} |
|
328 |
||
329 |
if (!validMove (--t)) { |
|
330 |
i--; |
|
331 |
continue; |
|
332 |
} |
|
333 |
||
334 |
board[t] = 'X'; |
|
335 |
||
336 |
bestMove = NULL; |
|
337 |
value = max_alpha_beta (depth, SHRT_MIN, SHRT_MAX); |
|
338 |
||
339 |
if (bestMove == NULL) { |
|
340 |
break; |
|
341 |
} |
|
342 |
||
343 |
board[bestMove->target] = 'O'; |
|
96
810acedf60d8
alpha_beta: memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
87
diff
changeset
|
344 |
free (bestMove); |
0 | 345 |
print (); |
346 |
||
347 |
if ((estimateFunction () * estimateFunction ()) == 2500) { |
|
87
b2f1756c17ca
[alpha_beta] Fix memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
348 |
free (actual); |
0 | 349 |
return EXIT_SUCCESS; |
350 |
} |
|
351 |
||
352 |
if (game_over ()) |
|
353 |
break; |
|
354 |
} |
|
355 |
||
356 |
print (); |
|
87
b2f1756c17ca
[alpha_beta] Fix memory leaks
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
77
diff
changeset
|
357 |
free (actual); |
8
96d16dfe787a
We use return EXIT_SUCCESS instead of return 0
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
0
diff
changeset
|
358 |
|
0 | 359 |
return EXIT_SUCCESS; |
360 |
} |