- if(color_of_piece(pc) != us )
- return MOVE_NONE;
-
- to = move_to(m);
-
- // En passant moves:
- if(move_is_ep(m)) {
-
- // The piece must be a pawn:
- if(type_of_piece(pc) != PAWN)
- return MOVE_NONE;
-
- // The destination square must be the en passant square:
- if(to != pos.ep_square())
- return MOVE_NONE;
-
- assert(pos.square_is_empty(to));
- assert(pos.piece_on(to - pawn_push(us)) == pawn_of_color(them));
-
- // The move is pseudo-legal. If it is legal, return it.
- if(pos.move_is_legal(m))
- return m;
- else
- return MOVE_NONE;
- }
-
- // Castling moves:
- else if(move_is_short_castle(m)) {
-
- // The piece must be a king:
- if(type_of_piece(pc) != KING)
- return MOVE_NONE;
-
- // The side to move must still have the right to castle kingside:
- if(!pos.can_castle_kingside(us))
- return MOVE_NONE;
-
- assert(from == pos.king_square(us));
- assert(to == pos.initial_kr_square(us));
- assert(pos.piece_on(to) == rook_of_color(us));
-
- Square g1 = relative_square(us, SQ_G1);
- Square f1 = relative_square(us, SQ_F1);
- Square s;
- bool illegal = false;
-
- for(s = Min(from, g1); s <= Max(from, g1); s++)
- if((s != from && s != to && !pos.square_is_empty(s)) ||
- pos.square_is_attacked(s, them))
- illegal = true;
- for(s = Min(to, f1); s <= Max(to, f1); s++)
- if(s != from && s != to && !pos.square_is_empty(s))
- illegal = true;
-
- if(!illegal)
- return m;
- else
- return MOVE_NONE;
- }
- else if(move_is_long_castle(m)) {
-
- // The piece must be a king:
- if(type_of_piece(pc) != KING)
- return MOVE_NONE;
-
- // The side to move must still have the right to castle kingside:
- if(!pos.can_castle_queenside(us))
- return MOVE_NONE;
-
- assert(from == pos.king_square(us));
- assert(to == pos.initial_qr_square(us));
- assert(pos.piece_on(to) == rook_of_color(us));
-
- Square c1 = relative_square(us, SQ_C1);
- Square d1 = relative_square(us, SQ_D1);
- Square s;
- bool illegal = false;
-
- for(s = Min(from, c1); s <= Max(from, c1); s++)
- if((s != from && s != to && !pos.square_is_empty(s)) ||
- pos.square_is_attacked(s, them))
- illegal = true;
- for(s = Min(to, d1); s <= Max(to, d1); s++)
- if(s != from && s != to && !pos.square_is_empty(s))
- illegal = true;
- if(square_file(to) == FILE_B &&
- (pos.piece_on(to + DELTA_W) == rook_of_color(them) ||
- pos.piece_on(to + DELTA_W) == queen_of_color(them)))
- illegal = true;
-
- if(!illegal)
- return m;
- else
- return MOVE_NONE;
- }
-
- // Normal moves
- else {
-
- // The destination square cannot be occupied by a friendly piece:
- if(pos.color_of_piece_on(to) == us)
- return MOVE_NONE;
-
- // Proceed according to the type of the moving piece.
- switch(type_of_piece(pc)) {
-
- case PAWN:
- // Pawn moves, as usual, are somewhat messy.
- if(us == WHITE) {
- // If the destination square is on the 8th rank, the move must be a
- // promotion.
- if(square_rank(to) == RANK_8 && !move_promotion(m))
- return MOVE_NONE;
-
- // Proceed according to the square delta between the source and
- // destionation squares.
- switch(to - from) {
-
- case DELTA_NW: case DELTA_NE:
- // Capture. The destination square must be occupied by an enemy piece
- // (en passant captures was handled earlier).
- if(pos.color_of_piece_on(to) != them)
- return MOVE_NONE;
+ if (color_of_piece(pc) != us)
+ return false;
+
+ // The destination square cannot be occupied by a friendly piece
+ if (pos.color_of_piece_on(to) == us)
+ return false;
+
+ // Handle the special case of a pawn move
+ if (type_of_piece(pc) == PAWN)
+ {
+ // Move direction must be compatible with pawn color
+ int direction = to - from;
+ if ((us == WHITE) != (direction > 0))
+ return false;
+
+ // We have already handled promotion moves, so destination
+ // cannot be on the 8/1th rank.
+ if (square_rank(to) == RANK_8 || square_rank(to) == RANK_1)
+ return false;
+
+ // Proceed according to the square delta between the origin and
+ // destination squares.
+ switch (direction)
+ {
+ case DELTA_NW:
+ case DELTA_NE:
+ case DELTA_SW:
+ case DELTA_SE:
+ // Capture. The destination square must be occupied by an enemy
+ // piece (en passant captures was handled earlier).
+ if (pos.color_of_piece_on(to) != them)
+ return false;