Skip to content

Commit

Permalink
fix: full move counter (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertnurnberg authored Sep 10, 2023
1 parent 4b50d01 commit 3e0fea9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
22 changes: 13 additions & 9 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Board::Board(const std::string &fen) {
en_passant_square_ = NO_SQ;
castling_rights_.clearAllCastlingRights();
half_move_clock_ = 0;
full_move_number_ = 1;
plies_played_ = 0;

board_.fill(NONE);

Expand All @@ -35,7 +35,7 @@ Board::Board(const Board &other) {

castling_rights_ = other.castling_rights_;

full_move_number_ = other.full_move_number_;
plies_played_ = other.plies_played_;

half_move_clock_ = other.half_move_clock_;

Expand Down Expand Up @@ -64,7 +64,7 @@ Board &Board::operator=(const Board &other) {

castling_rights_ = other.castling_rights_;

full_move_number_ = other.full_move_number_;
plies_played_ = other.plies_played_;

half_move_clock_ = other.half_move_clock_;

Expand Down Expand Up @@ -124,13 +124,17 @@ void Board::setFen(const std::string &fen, bool update_acc) {
const std::string en_passant = params[3];

half_move_clock_ = std::stoi(params.size() > 4 ? params[4] : "0");
full_move_number_ = std::stoi(params.size() > 4 ? params[5] : "1") * 2;
plies_played_ = std::stoi(params.size() > 5 ? params[5] : "1") * 2 - 2;

board_.fill(NONE);
pieces_bb_.fill(0ULL);

side_to_move_ = (move_right == "w") ? WHITE : BLACK;

if (side_to_move_ == BLACK) {
plies_played_++;
}

occupancy_bb_ = 0ULL;

auto square = Square(56);
Expand Down Expand Up @@ -246,7 +250,7 @@ std::string Board::getFen() const {
else
ss << " " << SQUARE_TO_STRING[en_passant_square_] << " ";

ss << int(half_move_clock_) << " " << int(full_move_number_ / 2);
ss << halfmoves() << " " << fullMoveNumber();

// Return the resulting FEN string
return ss.str();
Expand Down Expand Up @@ -331,7 +335,7 @@ void Board::makeNullMove() {

en_passant_square_ = NO_SQ;

full_move_number_++;
plies_played_++;
side_to_move_ = ~side_to_move_;
}

Expand All @@ -347,7 +351,7 @@ void Board::unmakeNullMove() {

castling_rights_ = restore.castling;
half_move_clock_ = restore.half_moves;
full_move_number_--;
plies_played_--;
side_to_move_ = ~side_to_move_;
}

Expand All @@ -367,8 +371,8 @@ std::ostream &operator<<(std::ostream &os, const Board &b) {
os << "Fen: " << b.getFen() << "\n";
os << "Side to move: " << static_cast<int>(b.side_to_move_) << "\n";
os << "Castling: " << b.getCastleString() << "\n";
os << "Halfmoves: " << static_cast<int>(b.half_move_clock_) << "\n";
os << "Fullmoves: " << static_cast<int>(b.full_move_number_) / 2 << "\n";
os << "Halfmoves: " << b.halfmoves() << "\n";
os << "Fullmoves: " << b.fullMoveNumber() << "\n";
os << "EP: " << static_cast<int>(b.en_passant_square_) << "\n";
os << "Hash: " << b.hash_key_ << "\n";
os << "Chess960: " << b.chess960;
Expand Down
10 changes: 6 additions & 4 deletions src/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ class Board {

[[nodiscard]] uint8_t halfmoves() const { return half_move_clock_; }

[[nodiscard]] int ply() const { return full_move_number_ * 2; }
[[nodiscard]] int fullMoveNumber() const { return 1 + plies_played_ / 2; }

[[nodiscard]] int ply() const { return plies_played_ + 1; }

[[nodiscard]] Color sideToMove() const { return side_to_move_; }

Expand Down Expand Up @@ -173,7 +175,7 @@ class Board {
CastlingRights castling_rights_;

// full moves start at 1
uint16_t full_move_number_;
uint16_t plies_played_;

// halfmoves start at 0
uint8_t half_move_clock_;
Expand Down Expand Up @@ -354,7 +356,7 @@ void Board::makeMove(const Move move) {
if constexpr (updateNNUE) accumulators_->push();

half_move_clock_++;
full_move_number_++;
plies_played_++;

// *****************************
// UPDATE HASH
Expand Down Expand Up @@ -446,7 +448,7 @@ void Board::unmakeMove(Move move) {

state_history_.pop_back();

full_move_number_--;
plies_played_--;
side_to_move_ = ~side_to_move_;

if (typeOf(move) == CASTLING) {
Expand Down

0 comments on commit 3e0fea9

Please sign in to comment.