-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
202 lines (157 loc) · 3.27 KB
/
search.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"math"
"time"
)
var divideDepth int = 0
var pv = []Move{}
var pvScore = []int{}
var pvMove Move = Move{}
func perft(pos Position, depth int, divide bool) uint64 {
if depth == 0 {
return 1
}
moves, nrMoves := generateMoves(pos, false)
var nrNodes uint64 = 0
for i := 0; i < nrMoves; i++ {
move := moves[i]
if !pos.isMoveLegal(move, true) {
continue
}
nodes := perft(pos, depth-1, divide)
pos.unmakeMove(move)
if divide && depth == divideDepth {
fmt.Printf("Move: %s\tNodes: %d\n", move.toStr(), nodes)
}
nrNodes += nodes
}
return nrNodes
}
func divide(pos Position, depth int) uint64 {
divideDepth = depth
return perft(pos, depth, true)
}
func searchGo(pos Position) {
pv = make([]Move, 100)
pvScore = make([]int, 100)
pvMove = Move{}
for i := 0; i < 100; i++ {
pvScore[i] = -32000
}
maxThinkTime := 5.0
if pos.fullMoveCounter >= 50 {
maxThinkTime = 90.0
} else if pos.fullMoveCounter >= 40 {
maxThinkTime = 75.0
} else if pos.fullMoveCounter >= 30 {
maxThinkTime = 60.0
} else if pos.fullMoveCounter >= 20 {
maxThinkTime = 45.0
} else if pos.fullMoveCounter >= 10 {
maxThinkTime = 30.0
} else if pos.fullMoveCounter >= 5 {
maxThinkTime = 15.0
}
start := time.Now()
latestDepth := 1
for depth := 1; depth < 100; depth++ {
latestDepth = depth
before := time.Now()
search(&pos, depth, -32000, 32000)
fullDuration := time.Since(start).Seconds()
thisDuration := time.Since(before).Seconds()
if fullDuration > maxThinkTime || fullDuration+(thisDuration*4) > maxThinkTime {
break
}
}
/*
fmt.Printf("info depth %d pv ", depth)
for i := depth - 1; i >= 0; i-- {
fmt.Printf("%s ", pv[i].toStr())
}*/
fmt.Println("Used depth", latestDepth)
pvMove = pv[latestDepth-1]
if !TESTING {
fmt.Printf("bestmove %s\n", pvMove.toStr())
}
}
func search(pos *Position, depth int, alpha int, beta int) int {
if depth == 0 {
return quiesce(pos, alpha, beta)
//return pos.evaluate()
}
// 50 move rule
if pos.state.rule50 >= 100 {
return 0
}
bestScore := -32000
mp := Movepick{}
mp.generateMoves(*pos, false)
nrLegalMoves := 0
isInCheck := pos.isInCheck()
for {
move := mp.getNextMove()
if move.move == 0 {
break
}
if !pos.isMoveLegal(move, true) {
continue
}
nrLegalMoves++
score := -search(pos, depth-1, -beta, -alpha)
pos.unmakeMove(move)
if score > pvScore[depth-1] {
pvScore[depth-1] = score
pv[depth-1] = move
}
if score >= beta {
return score
}
if score > bestScore {
bestScore = score
if score > alpha {
alpha = score
}
}
}
if nrLegalMoves == 0 {
// Checkmate
if isInCheck {
// Award checkmates that lead to it quicker
return -30000 - int(math.Abs(float64(1-depth)))
} else { // Stalemate
return 0
}
}
return bestScore
}
func quiesce(pos *Position, alpha int, beta int) int {
eval := pos.evaluate()
if eval >= beta {
return beta
}
if eval > alpha {
alpha = eval
}
mp := Movepick{}
mp.generateMoves(*pos, true)
for {
move := mp.getNextMove()
if move.move == 0 {
break
}
if !pos.isMoveLegal(move, true) {
continue
}
score := -quiesce(pos, -beta, -alpha)
pos.unmakeMove(move)
if score >= beta {
return beta
}
if score > alpha {
alpha = score
}
}
return alpha
}