Disservin / chess-library

C++ chess library
https://disservin.github.io/chess-library/
MIT License
63 stars 20 forks source link

Possible bug for Piece::type() #85

Closed ghost closed 3 months ago

ghost commented 3 months ago

Piece::type() returns PieceType::PAWN for Piece::NONE.

  Board board;
  std::cout << board << '\n';
  for (int i = 0; i < 64; i++)
  std::cout << (board.at(Square(i)).type() == PieceType::NONE) << ' ';

The code above should print out 1 for squares A3 to H6. Inspecting the code on line 1106

  [[nodiscard]] constexpr PieceType type() const noexcept {
      return static_cast<PieceType::underlying>(int(piece) % 6);
  }

Which means Piece::NONE == 12 returns PieceType::PAWN == 0 instead of PieceType::NONE == 6

Disservin commented 3 months ago

Oh crap, I’ll fix that later today, since I’m afk. Temporary workaround is just taking the piece and checking that before calling .type… but yeah this shouldn’t be the case

Disservin commented 3 months ago

Btw if you are trying to loop over all pieces this is faster


auto occ = board.occ();
while (occ.getBits())
{
  auto sq = occ.pop();
  auto pc = board.at(sq);
  // do something with sq/pc
}