From 2a85bbf2d3f091382670926d3ed0b42b95d7d7ee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bajusz=20Tam=C3=A1s?= Date: Sat, 1 Oct 2022 12:51:22 +0200 Subject: [PATCH] Epose variant capturesToHand to js/python (#522) --- setup.py | 2 +- src/ffishjs.cpp | 6 ++++++ src/pyffish.cpp | 14 +++++++++++++- test.py | 4 ++++ tests/js/test.js | 7 +++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index c3e37c6..9bb6812 100644 --- 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", diff --git a/src/ffishjs.cpp b/src/ffishjs.cpp index 3e7eb2b..bf1f342 100644 --- a/src/ffishjs.cpp +++ b/src/ffishjs.cpp @@ -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(&ffish::validate_fen)); function("validateFen", select_overload(&ffish::validate_fen)); diff --git a/src/pyffish.cpp b/src/pyffish.cpp index 89fb7b1..c0aa478 100644 --- a/src/pyffish.cpp +++ b/src/pyffish.cpp @@ -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 --- 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) diff --git a/tests/js/test.js b/tests/js/test.js index 6e5ff61..12465c9 100644 --- a/tests/js/test.js +++ b/tests/js/test.js @@ -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"); -- 1.7.0.4