Created
December 21, 2020 22:36
-
-
Save vinimonteiro/73b9ee4e7bf89a6effc9a74ab6071858 to your computer and use it in GitHub Desktop.
Minimax Alpha-Beta Pruning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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