movegen.rs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. use bitboard::Bitboard;
  2. use bitboard::Square;
  3. use bitboard::*;
  4. use game::Game;
  5. use hash::{Cache, EntryType};
  6. pub type Side = bool;
  7. pub const WHITE: Side = false;
  8. pub const BLACK: Side = true;
  9. pub type PieceType = u8;
  10. pub const NO_PIECE: PieceType = 255;
  11. pub const PAWN: PieceType = 0;
  12. pub const KNIGHT: PieceType = 1;
  13. pub const BISHOP: PieceType = 2;
  14. pub const ROOK: PieceType = 3;
  15. pub const QUEEN: PieceType = 4;
  16. pub const KING: PieceType = 5;
  17. #[derive(Copy, Clone, Debug, PartialEq, Eq)]
  18. pub struct SimpleMove {
  19. pub from: Square,
  20. pub to: Square,
  21. }
  22. #[derive(Debug, Copy, Clone, PartialEq, Eq)]
  23. pub enum Move {
  24. Default { mov: SimpleMove, piece_type: PieceType, captured: Option<PieceType> },
  25. Castling { side: Side, left: bool },
  26. EnPassant { mov: SimpleMove, beaten: Square },
  27. Promotion { mov: SimpleMove, promote_to: PieceType, captured: Option<PieceType> },
  28. Nullmove
  29. }
  30. #[derive(Debug, Copy, Clone, PartialEq, Eq)]
  31. pub struct MoveUndo {
  32. pub castling_rights_before: [bool; 4],
  33. pub halfmoves_since_last_event_before: i8,
  34. pub en_passant_before: Option<u8>,
  35. pub mov: Move
  36. }
  37. impl Default for Move {
  38. fn default() -> Move {
  39. Move::Nullmove
  40. }
  41. }
  42. impl Move {
  43. pub fn parse_square(sq: &str) -> Option<Square> {
  44. let col = sq.chars().nth(0)?;
  45. let row = sq.chars().nth(1)?;
  46. Some((row as u8 - '1' as u8) * 8 + 7 - (col as u8 - 'a' as u8))
  47. }
  48. pub fn square_to_string(sq: Square) -> String {
  49. let col = 7 - (sq % 8);
  50. let row = sq / 8;
  51. let colchar = (('a' as u8) + col) as char;
  52. let rowchar = (('1' as u8) + row) as char;
  53. let chars = [colchar, rowchar];
  54. //println!("{} {}", col, row);
  55. chars.iter().collect()
  56. }
  57. pub fn is_null(&self) -> bool {
  58. *self == Move::Nullmove
  59. }
  60. pub fn to_string(&self) -> String {
  61. match self {
  62. Move::Default{ mov, piece_type: _pt, captured: _c } => {
  63. Self::square_to_string(mov.from) + &Self::square_to_string(mov.to)
  64. },
  65. Move::Castling{ side, left } => {
  66. match (side, left) {
  67. (&WHITE, false) => {
  68. Self::square_to_string(3) + &Self::square_to_string(1)
  69. },
  70. (&WHITE, true) => {
  71. Self::square_to_string(3) + &Self::square_to_string(5)
  72. },
  73. (&BLACK, false) => {
  74. Self::square_to_string(56 + 3) + &Self::square_to_string(56 + 1)
  75. },
  76. (&BLACK, true) => {
  77. Self::square_to_string(56 + 3) + &Self::square_to_string(56 + 5)
  78. }
  79. }
  80. },
  81. Move::EnPassant{ mov, beaten: _ } => {
  82. Self::square_to_string(mov.from) + &Self::square_to_string(mov.to)
  83. },
  84. Move::Promotion{ mov, promote_to, captured: _c } => {
  85. Self::square_to_string(mov.from) + &Self::square_to_string(mov.to) +
  86. if *promote_to == QUEEN { "q" }
  87. else if *promote_to == ROOK { "r" }
  88. else if *promote_to == KNIGHT { "n" }
  89. else if *promote_to == BISHOP { "b" }
  90. else { "" }
  91. },
  92. Move::Nullmove => {
  93. String::from("0000")
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * @brief Iterator to serialize bitboard bit for bit
  100. *
  101. * Extracts every bit out of the bitboard and returns each one as a separate bitboard.
  102. */
  103. pub struct BitboardIterator (pub Bitboard);
  104. impl Iterator for BitboardIterator {
  105. type Item = Bitboard;
  106. fn next(&mut self) -> Option<Bitboard> {
  107. if self.0 != 0 {
  108. let lsb = self.0 & (self.0.wrapping_neg());
  109. self.0 ^= lsb;
  110. //Some(lsb.trailing_zeros())
  111. Some(lsb)
  112. } else {
  113. None
  114. }
  115. }
  116. }
  117. impl SimpleMove {
  118. pub fn apply_to(self, bitboard: Bitboard) -> Bitboard {
  119. (bitboard & !from_square(self.from)) | from_square(self.to)
  120. }
  121. }
  122. pub fn generate_moves(game: &Game, side: Side) -> Vec<Move> {
  123. let mut moves: Vec<Move> = Vec::with_capacity(128);
  124. generate_pawn_pushes(game, side, &mut moves);
  125. generate_pawn_doublepushes(game, side, &mut moves);
  126. generate_pawn_captures(game, side, &mut moves);
  127. generate_promotions(game, side, &mut moves);
  128. generate_en_passant(game, side, &mut moves);
  129. generate_knight_moves(game, side, &mut moves, false);
  130. generate_bishop_moves(game, side, &mut moves, false);
  131. generate_rook_moves(game, side, &mut moves, false);
  132. generate_queen_moves(game, side, &mut moves, false);
  133. generate_king_moves(game, side, &mut moves, false);
  134. generate_castling_moves(game, side, &mut moves);
  135. return moves;
  136. }
  137. /**
  138. * generates moves that could possibly attack a king,
  139. * this function is used to check if the king is in check
  140. */
  141. pub fn generate_possattacking_moves(game: &Game, side: Side) -> Vec<Move> {
  142. let mut moves: Vec<Move> = Vec::with_capacity(32);
  143. generate_pawn_captures(game, side, &mut moves);
  144. generate_promotions(game, side, &mut moves);
  145. generate_knight_moves(game, side, &mut moves, true);
  146. generate_bishop_moves(game, side, &mut moves, true);
  147. generate_rook_moves(game, side, &mut moves, true);
  148. generate_queen_moves(game, side, &mut moves, true);
  149. generate_king_moves(game, side, &mut moves, true);
  150. return moves;
  151. }
  152. pub fn generate_legal_moves(game: &mut Game, side: Side) -> Vec<Move> {
  153. let moves = generate_moves(game, side);
  154. let check_legality = |mov: &Move| {
  155. let undo = game.apply(*mov);
  156. let legal = !is_check(game, side);
  157. game.undo_move(undo);
  158. return legal;
  159. };
  160. moves.into_iter().filter(check_legality).collect::<Vec<Move>>()
  161. }
  162. pub fn generate_attacking_moves(game: &Game, side: Side) -> Vec<Move> {
  163. let mut moves: Vec<Move> = Vec::with_capacity(32);
  164. generate_pawn_captures(game, side, &mut moves);
  165. generate_knight_moves(game, side, &mut moves, true);
  166. generate_bishop_moves(game, side, &mut moves, true);
  167. generate_rook_moves(game, side, &mut moves, true);
  168. generate_queen_moves(game, side, &mut moves, true);
  169. generate_king_moves(game, side, &mut moves, true);
  170. return moves;
  171. }
  172. pub fn sort_moves(game: &mut Game, hash: &mut Cache, move_list: &mut Vec<Move>) {
  173. move_list.sort_by_cached_key(|mov| {
  174. let undo = game.apply(*mov);
  175. if let Some(e) = hash.lookup(game) {
  176. game.undo_move(undo);
  177. if let EntryType::Value = e.entry_type {
  178. return e.value;
  179. }
  180. else if let EntryType::LowerBound = e.entry_type {
  181. return e.value - 1000;
  182. }
  183. else if let EntryType::UpperBound = e.entry_type {
  184. return e.value + 1000;
  185. }
  186. else {
  187. return 0;
  188. }
  189. }
  190. else {
  191. let eval = crate::evaluate::evaluate(game);
  192. game.undo_move(undo);
  193. return eval;
  194. }
  195. });
  196. }
  197. pub fn sort_moves_no_hash(game: &mut Game, move_list: &mut Vec<Move>) {
  198. move_list.sort_by_cached_key(|mov| {
  199. let undo = game.apply(*mov);
  200. let eval = crate::evaluate::evaluate(game);
  201. game.undo_move(undo);
  202. return eval;
  203. });
  204. }
  205. fn generate_pawn_pushes(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  206. let pawns = game.pawns(side);
  207. let others = game.get_all_side(!side);
  208. let pieces = game.get_all_side(side);
  209. let moved_pawns =
  210. match side {
  211. WHITE => north_one(pawns),
  212. BLACK => south_one(pawns)
  213. } & !(ROW_1 | ROW_8) & !(others | pieces);
  214. let iter = BitboardIterator(moved_pawns & !others);
  215. for p in iter {
  216. let origin = match side {
  217. WHITE => south_one(p),
  218. BLACK => north_one(p)
  219. };
  220. move_list.push(Move::Default { mov: SimpleMove { from: square(origin), to: square(p) }, piece_type: PAWN, captured: None });
  221. }
  222. }
  223. fn generate_pawn_doublepushes(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  224. let pawns = game.pawns(side) & match side { WHITE => ROW_2, BLACK => ROW_7 };
  225. let others = game.get_all_side(!side);
  226. let pieces = game.get_all_side(side);
  227. let all_pieces = others | pieces;
  228. let moved_pawns =
  229. match side {
  230. WHITE => north_one(north_one(pawns) & !all_pieces),
  231. BLACK => south_one(south_one(pawns) & !all_pieces)
  232. } & !all_pieces;
  233. let iter = BitboardIterator(moved_pawns & !others);
  234. for p in iter {
  235. let origin = match side {
  236. WHITE => south_one(south_one(p)),
  237. BLACK => north_one(north_one(p))
  238. };
  239. move_list.push(Move::Default { mov: SimpleMove { from: square(origin), to: square(p) }, piece_type: PAWN, captured: None });
  240. }
  241. }
  242. fn generate_pawn_captures(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  243. let pawns = game.pawns(side);
  244. let others = game.get_all_side(!side);
  245. let promotion_mask = ROW_1 | ROW_8;
  246. let pawn_iterator = BitboardIterator(pawns);
  247. for pawn in pawn_iterator {
  248. let left_cap = match side {
  249. WHITE => northeast_one(pawn),
  250. BLACK => southeast_one(pawn)
  251. };
  252. let right_cap = match side {
  253. WHITE => northwest_one(pawn),
  254. BLACK => southwest_one(pawn)
  255. };
  256. for cap in [left_cap, right_cap].iter() {
  257. if cap & others != 0 {
  258. let captured = game.find_piece(*cap);
  259. let simple_move = SimpleMove { from: square(pawn), to: square(*cap)};
  260. if cap & promotion_mask != 0 {
  261. move_list.push(Move::Promotion { mov: simple_move, promote_to: QUEEN, captured: captured });
  262. move_list.push(Move::Promotion { mov: simple_move, promote_to: ROOK, captured: captured });
  263. move_list.push(Move::Promotion { mov: simple_move, promote_to: BISHOP, captured: captured });
  264. move_list.push(Move::Promotion { mov: simple_move, promote_to: KNIGHT, captured: captured });
  265. } else {
  266. move_list.push(Move::Default { mov: simple_move, piece_type: PAWN, captured: captured });
  267. }
  268. }
  269. }
  270. }
  271. }
  272. fn generate_promotions(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  273. let pawns = game.pawns(side);
  274. let others = game.get_all_side(!side);
  275. let pieces = game.get_all_side(side);
  276. let moved_pawns =
  277. match side {
  278. WHITE => north_one(pawns),
  279. BLACK => south_one(pawns)
  280. } & (ROW_1 | ROW_8) & !(others | pieces);
  281. let iter = BitboardIterator(moved_pawns);
  282. for p in iter {
  283. let origin = match side {
  284. WHITE => south_one(p),
  285. BLACK => north_one(p)
  286. };
  287. let movement = SimpleMove { from: square(origin), to: square(p) };
  288. //move_list.push(Move::Default { mov: SimpleMove { from: square(origin), to: square(p) }, piece_type: PAWN });
  289. move_list.push(Move::Promotion { mov: movement, promote_to: QUEEN, captured: None });
  290. move_list.push(Move::Promotion { mov: movement, promote_to: ROOK, captured: None });
  291. move_list.push(Move::Promotion { mov: movement, promote_to: BISHOP, captured: None });
  292. move_list.push(Move::Promotion { mov: movement, promote_to: KNIGHT, captured: None });
  293. }
  294. }
  295. fn generate_en_passant(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  296. if let Some(ep) = game.en_passant {
  297. let pawncolumn = FILE_A >> ep;
  298. let pawnrow: Bitboard = match side {
  299. WHITE => ROW_5,
  300. BLACK => ROW_4
  301. };
  302. let beaten_pawn = pawncolumn & pawnrow;
  303. if beaten_pawn & game.get_piece(PAWN, !side) == 0 {
  304. return;
  305. }
  306. let pawn_left = (beaten_pawn << 1) & pawnrow;
  307. let pawn_right = (beaten_pawn >> 1) & pawnrow;
  308. let attacking_pawns = game.get_piece(PAWN, side);
  309. let target_square = pawncolumn & match side {
  310. WHITE => ROW_6,
  311. BLACK => ROW_3
  312. };
  313. if pawn_left & attacking_pawns != 0 {
  314. let mov = Move::EnPassant{ mov: SimpleMove{ from: square(pawn_left), to: square(target_square) }, beaten: square(beaten_pawn) };
  315. move_list.push(mov);
  316. }
  317. if pawn_right & attacking_pawns != 0 {
  318. let mov = Move::EnPassant{ mov: SimpleMove{ from: square(pawn_right), to: square(target_square) }, beaten: square(beaten_pawn) };
  319. move_list.push(mov);
  320. }
  321. }
  322. }
  323. fn generate_knight_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  324. let friends = game.get_all_side(side);
  325. let others = game.get_all_side(!side);
  326. let knights = game.knights(side);
  327. let knight_iter = BitboardIterator(knights);
  328. for k in knight_iter {
  329. let cap_targets = BitboardIterator(get_knight_targets(square(k)) & (!friends & others));
  330. let nocap_targets = BitboardIterator(get_knight_targets(square(k)) & (!friends & !others));
  331. for target in cap_targets {
  332. let simple_move = SimpleMove { from: square(k), to: square(target) };
  333. let captured = game.find_piece(target);
  334. move_list.push(Move::Default{ mov: simple_move, piece_type: KNIGHT, captured });
  335. }
  336. if !captures_only {
  337. for target in nocap_targets {
  338. let simple_move = SimpleMove { from: square(k), to: square(target) };
  339. move_list.push(Move::Default{ mov: simple_move, piece_type: KNIGHT, captured: None });
  340. }
  341. }
  342. }
  343. }
  344. pub fn get_knight_targets(square: Square) -> Bitboard {
  345. /// @brief array containing the possible targets for a knight on each square.
  346. /// entry at index i is a bitboard with all bits set where a knight on square i can jump
  347. const TARGETS: [Bitboard; 64] = [132096, 329728, 659712, 1319424, 2638848, 5277696, 10489856,
  348. 4202496, 33816580, 84410376, 168886289, 337772578, 675545156, 1351090312, 2685403152,
  349. 1075839008, 8657044482, 21609056261, 43234889994, 86469779988, 172939559976, 345879119952,
  350. 687463207072, 275414786112, 2216203387392, 5531918402816, 11068131838464, 22136263676928,
  351. 44272527353856, 88545054707712, 175990581010432, 70506185244672, 567348067172352,
  352. 1416171111120896, 2833441750646784, 5666883501293568, 11333767002587136, 22667534005174272,
  353. 45053588738670592, 18049583422636032, 145241105196122112, 362539804446949376,
  354. 725361088165576704, 1450722176331153408, 2901444352662306816, 5802888705324613632,
  355. 11533718717099671552, 4620693356194824192, 288234782788157440, 576469569871282176,
  356. 1224997833292120064, 2449995666584240128, 4899991333168480256, 9799982666336960512,
  357. 1152939783987658752, 2305878468463689728, 1128098930098176, 2257297371824128, 4796069720358912,
  358. 9592139440717824, 19184278881435648, 38368557762871296, 4679521487814656, 9077567998918656];
  359. TARGETS[square as usize]
  360. }
  361. fn generate_bishop_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  362. let friends = game.get_all_side(side);
  363. let others = game.get_all_side(!side);
  364. generate_sliding_moves(game, friends, others, game.bishops(side), BISHOP, false, true, move_list, captures_only);
  365. }
  366. fn generate_rook_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  367. let friends = game.get_all_side(side);
  368. let others = game.get_all_side(!side);
  369. generate_sliding_moves(game, friends, others, game.rooks(side), ROOK, true, false, move_list, captures_only);
  370. }
  371. fn generate_queen_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  372. let friends = game.get_all_side(side);
  373. let others = game.get_all_side(!side);
  374. generate_sliding_moves(game, friends, others, game.queens(side), QUEEN, true, true, move_list, captures_only);
  375. }
  376. fn generate_sliding_moves(game: &Game, friends: Bitboard, others: Bitboard, pieces: Bitboard, piece_type: PieceType,
  377. straight: bool, diagonal: bool, move_list: &mut Vec<Move>, captures_only: bool) {
  378. let pieces_iter = BitboardIterator(pieces);
  379. let mask = if captures_only { others } else { !(0 as Bitboard) };
  380. for piece in pieces_iter {
  381. let destinations = generate_sliding_destinations(friends, others, piece, straight, diagonal);
  382. let dest_iter = BitboardIterator(destinations & mask);
  383. for dest in dest_iter {
  384. let smove = SimpleMove{ from: square(piece), to: square(dest) };
  385. if dest & others != 0 {
  386. move_list.push(Move::Default { mov: smove, piece_type: piece_type, captured: game.find_piece(dest) });
  387. }
  388. else {
  389. move_list.push(Move::Default { mov: smove, piece_type: piece_type, captured: None });
  390. }
  391. }
  392. }
  393. }
  394. fn generate_sliding_destinations(friends: Bitboard, others: Bitboard,
  395. piece: Bitboard, straight: bool,
  396. diagonal: bool) -> Bitboard {
  397. let occupied = friends | others;
  398. let straights = [north_one, south_one, east_one, west_one];
  399. let diagonals = [northeast_one, southeast_one, northwest_one, southwest_one];
  400. let mut result: Bitboard = 0;
  401. if straight {
  402. for direction in straights.iter() {
  403. result |= generate_direction(piece, *direction, occupied);
  404. }
  405. }
  406. if diagonal {
  407. for direction in diagonals.iter() {
  408. result |= generate_direction(piece, *direction, occupied);
  409. }
  410. }
  411. return result & !friends;
  412. }
  413. fn generate_direction(piece: Bitboard, direction: fn(Bitboard) -> Bitboard, occupied: Bitboard) -> Bitboard {
  414. let mut result: Bitboard = 0;
  415. let mut b = piece;
  416. loop {
  417. b = direction(b);
  418. result |= b;
  419. if b & (occupied) != 0 || b == 0 {
  420. break;
  421. }
  422. }
  423. return result;
  424. }
  425. fn generate_king_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  426. let friends = game.get_all_side(side);
  427. let others = game.get_all_side(!side);
  428. let king = game.kings(side);
  429. //assert_eq!(king.count_ones(), 1); // assume only one king per player
  430. if king.count_ones() != 1 { return; }
  431. let mask =
  432. if captures_only {
  433. !friends & others
  434. }
  435. else {
  436. !friends
  437. };
  438. let area = (north_one(king)
  439. | south_one(king)
  440. | east_one(king)
  441. | west_one(king)
  442. | northeast_one(king)
  443. | northwest_one(king)
  444. | southwest_one(king)
  445. | southeast_one(king))
  446. & mask;
  447. let area_iter = BitboardIterator(area);
  448. for destination in area_iter {
  449. let simple_move = SimpleMove{ from: square(king), to: square(destination) };
  450. let captured = game.find_piece(destination);
  451. move_list.push(Move::Default { mov: simple_move, piece_type: KING, captured });
  452. }
  453. }
  454. fn generate_castling_moves(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  455. let c1 = Move::Castling{ side: side, left: false };
  456. let c2 = Move::Castling{ side: side, left: true };
  457. let legal_castlings: &[bool] = match side {
  458. WHITE => &game.castling_rights[0..2],
  459. BLACK => &game.castling_rights[2..4],
  460. };
  461. let all_pieces = game.get_all_side(WHITE) | game.get_all_side(BLACK);
  462. // kingside castling
  463. if legal_castlings[0] {
  464. //info!("possible castling? {} {} {}", game.get_piece(KING, side), game.get_piece(ROOK, side), ((game.get_piece(KING, side) >> 3) & game.get_piece(ROOK, side)));
  465. if ((game.get_piece(KING, side) >> 3) & game.get_piece(ROOK, side)) != 0 &&
  466. ((game.get_piece(KING, side) >> 1) | (game.get_piece(KING, side) >> 2)) & all_pieces == 0 {
  467. let mut tg1 = game.clone();
  468. *tg1.get_piece_mut(KING, side) = game.get_piece(KING, side) >> 1;
  469. if !is_check(game, side) && !is_check(&tg1, side) {
  470. move_list.push(c1);
  471. }
  472. }
  473. }
  474. // queenside
  475. if legal_castlings[1] {
  476. //info!("possible castling? {} {} {}", game.get_piece(KING, side), game.get_piece(ROOK, side), ((game.get_piece(KING, side) >> 3) & game.get_piece(ROOK, side)));
  477. if ((game.get_piece(KING, side) << 4) & game.get_piece(ROOK, side)) != 0 &&
  478. ((game.get_piece(KING, side) << 1) | (game.get_piece(KING, side) << 2) | (game.get_piece(KING, side) << 3)) & all_pieces == 0 {
  479. let mut tg1 = game.clone();
  480. let mut tg2 = game.clone();
  481. *tg1.get_piece_mut(KING, side) = game.get_piece(KING, side) << 1;
  482. *tg2.get_piece_mut(KING, side) = game.get_piece(KING, side) << 2;
  483. if !is_check(game, side) && !is_check(&tg1, side) && !is_check(&tg2, side) {
  484. move_list.push(c2);
  485. }
  486. }
  487. }
  488. }
  489. pub fn is_check(game: &Game, side: Side) -> bool {
  490. let king = game.get_piece(KING, side);
  491. let king_square = square(king);
  492. let friends = game.get_all_side(side);
  493. let others = game.get_all_side(!side);
  494. let knights = get_knight_targets(king_square);
  495. if knights & game.get_piece(KNIGHT, !side) != 0 {
  496. return true;
  497. }
  498. let diagonal = generate_sliding_destinations(friends, others, king, false, true);
  499. let straight = generate_sliding_destinations(friends, others, king, true, false);
  500. if diagonal & game.get_piece(BISHOP, !side) != 0 {
  501. return true;
  502. }
  503. if diagonal & game.get_piece(QUEEN, !side) != 0 {
  504. return true;
  505. }
  506. if straight & game.get_piece(ROOK, !side) != 0 {
  507. return true;
  508. }
  509. if straight & game.get_piece(QUEEN, !side) != 0 {
  510. return true;
  511. }
  512. if side == BLACK {
  513. if ((southeast_one(king) | southwest_one(king)) & game.get_piece(PAWN, !side)) != 0 {
  514. return true;
  515. }
  516. }
  517. else {
  518. if ((northeast_one(king) | northwest_one(king)) & game.get_piece(PAWN, !side)) != 0 {
  519. return true;
  520. }
  521. }
  522. let king_area = north_one(king)
  523. | south_one(king)
  524. | east_one(king)
  525. | west_one(king)
  526. | northeast_one(king)
  527. | northwest_one(king)
  528. | southwest_one(king)
  529. | southeast_one(king);
  530. if king_area & game.get_piece(KING, !side) != 0 {
  531. return true;
  532. }
  533. return false;
  534. }
  535. /**
  536. * checks if side's king is in check
  537. */
  538. pub fn is_check_old(game: &Game, side: Side) -> bool {
  539. let king = game.get_piece(KING, side);
  540. let king_square = square(king);
  541. let possible_attacks = generate_possattacking_moves(game, !side);
  542. for mov in possible_attacks {
  543. if let Move::Default{ mov, piece_type: _, captured: _ } = mov {
  544. if mov.to == king_square {
  545. if !is_check(game, side) {
  546. panic!("incorrect non-check {}", game.beautiful_print());
  547. }
  548. return true;
  549. }
  550. }
  551. else if let Move::Promotion{ mov, promote_to: _, captured: _ } = mov {
  552. if mov.to == king_square {
  553. if !is_check(game, side) {
  554. panic!("incorrect non-check {}", game.beautiful_print());
  555. }
  556. return true;
  557. }
  558. }
  559. }
  560. if is_check(game, side) {
  561. panic!("incorrect check {}", game.beautiful_print());
  562. }
  563. false
  564. }
  565. /*
  566. #[cfg(test)]
  567. mod tests {
  568. use search::Game;
  569. use movegen::*;
  570. #[test]
  571. fn pawn_pushes() {
  572. let mut game: Game = Game::default();
  573. let pawn_moves = generate_pawn_moves(&game, WHITE);
  574. assert_eq!(pawn_moves, ());
  575. }
  576. }
  577. */