Beispiel #1
0
 public ChessPiece(ChessPieceColor color, ChessPieceType type, Position position, IMovementStrategy movementStrategy)
 {
     Color    = color;
     Type     = type;
     Position = position;
     movement = movementStrategy;
 }
Beispiel #2
0
        private int SideToMoveScore(int score, ChessPieceColor color)
        {
            if (color == ChessPieceColor.Black)
                return -score;

            return score;
        }
Beispiel #3
0
 public Knight(BoardCoordinate initialLocation, ChessPieceColor owningPlayerOrientation) : base(initialLocation, owningPlayerOrientation)
 {
     this._allowedMoves = new List <ChessPieceMoveDefinition>();
     this._allowedMoves.Add(StandardMoveDefinitions.KnightStandardFileMoveDefinition(owningPlayerOrientation));
     this._allowedMoves.Add(StandardMoveDefinitions.KnightStandardRankMoveDefinition(owningPlayerOrientation));
     base.AlgebraicNotationName = "N";
 }
        private void chessBoard_TurnChanged(ChessPieceColor whosMove)
        {
            if (whosMove == ChessPieceColor.White)
            {
                //notifyIcon.Visible = false;
                lblTurn.Text = @"White's Turn";

                WhosMove        = ChessPieceColor.White;
                notifyIcon.Text = @"White's Turn";
                notifyIcon.ShowBalloonTip(1000, @"White's Turn", @"White's Turn", ToolTipIcon.Info);

                notifyIcon.Visible = true;
            }
            if (whosMove == ChessPieceColor.Black)
            {
                //notifyIcon.Visible = false;
                lblTurn.Text = @"Black's Turn";

                WhosMove        = ChessPieceColor.Black;
                notifyIcon.Text = @"Black's Turn";
                notifyIcon.ShowBalloonTip(1000, @"Black's Turn", @"Black's Turn", ToolTipIcon.Info);

                notifyIcon.Visible = true;
            }
        }
        private static bool SetEnpassantMove(Board board, byte dstPosition, ChessPieceColor pcColor)
        {
            //En Passant
            if (board.EnPassantPosition == dstPosition)
            {
                //We have an En Passant Possible
                if (pcColor != board.EnPassantColor)
                {
                    int pieceLocationOffset = 8;

                    if (board.EnPassantColor == ChessPieceColor.White)
                    {
                        pieceLocationOffset = -8;
                    }

                    dstPosition = (byte)(dstPosition + pieceLocationOffset);

                    Square sqr = board.Squares[dstPosition];

                    board.LastMove.TakenPiece = new PieceTaken(sqr.Piece.PieceColor, sqr.Piece.PieceType, sqr.Piece.Moved, dstPosition);

                    board.Squares[dstPosition].Piece = null;

                    //Reset FiftyMoveCount if capture
                    board.FiftyMove = 0;

                    return(true);
                }
            }

            return(false);
        }
Beispiel #6
0
        internal static bool IsInCheck(ChessBoard board, ChessPieceColor kingColor)
        {
            ChessPieceColor colorToCheck = InvertColor(kingColor);
            ulong           checkTarget  = board.GetBoardState(kingColor).King;

            return(IsInCheck(board, colorToCheck, checkTarget));
        }
Beispiel #7
0
        internal static ulong UpdateZobristHash(ulong hash, MoveContent move, ChessPieceColor whosMove, bool pawnPromote)
        {
            hash = Zobrist.GetHashResult((int)move.MovingPiecePrimary.SrcPosition, move.MovingPiecePrimary.PieceType, move.MovingPiecePrimary.PieceColor, hash);
            if (move.MovingPieceSecondary.PieceType != ChessPieceType.None)
            {
                hash = Zobrist.GetHashResult((int)move.MovingPieceSecondary.SrcPosition, move.MovingPieceSecondary.PieceType, move.MovingPieceSecondary.PieceColor, hash);
                hash = Zobrist.GetHashResult((int)move.MovingPieceSecondary.DstPosition, move.MovingPieceSecondary.PieceType, move.MovingPieceSecondary.PieceColor, hash);
                if (move.MovingPieceSecondary.PieceColor == ChessPieceColor.White)
                {
                    hash ^= Zobrist.ZobristTable[770];
                }
                else
                {
                    hash ^= Zobrist.ZobristTable[771];
                }
            }
            if (move.TakenPiece.PieceType != ChessPieceType.None)
            {
                hash = Zobrist.GetHashResult((int)move.TakenPiece.Position, move.TakenPiece.PieceType, move.TakenPiece.PieceColor, hash);
            }
            hash = !pawnPromote?Zobrist.GetHashResult((int)move.MovingPiecePrimary.DstPosition, move.MovingPiecePrimary.PieceType, move.MovingPiecePrimary.PieceColor, hash) : Zobrist.GetHashResult((int)move.MovingPiecePrimary.DstPosition, ChessPieceType.Queen, move.MovingPiecePrimary.PieceColor, hash);

            if (whosMove == ChessPieceColor.White)
            {
                hash ^= Zobrist.ZobristTable[769];
                hash ^= Zobrist.ZobristTable[768];
            }
            else
            {
                hash ^= Zobrist.ZobristTable[768];
                hash ^= Zobrist.ZobristTable[769];
            }
            return(hash);
        }
