java - 修复 Tic-Tac-Toe Minimax ai

标签 java artificial-intelligence tic-tac-toe minimax

我有一个代表 Tic-Tac-Toe 中游戏板的棋盘类,但我的 AI 无法正常工作。我真的很感激任何帮助。谢谢!

我正在尝试使用极小极大类型的算法来尝试查看棋盘上可以采取的最佳 Action 是什么,但我得到了奇怪的结果。现在为了测试我的代码,我只是运行 testAI() 方法。

public class Board
{
    private int[] board = {0,0,0,0,0,0,0,0,0};
    private final int HUMAN = -1;
    private final int COMPUTER = 1;
    private final int[][] winList = {
            {0, 1, 2},
            {3, 4, 5},
            {6, 7, 8},
            {0, 3, 6},
            {1, 4, 7},
            {2, 5, 8},
            {0, 4, 8},
            {2, 4, 6}
        };
private int[] choice = new int[10000];
private int choiceCount = 0;
private int[] scoreArray = new int[10000];

public void reset() {
    for (int i = 0; i < board.length; i++) {
        board[i] = 0;
    }
}

public void set(int index, int player) {
    board[index] = player;
}

public int[] set(int[] board2, int index, int player) {
    board2[index] = player;
    return board2;
}

public boolean checkEmpty(int index) {
    if (board[index] == 0) {
        return true;
    } else {
        return false;
    }
}

public boolean isGameOver() {
    for (int i = 0; i < board.length; i++) {
        if (board[i] == 0) {
            return false;
        }
    }
    return true;
}

public boolean isGameOver(int[] board2) {
    for (int i = 0; i < board2.length; i++) {
        if (board2[i] == 0) {
            return false;
        }
    }
    return true;
}

public int chooseRandomSpot() {
    while (true) {
        int r = (int)(9 * Math.random());
        if (checkEmpty(r))
            return r;
    }
}

private String[] toStringArray() {
    String[] y = new String[9];
    for (int i = 0; i < board.length; i++) {
        if (board[i] == 0) {
            y[i] = " ";
        } else if (board[i] == 1) {
            y[i] = "x";
        } else if (board[i] == -1) {
            y[i] = "o";
        }
    }
    return y;
}

public void printBoard() {
    String[] y = toStringArray();
    System.out.println("  a b c");
    for (int i = 0; i < 3; i++) {
        if (i == 0) {
            System.out.println("a " + y[0] + " " + y[1] + " " + y[2]);
        } else if (i == 1) {
            System.out.println("b " + y[3] + " " + y[4] + " " + y[5]);
        } else if (i == 2) {
            System.out.println("c " + y[6] + " " + y[7] + " " + y[8]);
        }

    }
}

public boolean checkForWin(int player) {
    for (int i = 0; i < 8; i++) {
        int a = winList[i][0];
        int b = winList[i][1];
        int c = winList[i][2];

        if (board[a] == player && board[b] == player && board[c] == player) {
            return true;
        }
    }
    return false;
}

public boolean checkForWin(int[] board2, int player) {
    for (int i = 0; i < 8; i++) {
        int a = winList[i][0];
        int b = winList[i][1];
        int c = winList[i][2];

        if (board2[a] == player && board2[b] == player && board2[c] == player) {
            return true;
        }
    }
    return false;
}

public int getMaxChoice() {
    int loc = 0;
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < choice.length; i++) {
        if (scoreArray[i] > max) {
            max = scoreArray[i];
            loc = choice[i];
        }
    }
    return loc;
}

