-
Notifications
You must be signed in to change notification settings - Fork 1
/
MTD.js
179 lines (131 loc) · 3.49 KB
/
MTD.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
MTD(f)
- really fast minimax
Thanks to:
http://en.wikipedia.org/wiki/MTD-f
http://people.csail.mit.edu/plaat/mtdf.html
http://www.chessbin.com/post/transposition-table-and-zobrist-hashing.aspx
*/
// not the best transposition table...
var transpositionTable = [];
var nodeType = {};
// MTD(root, 0, depth)
function MTD(root:node_type, f:integer, d:integer):integer;
{
var g = f;
var upperbound = +Infinity;
var lowerbound = -Infinity;
var beta;
do {
if (g == lowerbound){
beta = g + 1;
} else {
beta = g;
}
g = AlphaBetaWithMemory(root, beta - 1, beta, d);
if (g < beta){
upperbound = g
} else {
lowerbound = g;
}
} while(lowerbound >= upperbound);
return g;
}
function AlphaBetaWithMemory(n:node_type, alpha, beta, d:integer):integer;
{
/* Transposition table lookup */
if (retrieve(n) !== false){
if (n.lowerbound >= beta){
return n.lowerbound;
}
if (n.upperbound <= alpha){
return n.upperbound;
}
alpha = Math.max(alpha, n.lowerbound);
beta = Math.min(beta, n.upperbound);
}
if (d == 0){
// leaf node
g = evaluate(n);
} else if (n.type == MAXNODE){
// save original alpha value
g = -Infinity;
a = alpha;
c = firstchild(n);
while (g < beta && c != NOCHILD){
g = Math.max(g, AlphaBetaWithMemory(c, a, beta, d - 1));
a = Math.max(a, g);
c = nextbrother(c);
}
} else {
// n is a MINNODE
g = +Infinity;
b = beta; // save original beta value
c = firstchild(n);
while (g > alpha && c != NOCHILD){
g = Math.min(g, AlphaBetaWithMemory(c, alpha, b, d - 1));
b = Math.min(b, g);
c = nextbrother(c);
}
}
// Traditional transposition table storing of bounds
// Fail low result implies an upper bound
if (g <= alpha){
n.upperbound = g;
//store n.upperbound;
store(n);
}
/* Found an accurate minimax value - will not occur if called with zero window */
if (g > alpha && g < beta){
n.lowerbound = g;
n.upperbound = g;
//store n.lowerbound
//store n.upperbound;
store(n);
}
/* Fail high result implies a lower bound */
if (g >= beta){
n.lowerbound = g;
//store n.lowerbound;
store(n);
}
return g;
}
function retrieve(node)
{
var hash = hashNode(node);
if(transpositionTable[node.hash] == undefined){
return false;
}
return transpositionTable[node.hash];
}
function store(node)
{
transpositionTable[node.hash] = node;
}
function evaluate(node)
{
return node.score;
}
function createNode()
{
var node = new Object();
node.hash = generateUUID();
node.children = [];
node.lowerbound = -Infinity;
node.upperbound = +Infinity;
node.score = 0;
node.depth = 0;
node.type =
return node;
}
function generateUUID()
{
var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};