Last active
December 21, 2020 22:33
-
-
Save vinimonteiro/8c72ebdcb30e8af118275b15d8d75f30 to your computer and use it in GitHub Desktop.
Minimax tree search
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, 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