Skip to content

Commit

Permalink
Change the Move enum to a class
Browse files Browse the repository at this point in the history
This changes the Move enum to a class, this way
all move related functions can be moved into the class
and be more self contained.

closes #4958

No functional change
  • Loading branch information
Disservin committed Jan 4, 2024
1 parent 28f8663 commit cafbe8e
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 219 deletions.
28 changes: 14 additions & 14 deletions src/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ ExtMove* make_promotions(ExtMove* moveList, [[maybe_unused]] Square to) {
constexpr bool all = Type == EVASIONS || Type == NON_EVASIONS;

if constexpr (Type == CAPTURES || all)
*moveList++ = make<PROMOTION>(to - D, to, QUEEN);
*moveList++ = Move::make<PROMOTION>(to - D, to, QUEEN);

if constexpr ((Type == CAPTURES && Enemy) || (Type == QUIETS && !Enemy) || all)
{
*moveList++ = make<PROMOTION>(to - D, to, ROOK);
*moveList++ = make<PROMOTION>(to - D, to, BISHOP);
*moveList++ = make<PROMOTION>(to - D, to, KNIGHT);
*moveList++ = Move::make<PROMOTION>(to - D, to, ROOK);
*moveList++ = Move::make<PROMOTION>(to - D, to, BISHOP);
*moveList++ = Move::make<PROMOTION>(to - D, to, KNIGHT);
}

return moveList;
Expand Down Expand Up @@ -89,13 +89,13 @@ ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard ta
while (b1)
{
Square to = pop_lsb(b1);
*moveList++ = make_move(to - Up, to);
*moveList++ = Move(to - Up, to);
}

while (b2)
{
Square to = pop_lsb(b2);
*moveList++ = make_move(to - Up - Up, to);
*moveList++ = Move(to - Up - Up, to);
}
}

Expand Down Expand Up @@ -128,13 +128,13 @@ ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard ta
while (b1)
{
Square to = pop_lsb(b1);
*moveList++ = make_move(to - UpRight, to);
*moveList++ = Move(to - UpRight, to);
}

while (b2)
{
Square to = pop_lsb(b2);
*moveList++ = make_move(to - UpLeft, to);
*moveList++ = Move(to - UpLeft, to);
}

if (pos.ep_square() != SQ_NONE)
Expand All @@ -150,7 +150,7 @@ ExtMove* generate_pawn_moves(const Position& pos, ExtMove* moveList, Bitboard ta
assert(b1);

while (b1)
*moveList++ = make<EN_PASSANT>(pop_lsb(b1), pos.ep_square());
*moveList++ = Move::make<EN_PASSANT>(pop_lsb(b1), pos.ep_square());
}
}

Expand All @@ -175,7 +175,7 @@ ExtMove* generate_moves(const Position& pos, ExtMove* moveList, Bitboard target)
b &= pos.check_squares(Pt);

while (b)
*moveList++ = make_move(from, pop_lsb(b));
*moveList++ = Move(from, pop_lsb(b));
}

return moveList;
Expand Down Expand Up @@ -213,12 +213,12 @@ ExtMove* generate_all(const Position& pos, ExtMove* moveList) {
b &= ~attacks_bb<QUEEN>(pos.square<KING>(~Us));

while (b)
*moveList++ = make_move(ksq, pop_lsb(b));
*moveList++ = Move(ksq, pop_lsb(b));

if ((Type == QUIETS || Type == NON_EVASIONS) && pos.can_castle(Us & ANY_CASTLING))
for (CastlingRights cr : {Us & KING_SIDE, Us & QUEEN_SIDE})
if (!pos.castling_impeded(cr) && pos.can_castle(cr))
*moveList++ = make<CASTLING>(ksq, pos.castling_rook_square(cr));
*moveList++ = Move::make<CASTLING>(ksq, pos.castling_rook_square(cr));
}

