From 155bed18f5224d401ddf5b4b71d93d8a8c379b3c Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 3 Jul 2011 10:38:20 +0100 Subject: [PATCH] Use MoveList also in Position::move_is_pl_slow() And rename it in Position::move_is_legal() No functional change. Signed-off-by: Marco Costalba --- src/move.cpp | 14 +++++++------- src/movegen.h | 2 ++ src/position.cpp | 20 +++++++------------- src/position.h | 7 +++---- 4 files changed, 19 insertions(+), 24 deletions(-) diff --git a/src/move.cpp b/src/move.cpp index 247f8a2a..39344aae 100644 --- a/src/move.cpp +++ b/src/move.cpp @@ -77,11 +77,17 @@ Move move_from_uci(const Position& pos, const string& str) { /// move_to_san() takes a position and a move as input, where it is assumed -/// that the move is a legal move from the position. The return value is +/// that the move is a legal move for the position. The return value is /// a string containing the move in short algebraic notation. const string move_to_san(Position& pos, Move m) { + if (m == MOVE_NONE) + return "(none)"; + + if (m == MOVE_NULL) + return "(null)"; + assert(pos.is_ok()); assert(move_is_ok(m)); @@ -92,12 +98,6 @@ const string move_to_san(Position& pos, Move m) { PieceType pt = piece_type(pos.piece_on(from)); string san; - if (m == MOVE_NONE) - return "(none)"; - - if (m == MOVE_NULL) - return "(null)"; - if (move_is_castle(m)) san = (move_to(m) < move_from(m) ? "O-O-O" : "O-O"); else diff --git a/src/movegen.h b/src/movegen.h index fdac5d0b..0c781a6b 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -36,6 +36,8 @@ enum MoveType { template MoveStack* generate(const Position& pos, MoveStack* mlist); +/// The MoveList struct is a simple wrapper around generate(), sometimes comes +/// handy to use this class instead of the low level generate() function. template struct MoveList { diff --git a/src/position.cpp b/src/position.cpp index 7490953d..7f0e1447 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -545,20 +545,14 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const { } -/// Position::move_is_pl_slow() takes a move and tests whether the move -/// is pseudo legal. This version is not very fast and should be used -/// only in non time-critical paths. +/// Position::move_is_legal() takes a move and tests whether the move +/// is legal. This version is not very fast and should be used only +/// in non time-critical paths. -bool Position::move_is_pl_slow(const Move m) const { +bool Position::move_is_legal(const Move m) const { - MoveStack mlist[MAX_MOVES]; - MoveStack *cur, *last; - - last = in_check() ? generate(*this, mlist) - : generate(*this, mlist); - - for (cur = mlist; cur != last; cur++) - if (cur->move == m) + for (MoveList ml(*this); !ml.end(); ++ml) + if (ml.move() == m) return true; return false; @@ -580,7 +574,7 @@ bool Position::move_is_pl(const Move m) const { // Use a slower but simpler function for uncommon cases if (move_is_special(m)) - return move_is_pl_slow(m); + return move_is_legal(m); // Is not a promotion, so promotion piece must be empty if (promotion_piece_type(m) - 2 != PIECE_TYPE_NONE) diff --git a/src/position.h b/src/position.h index f326e4fb..37d55ed8 100644 --- a/src/position.h +++ b/src/position.h @@ -221,7 +221,7 @@ private: void put_piece(Piece p, Square s); void set_castle(int f, Square ksq, Square rsq); void set_castling_rights(char token); - bool move_is_pl_slow(const Move m) const; + bool move_is_legal(const Move m) const; // Helper functions for doing and undoing moves void do_capture_move(Key& key, PieceType capture, Color them, Square to, bool ep); @@ -449,15 +449,14 @@ inline bool Position::is_chess960() const { inline bool Position::move_is_capture_or_promotion(Move m) const { - assert(m != MOVE_NONE && m != MOVE_NULL); + assert(move_is_ok(m)); return move_is_special(m) ? !move_is_castle(m) : !square_is_empty(move_to(m)); } inline bool Position::move_is_capture(Move m) const { - assert(m != MOVE_NONE && m != MOVE_NULL); - // Note that castle is coded as "king captures the rook" + assert(move_is_ok(m)); return (!square_is_empty(move_to(m)) && !move_is_castle(m)) || move_is_ep(m); } -- 2.39.2