[gnome-chess] King and bishop can checkmate king and bishop of opposite color



commit fb2725b526bd95290c1cc26cb0b03a27e88c908e
Author: Michael Catanzaro <mike catanzaro gmail com>
Date:   Tue May 28 20:24:03 2013 -0500

    King and bishop can checkmate king and bishop of opposite color
    
    Update insufficient material algorithm and tests accordingly

 src/chess-game.vala      |   27 +++++++++++++++++++++++++++
 src/test-chess-game.vala |   20 +++++++++++++-------
 2 files changed, 40 insertions(+), 7 deletions(-)
---
diff --git a/src/chess-game.vala b/src/chess-game.vala
index 0bd80d6..e535b26 100644
--- a/src/chess-game.vala
+++ b/src/chess-game.vala
@@ -980,6 +980,24 @@ public class ChessState
                 }
             }
 
+            /*
+             * We count the following positions as insufficient:
+             *
+             * 1) king versus king
+             * 2) king and bishop versus king
+             * 3) king and knight versus king
+             * 4) king and bishop versus king and bishop with the bishops on
+             *    the same colour. (Any number of additional bishops of either
+             *    color on the same color of square due to underpromotion do
+             *    not affect the situation.)
+             *
+             * From:
+             * https://en.wikipedia.org/wiki/Draw_(chess)#Draws_in_all_games
+             *
+             * Note also that this follows FIDE rules, not USCF rules. E.g.
+             * K+N+N vs. K cannot be forced, so it's not counted as a draw.
+             */
+
             /* Two knights versus king can checkmate (though not against an optimal opponent) */
             if (white_knight_count > 1 || black_knight_count > 1)
                 return true;
@@ -1001,6 +1019,15 @@ public class ChessState
                 return true;
             if ((black_bishop_count > 0 || black_knight_count > 0) && white_knight_count > 0)
                 return true;
+
+            /* King and bishop can checkmate vs. king and bishop if bishops are on opposite colors */
+            if (white_bishop_count > 0 && black_bishop_count > 0)
+            {
+                if (white_bishop_on_white_square && black_bishop_on_black_square)
+                    return true;
+                else if (white_bishop_on_black_square && black_bishop_on_white_square)
+                    return true;
+            }
         }
 
         return false;
diff --git a/src/test-chess-game.vala b/src/test-chess-game.vala
index c1c209e..fc43b1f 100644
--- a/src/test-chess-game.vala
+++ b/src/test-chess-game.vala
@@ -128,7 +128,7 @@ class GlChess
 
         /* Not checkmate (piece can be moved to intercept) */
         test_good_move ("k7/7r/8/8/8/8/1R6/1R6 w - - 0 1", "Ra1+",
-                        "k7/7r/8/8/8/8/1R6/R7 b - - 1 1");
+                        "k7/7r/8/8/8/8/1R6/R7 b - - 1 1", ChessResult.IN_PROGRESS);
 
         /* Stalemate */
         test_good_move ("k7/8/7R/8/8/8/8/1R6 w - - 0 1", "Rh7",
@@ -143,17 +143,23 @@ class GlChess
                         "k7/7K/8/8/8/8/8/7N b - - 0 1", ChessResult.DRAW, ChessRule.INSUFFICIENT_MATERIAL);
         /* Sufficient if a knight on each side */
         test_good_move("k7/7p/7K/8/8/8/8/6Nn w - - 0 1", "Kxh7",
-                       "k7/7K/8/8/8/8/8/6Nn b - - 0 1");
+                       "k7/7K/8/8/8/8/8/6Nn b - - 0 1", ChessResult.IN_PROGRESS);
         /* A bishop would suffice as well */
         test_good_move("k7/7p/7K/8/8/8/8/6Nb w - - 0 1", "Kxh7",
-                       "k7/7K/8/8/8/8/8/6Nb b - - 0 1");
+                       "k7/7K/8/8/8/8/8/6Nb b - - 0 1", ChessResult.IN_PROGRESS);
 
-        /* Insufficient material - King and n same-color bishops vs. King and n other-color bishops */
+        /* Insufficient material - King and n same-color bishops vs. King and n same-color bishops */
+        test_good_move("k2b1b1b/6bp/7K/4B1B1/8/8/8/8 w - - 0 1", "Kxh7",
+                       "k2b1b1b/6bK/8/4B1B1/8/8/8/8 b - - 0 1", ChessResult.DRAW, 
ChessRule.INSUFFICIENT_MATERIAL);
+        /* Sufficient if the players' bishops are on opposite colors */
         test_good_move("k2b1b1b/6bp/7K/5B1B/8/8/8/8 w - - 0 1", "Kxh7",
-                       "k2b1b1b/6bK/8/5B1B/8/8/8/8 b - - 0 1", ChessResult.DRAW, 
ChessRule.INSUFFICIENT_MATERIAL);
-        /* Sufficient if a bishop is on each color */
+                       "k2b1b1b/6bK/8/5B1B/8/8/8/8 b - - 0 1", ChessResult.IN_PROGRESS);
+        /* Still sufficient with only one bishop per player, opposite colors */
+        test_good_move("k6b/6bp/7K/7B/8/8/8/8 w - - 0 1", "Kxh7",
+                       "k6b/6bK/8/7B/8/8/8/8 b - - 0 1", ChessResult.IN_PROGRESS);
+        /* Sufficient if one player has a bishop on each color */
         test_good_move("k2b1b1b/6bp/7K/6BB/8/8/8/8 w - - 0 1", "Kxh7",
-                       "k2b1b1b/6bK/8/6BB/8/8/8/8 b - - 0 1");
+                       "k2b1b1b/6bK/8/6BB/8/8/8/8 b - - 0 1", ChessResult.IN_PROGRESS);
 
         // FIXME: Need to be able to test claim draw
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]