Ejemplo n.º 1
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;
        }
Ejemplo n.º 2
0
        private static void GenerateValidMovesKingCastle(ChessBoard board, ChessPiece king)
        {
            if (king == null || king.Moved)
            {
                return;
            }

            if (king.PieceColor == ChessPieceColor.White &&
                board.whiteCastled)
            {
                return;
            }
            if (king.PieceColor == ChessPieceColor.Black &&
                board.blackCastled)
            {
                return;
            }
            if (king.PieceColor == ChessPieceColor.Black &&
                board.blackInCheck)
            {
                return;
            }
            if (king.PieceColor == ChessPieceColor.White &&
                board.whiteInCheck)
            {
                return;
            }

            //This code will add the castleling move to the pieces available moves
            if (king.PieceColor == ChessPieceColor.White)
            {
                if (board.whiteInCheck)
                {
                    return;
                }

                if (board.pieces[63] != null
                    && board.pieces[63].Identifier == ChessPieceType.Rook
                    && board.pieces[63].PieceColor == king.PieceColor
                    && board.pieces[62] == null
                    && board.pieces[61] == null
                    && BlackAttackBoard[61] == false
                    && BlackAttackBoard[62] == false)
                {
                    //Ok looks like move is valid lets add it
                    king.ValidMoves.Push(62);
                    WhiteAttackBoard[62] = true;
                }

                if (board.pieces[56] != null
                    && board.pieces[56].Identifier == ChessPieceType.Rook
                    && board.pieces[56].PieceColor == king.PieceColor
                    && board.pieces[57] == null
                    && board.pieces[58] == null
                    && board.pieces[59] == null
                    && BlackAttackBoard[58] == false
                    && BlackAttackBoard[59] == false)
                {
                    //Ok looks like move is valid lets add it
                    king.ValidMoves.Push(58);
                    WhiteAttackBoard[58] = true;
                }
            }
            else if (king.PieceColor == ChessPieceColor.Black)
            {
                if (board.blackInCheck)
                {
                    return;
                }

                //There are two ways to castle, scenario 1:
                if (board.pieces[7] != null)
                {
                    //Check if the Right Rook is still in the correct position
                    if (board.pieces[7].Identifier == ChessPieceType.Rook
                        && !board.pieces[7].Moved)
                    {
                        if (board.pieces[7].PieceColor == king.PieceColor)
                        {
                            //Move one column to right see if its empty

                            if (board.pieces[6] == null)
                            {
                                if (board.pieces[5] == null)
                                {
                                    if (WhiteAttackBoard[5] == false && WhiteAttackBoard[6] == false)
                                    {
                                        //Ok looks like move is valid lets add it
                                        king.ValidMoves.Push(6);
                                        BlackAttackBoard[6] = true;
                                    }
                                }
                            }
                        }
                    }
                }
                //There are two ways to castle, scenario 2:
                if (board.pieces[0] != null)
                {
                    //Check if the Left Rook is still in the correct position
                    if (board.pieces[0].Identifier == ChessPieceType.Rook &&
                        !board.pieces[0].Moved)
                    {
                        if (board.pieces[0].PieceColor ==
                            king.PieceColor)
                        {
                            //Move one column to right see if its empty
                            if (board.pieces[1] == null)
                            {
                                if (board.pieces[2] == null)
                                {
                                    if (board.pieces[3] == null)
                                    {
                                        if (WhiteAttackBoard[2] == false &&
                                            WhiteAttackBoard[3] == false)
                                        {
                                            //Ok looks like move is valid lets add it
                                            king.ValidMoves.Push(2);
                                            BlackAttackBoard[2] = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        private static void GenerateValidMovesKing(ChessPiece piece, ChessBoard board, byte srcPosition)
        {
            if (piece == null)
            {
                return;
            }

            byte length = MoveArrays.KingTotalMoves[srcPosition];
            for (byte i = 0; i < length; ++i)
            {
                byte dstPos = MoveArrays.KingMoves[srcPosition][i];

                if (piece.PieceColor == ChessPieceColor.White)
                {
                    //I can't move where I am being attacked
                    if (BlackAttackBoard[dstPos])
                    {
                        WhiteAttackBoard[dstPos] = true;
                        continue;
                    }
                }
                else if (WhiteAttackBoard[dstPos])
                {
                    BlackAttackBoard[dstPos] = true;
                    continue;
                }

                AnalyzeMove(board, dstPos, piece);
            }
        }
Ejemplo n.º 4
0
        private static void CheckValidMovesPawn(byte[] moves, ChessPiece pcMoving, byte srcPosition,
                                                ChessBoard board, byte count)
        {
            for (byte i = 0; i < count; ++i)
            {
                byte dstPos = moves[i];

                // Piece in capture position
                if (dstPos % 8 != srcPosition % 8)
                {
                    //If there is a piece there I can potentialy kill
                    AnalyzeMovePawn(board, dstPos, pcMoving);

                    if (pcMoving.PieceColor == ChessPieceColor.White)
                    {
                        WhiteAttackBoard[dstPos] = true;
                    }
                    else
                    {
                        BlackAttackBoard[dstPos] = true;
                    }
                }
                    // if there is something in front pawns can't move there
                else if (board.pieces[dstPos] != null)
                {
                    return;
                }
                    //if there is nothing in front of me (blocked == false)
                else
                {
                    pcMoving.ValidMoves.Push(dstPos);
                }
            }
        }
Ejemplo n.º 5
0
        private static void AnalyzeMovePawn(ChessBoard board, byte dstPos, ChessPiece movingPiece)
        {
            bool movingPieceIsWhite = movingPiece.PieceColor == ChessPieceColor.White;

            //Because Pawns only kill diagonaly we handle the En Passant scenario specialy
            if (board.EnPassantPosition == dstPos && board.EnPassantPosition > 0 && movingPiece.PieceColor != board.EnPassantColor)
            {
                //We have an En Passant Possible
                movingPiece.ValidMoves.Push(dstPos);

                if (movingPieceIsWhite)
                {
                    WhiteAttackBoard[dstPos] = true;
                }
                else
                {
                    BlackAttackBoard[dstPos] = true;
                }
            }

            ChessPiece pcAttacked = board.pieces[dstPos];

            //If there no piece there I can potentialy kill
            if (pcAttacked == null)
                return;

            //Regardless of what is there I am attacking this square
            if (movingPieceIsWhite)
            {
                WhiteAttackBoard[dstPos] = true;

                //if that piece is the same color
                if (pcAttacked.PieceColor == movingPiece.PieceColor)
                {
                    pcAttacked.DefendedValue += movingPiece.PieceActionValue;
                    return;
                }

                pcAttacked.AttackedValue += movingPiece.PieceActionValue;

                //If this is a king set it in check
                if (pcAttacked.Identifier == ChessPieceType.King)
                {
                    board.blackInCheck = true;
                }
                else
                {
                    //Add this as a valid move
                    movingPiece.ValidMoves.Push(dstPos);
                }
            }
            else
            {
                BlackAttackBoard[dstPos] = true;

                //if that piece is the same color
                if (pcAttacked.PieceColor == movingPiece.PieceColor)
                {
                    pcAttacked.DefendedValue += movingPiece.PieceActionValue;
                    return;
                }

                pcAttacked.AttackedValue += movingPiece.PieceActionValue;

                //If this is a king set it in check
                if (pcAttacked.Identifier == ChessPieceType.King)
                {
                    board.whiteInCheck = true;
                }
                else
                {
                    //Add this as a valid move
                    movingPiece.ValidMoves.Push(dstPos);
                }
            }

            return;
        }
Ejemplo n.º 6
0
        private static bool AnalyzeMove(ChessBoard board, byte dstPos, ChessPiece movingPiece)
        {
            //If I am not a pawn everywhere I move I can attack

            bool movingPieceIsWhite = movingPiece.PieceColor == ChessPieceColor.White;
            if (movingPieceIsWhite)
            {
                WhiteAttackBoard[dstPos] = true;
            }
            else
            {
                BlackAttackBoard[dstPos] = true;
            }

            ChessPiece pcAttacked = board.pieces[dstPos];

            //If there no piece there I can potentialy kill just add the move and exit
            if (pcAttacked == null)
            {
                movingPiece.ValidMoves.Push(dstPos);
                return true;
            }

            if (pcAttacked.PieceColor != movingPiece.PieceColor)
            {
                // Different color I am attacking

                pcAttacked.AttackedValue += movingPiece.PieceActionValue;

                //If this is a king set it in check
                if (pcAttacked.Identifier == ChessPieceType.King)
                {
                    if (movingPieceIsWhite)
                    {
                        board.blackInCheck = true;
                    }
                    else
                    {
                        board.whiteInCheck = true;
                    }
                }
                else
                {
                    //Add this as a valid move
                    movingPiece.ValidMoves.Push(dstPos);
                }

                //We don't continue movement past this piece
                return false;
            }
            else
            {
                //Same Color I am defending
                pcAttacked.DefendedValue += movingPiece.PieceActionValue;

                //Since this piece is of my kind I can't move there
                return false;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            //Draw Squares and Pieces
            Color squareColor = Color.White;

            for (byte i = 0; i < boardInfo.pieces.Length; ++i)
            {
                if ((int)(i / Constants.NumberOfRanks) % 2 == 0)
                {
                    if (i % 2 == 0)
                    {
                        squareColor = Color.White;
                    }
                    else
                    {
                        squareColor = Color.Gray;
                    }
                }
                else
                {
                    if (i % 2 == 0)
                    {
                        squareColor = Color.Gray;
                    }
                    else
                    {
                        squareColor = Color.White;
                    }
                }

                ChessPiece piece = boardInfo.pieces[i];

                if (piece != null)
                {
                    if (i == selectedIndex)
                    {
                        squareColor = Color.Blue;
                    }

                    spriteBatch.Draw(dummyTexture, new Rectangle((i % Constants.NumberOfFiles) * Constants.SquareSize, (int)(i / Constants.NumberOfRanks) * Constants.SquareSize, Constants.SquareSize, Constants.SquareSize), squareColor);

                    Texture2D textureToDraw;
                    if (piece.PieceColor == ChessPieceColor.White)
                    {
                        textureToDraw = textureArray[(int)piece.Identifier];
                    }
                    else
                    {
                        textureToDraw = textureArray[(int)ChessPieceType.None + (int)piece.Identifier];
                    }

                    spriteBatch.Draw(textureToDraw, new Rectangle((i % Constants.NumberOfFiles) * Constants.SquareSize, (int)(i / Constants.NumberOfRanks) * Constants.SquareSize, Constants.SquareSize, Constants.SquareSize), Color.White);
                }
                else
                {
                    spriteBatch.Draw(dummyTexture, new Rectangle((i % Constants.NumberOfFiles) * Constants.SquareSize, (int)(i / Constants.NumberOfRanks) * Constants.SquareSize, Constants.SquareSize, Constants.SquareSize), squareColor);
                }
            }

            if (boardInfo.whiteInCheck)
            {
                spriteBatch.Draw(whiteCheckTexture, new Vector2(540, 200), Color.White);
            }
            else if (boardInfo.blackInCheck)
            {
                spriteBatch.Draw(blackCheckTexture, new Vector2(540, 200), Color.White);
            }
            spriteBatch.Draw(undoTexture, undoRectangle, Color.White);

            spriteBatch.End();

            base.Draw(gameTime);
        }