Skip to content

Commit

Permalink
CHANGE: vshogi::animal_shogi::State::in_check() considers try rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ctgk committed Oct 10, 2024
1 parent ac41116 commit b7a11aa
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cpp/include/vshogi/common/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ class Board
update_internals_based_on_pieces();
return sfen;
}
BitBoardType get_attacks_by_nocheck(const Square& sq) const
{
return BitBoardType::get_attacks_by(m_pieces[sq], sq, get_occupied());
}
Square find_attacker(
const ColorEnum& attacker_color,
const Square& attacked,
Expand Down
44 changes: 44 additions & 0 deletions cpp/include/vshogi/variants/animal_shogi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,50 @@ inline animal_shogi::ColoredPieceEnum animal_shogi::Board::apply(
return apply(dst, moving_piece, hash);
}

template <>
inline void animal_shogi::State::update_checkers()
{
using namespace animal_shogi;
std::fill_n(m_checker_locations, 2, SQ_NA);
uint index = 0u;
const auto king_sq = m_board.get_king_location(m_turn);
for (auto sq : m_board.get_attacks_by_nocheck(king_sq).square_iterator()) {
if (m_board.is_empty(sq))
continue;
if (m_board.get_attacks_by_nocheck(sq).is_one(king_sq))
m_checker_locations[index++] = sq;
if (index > 1)
return;
}
const auto mask_rank_2nd = (m_turn == BLACK) ? bb_rank3 : bb_rank2;
const auto enemy_king_sq = m_board.get_king_location(~m_turn);
if (mask_rank_2nd.is_one(enemy_king_sq))
m_checker_locations[index++] = enemy_king_sq;
}

template <>
inline void
animal_shogi::State::update_checkers_before_turn_update(const Square& dst)
{
using namespace animal_shogi;
const auto enemy_king_sq = m_board.get_king_location(~m_turn);
uint index = 0u;
std::fill_n(m_checker_locations, 2, SQ_NA);
if (m_board.get_attacks_by_nocheck(dst).is_one(enemy_king_sq))
m_checker_locations[index++] = dst;
const auto king_sq = m_board.get_king_location(m_turn);
const auto mask_rank_3rd = (m_turn == BLACK) ? bb_rank3 : bb_rank2;
if (mask_rank_3rd.is_one(king_sq))
m_checker_locations[index++] = king_sq;
}

template <>
inline void animal_shogi::State::update_checkers_before_turn_update(
const Square& dst, const Square&)
{
return update_checkers_before_turn_update(dst);
}

template <>
inline void
animal_shogi::KingMoveGenerator::increment_iterator_while_square_is_attacked()
Expand Down
24 changes: 24 additions & 0 deletions cpp/tests/test_vshogi/test_animal_shogi/test_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,28 @@ TEST(animal_shogi_state, zobrist_hash)
CHECK_TRUE(another != hash);
}

TEST(animal_shogi_state, in_check)
{
{
const auto s = State("1l1/3/3/1L1 b -");
CHECK_FALSE(s.in_check());
}
{
const auto s = State("1l1/3/1c1/1L1 b -");
CHECK_TRUE(s.in_check());
}
{
const auto s = State("3/3/l2/2L b -");
CHECK_TRUE(s.in_check());
}
{
auto s = State("3/l2/3/1L1 b -");
CHECK_FALSE(s.in_check());
s.apply(Move(SQ_B3, SQ_B4));
CHECK_TRUE(s.in_check()); // B_LI is attacking W_LI
s.apply(Move(SQ_A3, SQ_A2));
CHECK_TRUE(s.in_check()); // W_LI reached the second furthest rank
}
}

} // namespace test_vshogi::test_animal_shogi

0 comments on commit b7a11aa

Please sign in to comment.