[gnome-games/glchess-vala] Working without promotion



commit 369af1e46f20743ce8492bc1eff006287d4975d5
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Dec 27 11:28:58 2010 +1100

    Working without promotion

 glchess/src/chess-bitboard.vala |   32 ++++++++++++++++++++
 glchess/src/chess-game.vala     |   61 ++++++++++++++++++++++++--------------
 2 files changed, 70 insertions(+), 23 deletions(-)
---
diff --git a/glchess/src/chess-bitboard.vala b/glchess/src/chess-bitboard.vala
index bd4ea67..a74740f 100644
--- a/glchess/src/chess-bitboard.vala
+++ b/glchess/src/chess-bitboard.vala
@@ -1279,4 +1279,36 @@ public class BitBoard
         0x0203000000000000, 0x0507000000000000, 0x0a0e000000000000, 0x141c000000000000,
         0x6c38000000000000, 0x5070000000000000, 0xa0e0000000000000, 0x40c0000000000000
     };
+
+    public static string to_string (int64 mask)
+    {
+        var string = "+---+---+---+---+---+---+---+---+\n";
+        int rowCount = 0;
+        var colour = " ";
+        for (int rank = 7; rank >= 0; rank--)
+        {
+            for (int file = 0; file < 8; file++)
+            {
+                if ((mask & set_location_masks[rank * 8 + file]) != 0)
+                    string += "|[%s]".printf (colour);
+                else
+                    string += "| %s ".printf (colour);
+                rowCount++;
+                if (rowCount == 8)
+                {
+                    rowCount = 0;
+                    string += "|\n+---+---+---+---+---+---+---+---+\n";
+                }
+                else
+                {
+                    if (colour == " ")
+                        colour = ".";
+                    else
+                        colour = " ";
+                }
+            }
+        }
+
+        return string;
+    }
 }
\ No newline at end of file
diff --git a/glchess/src/chess-game.vala b/glchess/src/chess-game.vala
index bcf254d..463f77c 100644
--- a/glchess/src/chess-game.vala
+++ b/glchess/src/chess-game.vala
@@ -199,18 +199,20 @@ public class ChessState
     public bool move (string move, bool apply = true)
     {
         int r0, f0, r1, f1;
-        if (!decode_move (move, out r0, out f0, out r1, out f1))
+        PieceType promotion_type;
+        if (!decode_move (move, out r0, out f0, out r1, out f1, out promotion_type))
             return false;
-        return move_with_coords (r0, f0, r1, f1, apply);
+        return move_with_coords (r0, f0, r1, f1, promotion_type, apply);
     }
 
-    public bool move_with_coords (int r0, int f0, int r1, int f1, bool apply = true, bool test_check = true)
+    public bool move_with_coords (int r0, int f0, int r1, int f1, PieceType promotion_type = PieceType.QUEEN, bool apply = true, bool test_check = true)
     {
         // FIXME: Make this use indexes to be faster
         int start = get_index (r0, f0);
         int end = get_index (r1, f1);
 
         var color = current_player.color;
+        var opponent_color = opponent.color;
 
         /* Must be moving own piece */
         ChessPiece? piece = board[start];
@@ -232,7 +234,7 @@ public class ChessState
         ChessPiece? victim = board[end];
         if (victim != null && victim.player == current_player)
             return false;
-
+            
         /* Check special moves */
         int rook_start = -1, rook_end = -1;
         switch (piece.type)
@@ -245,6 +247,12 @@ public class ChessState
                     return false;
                 // FIXME: Check en passant
             }
+            else
+            {
+                /* If moving forward can't take enemy */
+                if (victim != null)
+                    return false;
+            }
             break;
         case PieceType.KING:
             /* If moving more than one square must be castling */
@@ -276,11 +284,21 @@ public class ChessState
         if (!apply && !test_check)
             return true;
 
+        var old_white_mask = piece_masks[Color.WHITE];
+        var old_black_mask = piece_masks[Color.BLACK];
+        var old_current_player = current_player;
+        var old_opponent = opponent;
+
         /* Update board */
         board[start] = null;
