示例#1
0
    public override void GeneratePossibleMoves(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                                               ChessPlayer enemyCopy, bool checkMovesValidity, bool checkSpecialMoves)
    {
        AvailableMoves.Clear();
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < possibleDirections.GetLength(0); i++)
        {
            if (i == 1 && MovedFirstTime)
            {
                break;
            }

            int newX = SquareIndex.x + possibleDirections[i, 0];
            int newY = SquareIndex.y + possibleDirections[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece piece = grid.GetPieceOnSquareIndex(newSquare);
                Move  move  = CheckPromotionFlag(newSquare, piece);

                if (checkMovesValidity && piece == null)
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy, move.PromotionFlag);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece == null)
                {
                    AvailableMoves.Add(move);
                }
                else
                {
                    break;
                }
            }
        }

        PieceAttack(grid, gridCopy, playerCopy, enemyCopy, checkMovesValidity);
        EnPassant(grid, gridCopy, playerCopy, enemyCopy, checkMovesValidity);
    }
示例#2
0
    public override void GeneratePossibleMoves(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                                               ChessPlayer enemyCopy, bool checkMovesValidity, bool checkSpecialMoves)
    {
        AvailableMoves.Clear();
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < possibleDirections.GetLength(0); i++)
        {
            int newX = SquareIndex.x + possibleDirections[i, 0];
            int newY = SquareIndex.y + possibleDirections[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece piece = grid.GetPieceOnSquareIndex(newSquare);
                Move  move  = new Move(this, newSquare, piece);

                if (checkMovesValidity && piece != null && piece.PieceType == PieceType.King)
                {
                    continue;
                }

                if (checkMovesValidity && (piece == null || (piece != null && piece.TeamColor != TeamColor)))
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece == null)
                {
                    AvailableMoves.Add(move);
                }
                else if (piece != null && TeamColor != piece.TeamColor)
                {
                    AvailableMoves.Add(move);
                }
            }
        }
    }
示例#3
0
    private void PieceAttack(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                             ChessPlayer enemyCopy, bool checkMovesValidity)
    {
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < possibleAttacks.GetLength(0); i++)
        {
            int newX = SquareIndex.x + possibleAttacks[i, 0];
            int newY = SquareIndex.y + possibleAttacks[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece piece = grid.GetPieceOnSquareIndex(newSquare);
                Move  move  = CheckPromotionFlag(newSquare, piece);

                if (checkMovesValidity && piece != null && piece.PieceType == PieceType.King)
                {
                    continue;
                }

                if (checkMovesValidity && (piece != null && piece.TeamColor != TeamColor))
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy, move.PromotionFlag);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece != null && TeamColor != piece.TeamColor)
                {
                    AvailableMoves.Add(move);
                }
            }
        }
    }
