1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
// move_evasion.cpp
// includes
#include "attack.h"
#include "board.h"
#include "colour.h"
#include "list.h"
#include "move.h"
#include "move_evasion.h"
#include "move_gen.h"
#include "piece.h"
#include "util.h"
// prototypes
static bool gen_evasions (list_t * list, const board_t * board, const attack_t * attack, bool legal, bool stop);
static bool add_pawn_moves (list_t * list, const board_t * board, int to, bool legal, bool stop);
static bool add_pawn_captures (list_t * list, const board_t * board, int to, bool legal, bool stop);
static bool add_piece_moves (list_t * list, const board_t * board, int to, bool legal, bool stop);
// functions
// gen_legal_evasions()
void gen_legal_evasions(list_t * list, const board_t * board, const attack_t * attack) {
ASSERT(list!=NULL);
ASSERT(board!=NULL);
ASSERT(attack!=NULL);
gen_evasions(list,board,attack,true,false);
// debug
ASSERT(list_is_ok(list));
}
// gen_pseudo_evasions()
void gen_pseudo_evasions(list_t * list, const board_t * board, const attack_t * attack) {
ASSERT(list!=NULL);
ASSERT(board!=NULL);
ASSERT(attack!=NULL);
gen_evasions(list,board,attack,false,false);
// debug
ASSERT(list_is_ok(list));
}
// legal_evasion_exist()
bool legal_evasion_exist(const board_t * board, const attack_t * attack) {
list_t list[1]; // dummy
ASSERT(board!=NULL);
ASSERT(attack!=NULL);
return gen_evasions(list,board,attack,true,true);
}
// gen_evasions()
static bool gen_evasions(list_t * list, const board_t * board, const attack_t * attack, bool legal, bool stop) {
int me, opp;
int opp_flag;
int king;
const inc_t * inc_ptr;
int inc;
int to;
int piece;
ASSERT(list!=NULL);
ASSERT(board!=NULL);
ASSERT(attack!=NULL);
ASSERT(legal==true||legal==false);
ASSERT(stop==true||stop==false);
ASSERT(board_is_check(board));
ASSERT(ATTACK_IN_CHECK(attack));
// init
LIST_CLEAR(list);
me = board->turn;
opp = COLOUR_OPP(me);
opp_flag = COLOUR_FLAG(opp);
king = KING_POS(board,me);
for (inc_ptr = KingInc; (inc=*inc_ptr) != IncNone; inc_ptr++) {
if (inc != -attack->di[0] && inc != -attack->di[1]) { // avoid escaping along a check line
to = king + inc;
piece = board->square[to];
if (piece == Empty || FLAG_IS(piece,opp_flag)) {
if (!legal || !is_attacked(board,to,opp)) {
if (stop) return true;
LIST_ADD(list,MOVE_MAKE(king,to));
}
}
}
}
if (attack->dn >= 2) return false; // double check, we are done
// single check
ASSERT(attack->dn==1);
// capture the checking piece
if (add_pawn_captures(list,board,attack->ds[0],legal,stop) && stop) return true;
if (add_piece_moves(list,board,attack->ds[0],legal,stop) && stop) return true;
// interpose a piece
inc = attack->di[0];
if (inc != IncNone) { // line
for (to = king+inc; to != attack->ds[0]; to += inc) {
ASSERT(SQUARE_IS_OK(to));
ASSERT(board->square[to]==Empty);
if (add_pawn_moves(list,board,to,legal,stop) && stop) return true;
if (add_piece_moves(list,board,to,legal,stop) && stop) return true;
}
}
return false;
}
// add_pawn_moves()
static bool add_pawn_moves(list_t * list, const board_t * board, int to, bool legal, bool stop) {
int me;
int inc;
int pawn;
int from;
int piece;
ASSERT(list!=NULL);
ASSERT(board!=NULL);
ASSERT(SQUARE_IS_OK(to));
ASSERT(legal==true||legal==false);
ASSERT(stop==true||stop==false);
ASSERT(board->square[to]==Empty);
me = board->turn;
inc = PAWN_MOVE_INC(me);
pawn = PAWN_MAKE(me);
from = to - inc;
piece = board->square[from];
if (piece == pawn) { // single push
if (!legal || !is_pinned(board,from,me)) {
if (stop) return true;
add_pawn_move(list,from,to);
}
} else if (piece == Empty && PAWN_RANK(to,me) == Rank4) { // double push
from = to - (2*inc);
if (board->square[from] == pawn) {
if (!legal || !is_pinned(board,from,me)) {
if (stop) return true;
ASSERT(!SQUARE_IS_PROMOTE(to));
LIST_ADD(list,MOVE_MAKE(from,to));
}
}
}
return false;
}
// add_pawn_captures()
static bool add_pawn_captures(list_t * list, const board_t * board, int to, bool legal, bool stop) {
int me;
int inc;
int pawn;
int from;
ASSERT(list!=NULL);
ASSERT(board!=NULL);
ASSERT(SQUARE_IS_OK(to));
ASSERT(legal==true||legal==false);
ASSERT(stop==true||stop==false);
ASSERT(COLOUR_IS(board->square[to],COLOUR_OPP(board->turn)));
me = board->turn;
inc = PAWN_MOVE_INC(me);
pawn = PAWN_MAKE(me);
from = to - (inc-1);
if (board->square[from] == pawn) {
if (!legal || !is_pinned(board,from,me)) {
if (stop) return true;
add_pawn_move(list,from,to);
}
}
from = to - (inc+1);
if (board->square[from] == pawn) {
if (!legal || !is_pinned(board,from,me)) {
if (stop) return true;
add_pawn_move(list,from,to);
}
}
if (board->ep_square != SquareNone
&& to == SQUARE_EP_DUAL(board->ep_square)) {
ASSERT(PAWN_RANK(to,me)==Rank5);
ASSERT(PIECE_IS_PAWN(board->square[to]));
to = board->ep_square;
ASSERT(PAWN_RANK(to,me)==Rank6);
ASSERT(board->square[to]==Empty);
from = to - (inc-1);
if (board->square[from] == pawn) {
if (!legal || !is_pinned(board,from,me)) {
if (stop) return true;
ASSERT(!SQUARE_IS_PROMOTE(to));
LIST_ADD(list,MOVE_MAKE_FLAGS(from,to,MoveEnPassant));
}
}
from = to - (inc+1);
if (board->square[from] == pawn) {
if (!legal || !is_pinned(board,from,me)) {
if (stop) return true;
ASSERT(!SQUARE_IS_PROMOTE(to));
LIST_ADD(list,MOVE_MAKE_FLAGS(from,to,MoveEnPassant));
}
}
}
return false;
}
// add_piece_moves()
static bool add_piece_moves(list_t * list, const board_t * board, int to, bool legal, bool stop) {
int me;
const sq_t * ptr;
int from, piece;
ASSERT(list!=NULL);
ASSERT(board!=NULL);
ASSERT(SQUARE_IS_OK(to));
ASSERT(legal==true||legal==false);
ASSERT(stop==true||stop==false);
me = board->turn;
for (ptr = &board->piece[me][1]; (from=*ptr) != SquareNone; ptr++) { // HACK: no king
piece = board->square[from];
if (PIECE_ATTACK(board,piece,from,to)) {
if (!legal || !is_pinned(board,from,me)) {
if (stop) return true;
LIST_ADD(list,MOVE_MAKE(from,to));
}
}
}
return false;
}
// end of move_evasion.cpp
|