From 9e48665ad37dee8de59465be34cbdb2125e3198b Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Wed, 13 Feb 2008 00:50:13 +0100 Subject: [PATCH] [#68] Fix Tori Shogi legalitycheck to detect in-check condition. This is a hack around a template-instantation problem: Base::canBeCaptured() instantiates a Base::LegalityCheck<>, so we cut'n'paste it to be sure. There may be a cleaner fix, but since this code will be rewritten for the new component API for 1.0beta, there is not point in spending more time on it. --- src/hlvariant/tori-shogi/legalitycheck.h | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/hlvariant/tori-shogi/legalitycheck.h b/src/hlvariant/tori-shogi/legalitycheck.h index 92e5ad2..7f8f811 100644 --- a/src/hlvariant/tori-shogi/legalitycheck.h +++ b/src/hlvariant/tori-shogi/legalitycheck.h @@ -32,6 +32,7 @@ public: virtual bool getMoveType(const Piece& piece, const Move& move) const; bool legal(Move& move) const; bool pseudolegal(Move& move) const; + bool canBeCaptured(const GameState& state, const Point& point) const; protected: virtual bool stuckPiece(const Piece& piece, const Point& p) const; }; @@ -163,6 +164,23 @@ bool LegalityCheck::pseudolegal(Move& move) const { } +// strict copy from Shogi, for template-instantation reasons +template + bool LegalityCheck::canBeCaptured(const GameState& state, const Point& point) const { + for (int i = 0; i < state.board().size().x; i++) { + for (int j = 0; j < state.board().size().y; j++) { + Point p(i, j); + Piece piece = state.board().get(p); + LegalityCheck check(state); + if (piece.color() == state.turn() && check.getMoveType(piece, Move(p, point))) { + kDebug() << state.board().get(point).name() << " can be captured"; + return true; + } + } + } + return false; +} + template bool LegalityCheck::legal(Move& move) const { if (!pseudolegal(move)) @@ -171,14 +189,13 @@ bool LegalityCheck::legal(Move& move) const { GameState tmp(Base::m_state); tmp.move(move); - // find king and prince positions + // find king position Point king_pos = tmp.board().find(Piece(Base::m_state.turn(), Piece::PHOENIX)); - if (!king_pos.valid()) return false; // check if the king can be captured - if (Base::canBeCaptured(tmp, king_pos)) + if (canBeCaptured(tmp, king_pos)) return false; return true; -- 2.11.4.GIT