Exemple #1
0
    // MiniMax
    private int AlphaBetaPruning(Vector2Int cell, int depth, int alpha = -int.MaxValue, int beta = int.MaxValue, bool maximazingPlayer = false)
    {
        if (depth == 0 || (boardData.IsCellOccupied(FigureType.King, cell) &&
                           boardData.IsCellOccupied(maximazingPlayer ? FigureType.Black : FigureType.White, cell)))
        {
            return(Evaluate(cell));
        }

        if (maximazingPlayer)
        {
            int          maxEvaluation     = -int.MaxValue;
            Vector2Int[] possiblePositions = boardData.GetPossibleMoves(figureType, cell, maximazingPlayer);
            foreach (Vector2Int position in possiblePositions)
            {
                int evaluation = AlphaBetaPruning(position, depth - 1, alpha, beta, false);
                maxEvaluation = Mathf.Max(maxEvaluation, evaluation);
                alpha         = Mathf.Max(alpha, evaluation);
                if (evaluation > alpha)
                {
                    alpha         = evaluation;
                    maxEvaluation = evaluation;
                    tempBestMove  = position;
                }
                if (beta <= alpha)
                {
                    break;
                }
            }
            return(maxEvaluation);
        }
        else
        {
            int          minEvaluation     = int.MaxValue;
            Vector2Int[] possiblePositions = boardData.GetPossibleMoves(figureType, cell, maximazingPlayer);
            foreach (Vector2Int position in possiblePositions)
            {
                int evaluation = AlphaBetaPruning(position, depth - 1, alpha, beta, true);
                if (evaluation < beta)
                {
                    beta          = evaluation;
                    minEvaluation = evaluation;
                    tempBestMove  = position;
                }
                if (beta <= alpha)
                {
                    break;
                }
            }
            return(minEvaluation);
        }
    }
    private int Negamax(Vector2Int cell, int depth, bool white)
    {
        if (depth == 0 || (boardData.IsCellOccupied(FigureType.King, cell) &&
                           boardData.IsCellOccupied(white ? FigureType.Black : FigureType.White, cell)))
        {
            return(Evaluate(boardData.GetFigureType(cell), cell));
        }

        int evaluation = int.MinValue;

        Vector2Int[] whitePieces = boardData.GetAllChessPiecesByColor(white);
        foreach (Vector2Int piece in whitePieces)
        {
            FigureType   type          = boardData.GetFigureType(piece);
            Vector2Int[] possibleMoves = boardData.GetPossibleMoves(type, piece, white);
            foreach (Vector2Int position in possibleMoves)
            {
                int i = -Negamax(position, depth - 1, !white);

                if (i > evaluation)
                {
                    evaluation    = i;
                    trueMove.from = piece;
                    trueMove.to   = position;
                }
            }
        }

        return(evaluation);
    }
Exemple #3
0
    public void HighlightPossibleMoves(Vector2Int pos)
    {
        UnhighlightPreviousMoves();
        FigureType figureType = boardData.GetFigureType(pos.x, pos.y);

        savedPossibleMoves = boardData.GetPossibleMoves(figureType, pos.x, pos.y);
        for (int i = 0; i < savedPossibleMoves.Length; i++)
        {
            HighlightCell(savedPossibleMoves[i]);
        }
    }
    public void HighlightPossibleMoves(Vector2Int pos)
    {
        UnhighlightPreviousMoves();
        FigureType figureType = boardData.GetFigureType(pos);

        savedPossibleMoves = boardData.GetPossibleMoves(figureType, pos);
        int size = savedPossibleMoves.Length;

        for (int i = 0; i < size; i++)
        {
            highlightCell[i].transform.position = new Vector3(savedPossibleMoves[i].x, savedPossibleMoves[i].y);
            highlightCell[i].SetActive(true);
        }
    }