Skip to content

Instantly share code, notes, and snippets.

@vinimonteiro
Created December 21, 2020 22:36
Show Gist options
  • Save vinimonteiro/73b9ee4e7bf89a6effc9a74ab6071858 to your computer and use it in GitHub Desktop.
Save vinimonteiro/73b9ee4e7bf89a6effc9a74ab6071858 to your computer and use it in GitHub Desktop.
Minimax Alpha-Beta Pruning
public static void main(String[] args) {
Tree tree = initialise();
System.out.println(minimax(tree.getRoot(), 2,
Integer.MIN_VALUE, Integer.MAX_VALUE, true));
}
private static int minimax(Node node, int depth, int alpha, int beta, boolean isMax) {
if(depth==0 || node.getChildren()==null || node.getChildren().isEmpty()) {
return getStateScore(); //evaluation function
}
if(isMax) {
for(Node child:node.getChildren()) {
alpha = Math.max(alpha, minimax(child, depth-1, alpha, beta, false));
if(alpha>=beta) {
break; //pruning
}
}
return alpha;
}else {
for(Node child:node.getChildren()) {
beta = Math.min(beta, minimax(child, depth-1, alpha, beta, true));
if(alpha>=beta) {
break; //pruning
}
}
return beta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment