添加深度后,MiniMax算法不智能

时间:2018-05-03 19:29:47

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

Tic-tac-toe游戏,AI类不够智能,无法成为无与伦比。

AI类应该计算一个tic-tac-toe板的最佳位置。 ' getBestSpot'方法调用min-max直到游戏结束。正在使用gameState类来检查游戏的状态并获得胜利者。

使用复制构造函数克隆电路板,因此不会修改实际的电路板属性。然后它获取复制板中的可用位置,并在每个位置播放。它一直这样做直到游戏结束,然后在给定“深度”的情况下对得分点进行评分。结果:

案例分数:

  • win = 1 - 深度
  • 输= -1 - 深度
  • tie = 0 - 深度

'对象[]'用于传递' int bestSpot,String bestSpot'在最小 - 最大之间它将被重构为使用' int []'代替。

经过一些研究,建议增加深度,因为如果每个可用点的得分相同,它可能有所帮助。添加深度后,玩这个位置所需的转弯越多,得分就越少,但仍无法返回最佳位置。

在这种情况下,对手令牌为O且AI(X)应在现场0中播放,以最小化/阻止对手(O)获胜,现场0应获得最高分,因为如果AI(X)没有在那里玩对手(O),那会使其他可用的分数得分较低,因为getScore ()当对手获胜时返回-1 - depth。测试失败:

@Test
public void testMinimizedSpotCaseLosing() {
    newGrid = new String[]{"0", "O", "X",
                           "O", "X", "5",
                           "O", "7", "8"};
    board.setGrid(newGrid);
    assertEquals( "0", miniMax.minimizedSpot(board, 0)[0] );
}


Test output:

    junit.framework.AssertionFailedError: 
    Expected :0
    Actual   :5

这是AI课程:

public class AI {

    private String token;
    private GameState gameState;
    private String opponentToken;

    AI(String token) {
        this.token = token;
        gameState = new GameState();
        opponentToken = null;

        setOpponentToken();
    }


    public String getToken() {
        return token;
    }

    public String getOpponentToken() {
        return opponentToken;
    }

    public void setOpponentToken() {
        if(token.equals("X")) {
            opponentToken = "O";
        } else {
            opponentToken = "X";
        }
    }

    public int getBestSpot(Board board) {
        String[] grid = board.getGrid();
        if( !grid[4].equals("X") && !grid[4].equals("O")) {
            return 4;
        } else {
            return Integer.parseInt((String) maximizedSpot(board, 0)[0]);
        }
    }

    public Object[] maximizedSpot(Board board, int depth) {
        Board boardClone = new Board(board);

        int bestScore = 0;
        String bestSpot = null;
        int score;

        String[] availableSpots = boardClone.getAvailableSpots();

        for(String availableSpot: availableSpots) {
            int spot = Integer.parseInt(availableSpot);
            boardClone.setSpot(spot, token);
            depth += 1;

            if( gameState.finished(boardClone) ) {
                score = getScore(boardClone, depth);
            } else {
                Object[] minimizedSpot = minimizedSpot(boardClone, depth);
                score = (int) minimizedSpot[1];
            }
            boardClone.setGrid(board.getGrid());

            if( bestScore == 0 || score > bestScore ) {
                bestScore = score;
                bestSpot = availableSpot;
            }

        }
        return new Object[]{bestSpot, bestScore};
    }

    public Object[] minimizedSpot(Board board, int depth) {
        Board boardClone = new Board(board);

        int bestScore = 0;
        String bestSpot = null;
        int score;


        String[] availableSpots = boardClone.getAvailableSpots();

        for(String availableSpot: availableSpots) {
            int spot = Integer.parseInt(availableSpot);
            boardClone.setSpot(spot, opponentToken);
            depth += 1;

            if ( gameState.finished(boardClone) ) {
                score = getScore(boardClone, depth);
            } else {
                Object[] maximizedSpot = maximizedSpot(boardClone, depth);
                score = (int) maximizedSpot[1];
            }
            boardClone.setGrid(board.getGrid());

            if (bestScore == 0 || score < bestScore) {
                bestScore = score;
                bestSpot = availableSpot;
            }
        }
        return new Object[]{bestSpot, bestScore};
    }

    public int getScore(Board board, int depth) {
        if( gameState.finished(board) && gameState.getWinnerToken() != null ) {
            String winnerToken = (gameState.getWinnerToken());
            if(winnerToken.equals(token)) {
                return 1 - depth;
            } else if (winnerToken.equals(opponentToken)) {
                return -1 - depth;
            }
        }
        return 0 - depth;
    }
}

董事会成员

public class Board {

private String[] grid;
private int[][] winCombinations;

public Board() {
    this.grid = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8"};
    this.winCombinations = new int[][]{{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7,}, {2,5,8}, {0,4,8}, {2,4,6}};
}

public Board(Board sourceBoard) {
    this();
    System.arraycopy(sourceBoard.grid, 0, this.grid, 0, sourceBoard.grid.length);

    for (int i = 0; i < winCombinations.length; i++) {
        int[] line = winCombinations[i];
        System.arraycopy(sourceBoard.winCombinations[i], 0, line, 0, line.length);
    }
}

public String[] getGrid() {
    return grid;
}

public int[][] getWinCombinations() {
    return winCombinations;
}

public String[] getAvailableSpots() {
    ArrayList<String> resultList = new ArrayList<String>();
    for (String aGrid : grid) {
        if (!aGrid.equals("X") && !aGrid.equals("O")) {
            resultList.add(aGrid);
        }
    }
    return resultList.toArray(new String[resultList.size()]);
}

public void reset() {
    grid = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8"};
}

public void setSpot(int i, String token) {
    grid[i] = token;
}

public void setGrid(String[] grid) {
    this.grid = grid;
}

}

GameState类:

public class GameState {

private String winner;

public GameState() {
    this.winner = null;
}

public boolean checkTie(Board board) {
    String[] availableSpots = board.getAvailableSpots();

    return availableSpots.length == 0 && !checkWin(board);
}

public boolean checkWin(Board board) {
    int[][] winCombinations = board.getWinCombinations();
    String[] grid = board.getGrid();

    for (int[] winCombination : winCombinations) {
        boolean combinationState = checkWinCombination(winCombination, board);

        if (combinationState) {
            // Get token inside winCombination and set to winner variable.
            setWinnerToken(grid[winCombination[0]]);
            return true;
        }
    }
    return false;
}

public boolean checkWinCombination(int[] combination, Board board) {
    String[] grid = board.getGrid();
    return grid[combination[0]].equals("X") && grid[combination[1]].equals("X") && grid[combination[2]].equals("X") || grid[combination[0]].equals("O") && grid[combination[1]].equals("O") && grid[combination[2]].equals("O");
}

public boolean finished(Board board) {
    return checkWin(board) || checkTie(board);
}

public String getWinnerToken() {
    return winner;
}

public void setWinnerToken(String token) {
    winner = token;
}
}

0 个答案:

没有答案