// Tries new position to see if it is a valid move. The move is valid if the // destination square is on the board, and either empty or containing an // opposing piece. Valid moves are added to the move list. // // Returns true if square is on the board and EMPTY. If it has an opposing piece, // the move is added to the move list, but we return false. Good for generating // moves for rook/bishop/queen. // bool try_square( piece_colour pc, const row_col& pos, const row_col& newpos, const board& b, move* movelist, int& num_moves) { if (newpos.is_out_of_bounds()) return false; square s = b.get(newpos); if (get_piece_colour(s) == pc) return false; // piece on destination square is the same as our colour movelist[num_moves] = move(pos, newpos, b); num_moves++; return is_empty(s); }
int check_captures(position src, position dst, board const& board, piece what, OutIter out) { piece::color_t const player = what.color(); board::mask supporters; int score = 0; if (trap(dst)) { supporters = neighbourhood(dst) & board.player(player); supporters[src] = false; // Check for piece stepping into a trap. if (supporters.empty()) { *out++ = elementary_step::make_capture(dst, what); score -= CAPTURE_COEF * cost(what, src); } } // Check for piece abandoning another one standing on a trap. if (neighbourhood(src) & board::mask::TRAPS && !trap(dst)) { position const trap = (neighbourhood(src) & board::mask::TRAPS).first_set(); boost::optional<piece> const trapped = board.get(trap); if (trapped) { piece::color_t const trapped_player = trapped->color(); supporters = neighbourhood(trap) & board.player(trapped_player); supporters[src] = false; if (supporters.empty()) { *out++ = elementary_step::make_capture(trap, *trapped); score += (trapped->color() == what.color() ? -1 : +1) * CAPTURE_COEF * cost(*trapped, trap); } } } return score; }
void gen_moves(const board& b, piece_colour pc, move* movelist, int& num_moves) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { row_col rc(i, j); square s = b.get(rc); if (pc == get_piece_colour(s)) { piece_type pt = get_piece_type(s); switch (pt) { case NONE: assert(0); // only here if square contains one of our pieces break; case PAWN: pawn_moves(pc, rc, b, movelist, num_moves); break; case KNIGHT: knight_moves(pc, rc, b, movelist, num_moves); break; case BISHOP: bishop_moves(pc, rc, b, movelist, num_moves); break; case ROOK: rook_moves(pc, rc, b, movelist, num_moves); break; case KING: king_moves(pc, rc, b, movelist, num_moves); break; case QUEEN: queen_moves(pc, rc, b, movelist, num_moves); break; } } } } }
move::move(const row_col& f, const row_col& t, const board& b) : from(f), to(t), from_sq(b.get(f)), to_sq(b.get(t)), score(0) { }