コード例 #1
0
 internal ChessPiece(bool white, PieceType type)
 {
     this.white = white;
     this.type = type;
     this.lastMove = 0;
     this.lastSquare = new SquareCoordinates(-1, -1);
 }
コード例 #2
0
 private ChessPiece(bool white, PieceType type, ushort lastMove, SquareCoordinates lastSquare)
 {
     this.white = white;
     this.type = type;
     this.lastMove = lastMove;
     this.lastSquare = lastSquare;
 }
コード例 #3
0
 internal static bool CanCastle(ChessBoard board, SquareCoordinates coord, Player p)
 {
     ChessPiece piece = board.GetPiece(coord);
     if (piece != null && piece.IsA(PieceType.ROOK) && piece.IsFriendly(p.White) && piece.HasNotMoved())
     {
         SquareCoordinates kingSq = p.White ? new SquareCoordinates("E1") : new SquareCoordinates("E8");
         piece = board.GetPiece(kingSq);
         if (piece != null && piece.IsA(PieceType.KING) && piece.IsFriendly(p.White) && piece.HasNotMoved() && !KingInCheck(board, p))
         {
             if (coord.File == 0)
             {
                 if (!board.WillPlaceKingInCheck(p, new SquareCoordinates[] { new SquareCoordinates(4, coord.Rank), new SquareCoordinates(0, coord.Rank) }, new SquareCoordinates[] { new SquareCoordinates(2, coord.Rank), new SquareCoordinates(3, coord.Rank) }))
                 {
                     for (int file = 1, rank = coord.Rank; file < 4; file++)
                         if (board.GetPiece(coord = new SquareCoordinates(file, rank)) != null || Attackers(board, coord, p.White, true).Length > 0)
                             return false;
                     return true;
                 }
             }
             else if (coord.File == 7)
             {
                 if (!board.WillPlaceKingInCheck(p, new SquareCoordinates[] { new SquareCoordinates(4, coord.Rank), new SquareCoordinates(7, coord.Rank) }, new SquareCoordinates[] { new SquareCoordinates(6, coord.Rank), new SquareCoordinates(5, coord.Rank) }))
                 {
                     for (int file = 5, rank = coord.Rank; file < 7; file++)
                         if (board.GetPiece(coord = new SquareCoordinates(file, rank)) != null || Attackers(board, coord, p.White, true).Length > 0)
                             return false;
                     return true;
                 }
             }
         }
     }
     return false;
 }
コード例 #4
0
ファイル: Player.cs プロジェクト: Kevin-Jin/kvj-board-games
 internal void MovePiece(PieceType type, SquareCoordinates from, SquareCoordinates to)
 {
     if (type != PieceType.KING)
     {
         pieces.Remove(from);
         pieces.Add(to);
     }
     else
     {
         kingSquare = to;
     }
 }
コード例 #5
0
 internal static byte[] WritePromotion(SquareCoordinates coord, PieceType newType)
 {
     return new byte[] { ChessOpHeaders.PROMOTION, (byte)coord.File, (byte)coord.Rank, (byte)newType };
 }
コード例 #6
0
        private Material[] GetMaterial()
        {
            List<SquareCoordinates> wPieces = new List<SquareCoordinates>(), bPieces = new List<SquareCoordinates>();
            SquareCoordinates wKing = new SquareCoordinates(-1, -1), bKing = new SquareCoordinates(-1, -1);
            ChessPiece piece;
            for (int i = 0; i < 8; i++)
                for (int j = 0; j < 8; j++)
                    if ((piece = pieces[i, j]) != null)
                        if (piece.White)
                            if (piece.IsA(PieceType.KING))
                                wKing = new SquareCoordinates(i, j);
                            else
                                wPieces.Add(new SquareCoordinates(i, j));
                        else
                            if (piece.IsA(PieceType.KING))
                                bKing = new SquareCoordinates(i, j);
                            else
                                bPieces.Add(new SquareCoordinates(i, j));

            if (wKing.File == -1 && wKing.Rank == -1)
                throw new ApplicationException("White king not found in save file.");
            if (bKing.File == -1 && bKing.Rank == -1)
                throw new ApplicationException("Black king not found in save file.");

            return new Material[] { new Material(wPieces, wKing), new Material(bPieces, bKing) };
        }
