From 0a7f8105a606d9f462be8a8e10bec75fd7cde28f Mon Sep 17 00:00:00 2001 From: gbtami Date: Tue, 11 Jun 2024 11:11:13 +0200 Subject: [PATCH] Rename and fix non shogi style promotion. --- src/position.h | 11 ++++++++--- src/pyffish.cpp | 6 +++--- test.py | 13 ++++++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/position.h b/src/position.h index ff09eabb5..d02372567 100644 --- a/src/position.h +++ b/src/position.h @@ -279,7 +279,7 @@ class Position { bool gives_check(Move m) const; Piece moved_piece(Move m) const; Piece captured_piece() const; - Piece unpromoted_captured_piece() const; + const std::string piece_to_partner() const; // Piece specific bool pawn_passed(Color c, Square s) const; @@ -1411,8 +1411,13 @@ inline Piece Position::captured_piece() const { return st->capturedPiece; } -inline Piece Position::unpromoted_captured_piece() const { - return st->unpromotedCapturedPiece; +inline const std::string Position::piece_to_partner() const { + if (!st->capturedPiece) return std::string(); + Color color = color_of(st->capturedPiece); + Piece piece = st->capturedpromoted ? + (st->unpromotedCapturedPiece ? st->unpromotedCapturedPiece : make_piece(color, promotion_pawn_type(color))) : + st->capturedPiece; + return std::string(1, piece_to_char()[piece]); } inline Thread* Position::this_thread() const { diff --git a/src/pyffish.cpp b/src/pyffish.cpp index a632ea5f6..0fa355c9f 100644 --- a/src/pyffish.cpp +++ b/src/pyffish.cpp @@ -280,7 +280,7 @@ extern "C" PyObject* pyffish_isCapture(PyObject* self, PyObject *args) { } // INPUT variant, fen, move list -extern "C" PyObject* pyffish_unpromotedCapturedPiece(PyObject* self, PyObject *args) { +extern "C" PyObject* pyffish_pieceToPartner(PyObject* self, PyObject *args) { PyObject *moveList; Position pos; const char *fen, *variant; @@ -291,7 +291,7 @@ extern "C" PyObject* pyffish_unpromotedCapturedPiece(PyObject* self, PyObject *a StateListPtr states(new std::deque(1)); buildPosition(pos, states, variant, fen, moveList, chess960); - return Py_BuildValue("i", pos.unpromoted_captured_piece()); + return Py_BuildValue("s", pos.piece_to_partner().c_str()); } // INPUT variant, fen, move list @@ -399,7 +399,7 @@ static PyMethodDef PyFFishMethods[] = { {"get_fen", (PyCFunction)pyffish_getFEN, METH_VARARGS, "Get resulting FEN from given FEN and movelist."}, {"gives_check", (PyCFunction)pyffish_givesCheck, METH_VARARGS, "Get check status from given FEN and movelist."}, {"is_capture", (PyCFunction)pyffish_isCapture, METH_VARARGS, "Get whether given move is a capture from given FEN and movelist."}, - {"unpromoted_captured_piece", (PyCFunction)pyffish_unpromotedCapturedPiece, METH_VARARGS, "Get unpromoted captured piece from given FEN and movelist."}, + {"piece_to_partner", (PyCFunction)pyffish_pieceToPartner, METH_VARARGS, "Get unpromoted captured piece from given FEN and movelist."}, {"game_result", (PyCFunction)pyffish_gameResult, METH_VARARGS, "Get result from given FEN, considering variant end, checkmate, and stalemate."}, {"is_immediate_game_end", (PyCFunction)pyffish_isImmediateGameEnd, METH_VARARGS, "Get result from given FEN if variant rules ends the game."}, {"is_optional_game_end", (PyCFunction)pyffish_isOptionalGameEnd, METH_VARARGS, "Get result from given FEN it rules enable game end by player."}, diff --git a/test.py b/test.py index 4e609c416..93cc16d2c 100644 --- a/test.py +++ b/test.py @@ -974,12 +974,15 @@ def test_is_capture(self): result = sf.is_capture("sittuyin", "8/2k5/8/4P3/4P1N1/5K2/8/8[] w - - 0 1", [], "e5e5f") self.assertFalse(result) - def test_unpromoted_captured_piece(self): - result = sf.unpromoted_captured_piece("bughouse", "r2qkbnr/1Ppppppp/2n5/8/8/8/1PPPPPPP/RNBQKBNR[] w KQkq - 0 1", ["b7a8q", "d8a8"]) - self.assertEqual(result, 1) + def test_piece_to_partner(self): + result = sf.piece_to_partner("bughouse", "r2qkbnr/1Ppppppp/2n5/8/8/8/1PPPPPPP/RNBQKBNR[] w KQkq - 0 1", ["b7a8q"]) + self.assertEqual(result, "r") - result = sf.unpromoted_captured_piece("bughouse", "r2qkbnr/1Ppppppp/2n5/8/8/8/1PPPPPPP/RNBQKBNR[] w KQkq - 0 1", ["b7a8q", "d8b8"]) - self.assertEqual(result, 0) + result = sf.piece_to_partner("bughouse", "r2qkbnr/1Ppppppp/2n5/8/8/8/1PPPPPPP/RNBQKBNR[] w KQkq - 0 1", ["b7a8q", "d8a8"]) + self.assertEqual(result, "P") + + result = sf.piece_to_partner("bughouse", "r2qkbnr/1Ppppppp/2n5/8/8/8/1PPPPPPP/RNBQKBNR[] w KQkq - 0 1", ["b7a8q", "d8b8"]) + self.assertEqual(result, "") def test_game_result(self): result = sf.game_result("chess", CHESS, ["f2f3", "e7e5", "g2g4", "d8h4"])