Beispiel #1
0
    //TODO: Rewrite for better reading/understanding
    public void CalculateNextMove(bool white = false)
    {
        Vector2Int[] gamePieces = boardData.GetAllChessPiecesByColor(white);
        int          index      = white ? -int.MaxValue : int.MaxValue;

        foreach (Vector2Int cell in gamePieces)
        {
            figureType = boardData.GetFigureType(cell);
            int i = AlphaBetaPruning(cell, difficulty, -int.MaxValue, int.MaxValue, white);
            if (white)
            {
                if (i > index)
                {
                    index        = i;
                    figureToMove = cell;
                    bestMove     = tempBestMove;
                }
            }
            else
            {
                if (i < index)
                {
                    index        = i;
                    figureToMove = cell;
                    bestMove     = tempBestMove;
                }
            }
        }
    }
    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);
    }