コード例 #7
0
 internal bool WillPlaceKingInCheck(Player p, SquareCoordinates[] fromSquares, SquareCoordinates[] toSquares)
 {
     SquareCoordinates king = p.Material.KingSquare;
     ChessPiece[] overwrittenPieces = new ChessPiece[fromSquares.Length];
     for (int i = 0; i < fromSquares.Length; i++)
     {
         SquareCoordinates from = fromSquares[i];
         SquareCoordinates to = toSquares[i];
         ChessPiece fromPiece = pieces[from.File, from.Rank];
         overwrittenPieces[i] = pieces[to.File, to.Rank];
         pieces[to.File, to.Rank] = fromPiece;
         pieces[from.File, from.Rank] = null;
         if (fromPiece.IsA(PieceType.KING) && fromPiece.IsFriendly(p.White))
             king = to;
     }
     bool inCheck = GameLogic.KingInCheck(this, king, p.White);
     for (int i = 0; i < fromSquares.Length; i++)
     {
         SquareCoordinates from = fromSquares[i];
         SquareCoordinates to = toSquares[i];
         ChessPiece fromPiece = pieces[to.File, to.Rank];
         pieces[from.File, from.Rank] = fromPiece;
         pieces[to.File, to.Rank] = overwrittenPieces[i];
     }
     return inCheck;
 }
コード例 #8
0
 internal void UpdateLastMove(SquareCoordinates from, Move m, ChessPiece moved, ChessPiece taken)
 {
     moveLabel.Text = MoveRecorder.GetMoveNotation(from, m, moved, taken);
     if (m.Overtaken)
         WaitingPlayer().Material.Pieces.Remove(m.TakenPiece);
 }
コード例 #9
0
 internal void HighlightSquare(SquareCoordinates coord, Color color)
 {
     highlightedSquares[coord] = color;
     using (Graphics g = this.CreateGraphics())
     {
         ChangeSquareBorderColor(g, color, new BoardIndices(coord, whiteOnBottom));
     }
 }
コード例 #10
0
 internal void DrawPiece(SquareCoordinates coord, ChessPiece piece)
 {
     SetPiece(coord, piece);
     using (Graphics g = this.CreateGraphics())
     {
         DrawPiece(g, new BoardIndices(coord, whiteOnBottom), piece.White, piece.Type);
     }
 }
コード例 #11
0
 private static SquareCoordinates EnPassant(ChessBoard board, SquareCoordinates moveTo, bool white)
 {
     if (white)
     {
         if (moveTo.Rank != 5)
             return new SquareCoordinates(-1, -1);
         ChessPiece pawn = board.GetPiece(new SquareCoordinates(moveTo.File, 4));
         if (pawn == null || !pawn.IsA(PieceType.PAWN) || pawn.IsFriendly(white))
             return new SquareCoordinates(-1, -1);
         if (pawn.LastMove != board.CurrentMove - 1 || pawn.LastSquare != new SquareCoordinates(moveTo.File, 6))
             return new SquareCoordinates(-1, -1);
         return new SquareCoordinates(moveTo.File, moveTo.Rank - 1);
     }
     else
     {
         if (moveTo.Rank != 2)
             return new SquareCoordinates(-1, -1);
         ChessPiece pawn = board.GetPiece(new SquareCoordinates(moveTo.File, 3));
         if (pawn == null || !pawn.IsA(PieceType.PAWN) || pawn.IsFriendly(white))
             return new SquareCoordinates(-1, -1);
         if (pawn.LastMove != board.CurrentMove - 1 || pawn.LastSquare != new SquareCoordinates(moveTo.File, 1))
             return new SquareCoordinates(-1, -1);
         return new SquareCoordinates(moveTo.File, moveTo.Rank + 1);
     }
 }
