-
Notifications
You must be signed in to change notification settings - Fork 1
/
opening.go
214 lines (183 loc) · 4.37 KB
/
opening.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
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"fmt"
"math"
"math/rand"
"strings"
"github.com/0hq/chess"
"github.com/0hq/chess/opening"
)
/*
Opening Book
This is old code, so it is not documented. Sorry.
*/
type OpeningWrapper struct {
engine Engine
book *opening.BookECO
game *chess.Game
opening bool
}
func NewOpeningWrapper(engine Engine, game *chess.Game) *OpeningWrapper {
return &OpeningWrapper{
engine: engine,
book: opening.NewBookECO(),
game: game,
opening: len(game.Moves()) == 0 && game.FEN() == chess.StartingPosition().String(),
}
}
// func (o OpeningWrapper) Set_Time(time int) {
// o.engine.Set_Time(time)
// }
func (o *OpeningWrapper) Set_Time(time float64) {
o.engine.Set_Time(time)
}
func (o *OpeningWrapper) Reset_Time() {
o.engine.Reset_Time()
}
func (o *OpeningWrapper) Reset(pos *chess.Position) {
o.engine.Reset(pos)
}
func (o OpeningWrapper) Run_Engine(position *chess.Position) (*chess.Move, int) {
return o.engine.Run_Engine(position)
}
func (o *OpeningWrapper) Run_Engine_Game_Shit(g *chess.Game) (*chess.Move, int) {
if o.opening {
possible := get_opening_uci_with_analysis(g, 0)
if possible == nil || len(possible) == 0 {
o.opening = false
return o.engine.Run_Engine(g.Position())
} else {
var chosen *chess.Move
var eval int = math.MinInt
out(possible)
for i := 0; i < len(possible); i++ {
p := possible[i]
move, err := global_UCINotation.Decode(g.Position(), p)
if err != nil {
panic(err)
}
out("Looking at", move)
clone_game := *g
clone_game.Move(move)
clone_engine := new_engine(o.engine, 0.5, &clone_game)
_, score := clone_engine.Run_Engine(clone_game.Position())
if score > eval {
chosen = move
eval = score
}
}
if chosen == nil {
panic("Nil opening move.")
}
return chosen, 0
}
}
return o.engine.Run_Engine(g.Position())
}
func (o *OpeningWrapper) Run_Engine_Game(g *chess.Game) (*chess.Move, int) {
if o.opening {
uci := get_opening_uci(g, 0)
// out(get_opening_uci_with_analysis(g, 0))
if uci == "" {
o.opening = false
return o.engine.Run_Engine(g.Position())
} else {
move, _ := global_UCINotation.Decode(g.Position(), uci)
return move, 0
}
}
return o.engine.Run_Engine(g.Position())
}
func (o OpeningWrapper) Name() string {
return o.engine.Name()
}
// Set_Config
func (o *OpeningWrapper) Set_Config(config EngineConfig) {
o.engine.Set_Config(config)
}
// Check_Time_Up
func (o OpeningWrapper) Check_Time_Up() bool {
return o.engine.Check_Time_Up()
}
func test_opening(){
g := chess.NewGame(chess.UseNotation(chess.UCINotation{}))
// g.MoveStr("e2e4")
// g.MoveStr("e6")
opening := true
for opening {
move := get_opening_uci(g, 0)
if move == "" {
opening = false
}
tmove, _ := global_UCINotation.Decode(g.Position(), move)
g.Move(tmove)
fmt.Println(g.Position().Board().Draw())
}
}
func get_opening_uci(g *chess.Game, retries int) string {
// out("Get opening uci.")
book := opening.NewBookECO()
moves := g.Moves()
if len(moves) == 0 {
return "e2e4"
}
o := book.Find(moves) // find current opening
if o == nil {
return ""
}
p := book.Possible(g.Moves()) // all openings available
if len(p) > 0 {
// out("Possible openings", len(p))
r := p[rand.Intn(len(p))] // random opening available
split := strings.Split(r.PGN(), " ")
if len(split) <= len(g.Moves()) {
if len(p) == 1 || retries > 3 {
return ""
}
return get_opening_uci(g, retries + 1)
}
m := split[len(g.Moves())]
return m
}
return ""
}
func get_opening_uci_with_analysis(g *chess.Game, retries int) []string {
book := opening.NewBookECO()
moves := g.Moves()
if len(moves) == 0 {
return []string{"e2e4"}
}
o := book.Find(moves) // find current opening
if o == nil {
return nil
}
p := book.Possible(g.Moves()) // all openings available
if len(p) > 0 {
valid := make([]string, 0)
for i := 0; i < len(p); i++ {
r := p[i] // random opening available
// out("Looking at", r, i)
split := strings.Split(r.PGN(), " ")
if len(split) <= len(g.Moves()) {
continue
// if len(p) == 1 || retries > 3 {
// return nil
// }
// return get_opening_uci_with_analysis(g, retries + 1)
}
m := split[len(g.Moves())]
fail := false
for i := 0; i < len(valid); i++ {
if m == valid[i] {
fail = true
}
}
if fail {
continue
}
valid = append(valid, m)
}
return valid
}
return nil
}