Beispiel #8
0
        public void SetTeamColor(ChessPieceColor color)
        {
            var cl            = Global.TeamBlueColor;
            var tankerImage   = Properties.Resources.Tanker_Blue;
            var rangerImage   = Properties.Resources.Ranger_Blue;
            var ambusherImage = Properties.Resources.Ambusher_Blue;
            var campImage     = Properties.Resources.Camp_Blue;

            if (color == ChessPieceColor.Green)
            {
                cl = Global.TeamGreenColor;

                tankerImage   = Properties.Resources.Tanker_Green;
                rangerImage   = Properties.Resources.Ranger_Green;
                ambusherImage = Properties.Resources.Ambusher_Green;
                campImage     = Properties.Resources.Camp_Green;
            }

            this.AmbusherImage = ambusherImage;
            this.RangerImage   = rangerImage;
            this.TankerImage   = tankerImage;
            this.CampImage     = campImage;

            lbTeam.BackColor          = cl;
            picTanker.SelectedColor   = cl;
            picRanger.SelectedColor   = cl;
            picAmbusher.SelectedColor = cl;
            picCamp.SelectedColor     = cl;
            btnReady.BackColor        = cl;
        }
Beispiel #9
0
        private static bool AddKnightMoves(ChessBoard board, ChessPieceColor color, List <ChessMove> moveList, ulong checkTarget)
        {
            bool  res         = false;
            ulong eligSquares = ~board.GetBoardState(color).Pieces;   // Can't move where we have own pieces.

            ulong remKnights = board.GetBoardState(color).Knights;

            while (remKnights != 0)
            {
                uint  squareFrom = Util.bitScanForward(remKnights);
                ulong target     = Precomputed.KnightMoves[squareFrom] & eligSquares;

                if (moveList != null)
                {
                    GenerateMoves(squareFrom, target, ChessPieceType.Knight, moveList);
                }
                else
                {
                    if ((target & checkTarget) != 0)
                    {
                        res = true;
                        break;
                    }
                }

                remKnights ^= Precomputed.IndexToBitBoard[squareFrom];  // Remove the knight.
            }
            return(res);
        }
Beispiel #10
0
        private static bool AddQueenMoves(ChessBoard board, ChessPieceColor color, List <ChessMove> moveList, ulong checkTarget)
        {
            bool  res         = false;
            ulong eligSquares = ~board.GetBoardState(color).Pieces;   // Can't move where we have own pieces.

            ulong occBoard = board.whiteBoardState.Pieces | board.blackBoardState.Pieces;

            ulong remQueens = board.GetBoardState(color).Queens;

            while (remQueens != 0)
            {
                uint  squareFrom = Util.bitScanForward(remQueens);
                ulong target     = GetRookMoves(squareFrom, occBoard, eligSquares) | GetBishopMoves(squareFrom, occBoard, eligSquares);

                if (moveList != null)
                {
                    GenerateMoves(squareFrom, target, ChessPieceType.Queen, moveList);
                }
                else
                {
                    if ((target & checkTarget) != 0)
                    {
                        res = true;
                        break;
                    }
                }

                remQueens ^= Precomputed.IndexToBitBoard[squareFrom];  // Remove the queen.
            }
            return(res);
        }
Beispiel #11
0
 public PieceTaken(ChessPieceColor pieceColor, ChessPieceType pieceType, bool moved, byte position)
 {
     this.PieceColor = pieceColor;
     this.PieceType  = pieceType;
     this.Position   = position;
     this.Moved      = moved;
 }
Beispiel #12
0
        internal static bool IsInCheck(ChessBoard board, ChessPieceColor colorToCheck, ulong checkTarget)
        {
            bool res = false;

            if (AddPawnMoves(board, colorToCheck, null, checkTarget))
            {
                res = true;
            }
            else if (AddKnightMoves(board, colorToCheck, null, checkTarget))
            {
                res = true;
            }
            else if (AddBishopMoves(board, colorToCheck, null, checkTarget))
            {
                res = true;
            }
            else if (AddRookMoves(board, colorToCheck, null, checkTarget))
            {
                res = true;
            }
            else if (AddQueenMoves(board, colorToCheck, null, checkTarget))
            {
                res = true;
            }
            else if (AddKingMoves(board, colorToCheck, null, checkTarget))
            {
                res = true;
            }

            return(res);
        }
