Returning -1 for my tic tac toe minimax algorithm

ePirate

New Member
I'm not sure why my code is printing -1 for the first depth of the values of my minimax algorithm. I have been looking over and over at it and just cant seem to see it. Can anyone spot what is wrong with my minimax algorithm? Players are intiialized as 1 and 2 and compares to the utility value of 1 or 2 if there is a winner to actually compute 1 0 or -1Here is my code below\[code\]public static double miniMax(Agent player, int depth) { if (gameIsTerminalState() || depth == 9) { if (player.getPlayerNum() == utilityWinner) { return 1; } else if (utilityWinner == 0) { return 0; } else { return -1; } } // Children ArrayList<Integer> child = getEmptySpaces(); // Maximize 2 if player is 1 if(player.getPlayerNum() == 1) { double bestScore = Double.NEGATIVE_INFINITY; for( int i = 0; i < child.size(); i++) { addMark(child.get(i), player.getPlayerNum()); double score = miniMax(player2, depth + 1); if(depth == 0){ if(printCounter < 2) { System.out.print(score); printCounter++; } else { System.out.println(score); printCounter = 0; } } removeMark(child.get(i)); if(score > bestScore) { bestScore = score; } } return bestScore; } else { // player is player2 double bestScore = Double.POSITIVE_INFINITY; for( int i = 0; i < child.size(); i++) { addMark(child.get(i), player.getPlayerNum()); double score = miniMax(player1, depth + 1); if(depth == 0){ if(printCounter < 2) { System.out.print(score); printCounter++; } else { System.out.println(score); printCounter = 0; } } removeMark(child.get(i)); if(score < bestScore) { bestScore = score; } } return bestScore; } } public static boolean gameIsTerminalState() { // 2nd case: if 3 in a row, 8 possible winning scenarios for EACH // player // Checks for all horizontal rows combinations if (isWinningRow(environment.get(0), environment.get(1), environment.get(2)) || isWinningRow(environment.get(3), environment.get(4), environment.get(5)) || isWinningRow(environment.get(6), environment.get(7), environment.get(8)) || isWinningRow(environment.get(0), environment.get(3), environment.get(6)) || isWinningRow(environment.get(1), environment.get(4), environment.get(7)) || isWinningRow(environment.get(2), environment.get(5), environment.get(8)) || isWinningRow(environment.get(0), environment.get(4), environment.get(8)) || isWinningRow(environment.get(2), environment.get(4), environment.get(6))) { return true; } // 1st case: checks if all squares are marked up // false if one of them are false for (square s : environment) { if (!s.isMarked()) { return false; } } return true; } private static boolean isWinningRow(square square1, square square2, square square3) { if (square1.getPlayerWhoMarked() == 1 && square2.getPlayerWhoMarked() == 1 && square3.getPlayerWhoMarked() == 1) { utilityWinner = 1; return true; } else if (square1.getPlayerWhoMarked() == 2 && square2.getPlayerWhoMarked() == 2 && square3.getPlayerWhoMarked() == 2) { utilityWinner = 2; return true; } else { return false; } }\[/code\]
 
Back
Top