Hi,

I have to create a tree for Minimax algorithm upto depth d.
I am not getting how to create.Could anyone please suggest me a method?

Thanks.

Dani AI

Generated

As asked for a way to build a tree for Minimax to depth d, the standard pattern is recursive expansion of legal moves until either the depth limit or a terminal state, evaluate leaf nodes, and propagate values upward. 's "start typing" is a blunt nudge; pointed to external reading. A practical rule: generate children on demand during recursion instead of pre-building the whole tree unless an explicit tree structure is required for debugging or visualization.

Core recursion pattern (Board and Move are game-specific):

// evaluate() and legalMoves() are provided by the game state
int minimax(Board state, int depth, boolean maximizing) {
    if (depth == 0 || state.isTerminal()) {
        return state.evaluate();
    }
    List<Move> moves = state.legalMoves();
    if (maximizing) {
        int best = Integer.MIN_VALUE;
        for (Move m : moves) {
            Board child = state.applyMove(m); // or make/undo
            best = Math.max(best, minimax(child, depth - 1, false));
        }
        return best;
    } else {
        int best = Integer.MAX_VALUE;
        for (Move m : moves) {
            Board child = state.applyMove(m);
            best = Math.min(best, minimax(child, depth - 1, true));
        }
        return best;
    }
}

If an explicit Node class is needed, store minimal information (move, player, depth) and avoid full-board copies when possible — prefer make/undo move or lightweight cloning. For performance use alpha-beta pruning, iterative deepening for time limits, and a transposition table for repeated states. Background reading: Minimax algorithm and Alpha-beta pruning.

Recommended Answers

All 2 Replies

open text editor.
start typing.

Or did you mean you don't know what minimax is? In which case some research would be recommended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.