ในบทนี้จะแสดงผลการทำงานของแต่ละขั้นตอนโดยจะแก้ไขโปรแกรมเพื่อกำหนดสถานะตั้งต้นของ board เพื่อให้เหลือระดับความลึกไม่เยอะมาก

String[] board = new String[] {"O", "1", "X", "X", "4", "X", "6", "O", "O"}; // initial stage for test

เพิ่มตัวแปร depth เพื่อแสดงระดับความลึก

public static boolean getAndMark(String[] board, String player, Scanner scanner) { // get position and mark
    boolean loop = true;
    if (player.equals("X")) {
        System.out.println("playing by AI");
        int[] bestMove = new int[1];
        int depth = 0; // add depth to check depth ==================================
        minimax(board, player,bestMove, depth);
        board[bestMove[0]] = "X";
        if (winning(board,"X")) {
            showGrid(board);
            System.out.println("X win.");
            loop = false;
        }
    } else {
        System.out.print("Enter the number: ");
        int move = scanner.nextInt();
        scanner.nextLine();
        board[move] = "O";
        if (winning(board,"O")) {
            showGrid(board);
            System.out.println("O win.");
            loop = false;
        }
    }
    int[] available = emptyIndexies(board);
    if (available.length == 0) {
        showGrid(board);
        System.out.println("Draw");
        loop = false;
    }
    return loop; // return boolean as true in case game over
}

และเพิ่มการพิมพ์ผลลัพธ์ในแต่ละขั้นตอนในเมธอด minimax

public static int minimax (String[] newBoard, String player, int[] bestMove, int depth) { // minimax algorithm
depth++; //checkpoint <<<=====
System.out.println("depth : "+depth); //checkpoint ======
int[] availSpots = emptyIndexies(newBoard); // get list of available cell
// check if terminal state
if (winning(newBoard, "O" )){
System.out.println("terminal return : -10"); // checkpoint <<<=====
return -10;
} else if (winning(newBoard, "X" )){
System.out.println("terminal return : 10"); // checkpoint <<<=====
return 10;
} else if (availSpots.length == 0){
System.out.println("terminal return : 0"); // checkpoint <<<=====
return 0;
}
// check it terminal state
int[][] score = new int[availSpots.length][2];
for (int i = 0; i < availSpots.length; i++) {
score[i][0] = availSpots[i];
newBoard[availSpots[i]] = player;
showGrid(newBoard); // checkpoint <<<=====
if (player.equals("X")) {
player = "O";
} else {
player = "X";
}
score[i][1] = minimax(newBoard, player, bestMove, depth);
System.out.println("^^^^"+score[i][1]); // checkpoint <<<=====
if (player.equals("X")) { // return char value
player = "O";
} else {
player = "X";
}
newBoard[availSpots[i]] = String.valueOf(score[i][0]);
}
// get best move
int bestScore = -1;
// if it is process owner turn, select index with highest score
if (player.equals("X")){
bestScore = -10000;
for(var i = 0; i < score.length; i++){
if(score[i][1] > bestScore){
bestScore = score[i][1];
bestMove[0] = score[i][0];
}
}
} else {
// else select index with lowest score
bestScore = 10000;
for(var i = 0; i < score.length; i++){
if(score[i][1] < bestScore){
bestScore = score[i][1];
bestMove[0] = score[i][0];
}
}
}
// return the chosen move (object) from the moves array
System.out.println("========================="); // checkpoint <<<=====
return bestScore;
}

เราจะเปรียบเทียบผลลัพธ์ของโปรแกรมกับภาพด้านล่างซึ่งจะต้องได้ผลลัพธ์ที่เหมือนกัน

จะเห็นว่าผลลัพธ์ที่ได้เป็นไปตามคาดหมาย