stockfish for godot
fix bugs
bendn 2022-09-14
parent 25e4303 · commit 972f4cd
-rw-r--r--Main.gd8
-rw-r--r--README.md8
-rw-r--r--addons/stockfish.gd/README.md8
-rw-r--r--addons/stockfish.gd/chess.gd4
-rw-r--r--addons/stockfish.gd/package.json2
-rw-r--r--addons/stockfish.gd/stockfish_loader.gd24
-rw-r--r--project.godot2
7 files changed, 39 insertions, 17 deletions
diff --git a/Main.gd b/Main.gd
index 2c764d5..bf64f0e 100644
--- a/Main.gd
+++ b/Main.gd
@@ -7,5 +7,9 @@ func _ready() -> void:
var loader := StockfishLoader.new()
fish = loader.load_stockfish()
fish.game = Chess.new()
- print("GO FISH")
- fish.go()
+ while not fish.game.game_over():
+ fish.go(5)
+ var bestmove = yield(fish, "bestmove")
+ prints("bestmove", "is", fish.game.move(bestmove).san)
+ fish._position()
+ print(fish.game.pgn(), "\n", fish.game.fen())
diff --git a/README.md b/README.md
index ea73f60..1c1ab37 100644
--- a/README.md
+++ b/README.md
@@ -16,6 +16,10 @@ func _ready() -> void:
var loader := StockfishLoader.new()
fish = loader.load_stockfish()
fish.game = Chess.new()
- print("GO FISH")
- fish.go()
+ while not fish.game.game_over():
+ fish.go(5)
+ var bestmove = yield(fish, "bestmove")
+ prints("bestmove", "is", fish.game.move(bestmove).san)
+ fish._position()
+ print(fish.game.pgn(), "\n", fish.game.fen())
```
diff --git a/addons/stockfish.gd/README.md b/addons/stockfish.gd/README.md
index d79e036..2d02656 100644
--- a/addons/stockfish.gd/README.md
+++ b/addons/stockfish.gd/README.md
@@ -12,6 +12,10 @@ func _ready() -> void:
var loader := StockfishLoader.new()
fish = loader.load_stockfish()
fish.game = Chess.new()
- print("GO FISH")
- fish.go()
+ while not fish.game.game_over():
+ fish.go(5)
+ var bestmove = yield(fish, "bestmove")
+ prints("bestmove", "is", fish.game.move(bestmove).san)
+ fish._position()
+ print(fish.game.pgn(), "\n", fish.game.fen())
```
diff --git a/addons/stockfish.gd/chess.gd b/addons/stockfish.gd/chess.gd
index b84b736..5316feb 100644
--- a/addons/stockfish.gd/chess.gd
+++ b/addons/stockfish.gd/chess.gd
@@ -1,4 +1,4 @@
-extends Resource
+extends Reference
class_name Chess
# ported from https://github.com/jhlywa/chess.js
const SYMBOLS := "pnbrqkPNBRQK"
@@ -1231,7 +1231,7 @@ func move(move, sloppy := false) -> Dictionary:
static func move_to_uci(move: Dictionary) -> String:
- if move.promotion:
+ if move.get("promotion", false):
return algebraic(move.from) + algebraic(move.to) + move.promotion
else:
return algebraic(move.from) + algebraic(move.to)
diff --git a/addons/stockfish.gd/package.json b/addons/stockfish.gd/package.json
index 7f99539..84b4a0f 100644
--- a/addons/stockfish.gd/package.json
+++ b/addons/stockfish.gd/package.json
@@ -1,6 +1,6 @@
{
"name": "@bendn/stockfish.gd",
- "version": "1.1.6",
+ "version": "1.2.0",
"description": "godot stockfish",
"main": "stockfish_loader.gd",
"scripts": {
diff --git a/addons/stockfish.gd/stockfish_loader.gd b/addons/stockfish.gd/stockfish_loader.gd
index 0aba291..0961535 100644
--- a/addons/stockfish.gd/stockfish_loader.gd
+++ b/addons/stockfish.gd/stockfish_loader.gd
@@ -29,6 +29,7 @@ class Stockfish:
var sent_isready := false
var engine_ready := false
var call_queue := PoolStringArray()
+ var searching_bestmove := false
signal engine_ready
signal line_recieved
@@ -39,7 +40,7 @@ class Stockfish:
if not engine_ready:
call_queue.append(cmd)
return
- print("%s --> stockfish" % cmd)
+ dbg_prints("%s --> stockfish" % cmd)
_send_line(cmd)
# @override
@@ -50,6 +51,10 @@ class Stockfish:
connect("line_recieved", self, "_line_recieved")
connect("engine_ready", self, "_engine_ready")
+ func dbg_prints(a1 := "", a2 := "") -> void:
+ if OS.is_debug_build():
+ prints(a1, a2)
+
func _engine_ready() -> void:
engine_ready = true
for call in call_queue:
@@ -63,18 +68,18 @@ class Stockfish:
func _position():
var command := PoolStringArray(["position", "startpos"])
-
if game.__history:
command.append("moves")
- for move in game.__history:
- command.append(Chess.move_to_uci(move))
+ for history in game.__history:
+ command.append(Chess.move_to_uci(history.move))
send_line(command.join(" "))
func _line_recieved(line: String) -> void:
if line.begins_with("info "):
- prints("(stockfish)", line)
- elif line.begins_with("bestmove "):
+ dbg_prints("(stockfish)", line)
+ elif searching_bestmove and line.begins_with("bestmove "):
+ searching_bestmove = false
parse_bestmove(line.split(" ", true, 1)[1])
elif (sent_isready) && (line == "readyok" || line.begins_with("Stockfish [commit: ")):
sent_isready = false
@@ -88,9 +93,14 @@ class Stockfish:
if game.move(tokens[0]):
var bm = game.undo()
emit_signal("bestmove", bm)
+ return
emit_signal("bestmove", null)
- func go(depth: int = 15):
+ func go(depth: int = 15) -> void:
+ if searching_bestmove:
+ push_error("already searching. did you mean `stop()`?")
+ return
+ searching_bestmove = true
var command := PoolStringArray(["go"])
command.append("depth")
command.append(str(depth))
diff --git a/project.godot b/project.godot
index 9510d9c..4f3a864 100644
--- a/project.godot
+++ b/project.godot
@@ -9,7 +9,7 @@
config_version=4
_global_script_classes=[ {
-"base": "Resource",
+"base": "Reference",
"class": "Chess",
"language": "GDScript",
"path": "res://addons/stockfish.gd/chess.gd"