Skip to content

Instantly share code, notes, and snippets.

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