-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolverab.cpp
More file actions
138 lines (107 loc) · 2.96 KB
/
solverab.cpp
File metadata and controls
138 lines (107 loc) · 2.96 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "solverab.h"
#include "time.h"
#include "alarm.h"
#include "log.h"
void SolverAB::solve(double time){
reset();
if(rootboard.won() >= 0){
outcome = rootboard.won();
return;
}
rootboard.setswap(false);
if(TT == NULL && maxnodes)
TT = new ABTTNode[maxnodes];
Alarm timer(time, std::tr1::bind(&SolverAB::timedout, this));
Time start;
int turn = rootboard.toplay();
for(maxdepth = startdepth; !timeout; maxdepth++){
// logerr("Starting depth " + to_str(maxdepth) + "\n");
//the first depth of negamax
int ret, alpha = -2, beta = 2;
for(Board::MoveIterator move = rootboard.moveit(true); !move.done(); ++move){
nodes_seen++;
Board next = rootboard;
next.move(*move, true, false);
int value = -negamax(next, maxdepth - 1, -beta, -alpha);
if(value > alpha){
alpha = value;
bestmove = *move;
}
if(alpha >= beta){
ret = beta;
break;
}
}
ret = alpha;
if(ret){
if( ret == -2){ outcome = (turn == 1 ? 2 : 1); bestmove = Move(M_NONE); }
else if(ret == 2){ outcome = turn; }
else /*-1 || 1*/ { outcome = 0; }
break;
}
}
time_used = Time() - start;
}
int SolverAB::negamax(const Board & board, const int depth, int alpha, int beta){
if(board.won() >= 0)
return (board.won() ? -2 : -1);
if(depth <= 0 || timeout)
return 0;
int b = beta;
int first = true;
int value, losses = 0;
static const int lookup[6] = {0, 0, 0, 1, 2, 2};
for(Board::MoveIterator move = board.moveit(true); !move.done(); ++move){
nodes_seen++;
hash_t hash = board.test_hash(*move);
if(int ttval = tt_get(hash)){
value = ttval;
}else if(depth <= 2){
value = lookup[board.test_win(*move)+3];
if(board.test_win(*move, 3 - board.toplay()) > 0)
losses++;
}else{
Board next = board;
next.move(*move, true, false);
value = -negamax(next, depth - 1, -b, -alpha);
if(scout && value > alpha && value < beta && !first) // re-search
value = -negamax(next, depth - 1, -beta, -alpha);
}
tt_set(hash, value);
if(value > alpha)
alpha = value;
if(alpha >= beta)
return beta;
if(scout){
b = alpha + 1; // set up null window
first = false;
}
}
if(losses >= 2)
return -2;
return alpha;
}
int SolverAB::negamax_outcome(const Board & board, const int depth){
int abval = negamax(board, depth, -2, 2);
if( abval == 0) return -3; //unknown
else if(abval == 2) return board.toplay(); //win
else if(abval == -2) return 3 - board.toplay(); //loss
else return 0; //draw
}
int SolverAB::tt_get(const Board & board){
return tt_get(board.gethash());
}
int SolverAB::tt_get(const hash_t & hash){
if(!TT) return 0;
ABTTNode * node = & TT[hash % maxnodes];
return (node->hash == hash ? node->value : 0);
}
void SolverAB::tt_set(const Board & board, int value){
tt_set(board.gethash(), value);
}
void SolverAB::tt_set(const hash_t & hash, int value){
if(!TT || value == 0) return;
ABTTNode * node = & TT[hash % maxnodes];
node->hash = hash;
node->value = value;
}