From 70968ac39db0b338a648f936cb949a84032fe7c9 Mon Sep 17 00:00:00 2001 From: James Hochadel Date: Tue, 26 Jan 2021 18:49:56 -0500 Subject: [PATCH] Stockfish v stockfish and beginnings of minimax --- go.mod | 5 ++++ go.sum | 3 +++ main.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7038ea3 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/jameshochadel/chess-bot + +go 1.15 + +require github.com/notnil/chess v1.5.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3256ec6 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +github.com/ajstarks/svgo v0.0.0-20200320125537-f189e35d30ca/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= +github.com/notnil/chess v1.5.0 h1:BcdmSGqZYhoqHsAqNpVTtPwRMOA4Sj8iZY1ZuPW4Umg= +github.com/notnil/chess v1.5.0/go.mod h1:cRuJUIBFq9Xki05TWHJxHYkC+fFpq45IWwk94DdlCrA= diff --git a/main.go b/main.go new file mode 100644 index 0000000..4664a83 --- /dev/null +++ b/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "strings" + "time" + + "github.com/notnil/chess" + "github.com/notnil/chess/uci" +) + +func main() { + 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) + } + + game := chess.NewGame() + for game.Outcome() == chess.NoOutcome { + cmdPos := uci.CmdPosition{Position: game.Position()} + cmdGo := uci.CmdGo{MoveTime: time.Second / 100} + if err = eng.Run(cmdPos, cmdGo); err != nil { + panic(err) + } + move := eng.SearchResults().BestMove + if err := game.Move(move); err != nil { + panic(err) + } + } + + fmt.Println(game.String()) +} + +func minimax(pos chess.Position, depth int, maxPlayer bool) (bestMove chess.Move, posValue float32) { + /* + if depth == 0 or len(game.ValidMoves) == 0, + return evaluatePosition(pos) + if maxPlayer { + + } else { + + } + */ + return chess.Move{}, 0 +} + +var pieceVals = map[string]float32 { + "p": 1, + "n": 3, + "b": 3, + "r": 6, + "q": 9, +} + +func evaluatePosition(pos chess.Position) float32 { + str := pos.String() + + var acc float32 + + for piece, value := range pieceVals { + acc += float32(strings.Count(str, piece)) * value + } + strings.Count(str, "q") + // count number of each piece + return 0.0 +}