-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge_stockfish.go
67 lines (57 loc) · 1.49 KB
/
challenge_stockfish.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
package main
import (
"math"
"time"
"github.com/0hq/chess"
"github.com/0hq/chess/uci"
)
func challenge_stockfish(engine Engine, play_as_color chess.Color, game *chess.Game) {
max_moves := math.MaxInt
// game.UseNotation(global_AlgebraicNotation)
eng, err := uci.New("stockfish")
if err != nil {
panic(err)
}
defer eng.Close()
if err := eng.Run(uci.CmdUCI, uci.CmdIsReady, uci.CmdUCINewGame); err != nil {
panic(err)
}
out("Stockfish challenge started.")
out("Engine 1:", engine.Name(), "playing as", play_as_color)
out("Game:", game.String())
out(game.Position().Board().Draw())
out()
for game.Outcome() == chess.NoOutcome && len(game.Moves()) < max_moves {
var move *chess.Move
var eval int
if game.Position().Turn() == play_as_color {
move, eval = engine.Run_Engine_Game(game)
} else {
move, eval = stockfish(game, eng)
}
if move == nil {
panic("NO MOVE")
}
err := game.Move(move)
if err != nil {
out("ERROR:", err)
out("GAME:", game.String())
out("MOVE:", move)
out("FEN:", game.FEN())
panic(err)
}
out(game.Position().Turn(), move, eval)
out(game.FEN())
out(game.String())
out(game.Position().Board().Draw())
out()
}
}
func stockfish(game *chess.Game, eng *uci.Engine) (*chess.Move, int) {
cmdPos := uci.CmdPosition{Position: game.Position()}
cmdGo := uci.CmdGo{MoveTime: time.Second / 100}
if err := eng.Run(cmdPos, cmdGo); err != nil {
panic(err)
}
return eng.SearchResults().BestMove, eng.SearchResults().Info.Score.CP / 100
}