blunderboard/blunderboard.go

92 lines
2.3 KiB
Go
Raw Normal View History

2021-08-04 01:00:55 +00:00
package main
import (
"fmt"
"github.com/notnil/chess"
"github.com/notnil/chess/uci"
2021-08-04 22:05:23 +00:00
"math"
2021-08-28 20:34:07 +00:00
"os"
2021-08-04 01:00:55 +00:00
)
2021-08-04 22:05:23 +00:00
// stolen^H^H inspired from lichess https://github.com/ornicar/lila/blob/master/modules/analyse/src/main/Advice.scala#L79
func WinningChance(cp int) float64 {
winning_chance := 2 / (1 + math.Exp(-0.004 * float64(cp))) - 1
return winning_chance
}
2021-08-04 01:00:55 +00:00
func main() {
2021-08-28 20:34:07 +00:00
reader, err := os.Open("spongeboyahoy_vs_tomlx.pgn")
if err != nil {
panic(err)
}
pgn, err := chess.PGN(reader)
if err != nil {
panic(err)
}
spongeboyahoy_vs_tomlx := chess.NewGame(pgn)
fmt.Println(spongeboyahoy_vs_tomlx)
2021-08-04 01:00:55 +00:00
engine, err := uci.New("stockfish")
if err != nil {
panic(err)
}
defer engine.Close()
if err := engine.Run(uci.CmdUCI, uci.CmdIsReady, uci.CmdUCINewGame); err != nil {
panic(err)
}
game := chess.NewGame()
2021-08-28 20:51:21 +00:00
// prevprev_winning_chance := 0.0
2021-08-28 19:25:10 +00:00
prev_winning_chance := 0.0
2021-08-04 01:00:55 +00:00
for game.Outcome() == chess.NoOutcome {
2021-08-28 20:51:21 +00:00
num_of_moves := len(game.Moves())
2021-08-04 22:05:23 +00:00
if err := engine.Run(uci.CmdPosition{Position: game.Position()}, uci.CmdGo{Depth: 12}); err != nil {
2021-08-04 01:00:55 +00:00
panic(err)
}
2021-08-04 22:05:23 +00:00
search_results := engine.SearchResults()
cp := search_results.Info.Score.CP
2021-08-28 20:51:21 +00:00
if (num_of_moves % 2 == 1) {
cp *= -1
}
2021-08-04 22:05:23 +00:00
winning_chance := WinningChance(cp)
2021-08-28 19:25:10 +00:00
if (num_of_moves > 0) {
2021-08-28 20:51:21 +00:00
delta := prev_winning_chance - winning_chance
2021-08-28 19:25:10 +00:00
if (num_of_moves % 2 == 0) {
delta *= -1;
}
if delta > 0.3 {
fmt.Print("B-b-b-blunder!!")
} else if delta > 0.2 {
fmt.Print("That was a mistake.")
} else if delta > 0.1 {
fmt.Print("Meh...")
} else {
fmt.Print("Ok")
2021-08-04 22:05:23 +00:00
}
2021-08-28 20:34:07 +00:00
fmt.Printf(" (%0.2f, %0.2f, %0.2f)\n", float64(cp) / 100, winning_chance, -delta)
2021-08-28 19:25:10 +00:00
}
2021-08-28 20:51:21 +00:00
// prevprev_winning_chance = prev_winning_chance
2021-08-28 19:25:10 +00:00
prev_winning_chance = winning_chance
2021-08-28 20:34:07 +00:00
// fmt.Println(game.Position().Board().Draw())
// fmt.Println("Score (centipawns):", cp, "Winning chance:", winning_chance, "Best Move: ", search_results.BestMove)
// fmt.Println("Move: ", search_results.BestMove)
move := spongeboyahoy_vs_tomlx.Moves()[num_of_moves]
fmt.Print(num_of_moves / 2 + 1, move, "\t")
if err := game.Move(move); err != nil {
2021-08-28 19:25:10 +00:00
panic(err)
2021-08-04 22:05:23 +00:00
}
2021-08-28 19:25:10 +00:00
// for {
// var move string
// fmt.Print("Move: ")
// fmt.Scanln(&move)
// if err := game.MoveStr(move); err == nil {
// break
// }
// fmt.Println("Illegal move!")
// }
2021-08-04 01:00:55 +00:00
}
2021-08-04 22:05:23 +00:00
fmt.Println(game.Outcome())
2021-08-04 01:00:55 +00:00
fmt.Println(game.Position().Board().Draw())
}