[gnome-games] glchess: Fix insufficient material logic



commit 82be90a5475c2ab073e3d0c165ff713650cec296
Author: Robert Ancell <robert ancell gmail com>
Date:   Fri Jun 11 09:54:04 2010 +1000

    glchess: Fix insufficient material logic

 glchess/src/lib/chess/board.py |   61 +++++++++++++++++++++++++++++----------
 1 files changed, 45 insertions(+), 16 deletions(-)
---
diff --git a/glchess/src/lib/chess/board.py b/glchess/src/lib/chess/board.py
index f227916..5ed5828 100644
--- a/glchess/src/lib/chess/board.py
+++ b/glchess/src/lib/chess/board.py
@@ -391,32 +391,61 @@ class ChessBoardState:
         
         Return True if sufficient pieces to make checkmate or False otherwise.
         """
-        knightCount = 0
-        bishopCount = 0
+        whiteKnightCount = 0
+        whiteBishopCount = 0
+        whiteBishopOnWhiteSquare = False
+        whiteBishopOnBlackSquare = False
+        blackKnightCount = 0
+        blackBishopCount = 0
+        blackBishopOnWhiteSquare = False
+        blackBishopOnBlackSquare = False
+
         for coord, piece in self.squares.iteritems():
             pieceType = piece.getType()
-            
-            # Any pawns, rooks or queens can perform check
+            pieceColour = piece.getColour()
+
+            # Any pawns, rooks or queens can perform checkmate
             if pieceType == PAWN or pieceType == ROOK or pieceType == QUEEN:
                 return True
 
-            # Multiple knights can check
+            # Otherwise, count the minor pieces for each colour...
             if pieceType == KNIGHT:
-                knightCount += 1
-                if knightCount > 1:
-                    return True
+                if pieceColour == WHITE:
+                    whiteKnightCount += 1
+                else:
+                    blackKnightCount += 1
 
-            # Bishops on different colours can check
             if pieceType == BISHOP:
-                bishopCount += 1 
-                colour = self._getSquareColour(coord)
-                if bishopCount > 1:
-                    if colour != bishopSquareColour:
-                        return True
-                bishopSquareColour = colour
+                if pieceColour == WHITE:
+                    if self._getSquareColour(coord) == WHITE:
+                        whiteBishopOnWhiteSquare = True
+                    else:
+                        whiteBishopOnBlackSquare = True
+                    whiteBishopCount += 1
+                else:
+                    if self._getSquareColour(coord) == WHITE:
+                        blackBishopOnWhiteSquare = True
+                    else:
+                        blackBishopOnBlackSquare = True
+                    blackBishopCount += 1
+
+            whiteMinorCount = whiteBishopCount + whiteKnightCount
+            blackMinorCount = blackBishopCount + blackKnightCount
+            bishopsOnOppositeSquares = (whiteBishopOnWhiteSquare and blackBishopOnBlackSquare) or \
+                                       (blackBishopOnWhiteSquare and whiteBishopOnBlackSquare)
+
+            # Bishop and knight versus king can checkmate
+            if whiteBishopCount > 0 and whiteKnightCount > 0:
+                return True
+            if blackBishopCount > 0 and blackKnightCount > 0:
+                return True
+
+            # King and bishop versus king and bishop can checkmate as long as the bishops are on opposite colours
+            if whiteBishopCount > 0 and blackBishopCount > 0 and bishopsOnOppositeSquares:
+                return True
 
         return False
-        
+
     allowedMoves = {WHITE: {PAWN:   bitboard.WHITE_PAWN_MOVES,
                             ROOK:   bitboard.ROOK_MOVES,
                             BISHOP: bitboard.BISHOP_MOVES,



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