Example #1
0
    List <Vector2> PawnMoves(Vector2 position, bool isWhite, bool firstMove)
    {
        Vector2         gridCoordinate = position;
        List <Vector2>  possibleMoves  = new List <Vector2>();
        Vector2         checkSpace     = new Vector2();
        PieceController checkInfo      = null;

        if (isWhite)
        {
            //check space in front is empty
            if (gridCoordinate.y + 1 < 8)                                         // checking if space is on the board
            {
                checkSpace = new Vector2(gridCoordinate.x, gridCoordinate.y + 1); //checking space directly infront
                if (chessController.CheckPieceOnSquare(checkSpace) == null)
                {
                    possibleMoves.Add(checkSpace);
                }
            }

            //check if first move
            if (firstMove && gridCoordinate.y + 2 < 8)                                //if first move can move two spaces
            {
                if (possibleMoves.Count > 0)                                          //if piece can't move in front then it wont be able to move two in front
                {
                    checkSpace = new Vector2(gridCoordinate.x, gridCoordinate.y + 2); //checking space two infront
                    if (chessController.CheckPieceOnSquare(checkSpace) == null)       //if piece doesn't exist
                    {
                        possibleMoves.Add(checkSpace);                                //add coordinate for button
                    }
                }
            }

            //check diagonal left
            if (gridCoordinate.x - 1 >= 0 && gridCoordinate.y + 1 < 8)                //check if space is on board
            {
                checkSpace = new Vector2(gridCoordinate.x - 1, gridCoordinate.y + 1); //checking space up and to the left
                checkInfo  = chessController.CheckPieceOnSquare(checkSpace);
                //if there's no piece on the space, check if it's a en passant move
                if (checkInfo == null)
                {
                    checkInfo = chessController.CheckEnPassant(checkSpace);
                }

                if (checkInfo != null && checkInfo.isWhite == !isWhite) //checking if piece exists AND is piece is opposite colour
                {
                    if (!checkInfo.isKing)                              //checking if piece is not a king
                    {
                        possibleMoves.Add(checkSpace);
                    }
                }
            }
            //check diagonal right
            if (gridCoordinate.x + 1 < 8 && gridCoordinate.y + 1 < 8)                 //check if space is on board
            {
                checkSpace = new Vector2(gridCoordinate.x + 1, gridCoordinate.y + 1); //checking space up and to the right
                checkInfo  = chessController.CheckPieceOnSquare(checkSpace);

                //if there's no piece on the space, check if it's a en passant move
                if (checkInfo == null)
                {
                    checkInfo = chessController.CheckEnPassant(checkSpace);
                }

                if (checkInfo != null && checkInfo.isWhite == !isWhite) //checking if piece exists AND is piece is opposite colour
                {
                    if (!checkInfo.isKing)                              //checking if piece is not a king
                    {
                        possibleMoves.Add(checkSpace);
                    }
                }
            }
        }
        else
        {
            if (gridCoordinate.y - 1 >= 0)                                        // checking if space is on the board
            {
                checkSpace = new Vector2(gridCoordinate.x, gridCoordinate.y - 1); //checking space directly infront
                checkInfo  = chessController.CheckPieceOnSquare(checkSpace);      //getting info on piece (if piece is there) on square
                if (checkInfo == null)                                            //if piece doesn't exist
                {
                    possibleMoves.Add(checkSpace);                                //add coordinate for button
                }
            }
            //check if first move
            if (firstMove && gridCoordinate.y - 2 >= 0)                               //if first move can move two spaces
            {
                if (possibleMoves.Count > 0)                                          //if piece can't move in front then it wont be able to move two in front
                {
                    checkSpace = new Vector2(gridCoordinate.x, gridCoordinate.y - 2); //checking space two infront
                    checkInfo  = chessController.CheckPieceOnSquare(checkSpace);      //getting info on piece (if piece is there) on square
                    if (checkInfo == null)                                            //if piece doesn't exist
                    {
                        possibleMoves.Add(checkSpace);                                //add coordinate for button
                    }
                }
            }
            //check diagonal left
            if (gridCoordinate.x - 1 >= 0 && gridCoordinate.y - 1 >= 0)               //check if space is on board
            {
                checkSpace = new Vector2(gridCoordinate.x - 1, gridCoordinate.y - 1); //checking space up and to the left
                checkInfo  = chessController.CheckPieceOnSquare(checkSpace);

                //if there's no piece on the space, check if it's a en passant move
                if (checkInfo == null)
                {
                    checkInfo = chessController.CheckEnPassant(checkSpace);
                }

                if (checkInfo != null && checkInfo.isWhite == !isWhite) //checking if piece exists AND is piece is opposite colour
                {
                    if (!checkInfo.isKing)                              //checking if piece is not a king
                    {
                        possibleMoves.Add(checkSpace);
                    }
                }
            }
            //check diagonal right
            if (gridCoordinate.x + 1 < 8 && gridCoordinate.y - 1 >= 0)                //check if space is on board
            {
                checkSpace = new Vector2(gridCoordinate.x + 1, gridCoordinate.y - 1); //checking space up and to the right
                checkInfo  = chessController.CheckPieceOnSquare(checkSpace);

                //if there's no piece on the space, check if it's a en passant move
                if (checkInfo == null)
                {
                    checkInfo = chessController.CheckEnPassant(checkSpace);
                }

                if (checkInfo != null && checkInfo.isWhite == !isWhite) //checking if piece exists AND is piece is opposite colour
                {
                    if (!checkInfo.isKing)                              //checking if piece is not a king
                    {
                        possibleMoves.Add(checkSpace);
                    }
                }
            }
        }
        return(possibleMoves);
    }