movegen.rs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 crate::evaluate::evaluate(game);
  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. pub fn sort_quiescence(game: &mut Game, move_list: &mut Vec<Move>) {
  206. let value_piece = |pt| {
  207. match pt {
  208. PAWN => 1,
  209. KNIGHT => 2,
  210. BISHOP => 3,
  211. ROOK => 4,
  212. QUEEN => 5,
  213. KING => 6,
  214. _ => 0,
  215. }
  216. };
  217. move_list.sort_by_cached_key(|mov| {
  218. match *mov {
  219. Move::Default{ mov: _, piece_type, captured: Some(c) } => {
  220. value_piece(piece_type) - value_piece(c)
  221. },
  222. Move::Promotion{ mov: _, promote_to: _, captured: Some(c) } => {
  223. value_piece(PAWN) - value_piece(c)
  224. },
  225. Move::EnPassant{ mov: _, beaten } => {
  226. value_piece(PAWN) - value_piece(game.get_square(beaten).0)
  227. },
  228. _ => 0
  229. }
  230. });
  231. }
  232. fn generate_pawn_pushes(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  233. let pawns = game.pawns(side);
  234. let others = game.get_all_side(!side);
  235. let pieces = game.get_all_side(side);
  236. let moved_pawns =
  237. match side {
  238. WHITE => north_one(pawns),
  239. BLACK => south_one(pawns)
  240. } & !(ROW_1 | ROW_8) & !(others | pieces);
  241. let iter = BitboardIterator(moved_pawns & !others);
  242. for p in iter {
  243. let origin = match side {
  244. WHITE => south_one(p),
  245. BLACK => north_one(p)
  246. };
  247. move_list.push(Move::Default { mov: SimpleMove { from: square(origin), to: square(p) }, piece_type: PAWN, captured: None });
  248. }
  249. }
  250. fn generate_pawn_doublepushes(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  251. let pawns = game.pawns(side) & match side { WHITE => ROW_2, BLACK => ROW_7 };
  252. let others = game.get_all_side(!side);
  253. let pieces = game.get_all_side(side);
  254. let all_pieces = others | pieces;
  255. let moved_pawns =
  256. match side {
  257. WHITE => north_one(north_one(pawns) & !all_pieces),
  258. BLACK => south_one(south_one(pawns) & !all_pieces)
  259. } & !all_pieces;
  260. let iter = BitboardIterator(moved_pawns & !others);
  261. for p in iter {
  262. let origin = match side {
  263. WHITE => south_one(south_one(p)),
  264. BLACK => north_one(north_one(p))
  265. };
  266. move_list.push(Move::Default { mov: SimpleMove { from: square(origin), to: square(p) }, piece_type: PAWN, captured: None });
  267. }
  268. }
  269. fn generate_pawn_captures(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 promotion_mask = ROW_1 | ROW_8;
  273. let pawn_iterator = BitboardIterator(pawns);
  274. for pawn in pawn_iterator {
  275. let left_cap = match side {
  276. WHITE => northeast_one(pawn),
  277. BLACK => southeast_one(pawn)
  278. };
  279. let right_cap = match side {
  280. WHITE => northwest_one(pawn),
  281. BLACK => southwest_one(pawn)
  282. };
  283. for cap in [left_cap, right_cap].iter() {
  284. if cap & others != 0 {
  285. let captured = game.find_piece(*cap);
  286. let simple_move = SimpleMove { from: square(pawn), to: square(*cap)};
  287. if cap & promotion_mask != 0 {
  288. move_list.push(Move::Promotion { mov: simple_move, promote_to: QUEEN, captured: captured });
  289. move_list.push(Move::Promotion { mov: simple_move, promote_to: ROOK, captured: captured });
  290. move_list.push(Move::Promotion { mov: simple_move, promote_to: BISHOP, captured: captured });
  291. move_list.push(Move::Promotion { mov: simple_move, promote_to: KNIGHT, captured: captured });
  292. } else {
  293. move_list.push(Move::Default { mov: simple_move, piece_type: PAWN, captured: captured });
  294. }
  295. }
  296. }
  297. }
  298. }
  299. fn generate_promotions(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  300. let pawns = game.pawns(side);
  301. let others = game.get_all_side(!side);
  302. let pieces = game.get_all_side(side);
  303. let moved_pawns =
  304. match side {
  305. WHITE => north_one(pawns),
  306. BLACK => south_one(pawns)
  307. } & (ROW_1 | ROW_8) & !(others | pieces);
  308. let iter = BitboardIterator(moved_pawns);
  309. for p in iter {
  310. let origin = match side {
  311. WHITE => south_one(p),
  312. BLACK => north_one(p)
  313. };
  314. let movement = SimpleMove { from: square(origin), to: square(p) };
  315. //move_list.push(Move::Default { mov: SimpleMove { from: square(origin), to: square(p) }, piece_type: PAWN });
  316. move_list.push(Move::Promotion { mov: movement, promote_to: QUEEN, captured: None });
  317. move_list.push(Move::Promotion { mov: movement, promote_to: ROOK, captured: None });
  318. move_list.push(Move::Promotion { mov: movement, promote_to: BISHOP, captured: None });
  319. move_list.push(Move::Promotion { mov: movement, promote_to: KNIGHT, captured: None });
  320. }
  321. }
  322. fn generate_en_passant(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  323. if let Some(ep) = game.en_passant {
  324. let pawncolumn = FILE_A >> ep;
  325. let pawnrow: Bitboard = match side {
  326. WHITE => ROW_5,
  327. BLACK => ROW_4
  328. };
  329. let beaten_pawn = pawncolumn & pawnrow;
  330. if beaten_pawn & game.get_piece(PAWN, !side) == 0 {
  331. return;
  332. }
  333. let pawn_left = (beaten_pawn << 1) & pawnrow;
  334. let pawn_right = (beaten_pawn >> 1) & pawnrow;
  335. let attacking_pawns = game.get_piece(PAWN, side);
  336. let target_square = pawncolumn & match side {
  337. WHITE => ROW_6,
  338. BLACK => ROW_3
  339. };
  340. if pawn_left & attacking_pawns != 0 {
  341. let mov = Move::EnPassant{ mov: SimpleMove{ from: square(pawn_left), to: square(target_square) }, beaten: square(beaten_pawn) };
  342. move_list.push(mov);
  343. }
  344. if pawn_right & attacking_pawns != 0 {
  345. let mov = Move::EnPassant{ mov: SimpleMove{ from: square(pawn_right), to: square(target_square) }, beaten: square(beaten_pawn) };
  346. move_list.push(mov);
  347. }
  348. }
  349. }
  350. fn generate_knight_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  351. let friends = game.get_all_side(side);
  352. let others = game.get_all_side(!side);
  353. let knights = game.knights(side);
  354. let knight_iter = BitboardIterator(knights);
  355. for k in knight_iter {
  356. let cap_targets = BitboardIterator(get_knight_targets(square(k)) & (!friends & others));
  357. let nocap_targets = BitboardIterator(get_knight_targets(square(k)) & (!friends & !others));
  358. for target in cap_targets {
  359. let simple_move = SimpleMove { from: square(k), to: square(target) };
  360. let captured = game.find_piece(target);
  361. move_list.push(Move::Default{ mov: simple_move, piece_type: KNIGHT, captured });
  362. }
  363. if !captures_only {
  364. for target in nocap_targets {
  365. let simple_move = SimpleMove { from: square(k), to: square(target) };
  366. move_list.push(Move::Default{ mov: simple_move, piece_type: KNIGHT, captured: None });
  367. }
  368. }
  369. }
  370. }
  371. pub fn get_knight_targets(square: Square) -> Bitboard {
  372. /// @brief array containing the possible targets for a knight on each square.
  373. /// entry at index i is a bitboard with all bits set where a knight on square i can jump
  374. const TARGETS: [Bitboard; 64] = [132096, 329728, 659712, 1319424, 2638848, 5277696, 10489856,
  375. 4202496, 33816580, 84410376, 168886289, 337772578, 675545156, 1351090312, 2685403152,
  376. 1075839008, 8657044482, 21609056261, 43234889994, 86469779988, 172939559976, 345879119952,
  377. 687463207072, 275414786112, 2216203387392, 5531918402816, 11068131838464, 22136263676928,
  378. 44272527353856, 88545054707712, 175990581010432, 70506185244672, 567348067172352,
  379. 1416171111120896, 2833441750646784, 5666883501293568, 11333767002587136, 22667534005174272,
  380. 45053588738670592, 18049583422636032, 145241105196122112, 362539804446949376,
  381. 725361088165576704, 1450722176331153408, 2901444352662306816, 5802888705324613632,
  382. 11533718717099671552, 4620693356194824192, 288234782788157440, 576469569871282176,
  383. 1224997833292120064, 2449995666584240128, 4899991333168480256, 9799982666336960512,
  384. 1152939783987658752, 2305878468463689728, 1128098930098176, 2257297371824128, 4796069720358912,
  385. 9592139440717824, 19184278881435648, 38368557762871296, 4679521487814656, 9077567998918656];
  386. TARGETS[square as usize]
  387. }
  388. fn generate_bishop_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  389. let friends = game.get_all_side(side);
  390. let others = game.get_all_side(!side);
  391. generate_sliding_moves(game, friends, others, game.bishops(side), BISHOP, false, true, move_list, captures_only);
  392. }
  393. fn generate_rook_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  394. let friends = game.get_all_side(side);
  395. let others = game.get_all_side(!side);
  396. generate_sliding_moves(game, friends, others, game.rooks(side), ROOK, true, false, move_list, captures_only);
  397. }
  398. fn generate_queen_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  399. let friends = game.get_all_side(side);
  400. let others = game.get_all_side(!side);
  401. generate_sliding_moves(game, friends, others, game.queens(side), QUEEN, true, true, move_list, captures_only);
  402. }
  403. fn generate_sliding_moves(game: &Game, friends: Bitboard, others: Bitboard, pieces: Bitboard, piece_type: PieceType,
  404. straight: bool, diagonal: bool, move_list: &mut Vec<Move>, captures_only: bool) {
  405. let pieces_iter = BitboardIterator(pieces);
  406. let mask = if captures_only { others } else { !(0 as Bitboard) };
  407. for piece in pieces_iter {
  408. let destinations = generate_sliding_destinations(friends, others, piece, straight, diagonal);
  409. let dest_iter = BitboardIterator(destinations & mask);
  410. for dest in dest_iter {
  411. let smove = SimpleMove{ from: square(piece), to: square(dest) };
  412. if dest & others != 0 {
  413. move_list.push(Move::Default { mov: smove, piece_type: piece_type, captured: game.find_piece(dest) });
  414. }
  415. else {
  416. move_list.push(Move::Default { mov: smove, piece_type: piece_type, captured: None });
  417. }
  418. }
  419. }
  420. }
  421. fn generate_sliding_destinations(friends: Bitboard, others: Bitboard,
  422. piece: Bitboard, straight: bool,
  423. diagonal: bool) -> Bitboard {
  424. let occupied = friends | others;
  425. let straights = [north_one, south_one, east_one, west_one];
  426. let diagonals = [northeast_one, southeast_one, northwest_one, southwest_one];
  427. let mut result: Bitboard = 0;
  428. if straight {
  429. for direction in straights.iter() {
  430. result |= generate_direction(piece, *direction, occupied);
  431. }
  432. }
  433. if diagonal {
  434. for direction in diagonals.iter() {
  435. result |= generate_direction(piece, *direction, occupied);
  436. }
  437. }
  438. return result & !friends;
  439. }
  440. fn generate_direction(piece: Bitboard, direction: fn(Bitboard) -> Bitboard, occupied: Bitboard) -> Bitboard {
  441. let mut result: Bitboard = 0;
  442. let mut b = piece;
  443. loop {
  444. b = direction(b);
  445. result |= b;
  446. if b & (occupied) != 0 || b == 0 {
  447. break;
  448. }
  449. }
  450. return result;
  451. }
  452. fn generate_king_moves(game: &Game, side: Side, move_list: &mut Vec<Move>, captures_only: bool) {
  453. let friends = game.get_all_side(side);
  454. let others = game.get_all_side(!side);
  455. let king = game.kings(side);
  456. //assert_eq!(king.count_ones(), 1); // assume only one king per player
  457. if king.count_ones() != 1 { return; }
  458. let mask =
  459. if captures_only {
  460. !friends & others
  461. }
  462. else {
  463. !friends
  464. };
  465. let area = (north_one(king)
  466. | south_one(king)
  467. | east_one(king)
  468. | west_one(king)
  469. | northeast_one(king)
  470. | northwest_one(king)
  471. | southwest_one(king)
  472. | southeast_one(king))
  473. & mask;
  474. let area_iter = BitboardIterator(area);
  475. for destination in area_iter {
  476. let simple_move = SimpleMove{ from: square(king), to: square(destination) };
  477. let captured = game.find_piece(destination);
  478. move_list.push(Move::Default { mov: simple_move, piece_type: KING, captured });
  479. }
  480. }
  481. fn generate_castling_moves(game: &Game, side: Side, move_list: &mut Vec<Move>) {
  482. let c1 = Move::Castling{ side: side, left: false };
  483. let c2 = Move::Castling{ side: side, left: true };
  484. let legal_castlings: &[bool] = match side {
  485. WHITE => &game.castling_rights[0..2],
  486. BLACK => &game.castling_rights[2..4],
  487. };
  488. let all_pieces = game.get_all_side(WHITE) | game.get_all_side(BLACK);
  489. // kingside castling
  490. if legal_castlings[0] {
  491. //info!("possible castling? {} {} {}", game.get_piece(KING, side), game.get_piece(ROOK, side), ((game.get_piece(KING, side) >> 3) & game.get_piece(ROOK, side)));
  492. if ((game.get_piece(KING, side) >> 3) & game.get_piece(ROOK, side)) != 0 &&
  493. ((game.get_piece(KING, side) >> 1) | (game.get_piece(KING, side) >> 2)) & all_pieces == 0 {
  494. let mut tg1 = game.clone();
  495. *tg1.get_piece_mut(KING, side) = game.get_piece(KING, side) >> 1;
  496. if !is_check(game, side) && !is_check(&tg1, side) {
  497. move_list.push(c1);
  498. }
  499. }
  500. }
  501. // queenside
  502. if legal_castlings[1] {
  503. //info!("possible castling? {} {} {}", game.get_piece(KING, side), game.get_piece(ROOK, side), ((game.get_piece(KING, side) >> 3) & game.get_piece(ROOK, side)));
  504. if ((game.get_piece(KING, side) << 4) & game.get_piece(ROOK, side)) != 0 &&
  505. ((game.get_piece(KING, side) << 1) | (game.get_piece(KING, side) << 2) | (game.get_piece(KING, side) << 3)) & all_pieces == 0 {
  506. let mut tg1 = game.clone();
  507. let mut tg2 = game.clone();
  508. *tg1.get_piece_mut(KING, side) = game.get_piece(KING, side) << 1;
  509. *tg2.get_piece_mut(KING, side) = game.get_piece(KING, side) << 2;
  510. if !is_check(game, side) && !is_check(&tg1, side) && !is_check(&tg2, side) {
  511. move_list.push(c2);
  512. }
  513. }
  514. }
  515. }
  516. pub fn is_check(game: &Game, side: Side) -> bool {
  517. let king = game.get_piece(KING, side);
  518. let king_square = square(king);
  519. let friends = game.get_all_side(side);
  520. let others = game.get_all_side(!side);
  521. let knights = get_knight_targets(king_square);
  522. if knights & game.get_piece(KNIGHT, !side) != 0 {
  523. return true;
  524. }
  525. let diagonal = generate_sliding_destinations(friends, others, king, false, true);
  526. let straight = generate_sliding_destinations(friends, others, king, true, false);
  527. if diagonal & game.get_piece(BISHOP, !side) != 0 {
  528. return true;
  529. }
  530. if diagonal & game.get_piece(QUEEN, !side) != 0 {
  531. return true;
  532. }
  533. if straight & game.get_piece(ROOK, !side) != 0 {
  534. return true;
  535. }
  536. if straight & game.get_piece(QUEEN, !side) != 0 {
  537. return true;
  538. }
  539. if side == BLACK {
  540. if ((southeast_one(king) | southwest_one(king)) & game.get_piece(PAWN, !side)) != 0 {
  541. return true;
  542. }
  543. }
  544. else {
  545. if ((northeast_one(king) | northwest_one(king)) & game.get_piece(PAWN, !side)) != 0 {
  546. return true;
  547. }
  548. }
  549. let king_area = north_one(king)
  550. | south_one(king)
  551. | east_one(king)
  552. | west_one(king)
  553. | northeast_one(king)
  554. | northwest_one(king)
  555. | southwest_one(king)
  556. | southeast_one(king);
  557. if king_area & game.get_piece(KING, !side) != 0 {
  558. return true;
  559. }
  560. return false;
  561. }
  562. /**
  563. * checks if side's king is in check
  564. */
  565. pub fn is_check_old(game: &Game, side: Side) -> bool {
  566. let king = game.get_piece(KING, side);
  567. let king_square = square(king);
  568. let possible_attacks = generate_possattacking_moves(game, !side);
  569. for mov in possible_attacks {
  570. if let Move::Default{ mov, piece_type: _, captured: _ } = mov {
  571. if mov.to == king_square {
  572. if !is_check(game, side) {
  573. panic!("incorrect non-check {}", game.beautiful_print());
  574. }
  575. return true;
  576. }
  577. }
  578. else if let Move::Promotion{ mov, promote_to: _, captured: _ } = mov {
  579. if mov.to == king_square {
  580. if !is_check(game, side) {
  581. panic!("incorrect non-check {}", game.beautiful_print());
  582. }
  583. return true;
  584. }
  585. }
  586. }
  587. if is_check(game, side) {
  588. panic!("incorrect check {}", game.beautiful_print());
  589. }
  590. false
  591. }
  592. /*
  593. #[cfg(test)]
  594. mod tests {
  595. use search::Game;
  596. use movegen::*;
  597. #[test]
  598. fn pawn_pushes() {
  599. let mut game: Game = Game::default();
  600. let pawn_moves = generate_pawn_moves(&game, WHITE);
  601. assert_eq!(pawn_moves, ());
  602. }
  603. }
  604. */