コード例 #12
0
 private static bool CanInterpose(ChessBoard board, SquareCoordinates from, SquareCoordinates to, Player p)
 {
     switch (board.GetPiece(from).Type)
     {
         case PieceType.PAWN:
             return false; //if pawns cannot be captured, they cannot be blocked by friendly pieces!
         case PieceType.KNIGHT:
             return false; //if knights cannot be captured, they cannot be blocked by friendly pieces!
         case PieceType.BISHOP:
             if (from.File - to.File == from.Rank - to.Rank)
             {
                 for (int file = Math.Min(from.File, to.File) + 1, rank = Math.Min(from.Rank, to.Rank) + 1; file < Math.Max(from.File, to.File); file++, rank++)
                     if (Attackers(board, new SquareCoordinates(file, rank), !p.White, false).Length > 0)
                         return true;
             }
             else if (from.File - to.File == -(from.Rank - to.Rank))
             {
                 for (int file = Math.Min(from.File, to.File) + 1, rank = Math.Max(from.Rank, to.Rank) + 1; file < Math.Max(from.File, to.File) && rank < 8; file++, rank--)
                     if (Attackers(board, new SquareCoordinates(file, rank), !p.White, false).Length > 0)
                         return true;
             }
             return false;
         case PieceType.ROOK:
             if (from.File == to.File)
             {
                 for (int file = from.File, rank = Math.Min(from.Rank, to.Rank) + 1; rank < Math.Max(from.Rank, to.Rank); rank++)
                     if (Attackers(board, new SquareCoordinates(file, rank), !p.White, false).Length > 0)
                         return true;
             }
             else if (from.Rank == to.Rank)
             {
                 for (int file = Math.Min(from.File, to.File) + 1, rank = from.Rank; file < Math.Max(from.File, to.File); file++)
                     if (Attackers(board, new SquareCoordinates(file, rank), !p.White, false).Length > 0)
                         return true;
             }
             return false;
         case PieceType.QUEEN:
             if (from.File == to.File || from.Rank == to.Rank)
                 goto case PieceType.ROOK;
             else if (Math.Abs(from.File - to.File) == Math.Abs(from.Rank - to.Rank))
                 goto case PieceType.BISHOP;
             return false;
         default:
             return false;
     }
 }
