0

我正在尝试制作 Othello AI,目前正在尝试实现 MiniMax。我创建了一个棋盘对象,从当前棋盘我想创建一个所有“子”棋盘的列表,这些是当前棋盘的副本,但每个棋盘都有一个合法的移动。我在创建多个“子”板时遇到问题,到目前为止,我的代码仅将所有合法动作应用于一个板,并且仅在子板列表中包含该单个板。任何帮助将非常感激。

MiniMax 类中的 minimax 方法

       public static void minimax(){
    
 
   
            ArrayList<int[]> legalMoves;
            legalMoves = gameHandler.potentialMovesList();
            int min = -1000;
            int max = 1000;
    
            //-----legal moves list-----
            char disc;
            if (Main.blackTurn = true){
                disc = 'b';
            }
            else {
                disc = 'w';
            }
    
            int count = 0;
            
            for (int[] i : legalMoves) {
                System.out.println(i[0] + " " + i[1]);
    
                ArrayList<board> children = board.addChild(Main.grid, i, disc, count);
                count++;
            }
            //-------------------------
    
    
    //        ----------children list-----------;
    ////        for (int x = 0; x < board.getChildren().size(); x++){
    ////            System.out.println(board.getChildren().get(x));
    ////        }
    //        -------------------------
    
            //-----------list scores----------
            int maxIndex = 0;
            int maxBoardVal = 0;
    
            for (int i =0; i < board.getChildren().size() ; i++){
    
                if (board.getChildren().get(i).getBoardVal() > maxBoardVal){
                    maxBoardVal = board.getChildren().get(i).getBoardVal();
                    maxIndex = i;
                }
                System.out.println(board.getChildren().get(i).getBoardVal()); //children only has 1 item in currently
            }
            //-------------------------
    
            int[] coord = legalMoves.get(maxIndex);
            int x = coord[0];
            int y = coord[1];
    
    
            Main.blackTurn = false; //set it to be white turn
    
            System.out.println("minimax move select");
            System.out.println(x);
            System.out.println(y);
            System.out.println("------");
            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());
    
            Main.blackTurn = !Main.blackTurn; //swap back to black (player) turn
    
            //(this is only for one depth), for multiple depth we need to then make a list of all moves
            //from each next ply (after accouting for every move that the opposite colour could make)
        }

公共课板{

private static char disc;
private static char[][] gameboard;
private static boolean blackTurn;
private static ArrayList<board> children;
private static int boardEval;

//private static board[] children;
//depth 0 = Main.grid
//depth 1 = Main.grid with a move made

//initializer to make first empty board
public board() {
    gameboard = new char[8][8];
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 8; j++){
            gameboard[i][j] = ' ';
        }
    }
}

public static void setGameboard(char[][] gameboard) {
    board.gameboard = gameboard;
}

public static void setChildren(ArrayList<board> children) {
    board.children = children;
}

public static ArrayList<board> getChildren(){
    return children;
}

public char[][] getBoard(){
    return gameboard;
}

public boolean getTurn(){
    return blackTurn;
}

public static char getTile(int[] yo) {
    char value = gameboard[yo[0]][yo[1]];
    return value;
}

public static void setMove(int[] coord, char discColour){
    gameboard[coord[0]][coord[1]] = discColour;
}

public static void printBoard(board grid){
   //prints the logical grid in command line
        for (int i =0; i< 8; i++){
            for (int z =0; z< 8; z++){
                System.out.print(grid.getBoard()[z][i]);
                System.out.print("|");
            }
            System.out.println();
            for (int q =0; q< 16; q++) {
                System.out.print("-");
            }
            System.out.println();
        }
        System.out.println("end of grid");
}

public static void initialBoard(board grid){
    for (int i = 0; i < 8; i++){
        for (int j = 0; j < 8; j++){
            int[] yo = {i, j};
            board.setMove(yo, ' ');
        }
    }

    board.setMove(new int[]{3, 3}, 'w');
    board.setMove(new int[]{4, 3}, 'b');
    board.setMove(new int[]{3, 4}, 'b');
    board.setMove(new int[]{4, 4}, 'w');
}

public static int[] getScore(board board){
    int blackScore = 0;
    int whiteScore = 0;
    for (int i = 0; i <= 7; i++){   //iterate over all tiles on board
        for (int j = 0; j <= 7; j++) {

            if (board.getTile(new int[]{i, j}) == 'b'){
                blackScore++;
            }
            else if(board.getTile(new int[]{i, j}) == 'w'){
                whiteScore++;
            }
        }
    }
    int score[] = {blackScore, whiteScore};
    return score;
}

public static ArrayList<board> addChild(board board, int[] move, char discColour, int count{

    board child = board;
    ArrayList<board> children = board.getChildren();
    child.setMove(move, discColour);    //make the move on the child board
    child.setBoardVal(MiniMax.evalFunction(board, Main.blackTurn)); //set value of child
    children.add(child);   //add child to list
    board.setChildren(children);   //set global list to local list

    return children;
}

public static void setBoardVal(int boardVal){
    boardEval = boardVal;
}

public static int getBoardVal(){
    return boardEval;
}
4

0 回答 0