-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.c
57 lines (46 loc) · 1.07 KB
/
solver.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
#include <stdlib.h>
#include "position.h"
#include "solver.h"
int negamax(position *pos, int alpha, int beta, entry **table) {
if (num_moves(pos) == WIDTH * HEIGHT) {
return 0;
}
int *order = generate_column_order(WIDTH);
int i;
for (i = 0; i < WIDTH; i++) {
if (playable(*(order + i), pos) && check_will_win(*(order + i), pos)) {
return (WIDTH + HEIGHT+1 - num_moves(pos)) / 2;
}
}
int max_score = (WIDTH * HEIGHT - 1 - num_moves(pos)) / 2;
int val = transtable_get(pos->key, *table);
if (val) {
max_score = val + MIN;
}
if (beta > max_score) {
beta = max_score;
if (alpha >= beta) {
return beta;
}
}
for (i = 0; i < WIDTH; i++) {
if (playable(*(order + i), pos)) {
position *t = malloc(sizeof(position));
t->pos = pos->pos;
t->mask = pos->mask;
t->height = HEIGHT;
t->width = WIDTH;
play(*(order + i), &t);
int score = -negamax(t, -beta, -alpha);
if (score >= beta) {
return score;
}
if (score > alpha) {
alpha = score;
}
free(t);
}
}
transtable_put(pos->key, alpha - MIN + 1, table);
return alpha;
}