コード例 #13
0
        private static SquareCoordinates[] Attackers(ChessBoard board, SquareCoordinates pieceLoc, bool pieceWhite, bool allowPawnDiagonal)
        {
            List<SquareCoordinates> attackingPieces = new List<SquareCoordinates>();
            int file, rank;
            ChessPiece existingPiece;
            SquareCoordinates coord;
            //go vertically/horizontally and see if there's an unblocked opponent queen or rook.
            existingPiece = null;
            coord = new SquareCoordinates(-1, -1);
            for (file = pieceLoc.File + 1, rank = pieceLoc.Rank; file < 8 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; file++) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.ROOK) || existingPiece.IsA(PieceType.QUEEN)))
                attackingPieces.Add(coord);
            existingPiece = null;
            coord = new SquareCoordinates(-1, -1);
            for (file = pieceLoc.File - 1, rank = pieceLoc.Rank; file >= 0 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; file--) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.ROOK) || existingPiece.IsA(PieceType.QUEEN)))
                attackingPieces.Add(coord);
            existingPiece = null;
            coord = new SquareCoordinates(-1, -1);
            for (file = pieceLoc.File, rank = pieceLoc.Rank + 1; rank < 8 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; rank++) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.ROOK) || existingPiece.IsA(PieceType.QUEEN) || !allowPawnDiagonal && pieceWhite && rank - pieceLoc.Rank == 1 && existingPiece.IsA(PieceType.PAWN)))
                attackingPieces.Add(coord);
            existingPiece = null;
            coord = new SquareCoordinates(-1, -1);
            for (file = pieceLoc.File, rank = pieceLoc.Rank - 1; rank >= 0 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; rank--) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.ROOK) || existingPiece.IsA(PieceType.QUEEN) || !allowPawnDiagonal && !pieceWhite && pieceLoc.Rank - rank == 1 && existingPiece.IsA(PieceType.PAWN)))
                attackingPieces.Add(coord);

            //go diagonally and see if there's an unblocked opponent queen or bishop (or pawn if only one diagonal away).
            existingPiece = null;
            for (file = pieceLoc.File + 1, rank = pieceLoc.Rank + 1; file < 8 && rank < 8 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; file++, rank++) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.BISHOP) || existingPiece.IsA(PieceType.QUEEN) || allowPawnDiagonal && pieceWhite && file - pieceLoc.File == 1 && rank - pieceLoc.Rank == 1 && existingPiece.IsA(PieceType.PAWN)))
                attackingPieces.Add(coord);
            existingPiece = null;
            for (file = pieceLoc.File + 1, rank = pieceLoc.Rank - 1; file < 8 && rank >= 0 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; file++, rank--) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.BISHOP) || existingPiece.IsA(PieceType.QUEEN) || allowPawnDiagonal && !pieceWhite && file - pieceLoc.File == 1 && pieceLoc.Rank - rank == 1 && existingPiece.IsA(PieceType.PAWN)))
                attackingPieces.Add(coord);
            existingPiece = null;
            for (file = pieceLoc.File - 1, rank = pieceLoc.Rank + 1; file >= 0 && rank < 8 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; file--, rank++) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.BISHOP) || existingPiece.IsA(PieceType.QUEEN) || allowPawnDiagonal && pieceWhite && pieceLoc.File - file == 1 && rank - pieceLoc.Rank == 1 && existingPiece.IsA(PieceType.PAWN)))
                attackingPieces.Add(coord);
            existingPiece = null;
            for (file = pieceLoc.File - 1, rank = pieceLoc.Rank - 1; file >= 0 && rank >= 0 && (existingPiece = board.GetPiece((coord = new SquareCoordinates(file, rank)))) == null; file--, rank--) ;
            if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && (existingPiece.IsA(PieceType.BISHOP) || existingPiece.IsA(PieceType.QUEEN) || allowPawnDiagonal && !pieceWhite && pieceLoc.File - file == 1 && pieceLoc.Rank - rank == 1 && existingPiece.IsA(PieceType.PAWN)))
                attackingPieces.Add(coord);

            //go through all 8 L-shapes and see if there's a knight in each (doesn't matter if another piece is in the way).
            file = pieceLoc.File + 2;
            rank = pieceLoc.Rank + 1;
            if (file < 8 && rank < 8)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            file = pieceLoc.File + 2;
            rank = pieceLoc.Rank - 1;
            if (file < 8 && rank >= 0)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            file = pieceLoc.File + 1;
            rank = pieceLoc.Rank + 2;
            if (file < 8 && rank < 8)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            file = pieceLoc.File + 1;
            rank = pieceLoc.Rank - 2;
            if (file < 8 && rank >= 0)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            file = pieceLoc.File - 2;
            rank = pieceLoc.Rank + 1;
            if (file >= 0 && rank < 8)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            file = pieceLoc.File - 2;
            rank = pieceLoc.Rank - 1;
            if (file >= 0 && rank >= 0)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            file = pieceLoc.File - 1;
            rank = pieceLoc.Rank + 2;
            if (file >= 0 && rank < 8)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            file = pieceLoc.File - 1;
            rank = pieceLoc.Rank - 2;
            if (file >= 0 && rank >= 0)
            {
                coord = new SquareCoordinates(file, rank);
                existingPiece = board.GetPiece(coord);
                if (existingPiece != null && !existingPiece.IsFriendly(pieceWhite) && existingPiece.IsA(PieceType.KNIGHT))
                    attackingPieces.Add(coord);
            }
            return attackingPieces.ToArray();
        }
コード例 #14
0
 internal static MoveSet MoveSet(ChessBoard board, SquareCoordinates coord, Player p)
 {
     switch (board.GetPiece(coord).Type)
     {
         case PieceType.PAWN:
             return PawnMoves(board, coord, p);
         case PieceType.KNIGHT:
             return KnightMoves(board, coord, p);
         case PieceType.BISHOP:
             return BishopMoves(board, coord, p);
         case PieceType.ROOK:
             return RookMoves(board, coord, p);
         case PieceType.QUEEN:
             return QueenMoves(board, coord, p);
         case PieceType.KING:
             return KingMoves(board, coord, p);
         default:
             return new MoveSet();
     }
 }
コード例 #15
0
 internal static bool KingInCheck(ChessBoard board, SquareCoordinates kingSquare, bool white)
 {
     return Attackers(board, kingSquare, white, true).Length != 0;
 }
コード例 #16
0
 internal BoardIndices(SquareCoordinates coord, bool whiteOnBottom)
 {
     this.row = whiteOnBottom ? (7 - coord.Rank) : coord.Rank;
     this.col = whiteOnBottom ? coord.File : (7 - coord.File);
 }
コード例 #17
0
 internal ChessPiece ClearSquare(SquareCoordinates coord)
 {
     ChessPiece removed = GetPiece(coord);
     SetPiece(coord, null);
     using (Graphics g = this.CreateGraphics())
     {
         DrawSquare(g, new BoardIndices(coord, whiteOnBottom));
     }
     return removed;
 }
コード例 #18
0
        private static MoveSet KnightMoves(ChessBoard board, SquareCoordinates coord, Player p)
        {
            List<Move> allowedMoves = new List<Move>();
            List<SquareCoordinates> movesWithCheck = new List<SquareCoordinates>();
            List<SquareCoordinates> blockedMoves = new List<SquareCoordinates>();
            bool boardOrientation = board.WhiteOnBottom;
            bool white = p.White;
            //SquareCoordinates opponentEnPassantPawn;
            ChessPiece existingPiece;
            Move m;
            int nextFile, nextRank;

            ProcessSquare proc = delegate(SquareCoordinates to)
            {
                if ((existingPiece = board.GetPiece(to)) == null)
                    /*if ((opponentEnPassantPawn = EnPassant(board, to, p.White)).File != -1 && opponentEnPassantPawn.Rank != -1)
                        if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to, opponentEnPassantPawn)))
                            allowedMoves.Add(m);
                        else
                            movesWithCheck.Add(to);
                    else*/
                    if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to)))
                        allowedMoves.Add(m);
                    else
                        movesWithCheck.Add(to);
                else if (!existingPiece.IsFriendly(p.White))
                    if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to, to)))
                        allowedMoves.Add(m);
                    else
                        movesWithCheck.Add(to);
                else
                    blockedMoves.Add(to);
            };

            nextFile = coord.File + 1;
            if (nextFile < 8)
            {
                nextRank = coord.Rank + 2;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank < 8)
                    proc(new SquareCoordinates(nextFile, nextRank)); //right 1 up 2
                nextRank = coord.Rank - 2;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank >= 0)
                    proc(new SquareCoordinates(nextFile, nextRank)); //right 1 down 2
            }
            nextFile = coord.File + 2;
            if (nextFile < 8)
            {
                nextRank = coord.Rank + 1;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank < 8)
                    proc(new SquareCoordinates(nextFile, nextRank)); //right 2 up 1
                nextRank = coord.Rank - 1;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank >= 0)
                    proc(new SquareCoordinates(nextFile, nextRank)); //right 2 down 1
            }
            nextFile = coord.File - 1;
            if (nextFile >= 0)
            {
                nextRank = coord.Rank + 2;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank < 8)
                    proc(new SquareCoordinates(nextFile, nextRank)); //left 1 up 2
                nextRank = coord.Rank - 2;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank >= 0)
                    proc(new SquareCoordinates(nextFile, nextRank)); //left 1 down 2
            }
            nextFile = coord.File - 2;
            if (nextFile >= 0)
            {
                nextRank = coord.Rank + 1;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank < 8)
                    proc(new SquareCoordinates(nextFile, nextRank)); //left 2 up 1
                nextRank = coord.Rank - 1;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextRank >= 0)
                    proc(new SquareCoordinates(nextFile, nextRank)); //left 2 down 1
            }
            return new MoveSet(allowedMoves, movesWithCheck, blockedMoves);
        }
コード例 #19
0
 internal ChessPiece GetPiece(SquareCoordinates coord)
 {
     return pieces[coord.File, coord.Rank];
 }
