Epose variant capturesToHand to js/python (#522)
authorBajusz Tamás <gbtami@users.noreply.github.com>
Sat, 1 Oct 2022 10:51:22 +0000 (12:51 +0200)
committerGitHub <noreply@github.com>
Sat, 1 Oct 2022 10:51:22 +0000 (12:51 +0200)
setup.py
src/ffishjs.cpp
src/pyffish.cpp
test.py
tests/js/test.js

index c3e37c6..9bb6812 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -39,7 +39,7 @@ pyffish_module = Extension(
     sources=sources,
     extra_compile_args=args)
 
-setup(name="pyffish", version="0.0.73",
+setup(name="pyffish", version="0.0.74",
       description="Fairy-Stockfish Python wrapper",
       long_description=long_description,
       long_description_content_type="text/markdown",
index 3e7eb2b..bf1f342 100644 (file)
@@ -469,6 +469,11 @@ namespace ffish {
     Board::sfInitialized = true;
   }
 
+  bool captures_to_hand(std::string uciVariant) {
+    const Variant* v = get_variant(uciVariant);
+    return v->capturesToHand;
+  }
+
   std::string starting_fen(std::string uciVariant) {
     const Variant* v = get_variant(uciVariant);
     return v->startFen;
@@ -732,6 +737,7 @@ EMSCRIPTEN_BINDINGS(ffish_js) {
   function("readGamePGN", &read_game_pgn);
   function("variants", &ffish::available_variants);
   function("loadVariantConfig", &ffish::load_variant_config);
+  function("capturesToHand", &ffish::captures_to_hand);
   function("startingFen", &ffish::starting_fen);
   function("validateFen", select_overload<int(std::string)>(&ffish::validate_fen));
   function("validateFen", select_overload<int(std::string, std::string)>(&ffish::validate_fen));
index 89fb7b1..c0aa478 100644 (file)
@@ -54,7 +54,7 @@ void buildPosition(Position& pos, StateListPtr& states, const char *variant, con
 }
 
 extern "C" PyObject* pyffish_version(PyObject* self) {
-    return Py_BuildValue("(iii)", 0, 0, 73);
+    return Py_BuildValue("(iii)", 0, 0, 74);
 }
 
 extern "C" PyObject* pyffish_info(PyObject* self) {
@@ -129,6 +129,17 @@ extern "C" PyObject* pyffish_twoBoards(PyObject* self, PyObject *args) {
     return Py_BuildValue("O", variants.find(std::string(variant))->second->twoBoards ? Py_True : Py_False);
 }
 
+// INPUT variant
+extern "C" PyObject* pyffish_capturesToHand(PyObject* self, PyObject *args) {
+    const char *variant;
+
+    if (!PyArg_ParseTuple(args, "s", &variant)) {
+        return NULL;
+    }
+
+    return Py_BuildValue("O", variants.find(std::string(variant))->second->capturesToHand ? Py_True : Py_False);
+}
+
 // INPUT variant, fen, move
 extern "C" PyObject* pyffish_getSAN(PyObject* self, PyObject *args) {
     PyObject* moveList = PyList_New(0);
@@ -349,6 +360,7 @@ static PyMethodDef PyFFishMethods[] = {
     {"load_variant_config", (PyCFunction)pyffish_loadVariantConfig, METH_VARARGS, "Load variant configuration."},
     {"start_fen", (PyCFunction)pyffish_startFen, METH_VARARGS, "Get starting position FEN."},
     {"two_boards", (PyCFunction)pyffish_twoBoards, METH_VARARGS, "Checks whether the variant is played on two boards."},
+    {"captures_to_hand", (PyCFunction)pyffish_capturesToHand, METH_VARARGS, "Checks whether the variant rules contains capturesToHand."},
     {"get_san", (PyCFunction)pyffish_getSAN, METH_VARARGS, "Get SAN move from given FEN and UCI move."},
     {"get_san_moves", (PyCFunction)pyffish_getSANmoves, METH_VARARGS, "Get SAN movelist from given FEN and UCI movelist."},
     {"legal_moves", (PyCFunction)pyffish_legalMoves, METH_VARARGS, "Get legal moves from given FEN and movelist."},
diff --git a/test.py b/test.py
index f56635d..ba56ad1 100644 (file)
--- a/test.py
+++ b/test.py
@@ -257,6 +257,10 @@ class TestPyffish(unittest.TestCase):
         self.assertFalse(sf.two_boards("chess"))
         self.assertTrue(sf.two_boards("bughouse"))
 
+    def test_captures_to_hand(self):
+        self.assertFalse(sf.captures_to_hand("seirawan"))
+        self.assertTrue(sf.captures_to_hand("shouse"))
+
     def test_start_fen(self):
         result = sf.start_fen("capablanca")
         self.assertEqual(result, CAPA)
index 6e5ff61..12465c9 100644 (file)
@@ -684,6 +684,13 @@ describe('ffish.setOptionBool(name, value)', function () {
   });
 });
 
+describe('ffish.capturesToHand(uciVariant)', function () {
+    it("it checks if the given uci-variant rules contain capturesToHand", () => {
+      chai.expect(ffish.capturesToHand("seirawan")).to.equal(false);
+      chai.expect(ffish.capturesToHand("shouse")).to.equal(true);
+    });
+});
+
 describe('ffish.startingFen(uciVariant)', function () {
     it("it returns the starting fen for the given uci-variant.", () => {
       chai.expect(ffish.startingFen("chess")).to.equal("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");