Skip to content
This repository
Browse code

Add scan for X-ray attacks in piece_attacks_square()

Used to avoid pruning interesting moves.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
  • Loading branch information...
commit e71d52075836e449cb3f4226cf1010c55e2900be 1 parent 52ed0e9
Marco Costalba authored

Showing 1 changed file with 24 additions and 10 deletions. Show diff stats Hide diff stats

  1. 34  src/position.cpp
34  src/position.cpp
@@ -420,30 +420,44 @@ bool Position::piece_attacks_square(Square f, Square t) const {
420 420
 
421 421
 
422 422
 /// Position::move_attacks_square() tests whether a move from the current
423  
-/// position attacks a given square.  Only attacks by the moving piece are
424  
-/// considered; the function does not handle X-ray attacks.
  423
+/// position attacks a given square.
425 424
 
426 425
 bool Position::move_attacks_square(Move m, Square s) const {
427 426
 
428 427
   assert(move_is_ok(m));
429 428
   assert(square_is_ok(s));
430 429
 
  430
+  bool is_attack;
431 431
   Square f = move_from(m), t = move_to(m);
432 432
 
433 433
   assert(square_is_occupied(f));
434 434
 
435 435
   switch (piece_on(f))
436 436
   {
437  
-  case WP:          return pawn_attacks_square(WHITE, t, s);
438  
-  case BP:          return pawn_attacks_square(BLACK, t, s);
439  
-  case WN: case BN: return piece_attacks_square<KNIGHT>(t, s);
440  
-  case WB: case BB: return piece_attacks_square<BISHOP>(t, s);
441  
-  case WR: case BR: return piece_attacks_square<ROOK>(t, s);
442  
-  case WQ: case BQ: return piece_attacks_square<QUEEN>(t, s);
443  
-  case WK: case BK: return piece_attacks_square<KING>(t, s);
  437
+  case WP:          is_attack = pawn_attacks_square(WHITE, t, s); break;
  438
+  case BP:          is_attack = pawn_attacks_square(BLACK, t, s); break;
  439
+  case WN: case BN: is_attack = piece_attacks_square<KNIGHT>(t, s); break;
  440
+  case WB: case BB: is_attack = piece_attacks_square<BISHOP>(t, s); break;
  441
+  case WR: case BR: is_attack = piece_attacks_square<ROOK>(t, s); break;
  442
+  case WQ: case BQ: is_attack = piece_attacks_square<QUEEN>(t, s); break;
  443
+  case WK: case BK: is_attack = piece_attacks_square<KING>(t, s); break;
444 444
   default: break;
445 445
   }
446  
-  return false;
  446
+
  447
+  if (is_attack)
  448
+      return true;
  449
+
  450
+  // Move the piece and scan for X-ray attacks behind it
  451
+  Bitboard occ = occupied_squares();
  452
+  Color us = color_of_piece_on(f);
  453
+  clear_bit(&occ, f);
  454
+  set_bit(&occ, t);
  455
+  Bitboard xray = ( (rook_attacks_bb(s, occ) & rooks_and_queens())
  456
+                   |(bishop_attacks_bb(s, occ) & bishops_and_queens())) & pieces_of_color(us);
  457
+
  458
+  // If we have attacks we need to verify that are caused by our move
  459
+  // and are not already existent ones.
  460
+  return xray && (xray ^ (xray & piece_attacks<QUEEN>(s)));
447 461
 }
448 462
 
449 463
 

0 notes on commit e71d520

Please sign in to comment.
Something went wrong with that request. Please try again.