-        board[end] = piece;
-        piece_masks[color] &= BitBoard.clear_location_masks[start];
+        //if (piece.type == PieceType.PAWN && r1 == 0 || r1 == 7)
+        //    board[end] = new ChessPiece (old_current_player, promotion_type);
+        //else
+            board[end] = piece;
+        piece_masks[Color.WHITE] &= BitBoard.clear_location_masks[start];
+        piece_masks[Color.BLACK] &= BitBoard.clear_location_masks[start];
         piece_masks[color] |= end_mask;
+        piece_masks[opponent_color] &= BitBoard.clear_location_masks[end];
         if (rook_start >= 0)
         {
             var rook = board[rook_start];
@@ -289,9 +307,8 @@ public class ChessState
             piece_masks[color] &= BitBoard.clear_location_masks[rook_start];
             piece_masks[color] |= BitBoard.set_location_masks[rook_end];
         }
-        var player = current_player;
-        current_player = opponent;
-        opponent = player;
+        current_player = old_opponent;
+        opponent = old_current_player;
 
         /* Test if this move would leave that player in check */
         bool result = true;
@@ -305,7 +322,8 @@ public class ChessState
                     /* See if any enemy pieces can take the king */
                     for (int i = 0; i < 64; i++)
                     {
-                        if (move_with_coords (get_rank (i), get_file (i), get_rank (king_index), get_file (king_index), false, false))
+                        // FIXME: Should check all promotion types...
+                        if (move_with_coords (get_rank (i), get_file (i), get_rank (king_index), get_file (king_index), PieceType.QUEEN, false, false))
                         {
                             result = false;
                             break;
@@ -318,22 +336,18 @@ public class ChessState
         /* Undo move */
         if (!apply || !result)
         {
-            var t = current_player;
-            current_player = opponent;
-            opponent = t;
+            current_player = old_current_player;
+            opponent = old_opponent;
             board[start] = piece;
             board[end] = victim;
-            piece_masks[color] |= BitBoard.set_location_masks[start];
-            if (victim == null)
-                piece_masks[color] &= BitBoard.clear_location_masks[end];
             if (rook_start >= 0)
             {
                 var rook = board[rook_end];
                 board[rook_start] = rook;
                 board[rook_end] = null;
-                piece_masks[color] |= BitBoard.set_location_masks[rook_start];
-                piece_masks[color] &= BitBoard.clear_location_masks[rook_end];
             }
+            piece_masks[Color.WHITE] = old_white_mask;
+            piece_masks[Color.BLACK] = old_black_mask;
         }
 
         if (!apply)
@@ -384,10 +398,11 @@ public class ChessState
         }
     }
 
-    private bool decode_move (string move, out int r0, out int f0, out int r1, out int f1)
+    private bool decode_move (string move, out int r0, out int f0, out int r1, out int f1, out PieceType promotion_type)
     {
         int i = 0;
-        
+
+        promotion_type = PieceType.QUEEN;
         if (move.has_prefix ("O-O-O"))
         {
             if (current_player == white)
@@ -441,7 +456,6 @@ public class ChessState
             }
             if (move[i] == '=')
                 i++;
-            PieceType promotion_type;
             if (decode_piece_type (move[i], out promotion_type))
                 i++;
 
@@ -473,7 +487,8 @@ public class ChessState
                             continue;
 
                         /* See if can move here */
-                        if (!this.move_with_coords (rank, file, r1, f1, false))
+                        // FIXME: Should check all promotion types...
+                        if (!this.move_with_coords (rank, file, r1, f1, PieceType.QUEEN, false))
                             continue;
 
                         /* Duplicate match */
@@ -602,7 +617,7 @@ public class ChessGame
         }
         else
         {
-            if (!state.move_with_coords (r0, f0, r1, f1, apply))
+            if (!state.move_with_coords (r0, f0, r1, f1, PieceType.QUEEN, apply))
                 return false;
         }
 



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