movegen.rs 23 KB

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