public void testAI() {
    int[] x = {1,0,0,-1,1,0,-1,0,0};
    board = x;
    printBoard();
    minimax(x,COMPUTER);
    printBoard();
    System.out.println(getMaxChoice();
    int[] y = set(x,getMaxChoice(),COMPUTER);
    board = y;
    printBoard();
}

private int score(int[] board2) {
    if (checkForWin(board2, COMPUTER)) {
        return 10;
    } else if (checkForWin(board2, HUMAN)) {
        return -10;
    } else {
        return 0;
    }
}

private int minimax(int[] board2, int player) {
    //System.out.println("In here!!");
    int oppPlayer = 0;


    if (player == COMPUTER) {
        oppPlayer = HUMAN;
    } else {
        oppPlayer = COMPUTER;
    }

    if (isGameOver(board2) || checkForWin(board2, COMPUTER) || checkForWin(board2, HUMAN)) {
        return score(board2);
    }

    int amt = 0; // find the amount of possible moves
    for (int i = 0; i < board2.length; i++) {
        if (board2[i] == 0) {
            amt++;
        }
    }

    int[] scores = new int[amt];
    int[] moves = new int[amt];
    int count = 0; //the index of the moves array
    for (int i = 0; i < amt; i++) {
        if (board2[i] == 0) { //if the space is empty
            moves[count] = i;// appends the index of the next empty space to the moves array
            count++;
        }

        //int[] newBoard = set(board2, moves[count], player); //make a new board with each move
        //scores[count] = minimax(newBoard, oppPlayer);
    }
    for (int i = 0; i < moves.length; i++) {
        //int[] newBoard = set(board2, moves[i], player); //make a new board with each move
        int[] newBoard = new int[board2.length];
        for (int m = 0; m < board2.length; m++) {
            newBoard[m] = board2[m];
        }
        newBoard = set(newBoard, moves[i], player);
        scores[i] = minimax(newBoard, oppPlayer); //populate the scores array with the final score of each move
    }

    if (player == COMPUTER) {
        int max = Integer.MIN_VALUE;
        int indexOfMax = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] > max) {
                max = scores[i];
                indexOfMax = i;
            }
        }
        choice[choiceCount] = moves[indexOfMax];
        scoreArray[choiceCount] = scores[indexOfMax];
        choiceCount++;
        System.out.println(choice);
        return max;
    } else {
        int min = Integer.MAX_VALUE;
        int indexOfMin = 0;
        for (int i = 0; i < scores.length; i++) {
            if (scores[i] < min) {
                min = scores[i];
                indexOfMin = i;
            }
        }
        choice[choiceCount] = moves[indexOfMin];
        scoreArray[choiceCount] = scores[indexOfMin];
        choiceCount++;
        return min;
    }
}

public int getIndex(String r, String c) {
    if (r.equals("a")) {
        if (c.equals("a")) {
            return 0;
        } else if (c.equals("b")) {
            return 1;
        } else if (c.equals("c")) {
            return 2;
        }
    } else if (r.equals("b")) {
        if (c.equals("a")) {
            return 3;
        } else if (c.equals("b")) {
            return 4;
        } else if (c.equals("c")) {
            return 5;
        }
    } else if (r.equals("c")) {
        if (c.equals("a")) {
            return 6;
        } else if (c.equals("b")) {
            return 7;
        } else if (c.equals("c")) {
            return 8;
        }
    }
    return 0;
}

}

最佳答案

for (int i = 0; i < amt; i++) {
    if (board2[i] == 0) { //if the space is empty
        moves[count] = i;// appends the index of the next empty space to the moves array
        count++;
    }
}

这个循环不应该遍历整个板吗? 喜欢

for (int i = 0; i < board2.length; i++) {

(不知道这是否是您的问题,刚刚看到并认为它可能不正确)

关于java - 修复 Tic-Tac-Toe Minimax ai,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40197008/

相关文章:

python-3.x - 运行预训练 ImageAI 模型时出现问题

machine-learning - 如何使用 Word2Vec 获取单词列表的向量?

algorithm - 井字游戏启发式 AI

java - 如何避免在这个 Java bool 方法中检查井字棋游戏是否结束,而不是对每种可能的情况进行硬编码?

java - 在另一个方法中使用主要方法中的变量

java - Apache Camel 路由配置中返回 Future 的 Bean 方法

java - DBUtil - 返回错误代码、消息或抛出异常

java - 确定选择哪个 JRadioButton 的最佳方法是什么?

artificial-intelligence - 什么时候一种启发式比另一种更好?

java - Java 井字游戏程序设计第 1 级