コード例 #20
0
        //TODO: promotion (pawn reaching opponent's end of board -> any piece)
        private static MoveSet PawnMoves(ChessBoard board, SquareCoordinates coord, Player p)
        {
            List<Move> allowedMoves = new List<Move>();
            List<SquareCoordinates> movesWithCheck = new List<SquareCoordinates>();
            List<SquareCoordinates> blockedMoves = new List<SquareCoordinates>();
            bool boardOrientation = board.WhiteOnBottom;
            bool white = p.White;
            SquareCoordinates nextSquare;
            ChessPiece existingPiece;
            Move m;
            int nextRank, nextFile;
            //check if piece would go out of bounds
            if (white && (nextRank = coord.Rank + 1) < 8 || !white && (nextRank = coord.Rank - 1) >= 0)
            {
                nextSquare = new SquareCoordinates(coord.File, nextRank);
                existingPiece = board.GetPiece(nextSquare);
                if (existingPiece == null)
                    //move up 1 if no piece is in that space
                    if (!board.WillPlaceKingInCheck(p, coord, m = new Move(nextSquare)))
                        allowedMoves.Add(m);
                    else
                        movesWithCheck.Add(nextSquare);
                else
                    blockedMoves.Add(nextSquare);

                //diagonal take
                SquareCoordinates opponentEnPassantPawn;
                ProcessSquare diagonalProc = delegate(SquareCoordinates to)
                {
                    if ((existingPiece = board.GetPiece(to)) != null)
                    {
                        if (!existingPiece.IsFriendly(white))
                            if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to, to)))
                                allowedMoves.Add(m);
                            else
                                movesWithCheck.Add(to);
                    }
                    else if ((opponentEnPassantPawn = EnPassant(board, to, p.White)).File != -1 && opponentEnPassantPawn.Rank != -1)
                    {
                        if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to, opponentEnPassantPawn)))
                            allowedMoves.Add(m);
                        else
                            movesWithCheck.Add(nextSquare);
                    }
                };
                nextFile = coord.File + 1;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextFile < 8)
                    diagonalProc(new SquareCoordinates(nextFile, nextRank)); //move diagonally up right if an opponent piece is in that space
                nextFile = coord.File - 1;
                //check if we are in bounds and that either no piece exists there, or the piece is not a friendly
                if (nextFile >= 0)
                    diagonalProc(new SquareCoordinates(nextFile, nextRank)); //move diagonally up left if an opponent piece is in that space
            }
            //check if we are still in the home position
            if (white && (nextRank = coord.Rank + 2) == 3 || !white && (nextRank = coord.Rank - 2) == 4)
            {
                nextSquare = new SquareCoordinates(coord.File, nextRank);
                SquareCoordinates betweenSquare = new SquareCoordinates(coord.File, (nextRank + coord.Rank) / 2);
                existingPiece = board.GetPiece(nextSquare);
                ChessPiece betweenPiece = board.GetPiece(betweenSquare);
                if (existingPiece == null && betweenPiece == null)
                    //move up 2 spaces if no piece is at that space and no piece is in between
                    if (!board.WillPlaceKingInCheck(p, coord, m = new Move(nextSquare)))
                        allowedMoves.Add(m);
                    else
                        movesWithCheck.Add(nextSquare);
                else
                    blockedMoves.Add(nextSquare);
            }
            return new MoveSet(allowedMoves, movesWithCheck, blockedMoves);
        }
コード例 #21
0
 internal void UnhighlightSquare(SquareCoordinates coord)
 {
     highlightedSquares.Remove(coord);
     using (Graphics g = this.CreateGraphics())
     {
         ChangeSquareBorderColor(g, BackColor, new BoardIndices(coord, whiteOnBottom));
     }
 }
コード例 #22
0
 private static MoveSet QueenMoves(ChessBoard board, SquareCoordinates coord, Player p)
 {
     //Queen's allowed moves are basically a combination of those of rook (diagonals) and bishop (vertical/horizontal)
     //so just call the methods to get rook and bishop moves and return a union of them
     return new MoveSet(RookMoves(board, coord, p), BishopMoves(board, coord, p));
 }
コード例 #23
0
        internal bool WillPlaceKingInCheck(Player p, SquareCoordinates from, Move move)
        {
            //temporarily moves a piece from the square 'from' to the square 'to'
            //without showing it on the board, and checks GameLogic to see if we are in check.
            //no matter the result, the board will be restored right afterwards.

            //perhaps just create a copy of the board so we don't have to restore the old pieces?
            SquareCoordinates to = move.Coordinates;
            SquareCoordinates taken = move.TakenPiece;
            ChessPiece fromPiece = pieces[from.File, from.Rank];
            ChessPiece takenPiece = pieces[taken.File, taken.Rank];
            pieces[taken.File, taken.Rank] = null;
            pieces[to.File, to.Rank] = fromPiece;
            pieces[from.File, from.Rank] = null;
            bool movedKing = fromPiece.IsA(PieceType.KING) && fromPiece.White == p.White;
            bool inCheck = GameLogic.KingInCheck(this, movedKing ? to : p.Material.KingSquare, p.White);
            pieces[from.File, from.Rank] = fromPiece;
            pieces[to.File, to.Rank] = null;
            pieces[taken.File, taken.Rank] = takenPiece;
            return inCheck;
        }