Beispiel #13
0
        public ChessBoard(ChessBoard board)
        {
            Score        = board.Score;
            EndGamePhase = board.EndGamePhase;
            WhoseMove    = board.WhoseMove;
            MoveCount    = board.MoveCount;
            FiftyMove    = board.FiftyMove;
            RepeatedMove = board.RepeatedMove;

            blackCastled         = board.blackCastled;
            blackInCheck         = board.blackInCheck;
            blackInMate          = board.blackInMate;
            whiteCastled         = board.whiteCastled;
            whiteInCheck         = board.whiteInCheck;
            whiteInMate          = board.whiteInMate;
            staleMate            = board.staleMate;
            EnPassantColor       = board.EnPassantColor;
            EnPassantPosition    = board.EnPassantPosition;
            InsufficientMaterial = board.InsufficientMaterial;

            LastMove = new MoveContent(board.LastMove);

            pieces = new ChessPiece[64];
            for (byte x = 0; x < 64; ++x)
            {
                if (board.pieces[x] != null)
                {
                    pieces[x] = new ChessPiece(board.pieces[x]);
                }
            }
        }
 public static void InitChessPiece(ref ChessPiece piece, ChessPieceColor color, ChessPieceType type)
 {
     piece           = new ChessPiece();
     piece.color     = color;
     piece.type      = type;
     piece.firstTurn = true;
 }
Beispiel #15
0
        public ChessBoard(ChessBoard board)
        {
            Score = board.Score;
            EndGamePhase = board.EndGamePhase;
            WhoseMove = board.WhoseMove;
            MoveCount = board.MoveCount;
            FiftyMove = board.FiftyMove;
            RepeatedMove = board.RepeatedMove;

            blackCastled = board.blackCastled;
            blackInCheck = board.blackInCheck;
            blackInMate = board.blackInMate;
            whiteCastled = board.whiteCastled;
            whiteInCheck = board.whiteInCheck;
            whiteInMate = board.whiteInMate;
            staleMate = board.staleMate;
            EnPassantColor = board.EnPassantColor;
            EnPassantPosition = board.EnPassantPosition;
            InsufficientMaterial = board.InsufficientMaterial;

            LastMove = new MoveContent(board.LastMove);

            pieces = new ChessPiece[64];
            for (byte x = 0; x < 64; ++x)
            {
                if (board.pieces[x] != null)
                {
                    pieces[x] = new ChessPiece(board.pieces[x]);
                }
            }
        }
Beispiel #16
0
 public King(BoardCoordinate initialLocation, ChessPieceColor owningPlayerOrientation) : base(initialLocation, owningPlayerOrientation)
 {
     this._allowedMoves = new List <ChessPieceMoveDefinition>();
     this._allowedMoves.Add(StandardMoveDefinitions.KingStandardFileMoveDefinition(owningPlayerOrientation));
     this._allowedMoves.Add(StandardMoveDefinitions.KingStandardRankMoveDefinition(owningPlayerOrientation));
     this._allowedMoves.Add(StandardMoveDefinitions.KingStandardDiagonalMoveDefinition(owningPlayerOrientation));
 }
Beispiel #17
0
 internal PieceTaken(ChessPieceType pieceType)
 {
     PieceColor = ChessPieceColor.White;
     PieceType = pieceType;
     Position = 0;
     Moved = false;
 }
Beispiel #18
0
 public PieceTaken(ChessPieceType pieceType)
 {
     this.PieceColor = ChessPieceColor.White;
     this.PieceType  = pieceType;
     this.Position   = (byte)0;
     this.Moved      = false;
 }
Beispiel #19
0
        internal static List <ChessMove> GetPossibleMoves(ChessBoard board, ChessPieceColor color)
        {
            List <ChessMove> moveList = new List <ChessMove>();

            AddPawnMoves(board, color, moveList, 0);
            AddKnightMoves(board, color, moveList, 0);
            AddBishopMoves(board, color, moveList, 0);
            AddRookMoves(board, color, moveList, 0);
            AddQueenMoves(board, color, moveList, 0);
            AddKingMoves(board, color, moveList, 0);

            // Check so the moves do not put own king in check.
            List <ChessMove> moves = new List <ChessMove>();

            foreach (ChessMove move in moveList)
            {
                ChessBoard resBoard = new ChessBoard(board);
                resBoard.ApplyMove(color, move, null);
                if (!IsInCheck(resBoard, color))
                {
                    moves.Add(move);
                }
            }

            return(moves);
        }
Beispiel #20
0
 public PieceTaken(ChessPieceType pieceType)
 {
     PieceColor = ChessPieceColor.White;
     PieceType  = pieceType;
     Position   = 0;
     Moved      = false;
 }
Beispiel #21
0
        private void chessBoard_TurnChanged(ChessPieceColor whosMove)
        {
            if (whosMove == ChessPieceColor.White)
            {
                //notifyIcon.Visible = false;
                lblTurn.Text = @"White's Turn";

                WhosMove = ChessPieceColor.White;
                notifyIcon.Text = @"White's Turn";
                notifyIcon.ShowBalloonTip(1000, @"White's Turn", @"White's Turn", ToolTipIcon.Info);

                notifyIcon.Visible = true;
            }
            if (whosMove == ChessPieceColor.Black)
            {
                //notifyIcon.Visible = false;
                lblTurn.Text = @"Black's Turn";

                WhosMove = ChessPieceColor.Black;
                notifyIcon.Text = @"Black's Turn";
                notifyIcon.ShowBalloonTip(1000, @"Black's Turn", @"Black's Turn", ToolTipIcon.Info);

                notifyIcon.Visible = true;
            }
        }
