Created
March 7, 2024 16:56
-
-
Save nikhil-RGB/9a6d4c7826fc0ea9c3cf12e5228ac4cc to your computer and use it in GitHub Desktop.
Original implementation Minimax tictactoe
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 int[] minMax(TicTacToeMin obj) | |
{ | |
ArrayList<Integer> scores=new ArrayList<>(0); | |
ArrayList<TicTacToeMin> arrs=obj.simulate(); | |
ArrayList<Integer> moves=obj.emptySpaces(); | |
//return a list of cloned boards with current token fitted into all empty spaces. | |
if(arrs.size()==0) | |
{ | |
return new int[] {0,-1}; | |
} | |
for(TicTacToeMin board:arrs) | |
{ | |
int score=board.score(); | |
if(score==-2) | |
{ | |
score=minMax(board)[0]; | |
} | |
scores.add(score); | |
} | |
int optimalScore; | |
if(obj.currentTok.equals("O")) | |
{ | |
optimalScore=Collections.max(scores); | |
} | |
else | |
{ | |
optimalScore=Collections.min(scores); | |
} | |
return new int[] {optimalScore,moves.get(scores.indexOf(optimalScore))}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment