Skip to content

Commit

Permalink
Update book code to adopt latest changes done by Stockfish
Browse files Browse the repository at this point in the history
  • Loading branch information
khalid-a-omar committed Jan 5, 2024
1 parent 6ff6652 commit 059a95b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/book/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ namespace Polyfish::Book
Move probe(const Position& pos)
{
int moveNumber = 1 + pos.game_ply() / 2;
Move bookMove = MOVE_NONE;
Move bookMove = Move::none();

for (size_t i = 0; i < NumBooks; ++i)
{
if (books[i] != nullptr && (int)Options[Utility::format_string("Book %d Depth", i + 1)] >= moveNumber)
{
bookMove = books[i]->probe(pos, (size_t)(int)Options[Utility::format_string("Book %d Width", i + 1)], (bool)Options[Utility::format_string("(CTG) Book %d Only Green", i + 1)]);
if (bookMove != MOVE_NONE)
if (bookMove != Move::none())
break;
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/book/ctg/ctg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ namespace

CtgMove() : CtgMoveStats()
{
pseudoMove = MOVE_NONE;
sfMove = MOVE_NONE;
pseudoMove = Move::none();
sfMove = Move::none();

annotation = CtgMoveAnnotation::Unknown;
recommendation = CtgMoveRecommendation::Unknown;
Expand All @@ -313,12 +313,12 @@ namespace
else if (((rank_of(from) == RANK_7 && rank_of(to) == RANK_8) || (rank_of(from) == RANK_2 && rank_of(to) == RANK_1)) && type_of(pos.piece_on(from)) == PAWN)
promotionPiece = QUEEN;

pseudoMove = promotionPiece == NO_PIECE_TYPE ? make_move(from, to) : make<PROMOTION>(from, to, promotionPiece);
pseudoMove = promotionPiece == NO_PIECE_TYPE ? Move(from, to) : Move::make<PROMOTION>(from, to, promotionPiece);
}

Move pseudo_move() const
{
assert(pseudoMove != MOVE_NONE);
assert(pseudoMove != Move::none());
return pseudoMove;
}

Expand All @@ -329,7 +329,7 @@ namespace

Move sf_move() const
{
assert(sfMove != MOVE_NONE);
assert(sfMove != Move::none());
return sfMove;
}

Expand Down Expand Up @@ -944,7 +944,7 @@ namespace Polyfish::Book::CTG

//Check
if (index == MoveEncSize)
return MOVE_NONE;
return Move::none();

//Find/Read the move
const MoveEnc& moveEnc = moveTable[index];
Expand All @@ -961,24 +961,24 @@ namespace Polyfish::Book::CTG
Square from = make_square(File(x), Rank(y));
Square to = make_square(File((x + 8 + moveEnc.right) % 8), Rank((y + 8 + moveEnc.forward) % 8));

return make_move(from, to);
return Move(from, to);
}
}
}

//Should never get here
assert(false);
return MOVE_NONE;
return Move::none();
}

bool CtgBook::get_move(const Position& pos, const CtgPositionData& positionData, int moveNum, CtgMove& ctgMove) const
{
Move m = get_pseudo_move(positionData, moveNum);
if (m == MOVE_NONE)
if (m == Move::none())
return false;

Square from = from_sq(m);
Square to = to_sq(m);
Square from = m.from_sq();
Square to = m.to_sq();

if (positionData.invert)
{
Expand Down Expand Up @@ -1022,10 +1022,10 @@ namespace Polyfish::Book::CTG
{
for (const auto& m : legalMoves)
{
if (ctgMove.pseudo_move() == (m.move ^ type_of(m.move)))
if (ctgMove.pseudo_move().raw() == (m.raw() ^ m.type_of()))
{
//Assign the move
ctgMove.set_sf_move(m.move);
ctgMove.set_sf_move(m);

//Play the move
p.do_move(ctgMove.sf_move(), si[1]);
Expand All @@ -1044,7 +1044,7 @@ namespace Polyfish::Book::CTG
}
}

assert(ctgMove.sf_move() != MOVE_NONE);
assert(ctgMove.sf_move() != Move::none());
}
}

Expand Down Expand Up @@ -1140,17 +1140,17 @@ namespace Polyfish::Book::CTG
Move CtgBook::probe(const Position& pos, size_t width, bool onlyGreen) const
{
if (!is_open())
return MOVE_NONE;
return Move::none();

CtgPositionData positionData;
if (!decode(pos, positionData))
return MOVE_NONE;
return Move::none();

CtgMoveList ctgMoveList;
get_moves(pos, positionData, ctgMoveList);

if (ctgMoveList.size() == 0)
return MOVE_NONE;
return Move::none();

//Remove red moves and any moves with negative weight
ctgMoveList.erase(
Expand All @@ -1165,7 +1165,7 @@ namespace Polyfish::Book::CTG

//Check move list again after removing unwanted moves
if (ctgMoveList.size() == 0)
return MOVE_NONE;
return Move::none();

//Sort moves accorging to their weights
stable_sort(ctgMoveList.begin(), ctgMoveList.end(), [](const CtgMove& mv1, const CtgMove& mv2) { return mv1.weight() > mv2.weight(); });
Expand Down
14 changes: 7 additions & 7 deletions src/book/polyglot/polyglot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,9 @@ namespace
// all other cases, we can directly compare with a Move after having masked
// out the special Move flags (bit 14-15) that are not supported by Polyglot.
Move move = Move(e.move);
int pt = (move >> 12) & 7;
int pt = (move.raw() >> 12) & 7;
if (pt)
move = make<PROMOTION>(from_sq(move), to_sq(move), PieceType(pt + 1));
move = Move::make<PROMOTION>(move.from_sq(), move.to_sq(), PieceType(pt + 1));

return move;
}
Expand All @@ -362,7 +362,7 @@ namespace
Move move;
PolyglotEntry entry;

PolyglotBookMove() { move = MOVE_NONE; memset(&entry, 0, sizeof(PolyglotEntry)); }
PolyglotBookMove() { move = Move::none(); memset(&entry, 0, sizeof(PolyglotEntry)); }
PolyglotBookMove(const PolyglotEntry& e, Move m) { memcpy(&entry, &e, sizeof(PolyglotEntry)); move = m; }
};

Expand Down Expand Up @@ -450,9 +450,9 @@ namespace Polyfish::Book::Polyglot
Move move = make_move(e);
for (const auto& m : MoveList<LEGAL>(pos))
{
if (move == (m.move ^ type_of(m.move)))
if (move.raw() == (m.raw() ^ m.type_of()))
{
bookMoves.push_back(PolyglotBookMove(e, m.move));
bookMoves.push_back(PolyglotBookMove(e, m));
}
}
}
Expand Down Expand Up @@ -528,13 +528,13 @@ namespace Polyfish::Book::Polyglot
Move PolyglotBook::probe(const Position& pos, size_t width, bool /*onlyGreen*/) const
{
if (!has_data())
return MOVE_NONE;
return Move::none();

vector<PolyglotBookMove> bookMoves;
get_moves(pos, bookMoves);

if (!bookMoves.size())
return MOVE_NONE;
return Move::none();

#if 1
//Remove any move with REALLY low weight compared to the total weight of all moves
Expand Down

0 comments on commit 059a95b

Please sign in to comment.