Beispiel #22
0
 public Pawn(IChessBoard board, ChessPieceColor color)
 {
     this.moved      = false;
     this.chessBoard = board;
     this.Color      = color;
     this.Type       = ChessPieceType.PAWN;
 }
Beispiel #23
0
 public CPClass(int _x, int _y, ChessPieceColor _pieceColor)
 {
     x          = _x;
     y          = _y;
     pieceColor = _pieceColor;
     moved      = false;
 }
Beispiel #24
0
        internal static bool SetEnpassantMove(ChessBoard board, byte dstPosition, ChessPieceColor pcColor)
        {
            //En Passant
            if (board.EnPassantPosition == dstPosition && pcColor != board.EnPassantColor)
            {
                int pieceLocationOffset = 8;
                if (board.EnPassantColor == ChessPieceColor.White)
                {
                    pieceLocationOffset = -8;
                }

                dstPosition = (byte)(dstPosition + pieceLocationOffset);

                ChessPiece piece = board.pieces[dstPosition];

                board.LastMove.TakenPiece = new PieceTaken(piece.PieceColor, piece.Identifier, piece.Moved, dstPosition);

                board.pieces[dstPosition] = null;

                //Reset FiftyMoveCount if capture
                board.FiftyMove = 0;

                return(true);
            }

            return(false);
        }
Beispiel #25
0
        private static ResultBoards GetPossibleBoards(ChessPieceColor movingSide, Board examineBoard)
        {
            //We are going to store our result boards here
            resultBoards = new ResultBoards
            {
                Positions = new List <Board>()
            };

            for (byte x = 0; x < 64; x++)
            {
                Square sqr = examineBoard.Squares[x];

                //Make sure there is a piece on the square
                if (sqr.Piece == null)
                {
                    continue;
                }

                //Make sure the color is the same color as the one we are moving.
                if (sqr.Piece.PieceColor != movingSide)
                {
                    continue;
                }

                //For each valid move for this piece
                foreach (byte dst in sqr.Piece.ValidMoves)
                {
                    //We make copies of the board and move so that we can move it without effecting the parent board
                    Board board = examineBoard.FastCopy();

                    //Make move so we can examine it
                    Board.MovePiece(board, x, dst, ChessPieceType.Queen);

                    //We Generate Valid Moves for Board
                    PieceValidMoves.GenerateValidMoves(board);


                    if (board.BlackCheck && movingSide == ChessPieceColor.Black)
                    {
                        continue;
                    }

                    if (board.WhiteCheck && movingSide == ChessPieceColor.White)
                    {
                        continue;
                    }

                    //We calculate the board score
                    Evaluation.EvaluateBoardScore(board);

                    //Invert Score to support Negamax
                    board.Score = SideToMoveScore(board.Score, GetOppositeColor(movingSide));

                    resultBoards.Positions.Add(board);
                }
            }

            return(resultBoards);
        }
Beispiel #26
0
 public PieceMoving(ChessPieceType pieceType)
 {
     PieceType   = pieceType;
     PieceColor  = ChessPieceColor.White;
     SrcPosition = 0;
     DstPosition = 0;
     Moved       = false;
 }
Beispiel #27
0
 public static ChessPieceMoveDefinition QueenStandardRankMoveDefinition(ChessPieceColor orientation)
 {
     if (queenStandardRankMoveDefinition == default(ChessPieceMoveDefinition))
     {
         queenStandardRankMoveDefinition = new ChessPieceMoveDefinition(0, 1, 8, ChessPieceMoveDirections.Rank);
     }
     return(queenStandardRankMoveDefinition);
 }
Beispiel #28
0
 public PieceTaken(ChessPieceColor pieceColor, ChessPieceType pieceType, bool moved,
                   byte position)
 {
     PieceColor = pieceColor;
     PieceType  = pieceType;
     Position   = position;
     Moved      = moved;
 }
Beispiel #29
0
 public static ChessPieceMoveDefinition KnightStandardRankMoveDefinition(ChessPieceColor orientation)
 {
     if (knightStandardRankMoveDefinition == default(ChessPieceMoveDefinition))
     {
         knightStandardRankMoveDefinition = new ChessPieceMoveDefinition(1, 2, 1, ChessPieceMoveDirections.AscendingDiagonal | ChessPieceMoveDirections.DescendingDiagonal);
     }
     return(knightStandardRankMoveDefinition);
 }
Beispiel #30
0
 public PieceMoving(PieceMoving pieceMoving)
 {
     PieceColor  = pieceMoving.PieceColor;
     PieceType   = pieceMoving.PieceType;
     SrcPosition = pieceMoving.SrcPosition;
     DstPosition = pieceMoving.DstPosition;
     Moved       = pieceMoving.Moved;
 }
Beispiel #31
0
 public static ChessPieceMoveDefinition QueenStandardDiagonalMoveDefinition(ChessPieceColor orientation)
 {
     if (queenStandardDiagonalMoveDefinition == default(ChessPieceMoveDefinition))
     {
         queenStandardDiagonalMoveDefinition = new ChessPieceMoveDefinition(1, 1, 8, ChessPieceMoveDirections.AscendingDiagonal | ChessPieceMoveDirections.DescendingDiagonal);
     }
     return(queenStandardDiagonalMoveDefinition);
 }
