Skip to content

Commit

Permalink
Rename and fix non shogi style promotion.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbtami committed Jun 11, 2024
1 parent e805f92 commit 0a7f810
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
11 changes: 8 additions & 3 deletions src/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/pyffish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -291,7 +291,7 @@ extern "C" PyObject* pyffish_unpromotedCapturedPiece(PyObject* self, PyObject *a

StateListPtr states(new std::deque<StateInfo>(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
Expand Down Expand Up @@ -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."},
Expand Down
13 changes: 8 additions & 5 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 0a7f810

Please sign in to comment.