Skip to content

Commit

Permalink
Re-add wasMoveLegal()
Browse files Browse the repository at this point in the history
  • Loading branch information
alex65536 committed Aug 6, 2023
1 parent 22f7158 commit 2f72963
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion selftest/sofcheck/intf.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ inline Board boardFromFen(const char *fen) {

inline MovePersistence tryMakeMove(Board &board, const Move &move) {
auto p = SoFCore::moveMake(board, move);
if (!isMoveLegal(board)) {
if (!wasMoveLegal(board)) {
SoFCore::moveUnmake(board, move, p);
return std::nullopt;
}
Expand Down
9 changes: 3 additions & 6 deletions src/bot_api/clients/uci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,16 +555,13 @@ PollResult UciServerConnector::processUciPosition(std::istream &tokens) {
vector<Move> moves;
while (tokens >> token) {
const Move move = moveParse(token.c_str(), dstBoard);
if (!move.isWellFormed(dstBoard.side) || !isMoveValid(dstBoard, move)) {
logError(UCI_SERVER) << "Move \"" << token << "\" is invalid";
if (!move.isWellFormed(dstBoard.side) || !isMoveValid(dstBoard, move) ||
!isMoveLegal(dstBoard, move)) {
logError(UCI_SERVER) << "Move \"" << token << "\" is illegal";
return PollResult::NoData;
}
moves.push_back(move);
moveMake(dstBoard, move);
if (!isMoveLegal(dstBoard)) {
logError(UCI_SERVER) << "Move \"" << token << "\" is not legal";
return PollResult::NoData;
}
}

// Finally, after everything is parsed, just call client API
Expand Down
2 changes: 1 addition & 1 deletion src/core/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ ValidateResult Board::validate() {
}

// Check for `OpponentKingAttacked`
if (!isMoveLegal(*this)) {
if (!wasMoveLegal(*this)) {
return ValidateResult::OpponentKingAttacked;
}

Expand Down
5 changes: 5 additions & 0 deletions src/core/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ bool isCheck(const Board &b) {
return isCellAttacked(b, b.kingPos(c), invert(c));
}

bool wasMoveLegal(const Board &b) {
const Color c = b.side;
return !isCellAttacked(b, b.kingPos(invert(c)), c);
}

enum class PromoteGenPolicy { All, PromoteOnly, NoPromote };

struct SimpleGenFilter {
Expand Down
15 changes: 11 additions & 4 deletions src/core/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace SoFCore {
// Checks if the cell is attacked by any of the pieces of color `C`
//
// For optimization purposes, enpassant captures are not considered by this function, as the primary
// purpose of this function is to check for king attacks (e. g. in `isMoveLegal()`)
// purpose of this function is to check for king attacks (e. g. in `wasMoveLegal()`)
template <Color C>
bool isCellAttacked(const Board &b, coord_t coord);

Expand All @@ -41,7 +41,7 @@ inline bool isCellAttacked(const Board &b, const coord_t coord, const Color c) {
// Returns the set of pieces of color `C` which attack the given cell
//
// For optimization purposes, enpassant captures are not considered by this function, as the primary
// purpose of this function is to check for king attacks (e. g. in `isMoveLegal()`)
// purpose of this function is to check for king attacks (e. g. in `wasMoveLegal()`)
template <Color C>
bitboard_t cellAttackers(const Board &b, coord_t coord);

Expand All @@ -53,6 +53,13 @@ inline bitboard_t cellAttackers(const Board &b, const coord_t coord, const Color
// Returns `true` is the king of the moving side is currenly under check
bool isCheck(const Board &b);

// Returns `true` if the last move applied to the board `b` was legal. Note that it doesn't mean
// that you can apply any illegal moves to the board, the applied move must be still pseudo-legal.
//
// So, the typical use of this function is to make a pseudo-legal move, then check whether it is
// legal, and then unmake it if it's illegal.
bool wasMoveLegal(const Board &b);

// Move generator
class MoveGen {
public:
Expand All @@ -68,7 +75,7 @@ class MoveGen {
// are valid according to the rules of chess. The moves are written into `list`, and the return
// value indicates the number of moves generated.
//
// To generate only legal moves you can use `isMoveLegal()` function
// To generate only legal moves you can use `isMoveLegal()` or `wasMoveLegal()` function
size_t genAllMoves(Move *list) const;
size_t genSimpleMoves(Move *list) const;
size_t genSimpleMovesNoPromote(Move *list) const;
Expand Down Expand Up @@ -112,7 +119,7 @@ constexpr size_t BUFSZ_SIMPLE_PROMOTES = 32;
// behavior is undefined. Null moves are considered invalid by this function, as they cannot be
// returned by `genAllMoves()`.
//
// To check for legality you can use `isMoveLegal()` function
// To check for legality you can use `isMoveLegal()` or `wasMoveLegal()` function
bool isMoveValid(const Board &b, Move move);

// Returns `true` if the move `move` is legal
Expand Down
8 changes: 3 additions & 5 deletions src/gameset/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,12 @@ auto GameReader::tryReadCommand() -> std::optional<CommandResult> {
for (const auto &srcMove : SoFUtil::split(bodyBegin)) {
const Move move =
SoFCore::moveParse(srcMove.data(), srcMove.data() + srcMove.size(), *lastBoard_);
if (!move.isWellFormed(lastBoard_->side) || !isMoveValid(*lastBoard_, move)) {
return error("Move #" + std::to_string(moves.size() + 1) + " is invalid");
if (!move.isWellFormed(lastBoard_->side) || !isMoveValid(*lastBoard_, move) ||
!isMoveLegal(*lastBoard_, move)) {
return error("Move #" + std::to_string(moves.size() + 1) + " is illegal");
}
moves.push_back(move);
moveMake(*lastBoard_, move);
if (!isMoveLegal(*lastBoard_)) {
return error("Move #" + std::to_string(moves.size() + 1) + " is illegal");
}
if (canCaptureBoards()) {
capturedBoards_.push_back(*lastBoard_);
}
Expand Down
4 changes: 2 additions & 2 deletions src/search/private/job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ score_t Searcher::quiescenseSearch(score_t alpha, const score_t beta, const Eval
}
DIAGNOSTIC(dgnMoves.add(move);)
MoveMakeGuard guard(board_, move, tag);
if (!isMoveLegal(board_)) {
if (!wasMoveLegal(board_)) {
continue;
}
const score_t score = -quiescenseSearch(-beta, -alpha, guard.tag());
Expand Down Expand Up @@ -512,7 +512,7 @@ score_t Searcher::doSearch(int32_t depth, const size_t idepth, score_t alpha, co
DIAGNOSTIC(dgnMoves.add(move);)
MoveMakeGuard guard(board_, move, tag);
tt_.prefetch(board_.hash);
if (!isMoveLegal(board_)) {
if (!wasMoveLegal(board_)) {
continue;
}
if constexpr (Node != NodeKind::Root) {
Expand Down
8 changes: 2 additions & 6 deletions src/search/private/job_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,9 @@ static Move pickRandomMove(Board board) {
SoFUtil::randomShuffle(moves, moves + count);
for (size_t i = 0; i < count; ++i) {
const Move move = moves[i];
const SoFCore::MovePersistence persistence = moveMake(board, move);
if (!isMoveLegal(board)) {
moveUnmake(board, move, persistence);
continue;
if (isMoveLegal(board, move)) {
return move;
}
moveUnmake(board, move, persistence);
return move;
}
return Move::null();
}
Expand Down

0 comments on commit 2f72963

Please sign in to comment.