Beispiel #32
0
 public static ChessPieceMoveDefinition RookStandardRankMoveDefinition(ChessPieceColor orientation)
 {
     if (rookStandardRankMoveDefinition == default(ChessPieceMoveDefinition))
     {
         rookStandardRankMoveDefinition = new ChessPieceMoveDefinition(0, 8, 8, ChessPieceMoveDirections.PositiveRank | ChessPieceMoveDirections.NegativeRank);
     }
     return(rookStandardRankMoveDefinition);
 }
Beispiel #33
0
 public PieceMoving(PieceMoving pieceMoving)
 {
     PieceColor = pieceMoving.PieceColor;
     PieceType = pieceMoving.PieceType;
     SrcPosition = pieceMoving.SrcPosition;
     DstPosition = pieceMoving.DstPosition;
     Moved = pieceMoving.Moved;
 }
Beispiel #34
0
 public PieceMoving(ChessPieceType pieceType)
 {
     PieceType = pieceType;
     PieceColor = ChessPieceColor.White;
     SrcPosition = 0;
     DstPosition = 0;
     Moved = false;
 }
Beispiel #35
0
 internal PieceTaken(ChessPieceColor pieceColor, ChessPieceType pieceType, bool moved,
                   byte position)
 {
     PieceColor = pieceColor;
     PieceType = pieceType;
     Position = position;
     Moved = moved;
 }
Beispiel #36
0
 private void InitiateBoard(string fen)
 {
     HumanPlayer          = ChessPieceColor.White;
     ChessBoard           = new Board(fen);
     ChessBoard.WhoseMove = ChessPieceColor.White;
     PieceMove.InitiateChessPieceMotion();
     GenerateValidMoves();
 }
Beispiel #37
0
 public PieceMoving(ChessPieceColor pieceColor, ChessPieceType pieceType, bool moved,
                    byte srcPosition, byte dstPosition)
 {
     PieceColor = pieceColor;
     PieceType = pieceType;
     SrcPosition = srcPosition;
     DstPosition = dstPosition;
     Moved = moved;
 }
Beispiel #38
0
        public ChessPiece(ChessPiece piece)
        {
            Identifier = piece.Identifier;
            PieceColor = piece.PieceColor;
            PieceValue = piece.PieceValue;
            PieceActionValue = piece.PieceActionValue;
            Moved = piece.Moved;

            if (piece.ValidMoves != null)
                LastValidMoveCount = piece.ValidMoves.Count;
        }
        internal Piece(Piece piece)
        {
            PieceColor = piece.PieceColor;
            PieceType = piece.PieceType;
            Moved = piece.Moved;
            PieceValue = piece.PieceValue;
            PieceActionValue = piece.PieceActionValue;

            if (piece.ValidMoves != null)
                LastValidMoveCount = piece.ValidMoves.Count;
        }
Beispiel #40
0
        internal static bool CheckForMate(ChessPieceColor whosTurn, ChessBoard chessBoard)
        {
            SearchMove.SearchForMate(whosTurn, chessBoard, ref chessBoard.blackInMate,
                  ref chessBoard.whiteInMate, ref chessBoard.staleMate);

            if (chessBoard.blackInMate || chessBoard.whiteInMate || chessBoard.staleMate)
            {
                return true;
            }

            return false;
        }
Beispiel #41
0
        public ChessPiece(ChessPieceType id, byte number, bool isBlack)
        {
            this.Identifier = id;
            if (isBlack) this.PieceColor = ChessPieceColor.Black;
            else this.PieceColor = ChessPieceColor.White;
            this.Moved = false;
            this.LastValidMoveCount = 0;
            this.ValidMoves = new Stack<byte>();

            PieceValue = CalculatePieceValue(id);
            PieceActionValue = CalculatePieceActionValue(id);
        }
        internal Piece(ChessPieceType chessPiece, ChessPieceColor chessPieceColor)
        {
            PieceType = chessPiece;
            PieceColor = chessPieceColor;

            if (PieceType == ChessPieceType.Pawn || PieceType == ChessPieceType.Knight)
            {
                LastValidMoveCount = 2;
            }
            else
            {
                LastValidMoveCount = 0;
            }

            ValidMoves = new Stack<byte>(LastValidMoveCount);

            PieceValue = CalculatePieceValue(PieceType);
            PieceActionValue = CalculatePieceActionValue(PieceType);
        }
Beispiel #43
0
 public ChessBoard()
 {
     Score = 0;
     blackInCheck = false;
     blackInMate = false;
     whiteInCheck = false;
     whiteInMate = false;
     staleMate = false;
     FiftyMove = 0;
     RepeatedMove = 0;
     blackCastled = false;
     whiteCastled = false;
     EndGamePhase = false;
     MoveCount = 0;
     EnPassantPosition = 0;
     InsufficientMaterial = false;
     EnPassantColor = ChessPieceColor.White;
     WhoseMove = ChessPieceColor.White;
     LastMove = new MoveContent();
     pieces = new ChessPiece[64];
 }
        public static bool SearchForMate(ChessPieceColor movingSide, Board examineBoard, ref bool blackMate, ref bool whiteMate, ref bool staleMate)
        {
            bool foundNonCheckBlack = false;
            bool foundNonCheckWhite = false;

            for (byte x = 0; x < 64; x++)
            {
                Square sqr = examineBoard.Squares[x];

                //Make sure there is a piece on the square
                if (sqr.Piece == null)
                    continue;

                //Make sure the color is the same color as the one we are moving.
                if (sqr.Piece.PieceColor != movingSide)
                    continue;

                //For each valid move for this piece
                foreach (byte dst in sqr.Piece.ValidMoves)
                {

                    //We make copies of the board and move so we don't change the original
                    Board board = examineBoard.FastCopy();

                    //Make move so we can examine it
                    Board.MovePiece(board, x, dst, ChessPieceType.Queen);

                    //We Generate Valid Moves for Board
                    PieceValidMoves.GenerateValidMoves(board);

                    if (board.BlackCheck == false)
                    {
                        foundNonCheckBlack = true;
                    }
                    else if (movingSide == ChessPieceColor.Black)
                    {
                        continue;
                    }

                    if (board.WhiteCheck == false)
                    {
                        foundNonCheckWhite = true;
                    }
                    else if (movingSide == ChessPieceColor.White)
                    {
                        continue;
                    }
                }
            }

            if (foundNonCheckBlack == false)
            {
                if (examineBoard.BlackCheck)
                {
                    blackMate = true;
                    return true;
                }
                if (!examineBoard.WhiteMate && movingSide != ChessPieceColor.White)
                {
                    staleMate = true;
                    return true;
                }
            }

            if (foundNonCheckWhite == false)
            {
                if (examineBoard.WhiteCheck)
                {
                    whiteMate = true;
                    return true;
                }
                if (!examineBoard.BlackMate && movingSide != ChessPieceColor.Black)
                {
                    staleMate = true;
                    return true;
                }
            }

            return false;
        }
Beispiel #45
0
        internal static bool SearchForMate(ChessPieceColor movingSide, ChessBoard examineBoard, ref bool blackMate, ref bool whiteMate, ref bool staleMate)
        {
            bool foundNonCheckBlack = false;
            bool foundNonCheckWhite = false;

            for (byte x = 0; x < 64; ++x)
            {
                ChessPiece piece = examineBoard.pieces[x];

                //Make sure there is a piece on the square
                //Make sure the color is the same color as the one we are moving.
                if (piece == null || piece.PieceColor != movingSide)
                    continue;

                //For each valid move for this piece
                foreach (byte dst in piece.ValidMoves)
                {
                    //We make copies of the board and move so we don't change the original
                    ChessBoard board = new ChessBoard(examineBoard);

                    //Make move so we can examine it
                    ChessEngine.MoveContent(board, x, dst, ChessPieceType.Queen);

                    //We Generate Valid Moves for Board
                    PieceValidMoves.GenerateValidMoves(board);

                    if (!board.blackInCheck)
                    {
                        foundNonCheckBlack = true;
                    }
                    else if (movingSide == ChessPieceColor.Black)
                    {
                        continue;
                    }

                    if (!board.whiteInCheck)
                    {
                        foundNonCheckWhite = true;
                    }
                    else if (movingSide == ChessPieceColor.White)
                    {
                        continue;
                    }
                }
            }

            if (!foundNonCheckBlack)
            {
                if (examineBoard.blackInCheck)
                {
                    blackMate = true;
                    return true;
                }
                if (!examineBoard.whiteInMate && movingSide != ChessPieceColor.White)
                {
                    staleMate = true;
                    return true;
                }
            }

            if (!foundNonCheckWhite)
            {
                if (examineBoard.whiteInCheck)
                {
                    whiteMate = true;
                    return true;
                }
                if (!examineBoard.blackInMate && movingSide != ChessPieceColor.Black)
                {
                    staleMate = true;
                    return true;
                }
            }
            return false;
        }
Beispiel #46
0
 private static int SideToMoveScore(int score, ChessPieceColor color)
 {
     if (color == ChessPieceColor.Black)
         return -score;
     else
         return score;
 }
        private void SetChessPieceCount(ChessPieceColor color, ChessPieceType type, int count)
        {
            string canvasPropertyName = string.Format("{0}Player{1}", color, type);
            string textCountPropertyName = string.Format("{0}Player{1}Count", color, type);
            var canvasProperty = GetType().GetTypeInfo().GetDeclaredProperty(canvasPropertyName);
            var textCountcanvasProperty = GetType().GetTypeInfo().GetDeclaredProperty(textCountPropertyName);

            if (canvasProperty != null && textCountcanvasProperty != null)
                SetChessPieceCount((Canvas)canvasProperty.GetValue(this), (TextBlock)textCountcanvasProperty.GetValue(this), count);
        }
 public ChessPiece(ChessPieceColor color, ChessPieceType type)
 {
     this.color = color;
     this.type = type;
 }