コード例 #24
0
        private static MoveSet RookMoves(ChessBoard board, SquareCoordinates coord, Player p)
        {
            List<Move> allowedMoves = new List<Move>();
            List<SquareCoordinates> movesWithCheck = new List<SquareCoordinates>();
            List<SquareCoordinates> blockedMoves = new List<SquareCoordinates>();
            bool boardOrientation = board.WhiteOnBottom;
            bool white = p.White;
            SquareCoordinates nextSquare/*, opponentEnPassantPawn*/;
            Move m;
            int nextFile, nextRank;

            ProcessSquare linearProc = delegate(SquareCoordinates to)
            {
                /*if ((opponentEnPassantPawn = EnPassant(board, to, p.White)).File != -1 && opponentEnPassantPawn.Rank != -1)
                    if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to, opponentEnPassantPawn)))
                        allowedMoves.Add(m);
                    else
                        movesWithCheck.Add(to);
                else*/
                if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to)))
                    allowedMoves.Add(m);
                else
                    movesWithCheck.Add(to);
            };

            ProcessSquare blockingPieceProc = delegate(SquareCoordinates to)
            {
                if (!board.GetPiece(to).IsFriendly(white))
                    if (!board.WillPlaceKingInCheck(p, coord, m = new Move(to, to)))
                        allowedMoves.Add(m);
                    else
                        movesWithCheck.Add(to);
                else
                    blockedMoves.Add(to);
            };

            nextFile = coord.File;
            nextRank = coord.Rank;
            while (++nextFile < 8 && board.GetPiece(nextSquare = new SquareCoordinates(nextFile, nextRank)) == null)
                linearProc(nextSquare);
            //we stop the loop if we encounter any piece, or if we start going out of bounds off the board.
            //if we did not stop at the edge of the board, check to see if the piece we stopped at is an opponent piece. if it is, we can move there as well
            if (nextFile < 8)
                blockingPieceProc(new SquareCoordinates(nextFile, nextRank));

            nextFile = coord.File;
            nextRank = coord.Rank;
            while (++nextRank < 8 && board.GetPiece(nextSquare = new SquareCoordinates(nextFile, nextRank)) == null)
                linearProc(nextSquare);
            //we stop the loop if we encounter any piece, or if we start going out of bounds off the board.
            //if we did not stop at the edge of the board, check to see if the piece we stopped at is an opponent piece. if it is, we can move there as well
            if (nextRank < 8)
                blockingPieceProc(new SquareCoordinates(nextFile, nextRank));

            nextFile = coord.File;
            nextRank = coord.Rank;
            while (--nextFile >= 0 && board.GetPiece(nextSquare = new SquareCoordinates(nextFile, nextRank)) == null)
                linearProc(nextSquare);
            //we stop the loop if we encounter any piece, or if we start going out of bounds off the board.
            //if we did not stop at the edge of the board, check to see if the piece we stopped at is an opponent piece. if it is, we can move there as well
            if (nextFile >= 0)
                blockingPieceProc(new SquareCoordinates(nextFile, nextRank));

            nextFile = coord.File;
            nextRank = coord.Rank;
            while (--nextRank >= 0 && board.GetPiece(nextSquare = new SquareCoordinates(nextFile, nextRank)) == null)
                linearProc(nextSquare);
            //we stop the loop if we encounter any piece, or if we start going out of bounds off the board.
            //if we did not stop at the edge of the board, check to see if the piece we stopped at is an opponent piece. if it is, we can move there as well
            if (nextRank >= 0)
                blockingPieceProc(new SquareCoordinates(nextFile, nextRank));

            return new MoveSet(allowedMoves, movesWithCheck, blockedMoves);
        }