return moveList;
Expand Down Expand Up @@ -268,9 +268,9 @@ ExtMove* generate<LEGAL>(const Position& pos, ExtMove* moveList) {
moveList =
pos.checkers() ? generate<EVASIONS>(pos, moveList) : generate<NON_EVASIONS>(pos, moveList);
while (cur != moveList)
if (((pinned & from_sq(*cur)) || from_sq(*cur) == ksq || type_of(*cur) == EN_PASSANT)
if (((pinned & cur->from_sq()) || cur->from_sq() == ksq || cur->type_of() == EN_PASSANT)
&& !pos.legal(*cur))
*cur = (--moveList)->move;
*cur = *(--moveList);
else
++cur;

Expand Down
8 changes: 3 additions & 5 deletions src/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ enum GenType {
LEGAL
};

struct ExtMove {
Move move;
int value;
struct ExtMove: public Move {
int value;

operator Move() const { return move; }
void operator=(Move m) { move = m; }
void operator=(Move m) { data = m.raw(); }

// Inhibit unwanted implicit conversions to Move
// with an ambiguity that yields to a compile error.
Expand Down
32 changes: 15 additions & 17 deletions src/movepick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,19 +166,19 @@ void MovePicker::score() {
for (auto& m : *this)
if constexpr (Type == CAPTURES)
m.value =
(7 * int(PieceValue[pos.piece_on(to_sq(m))])
+ (*captureHistory)[pos.moved_piece(m)][to_sq(m)][type_of(pos.piece_on(to_sq(m)))])
(7 * int(PieceValue[pos.piece_on(m.to_sq())])
+ (*captureHistory)[pos.moved_piece(m)][m.to_sq()][type_of(pos.piece_on(m.to_sq()))])
/ 16;

else if constexpr (Type == QUIETS)
{
Piece pc = pos.moved_piece(m);
PieceType pt = type_of(pos.moved_piece(m));
Square from = from_sq(m);
Square to = to_sq(m);
Square from = m.from_sq();
Square to = m.to_sq();

// histories
m.value = 2 * (*mainHistory)[pos.side_to_move()][from_to(m)];
m.value = 2 * (*mainHistory)[pos.side_to_move()][m.from_to()];
m.value += 2 * (*pawnHistory)[pawn_structure_index(pos)][pc][to];
m.value += 2 * (*continuationHistory[0])[pc][to];
m.value += (*continuationHistory[1])[pc][to];
Expand Down Expand Up @@ -211,12 +211,12 @@ void MovePicker::score() {
else // Type == EVASIONS
{
if (pos.capture_stage(m))
m.value = PieceValue[pos.piece_on(to_sq(m))] - Value(type_of(pos.moved_piece(m)))
m.value = PieceValue[pos.piece_on(m.to_sq())] - Value(type_of(pos.moved_piece(m)))
+ (1 << 28);
else
m.value = (*mainHistory)[pos.side_to_move()][from_to(m)]
+ (*continuationHistory[0])[pos.moved_piece(m)][to_sq(m)]
+ (*pawnHistory)[pawn_structure_index(pos)][pos.moved_piece(m)][to_sq(m)];
m.value = (*mainHistory)[pos.side_to_move()][m.from_to()]
+ (*continuationHistory[0])[pos.moved_piece(m)][m.to_sq()]
+ (*pawnHistory)[pawn_structure_index(pos)][pos.moved_piece(m)][m.to_sq()];
}
}

Expand All @@ -235,7 +235,7 @@ Move MovePicker::select(Pred filter) {

cur++;
}
return MOVE_NONE;
return Move::none();
}

// Most important method of the MovePicker class. It
Expand Down Expand Up @@ -278,16 +278,15 @@ Move MovePicker::next_move(bool skipQuiets) {
endMoves = std::end(refutations);

// If the countermove is the same as a killer, skip it
if (refutations[0].move == refutations[2].move
|| refutations[1].move == refutations[2].move)
if (refutations[0] == refutations[2] || refutations[1] == refutations[2])
--endMoves;

++stage;
[[fallthrough]];

case REFUTATION :
if (select<Next>([&]() {
return *cur != MOVE_NONE && !pos.capture_stage(*cur) && pos.pseudo_legal(*cur);
return *cur != Move::none() && !pos.capture_stage(*cur) && pos.pseudo_legal(*cur);
}))
return *(cur - 1);
++stage;
Expand All @@ -308,8 +307,7 @@ Move MovePicker::next_move(bool skipQuiets) {

case QUIET :
if (!skipQuiets && select<Next>([&]() {
return *cur != refutations[0].move && *cur != refutations[1].move
&& *cur != refutations[2].move;
return *cur != refutations[0] && *cur != refutations[1] && *cur != refutations[2];
}))
return *(cur - 1);

Expand Down Expand Up @@ -343,7 +341,7 @@ Move MovePicker::next_move(bool skipQuiets) {

// If we did not find any move and we do not try checks, we have finished
if (depth != DEPTH_QS_CHECKS)
return MOVE_NONE;
return Move::none();

++stage;
[[fallthrough]];
Expand All @@ -360,7 +358,7 @@ Move MovePicker::next_move(bool skipQuiets) {
}

assert(false);
return MOVE_NONE; // Silence warning
return Move::none(); // Silence warning
}

} // namespace Stockfish
2 changes: 1 addition & 1 deletion src/movepick.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ using CorrectionHistory =
// MovePicker class is used to pick one pseudo-legal move at a time from the
// current position. The most important method is next_move(), which returns a
// new pseudo-legal move each time it is called, until there are no moves left,
// when MOVE_NONE is returned. In order to improve the efficiency of the
// when Move::none() is returned. In order to improve the efficiency of the
// alpha-beta algorithm, MovePicker attempts to return the moves which are most
// likely to get a cut-off first.
class MovePicker {
Expand Down
Loading

0 comments on commit cafbe8e

Please sign in to comment.