Beispiel #49
0
        //Default Constructor
        internal Board(string fen)
            : this()
        {
            byte index = 0;
            byte spc = 0;

            WhiteCastled = true;
            BlackCastled = true;
            byte spacers = 0;

            WhoseMove = ChessPieceColor.White;

            if (fen.Contains("a3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 40;
            }
            else if (fen.Contains("b3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 41;
            }
            else if (fen.Contains("c3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 42;
            }
            else if (fen.Contains("d3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 43;
            }
            else if (fen.Contains("e3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 44;
            }
            else if (fen.Contains("f3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 45;
            }
            else if (fen.Contains("g3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 46;
            }
            else if (fen.Contains("h3"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 47;
            }

            if (fen.Contains("a6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 16;
            }
            else if (fen.Contains("b6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 17;
            }
            else if (fen.Contains("c6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition =18;
            }
            else if (fen.Contains("d6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 19;
            }
            else if (fen.Contains("e6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 20;
            }
            else if (fen.Contains("f6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 21;
            }
            else if (fen.Contains("g6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 22;
            }
            else if (fen.Contains("h6"))
            {
                EnPassantColor = ChessPieceColor.White;
                EnPassantPosition = 23;
            }

            foreach (char c in fen)
            {

                if (index < 64 && spc == 0)
                {
                    if (c == '1' && index < 63)
                    {
                        index++;
                    }
                    else if (c == '2' && index < 62)
                    {
                        index += 2;
                    }
                    else if (c == '3' && index < 61)
                    {
                        index += 3;
                    }
                    else if (c == '4' && index < 60)
                    {
                        index += 4;
                    }
                    else if (c == '5' && index < 59)
                    {
                        index += 5;
                    }
                    else if (c == '6' && index < 58)
                    {
                        index += 6;
                    }
                    else if (c == '7' && index < 57)
                    {
                        index += 7;
                    }
                    else if (c == '8' && index < 56)
                    {
                        index += 8;
                    }
                    else if (c == 'P')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Pawn, ChessPieceColor.White);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'N')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Knight, ChessPieceColor.White);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'B')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Bishop, ChessPieceColor.White);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'R')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Rook, ChessPieceColor.White);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'Q')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Queen, ChessPieceColor.White);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'K')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.King, ChessPieceColor.White);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'p')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Pawn, ChessPieceColor.Black);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'n')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Knight, ChessPieceColor.Black);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'b')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Bishop, ChessPieceColor.Black);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'r')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Rook, ChessPieceColor.Black);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'q')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.Queen, ChessPieceColor.Black);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == 'k')
                    {
                        Squares[index].Piece = new Piece(ChessPieceType.King, ChessPieceColor.Black);
                        Squares[index].Piece.Moved = true;
                        index++;
                    }
                    else if (c == '/')
                    {
                        continue;
                    }
                    else if (c == ' ')
                    {
                        spc++;
                    }
                }
                else
                {
                    if (c == 'w')
                    {
                        WhoseMove = ChessPieceColor.White;
                    }
                    else if (c == 'b')
                    {
                        WhoseMove = ChessPieceColor.Black;
                    }
                    else if (c == 'K')
                    {
                        if (Squares[60].Piece != null)
                        {
                            if (Squares[60].Piece.PieceType == ChessPieceType.King)
                            {
                                Squares[60].Piece.Moved = false;
                            }
                        }

                        if (Squares[63].Piece != null)
                        {
                            if (Squares[63].Piece.PieceType == ChessPieceType.Rook)
                            {
                                Squares[63].Piece.Moved = false;
                            }
                        }

                        WhiteCastled = false;
                    }
                    else if (c == 'Q')
                    {
                        if (Squares[60].Piece != null)
                        {
                            if (Squares[60].Piece.PieceType == ChessPieceType.King)
                            {
                                Squares[60].Piece.Moved = false;
                            }
                        }

                        if (Squares[56].Piece != null)
                        {
                            if (Squares[56].Piece.PieceType == ChessPieceType.Rook)
                            {
                                Squares[56].Piece.Moved = false;
                            }
                        }

                        WhiteCastled = false;
                    }
                    else if (c == 'k')
                    {
                        if (Squares[4].Piece != null)
                        {
                            if (Squares[4].Piece.PieceType == ChessPieceType.King)
                            {
                                Squares[4].Piece.Moved = false;
                            }
                        }

                        if (Squares[7].Piece != null)
                        {
                            if (Squares[7].Piece.PieceType == ChessPieceType.Rook)
                            {
                                Squares[7].Piece.Moved = false;
                            }
                        }

                        BlackCastled = false;
                    }
                    else if (c == 'q')
                    {
                        if (Squares[4].Piece != null)
                        {
                            if (Squares[4].Piece.PieceType == ChessPieceType.King)
                            {
                                Squares[4].Piece.Moved = false;
                            }
                        }

                        if (Squares[0].Piece != null)
                        {
                            if (Squares[0].Piece.PieceType == ChessPieceType.Rook)
                            {
                                Squares[0].Piece.Moved = false;
                            }
                        }

                        BlackCastled = false;
                    }
                    else if (c == ' ')
                    {
                        spacers++;
                    }
                    else if (c == '1' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 1);
                    }
                    else if (c == '2' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 2);
                    }
                    else if (c == '3' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 3);
                    }
                    else if (c == '4' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 4);
                    }
                    else if (c == '5' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 5);
                    }
                    else if (c == '6' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 6);
                    }
                    else if (c == '7' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 7);
                    }
                    else if (c == '8' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 8);
                    }
                    else if (c == '9' && spacers == 4)
                    {
                        FiftyMove = (byte)((FiftyMove * 10) + 9);
                    }
                    else if (c == '0' && spacers == 4)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 0);
                    }
                    else if (c == '1' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 1);
                    }
                    else if (c == '2' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 2);
                    }
                    else if (c == '3' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 3);
                    }
                    else if (c == '4' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 4);
                    }
                    else if (c == '5' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 5);
                    }
                    else if (c == '6' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 6);
                    }
                    else if (c == '7' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 7);
                    }
                    else if (c == '8' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 8);
                    }
                    else if (c == '9' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 9);
                    }
                    else if (c == '0' && spacers == 5)
                    {
                        MoveCount = (byte)((MoveCount * 10) + 0);
                    }

                }
            }
        }