コード例 #25
0
        protected override void MouseClicked(object sender, MouseEventArgs e)
        {
            int x = e.X, y = e.Y;
            int leftBound = LEFT_PAD + BORDER_WIDTH, rightBound = LEFT_PAD + 8 * (SQUARE_LENGTH + BORDER_WIDTH);
            int topBound = TOP_PAD + BORDER_WIDTH, bottomBound = TOP_PAD + 8 * (SQUARE_LENGTH + BORDER_WIDTH);
            if (x >= leftBound && x <= rightBound &&
                y >= topBound && y <= bottomBound)
            {
                x -= leftBound;
                y -= topBound;
                int row = y / (SQUARE_LENGTH + BORDER_WIDTH);
                int col = x / (SQUARE_LENGTH + BORDER_WIDTH);
                int subX = x - col * (SQUARE_LENGTH + BORDER_WIDTH), subY = y - row * (SQUARE_LENGTH + BORDER_WIDTH);

                //if we're not in a border (space b/w two squares), then select the piece
                if (subX < ChessBoard.SQUARE_LENGTH && subY < ChessBoard.SQUARE_LENGTH)
                {
                    SquareCoordinates coord = new SquareCoordinates(whiteOnBottom, new BoardIndices(row, col));
                    if (e.Button == MouseButtons.Left)
                    {
                        if (turn == Turn.LOCAL_WHITE)
                            whitePlayer.Select(this, coord);
                        else if (turn == Turn.LOCAL_BLACK)
                            blackPlayer.Select(this, coord);
                    }
                    else if (e.Button == MouseButtons.Right)
                    {
                        if (turn == Turn.LOCAL_WHITE)
                        {
                            if (GameLogic.CanCastle(this, coord, whitePlayer))
                            {
                                castleContextMenu.Visible = true;
                                castleKingside = (coord.File == 7);
                            }
                        }
                        else if (turn == Turn.LOCAL_BLACK)
                        {
                            if (GameLogic.CanCastle(this, coord, blackPlayer))
                            {
                                castleContextMenu.Visible = true;
                                castleKingside = (coord.File == 7);
                            }
                        }
                    }
                }
            }
        }
コード例 #26
0
 internal Move(SquareCoordinates moveTo)
 {
     coord = moveTo;
     overtaken = false;
 }
コード例 #27
0
        private Material[] SetDefaultPieces()
        {
            //reset all pieces in case we played on this board before
            ClearAllSquares();

            List<SquareCoordinates> wPieces = new List<SquareCoordinates>(15), bPieces = new List<SquareCoordinates>(15);
            SquareCoordinates wKing, bKing;
            SquareCoordinates coord;
            SetPiece(coord = new SquareCoordinates("A1"), new ChessPiece(true, PieceType.ROOK));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("B1"), new ChessPiece(true, PieceType.KNIGHT));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("C1"), new ChessPiece(true, PieceType.BISHOP));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("D1"), new ChessPiece(true, PieceType.QUEEN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("E1"), new ChessPiece(true, PieceType.KING));
            wKing = coord;
            SetPiece(coord = new SquareCoordinates("F1"), new ChessPiece(true, PieceType.BISHOP));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("G1"), new ChessPiece(true, PieceType.KNIGHT));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("H1"), new ChessPiece(true, PieceType.ROOK));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("A2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("B2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("C2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("D2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("E2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("F2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("G2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("H2"), new ChessPiece(true, PieceType.PAWN));
            wPieces.Add(coord);

            SetPiece(coord = new SquareCoordinates("A8"), new ChessPiece(false, PieceType.ROOK));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("B8"), new ChessPiece(false, PieceType.KNIGHT));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("C8"), new ChessPiece(false, PieceType.BISHOP));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("D8"), new ChessPiece(false, PieceType.QUEEN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("E8"), new ChessPiece(false, PieceType.KING));
            bKing = coord;
            SetPiece(coord = new SquareCoordinates("F8"), new ChessPiece(false, PieceType.BISHOP));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("G8"), new ChessPiece(false, PieceType.KNIGHT));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("H8"), new ChessPiece(false, PieceType.ROOK));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("A7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("B7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("C7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("D7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("E7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("F7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("G7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);
            SetPiece(coord = new SquareCoordinates("H7"), new ChessPiece(false, PieceType.PAWN));
            bPieces.Add(coord);

            return new Material[] { new Material(wPieces, wKing), new Material(bPieces, bKing) };
        }
コード例 #28
0
 internal Move(SquareCoordinates moveTo, SquareCoordinates overtake)
 {
     coord = moveTo;
     this.overtaken = true;
     takePiece = overtake;
 }
コード例 #29
0
 internal static byte[] WriteSelectSquare(SquareCoordinates coord)
 {
     return new byte[] { ChessOpHeaders.SELECT_SQUARE, (byte)coord.File, (byte)coord.Rank };
 }
コード例 #30
0
 private void SetPiece(SquareCoordinates coord, ChessPiece piece)
 {
     pieces[coord.File, coord.Rank] = piece;
 }