diff --git a/src/board/include/tartan/board/board.hpp b/src/board/include/tartan/board/board.hpp index 3c233d1..1cf6091 100644 --- a/src/board/include/tartan/board/board.hpp +++ b/src/board/include/tartan/board/board.hpp @@ -857,7 +857,8 @@ class Board : private BoardT { * @brief Turn index * * Underlying varriable gets inclremented - * on Turn::apply() and decremented on Turn::undo(). + * on Turn::apply(). + * Acts like a timestamp fo current Board state. * * @return last Piece::Turn index * @sa b_turnIndex diff --git a/src/board/turn.cpp b/src/board/turn.cpp index 8e850fb..0d034d1 100644 --- a/src/board/turn.cpp +++ b/src/board/turn.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -26,7 +27,6 @@ void Turn::apply(int) { void Turn::undo() { Board& cb = *t_piece->p_board; - cb.b_turnIndex--; t_piece->move(t_from); diff --git a/src/chess/include/tartan/chess/chess.hpp b/src/chess/include/tartan/chess/chess.hpp index 6ab0183..0616a45 100644 --- a/src/chess/include/tartan/chess/chess.hpp +++ b/src/chess/include/tartan/chess/chess.hpp @@ -460,7 +460,13 @@ class King : public Piece { */ bool castled() const { return k_castled; }; private: + bool calculateCheck() const; + bool calculateCheckmate() const; bool k_castled = false; + mutable bool k_check = false; + mutable bool k_checkmate = false; + mutable std::size_t k_checkTurnIndex = -1; + mutable std::size_t k_checkmateTurnIndex = -1; }; } diff --git a/src/chess/pieces/king/king.cpp b/src/chess/pieces/king/king.cpp index dad81c3..95e8c16 100644 --- a/src/chess/pieces/king/king.cpp +++ b/src/chess/pieces/king/king.cpp @@ -72,6 +72,14 @@ TurnMap King::moveMap(int mode) const { } bool King::check() const { + if (k_checkTurnIndex != board()->turnIndex()) { + k_check = calculateCheck(); + k_checkTurnIndex = board()->turnIndex(); + } + return k_check; +} + +bool King::calculateCheck() const { for (auto& row : p_board->board()) { for (auto& p : row) { if (!p or p->color() == p_color) @@ -97,7 +105,16 @@ bool King::check() const { return false; } + bool King::checkmate() const { + if (k_checkmateTurnIndex != board()->turnIndex()) { + k_checkmate = calculateCheckmate(); + k_checkmateTurnIndex = board()->turnIndex(); + } + return k_checkmate; +} + +bool King::calculateCheckmate() const { if (!check() or p_board->possibleMoves(this).possible()) return false;