-1

我正在尝试为棋盘游戏 Othello 编写 MiniMax 代码。我有板对象,当前板是Main.grid. 我有一种方法可以在给定当前棋盘的情况下获取所有合法移动,使用返回的合法移动我想创建一个 ArrayList,其中的每个项目都是一个深度克隆,Main.grid但对其进行了一次合法移动。我的问题是克隆使每个棋盘都具有相同的指针,因此每个合法动作都被添加到单个棋盘中,导致下图,蓝色星是选择的棋,放置在白色,2 个红色星星是白棋本可以进行的另外两个合法棋步——都放在棋盘上。任何帮助将非常感激。

public static ArrayList<board> addChild(board currBoard, int[] move, char discColour, ArrayList<board> children) throws CloneNotSupportedException {
    
     board child = (board) currBoard.clone();

     child.setMove(move, discColour);    //make the move on the child board
     child.setBoardVal(MiniMax.evalFunction(currBoard, Main.blackTurn)); //set value of child
     children.add(child);   //add child to list
     
 return children;
}

public static void minimax() throws CloneNotSupportedException {

    System.out.println("-----MINIMAX-----");
    System.out.println(Main.GUI.getClickStatus());

    ArrayList<int[]> legalMoves;
    legalMoves = gameHandler.potentialMovesList();
    int min = -1000;
    int max = 1000;

    System.out.println("-----legal moves list-----");
    char disc;
    if (Main.blackTurn = true) {
        disc = 'b';
    } else {
        disc = 'w';
    }

    ArrayList<board> children = new ArrayList<>();
    for (int[] i : legalMoves) {
        System.out.println(i[0] + " " + i[1]);

        children = (board.addChild(Main.grid, i, disc, children));
    }

    System.out.println("-------------------------");


    System.out.println("----------children list-----------");
    for (int x = 0; x < children.size(); x++) {
        System.out.println(children.get(x).getBoard());
    }
    System.out.println("-------------------------");


    System.out.println("-----------list scores----------");
    int maxIndex = 0;
    int maxBoardVal = 0;
    System.out.println("children num: " + children.size());
    for (int i = 0; i < children.size(); i++) {

        if (children.get(i).getBoardVal() > maxBoardVal) {
            maxBoardVal = children.get(i).getBoardVal();
            maxIndex = i;
        }
        System.out.println(children.get(i).getBoardVal());
    }
    System.out.println("-------------------------");

    int[] coord = legalMoves.get(maxIndex);
    int x = coord[0];
    int y = coord[1];

    Main.blackTurn = false; //set it to be white turn


    Main.grid.setMove(new int[]{x, y}, 'w');    //need to change this from 'w' hardcoded to whichever AIs turn it is (both could be ai)

    System.out.println("pick: " + x + " " + y);

    gameHandler.flipDiscs('w', 'b', x, y, Main.grid.getBoard());
}

minimax 方法中的子列表输出为:

[[C@63213e85
[[C@63213e85
[[C@63213e85

表明它们都共享相同的指针/正在产生浅拷贝。

4

0 回答 0