示例#4
0
    private void EnPassant(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                           ChessPlayer enemyCopy, bool checkMovesValidity)
    {
        int[,] directions = { { -1, 0 }, { 1, 0 } };
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < directions.GetLength(0); i++)
        {
            int newX = SquareIndex.x + directions[i, 0];
            int newY = SquareIndex.y + directions[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece      piece  = grid.GetPieceOnSquareIndex(newSquare);
                Vector2Int square = new Vector2Int(SquareIndex.x + enPassantMoves[i, 0], SquareIndex.y + enPassantMoves[i, 1]);
                Move       move   = CheckPromotionFlag(square, piece);

                if (checkMovesValidity && piece != null && piece.PieceType == PieceType.Pawn && piece == grid.PieceMovedInPreviousTurn &&
                    Mathf.Abs(piece.SquareIndex.y - piece.PreviousSquareIndex.y) == 2 && piece.SquareIndex.x == piece.PreviousSquareIndex.x)
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), square, attackedPieceCopy, move.PromotionFlag);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece != null && piece.PieceType == PieceType.Pawn && piece == grid.PieceMovedInPreviousTurn &&
                    Mathf.Abs(piece.SquareIndex.y - piece.PreviousSquareIndex.y) == 2 && piece.SquareIndex.x == piece.PreviousSquareIndex.x)
                {
                    AvailableMoves.Add(move);
                    break;
                }
            }
        }
    }
    private int EvaluateGrid(ChessGridInfo grid, ChessPlayer player, ChessPlayer enemy)
    {
        int  score         = 0;
        bool isKingInCheck = player.IsKingInCheck(enemy);

        if (!isKingInCheck && player.AllPossibleMoves.Count == 0)
        {
            return(0);
        }

        if (player.TeamColor == TeamColor.White && player.AllPossibleMoves.Count == 0 && isKingInCheck)
        {
            return(BLACK_VICTORY);
        }

        if (player.TeamColor == TeamColor.Black && player.AllPossibleMoves.Count == 0 && isKingInCheck)
        {
            return(WHITE_VICTORY);
        }

        Vector2Int square = Vector2Int.zero;

        for (int i = 0; i < ChessGridInfo.GRID_SIZE; i++)
        {
            for (int j = 0; j < ChessGridInfo.GRID_SIZE; j++)
            {
                square.Set(j, i);
                Piece piece = grid.GetPieceOnSquareIndex(square);

                if (piece != null)
                {
                    if (piece.TeamColor == TeamColor.White)
                    {
                        score += PiecePositionEvaluation.PieceValue[piece.PieceType];
                    }
                    else
                    {
                        score -= PiecePositionEvaluation.PieceValue[piece.PieceType];
                    }

                    score += PiecePositionEvaluation.PositionEvaluation[(piece.PieceType, piece.TeamColor)][i, j];
示例#6
0
    public static ChessGridInfo CopyGrid(ChessGridInfo grid, ChessPlayer whitePlayer, ChessPlayer blackPlayer)
    {
        ChessGridInfo newGrid = new ChessGridInfo();
        Vector2Int    square  = Vector2Int.zero;

        for (int i = 0; i < GRID_SIZE; i++)
        {
            for (int j = 0; j < GRID_SIZE; j++)
            {
                square.Set(j, i);
                Piece pieceOnGrid = grid.GetPieceOnSquareIndex(square);

                if (pieceOnGrid != null)
                {
                    Piece copy = ChessGameController.PieceFactory.CreatePiece(pieceOnGrid.GetType().ToString());
                    copy.SetData(pieceOnGrid.SquareIndex, pieceOnGrid.PreviousSquareIndex, pieceOnGrid.PieceType, pieceOnGrid.TeamColor,
                                 pieceOnGrid.MovedFirstTime, pieceOnGrid.Movement, null);
                    newGrid.SetChessPieceOnGrid(copy, square);

                    if (copy.TeamColor == TeamColor.White)
                    {
                        whitePlayer?.AddPiece(copy);
                    }

                    if (copy.TeamColor == TeamColor.Black)
                    {
                        blackPlayer?.AddPiece(copy);
                    }
                }
            }
        }

        newGrid.SelectedPiece            = grid.SelectedPiece;
        newGrid.PieceMovedInPreviousTurn = grid.PieceMovedInPreviousTurn;
        return(newGrid);
    }
示例#7
0
    private void Castling(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer enemyCopy)
    {
        if (MovedFirstTime)
        {
            return;
        }

        enemyCopy.GenerateAllPossibleMoves(gridCopy, false, false);

        if (enemyCopy.IsFieldUnderAttack(SquareIndex))
        {
            return;
        }

        int   kingRow   = SquareIndex.y;
        Piece leftRook  = grid.GetPieceOnSquareIndex(new Vector2Int(0, kingRow));
        Piece rightRook = grid.GetPieceOnSquareIndex(new Vector2Int(ChessGridInfo.GRID_SIZE - 1, kingRow));

        if (leftRook != null && leftRook.PieceType == PieceType.Rook && leftRook.TeamColor == TeamColor &&
            !leftRook.MovedFirstTime && leftRook.SquareIndex.y == SquareIndex.y)
        {
            bool       isLeftCastlingPossible = true;
            Vector2Int square = Vector2Int.zero;

            for (int i = leftRook.SquareIndex.x + 1; i < SquareIndex.x; i++)
            {
                square.Set(i, kingRow);
                if (gridCopy.GetPieceOnSquareIndex(square) != null ||
                    (i > 1 && enemyCopy.IsFieldUnderAttack(square)))
                {
                    isLeftCastlingPossible = false;
                    break;
                }
            }

            if (isLeftCastlingPossible)
            {
                Move move = new Move(this, new Vector2Int(2, kingRow), null, false, true, leftRook, new Vector2Int(3, kingRow));
                AvailableMoves.Add(move);
            }
        }

        if (rightRook != null && rightRook.PieceType == PieceType.Rook && rightRook.TeamColor == TeamColor &&
            !rightRook.MovedFirstTime && rightRook.SquareIndex.y == SquareIndex.y)
        {
            bool       isRightCastlingPossible = true;
            Vector2Int square = Vector2Int.zero;

            for (int i = SquareIndex.x + 1; i < rightRook.SquareIndex.x; i++)
            {
                square.Set(i, kingRow);
                if (gridCopy.GetPieceOnSquareIndex(square) != null || enemyCopy.IsFieldUnderAttack(square))
                {
                    isRightCastlingPossible = false;
                    break;
                }
            }

            if (isRightCastlingPossible)
            {
                Move move = new Move(this, new Vector2Int(6, kingRow), null, false, true, rightRook, new Vector2Int(5, kingRow));
                AvailableMoves.Add(move);
            }
        }
    }