LibChess: Fix hashing of the chess board

The hash function should take the board by reference, not by value.
Also, the fact whether black can castle kingside or not was included
twice in the hash, unnecesarily.
This commit is contained in:
Martin Blicha 2021-07-30 18:18:25 +02:00 committed by Gunnar Beutner
parent 8e3431d56d
commit add3a02ddd
Notes: sideshowbarker 2024-07-19 17:21:58 +09:00

View file

@ -278,7 +278,7 @@ void Board::generate_moves(Callback callback, Color color) const
template<>
struct AK::Traits<Chess::Piece> : public GenericTraits<Chess::Piece> {
static unsigned hash(const Chess::Piece& piece)
static unsigned hash(Chess::Piece const& piece)
{
return pair_int_hash(static_cast<u32>(piece.color), static_cast<u32>(piece.type));
}
@ -286,7 +286,7 @@ struct AK::Traits<Chess::Piece> : public GenericTraits<Chess::Piece> {
template<>
struct AK::Traits<Chess::Board> : public GenericTraits<Chess::Board> {
static unsigned hash(const Chess::Board chess)
static unsigned hash(Chess::Board const& chess)
{
unsigned hash = 0;
hash = pair_int_hash(hash, static_cast<u32>(chess.m_white_can_castle_queenside));
@ -294,8 +294,6 @@ struct AK::Traits<Chess::Board> : public GenericTraits<Chess::Board> {
hash = pair_int_hash(hash, static_cast<u32>(chess.m_black_can_castle_queenside));
hash = pair_int_hash(hash, static_cast<u32>(chess.m_black_can_castle_kingside));
hash = pair_int_hash(hash, static_cast<u32>(chess.m_black_can_castle_kingside));
Chess::Square::for_each([&](Chess::Square sq) {
hash = pair_int_hash(hash, Traits<Chess::Piece>::hash(chess.get_piece(sq)));
return IterationDecision::Continue;