Beispiel #50
0
        private static bool SetEnpassantMove(Board board, byte dstPosition, ChessPieceColor pcColor)
        {
            //En Passant
            if (board.EnPassantPosition == dstPosition)
            {
                //We have an En Passant Possible
                if (pcColor != board.EnPassantColor)
                {
                    int pieceLocationOffset = 8;

                    if (board.EnPassantColor == ChessPieceColor.White)
                    {
                        pieceLocationOffset = -8;
                    }

                    dstPosition = (byte)(dstPosition + pieceLocationOffset);

                    Square sqr = board.Squares[dstPosition];

                    board.LastMove.TakenPiece = new PieceTaken(sqr.Piece.PieceColor, sqr.Piece.PieceType, sqr.Piece.Moved, dstPosition);

                    board.Squares[dstPosition].Piece = null;

                    //Reset FiftyMoveCount if capture
                    board.FiftyMove = 0;

                    return true;
                }
            }

            return false;
        }
Beispiel #51
0
        private static void RecordEnPassant(ChessPieceColor pcColor, ChessPieceType pcType, Board board, byte srcPosition, byte dstPosition)
        {
            //Record En Passant if Pawn Moving
            if (pcType == ChessPieceType.Pawn)
            {
                //Reset FiftyMoveCount if pawn moved
                board.FiftyMove = 0;

                int difference = srcPosition - dstPosition;

                if (difference == 16 || difference == -16)
                {
                    board.EnPassantPosition = (byte)(dstPosition + (difference / 2));
                    board.EnPassantColor = pcColor;
                }
            }
        }
Beispiel #52
0
        //Copy Constructor
        internal Board(Board board)
        {
            Squares = new Square[64];

            for (byte x = 0; x < 64; x++)
            {
                if (board.Squares[x].Piece != null)
                {
                    Squares[x] = new Square(board.Squares[x].Piece);
                }
            }
            EndGamePhase = board.EndGamePhase;

            FiftyMove = board.FiftyMove;
            RepeatedMove = board.RepeatedMove;

            WhiteCastled = board.WhiteCastled;
            BlackCastled = board.BlackCastled;

            BlackCheck = board.BlackCheck;
            WhiteCheck = board.WhiteCheck;
            StaleMate = board.StaleMate;
            WhiteMate = board.WhiteMate;
            BlackMate = board.BlackMate;
            WhoseMove = board.WhoseMove;
            EnPassantPosition = board.EnPassantPosition;
            EnPassantColor = board.EnPassantColor;

            ZobristHash = board.ZobristHash;

            Score = board.Score;

            LastMove = new MoveContent(board.LastMove);

            MoveCount = board.MoveCount;
        }
Beispiel #53
0
 private void InitiateBoard(string fen)
 {
     HumanPlayer = ChessPieceColor.White;
     ChessBoard = new Board(fen);
     ChessBoard.WhoseMove = ChessPieceColor.White;
     PieceMoves.InitiateChessPieceMotion();
     GenerateValidMoves();
 }
Beispiel #54
0
        internal static bool SetEnpassantMove(ChessBoard board, byte dstPosition, ChessPieceColor pcColor)
        {
            //En Passant
            if (board.EnPassantPosition == dstPosition && pcColor != board.EnPassantColor)
            {
                int pieceLocationOffset = 8;
                if (board.EnPassantColor == ChessPieceColor.White)
                {
                    pieceLocationOffset = -8;
                }

                dstPosition = (byte)(dstPosition + pieceLocationOffset);

                ChessPiece piece = board.pieces[dstPosition];

                board.LastMove.TakenPiece = new PieceTaken(piece.PieceColor, piece.Identifier, piece.Moved, dstPosition);

                board.pieces[dstPosition] = null;

                //Reset FiftyMoveCount if capture
                board.FiftyMove = 0;

                return true;
            }

            return false;
        }