/// 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));
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
template<MoveType>
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<MoveType T>
struct MoveList {
}
-/// 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<MV_EVASION>(*this, mlist)
- : generate<MV_NON_EVASION>(*this, mlist);
-
- for (cur = mlist; cur != last; cur++)
- if (cur->move == m)
+ for (MoveList<MV_LEGAL> ml(*this); !ml.end(); ++ml)
+ if (ml.move() == m)
return true;
return false;
// 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)
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);
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);
}