[gnome-games] chess: Fix checkmate detection (Bug #644180)



commit c263e81c6816224a12db98cbfe9d8d06c355b33c
Author: Robert Ancell <robert ancell canonical com>
Date:   Wed Mar 9 11:25:10 2011 +1100

    chess: Fix checkmate detection (Bug #644180)

 glchess/src/chess-game.vala      |   11 ++++++-----
 glchess/src/test-chess-game.vala |    4 ++++
 2 files changed, 10 insertions(+), 5 deletions(-)
---
diff --git a/glchess/src/chess-game.vala b/glchess/src/chess-game.vala
index fcd66ef..f6700b3 100644
--- a/glchess/src/chess-game.vala
+++ b/glchess/src/chess-game.vala
@@ -809,6 +809,7 @@ public class ChessState
     {
         var opponent = player.color == Color.WHITE ? players[Color.BLACK] : players[Color.WHITE];
 
+        /* Is in check if any piece can take the king */
         for (int king_index = 0; king_index < 64; king_index++)
         {
             var p = board[king_index];
@@ -831,16 +832,16 @@ public class ChessState
 
     private bool is_in_checkmate (ChessPlayer player)
     {
-        for (int king_index = 0; king_index < 64; king_index++)
+        /* Is in checkmate if no pieces can move */
+        for (int piece_index = 0; piece_index < 64; piece_index++)
         {
-            var p = board[king_index];
-            if (p != null && p.player == player && p.type == PieceType.KING)
+            var p = board[piece_index];
+            if (p != null && p.player == player)
             {
-                /* See if the king can move */
                 for (int end = 0; end < 64; end++)
                 {
                     if (move_with_coords (player,
-                                          get_rank (king_index), get_file (king_index),
+                                          get_rank (piece_index), get_file (piece_index),
                                           get_rank (end), get_file (end),
                                           PieceType.QUEEN, false, true))
                         return false;
diff --git a/glchess/src/test-chess-game.vala b/glchess/src/test-chess-game.vala
index 1071531..1e68ae0 100644
--- a/glchess/src/test-chess-game.vala
+++ b/glchess/src/test-chess-game.vala
@@ -111,6 +111,10 @@ class GlChess
         test_good_move ("k7/8/8/8/8/8/1R6/1R6 w - - 0 1", "Ra1#",
                         "k7/8/8/8/8/8/1R6/R7 b - - 1 1", ChessResult.WHITE_WON, ChessRule.CHECKMATE);
 
+        /* 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");
+
         /* Stalemate */
         test_good_move ("k7/8/7R/8/8/8/8/1R6 w - - 0 1", "Rh7",
                         "k7/7R/8/8/8/8/8/1R6 b - - 1 1", ChessResult.DRAW, ChessRule.STALEMATE);



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