]> git.sesse.net Git - stockfish/blobdiff - src/movegen.cpp
Fix a bug in king discoveries checks
[stockfish] / src / movegen.cpp
index 742a91f739b8c0f7097068b52feadb376d3afb4f..02268bffb32409124aa676b2c010b0f05ea38adb 100644 (file)
@@ -1,13 +1,13 @@
 /*
-  Glaurung, a UCI chess playing engine.
-  Copyright (C) 2004-2008 Tord Romstad
+  Stockfish, a UCI chess playing engine derived from Glaurung 2.1
+  Copyright (C) 2004-2008 Tord Romstad (Glaurung author) Copyright (C) 2008 Marco Costalba
 
-  Glaurung is free software: you can redistribute it and/or modify
+  Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   
-  Glaurung is distributed in the hope that it will be useful,
+  Stockfish is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
@@ -33,7 +33,6 @@
 namespace {
 
   struct PawnParams {
-
       Bitboard Rank3BB, Rank8BB;
       Rank RANK_8;
       SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
@@ -61,96 +60,10 @@ namespace {
 
   template<PieceType>
   int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*, int);
+  int generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*, int);
 
   template<PieceType>
   int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
-
-
-  /// Templates are defined here to avoid lookup issues with specializations
-
-  template<PieceType Piece>
-  int generate_piece_moves(const Position &pos, MoveStack *mlist, 
-                           Color side, Bitboard target) {
-    int n = 0;
-    for (int i = 0; i < pos.piece_count(side, Piece); i++)
-    {
-        Square from = pos.piece_list(side, Piece, i);
-        Bitboard b = pos.piece_attacks<Piece>(from) & target;
-        while (b)
-        {
-            Square to = pop_1st_bit(&b);
-            mlist[n++].move = make_move(from, to);
-        }
-    }
-    return n;
-  }
-
-
-  template<PieceType Piece>
-  int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
-                            Square ksq, MoveStack* mlist, int n) {
-    // Discovered checks
-    Bitboard b = target & dc;
-    while (b)
-    {
-        Square from = pop_1st_bit(&b);
-        Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
-        while (bb)
-        {
-            Square to = pop_1st_bit(&bb);
-            mlist[n++].move = make_move(from, to);
-        }
-    }
-    // Direct checks
-    b = target & ~dc;
-    Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
-    while (b)
-    {
-        Square from = pop_1st_bit(&b);
-        Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
-        while (bb)
-        {
-            Square to = pop_1st_bit(&bb);
-            mlist[n++].move = make_move(from, to);
-        }
-    }
-    return n;
-  }
-
-
-  template<> // Special case the King
-  int generate_piece_checks<KING>(const Position& pos, Bitboard, Bitboard dc,
-                                  Square ksq, MoveStack* mlist, int n) {
-    if (bit_is_set(dc, ksq))
-    {
-        Bitboard bb =   pos.piece_attacks<KING>(ksq)
-                      & pos.empty_squares()
-                      & ~QueenPseudoAttacks[ksq];
-        while (bb)
-        {
-            Square to = pop_1st_bit(&bb);
-            mlist[n++].move = make_move(ksq, to);
-        }
-    }
-    return n;
-  }
-
-  
-  template<PieceType Piece>
-  int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
-                                       Bitboard blockSquares, MoveStack* mlist, int n) {
-    while (b)
-    {
-        Square from = pop_1st_bit(&b);
-        Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
-        while (bb)
-        {
-            Square to = pop_1st_bit(&bb);
-            mlist[n++].move = make_move(from, to);
-        }
-    }
-    return n;
-  }
 }
 
 
@@ -254,7 +167,7 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
       n = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist, n);
 
   // Hopefully we always have a king ;-)
-  n = generate_piece_checks<KING>(pos, b, dc, pos.king_square(us), mlist, n);
+  n = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist, n);
 
   // TODO: Castling moves!
   
@@ -623,6 +536,41 @@ Move generate_move_if_legal(const Position &pos, Move m, Bitboard pinned) {
 
 namespace {
 
+  template<PieceType Piece>
+  int generate_piece_moves(const Position &pos, MoveStack *mlist, 
+                           Color side, Bitboard target) {
+    int n = 0;
+    for (int i = 0; i < pos.piece_count(side, Piece); i++)
+    {
+        Square from = pos.piece_list(side, Piece, i);
+        Bitboard b = pos.piece_attacks<Piece>(from) & target;
+        while (b)
+        {
+            Square to = pop_1st_bit(&b);
+            mlist[n++].move = make_move(from, to);
+        }
+    }
+    return n;
+  }
+
+
+  template<PieceType Piece>
+  int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
+                                       Bitboard blockSquares, MoveStack* mlist, int n) {
+    while (b)
+    {
+        Square from = pop_1st_bit(&b);
+        Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
+        while (bb)
+        {
+            Square to = pop_1st_bit(&bb);
+            mlist[n++].move = make_move(from, to);
+        }
+    }
+    return n;
+  }
+
+
   template<Color C>
   int generate_pawn_captures(const Position& pos, MoveStack* mlist) {
 
@@ -814,6 +762,54 @@ namespace {
     return n;
   }
 
+  template<PieceType Piece>
+  int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc,
+                            Square ksq, MoveStack* mlist, int n) {
+    // Discovered checks
+    Bitboard b = target & dc;
+    while (b)
+    {
+        Square from = pop_1st_bit(&b);
+        Bitboard bb = pos.piece_attacks<Piece>(from) & pos.empty_squares();
+        while (bb)
+        {
+            Square to = pop_1st_bit(&bb);
+            mlist[n++].move = make_move(from, to);
+        }
+    }
+    // Direct checks
+    b = target & ~dc;
+    Bitboard checkSqs = pos.piece_attacks<Piece>(ksq) & pos.empty_squares();
+    while (b)
+    {
+        Square from = pop_1st_bit(&b);
+        Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
+        while (bb)
+        {
+            Square to = pop_1st_bit(&bb);
+            mlist[n++].move = make_move(from, to);
+        }
+    }
+    return n;
+  }
+
+  int generate_piece_checks_king(const Position& pos, Square from, Bitboard dc,
+                                 Square ksq, MoveStack* mlist, int n) {
+    if (bit_is_set(dc, from))
+    {
+        Bitboard b =   pos.piece_attacks<KING>(from)
+                     & pos.empty_squares()
+                     & ~QueenPseudoAttacks[ksq];
+        while (b)
+        {
+            Square to = pop_1st_bit(&b);
+            mlist[n++].move = make_move(from, to);
+        }
+    }
+    return n;
+  }
+
+
   template<Color C>
   int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
                                       Bitboard blockSquares, MoveStack* mlist, int n) {