尝试使用minimax制作无与伦比的井字游戏

时间:2018-08-29 11:28:16

标签: java tic-tac-toe minimax

我正在打井字游戏,并且试图使计算机始终选择最佳移动方式。我在了解minimax方面做了很多研究,但似乎行不通(AI无法做出明智的选择)。我修改了一篇很棒的文章中关于该主题的代码-check it here。谁能帮我找出我的代码出了什么问题?

private Move minimax(JButton[][] boardCopy, String player){

    if (getResult(boardCopy) == 2) {
        return new Move(10);
    } else if (getResult(boardCopy) == 1){
        return new Move(-10);
    } else if (getResult(boardCopy) == 3) {
        return new Move(0);
    }

    ArrayList<Move> possibleMoves = getListOfPossibleMoves(boardCopy);

    for (Move move : possibleMoves){

        int i = move.i;
        int j = move.j;

        boardCopy[i][j].setText(player);

        if (player.equals(compPlayer)) {

            Move resMove = minimax(boardCopy, humanPlayer);
            move.moveScore = resMove.moveScore;

        } else {

            Move resMove = minimax(boardCopy, compPlayer);
            move.moveScore = resMove.moveScore;

        }

        boardCopy[i][j].setText("");
    }

    Move bestMove = new Move();
    if (player.equals(compPlayer)){

        int bestScore = -10000;
        for(Move move : possibleMoves){

            if (move.moveScore > bestScore){

                bestScore = move.moveScore;
                bestMove = new Move(move);
            }
        }
    } else {

        int bestScore = 10000;
        for(Move move : possibleMoves){

            if (move.moveScore < bestScore){

                bestScore = move.moveScore;
                bestMove = new Move(move);
            }
        }
    }

    return bestMove;
}

private void optimalComputerMove(JButton[][] board, String player){

    if (hasWinner) return;

    JButton[][] boardCopy = copyBoard(board);
    Move bestMove = minimax(boardCopy, player);
    int i = bestMove.i;
    int j = bestMove.j;
    board[i][j].setText(player);
    moveCount++;
    showResult(getResult(board));

}

private ArrayList<Move> getListOfPossibleMoves(JButton[][] board){

    ArrayList<Move> possibleMoves = new ArrayList<>();

    for (int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){

            if (board[i][j].getText().equals("")){
                Move availMove = new Move(i, j);
                possibleMoves.add(availMove);
            }
        }
    }

    return possibleMoves;
}

编辑:

GetResult方法:

private int getResult(JButton[][] board){

    if((board[0][0].getText().equals(currPlayer) && board[1][0].getText().equals(currPlayer) && board[2][0].getText().equals(currPlayer)) ||
       (board[0][1].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[2][1].getText().equals(currPlayer)) ||
       (board[0][2].getText().equals(currPlayer) && board[1][2].getText().equals(currPlayer) && board[2][2].getText().equals(currPlayer)) ||
       (board[0][0].getText().equals(currPlayer) && board[0][1].getText().equals(currPlayer) && board[0][2].getText().equals(currPlayer)) ||
       (board[1][0].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[1][2].getText().equals(currPlayer)) ||
       (board[2][0].getText().equals(currPlayer) && board[2][1].getText().equals(currPlayer) && board[2][2].getText().equals(currPlayer)) ||
       (board[0][0].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[2][2].getText().equals(currPlayer)) ||
       (board[2][0].getText().equals(currPlayer) && board[1][1].getText().equals(currPlayer) && board[0][2].getText().equals(currPlayer))){

        if(board == this.board){
            hasWinner = true;
        }

        if (currPlayer.equals(humanPlayer)) return 1; //X won
        else return 2; //O won
    }

    boolean full = true;

    for(int i = 0; i < 3 && full; i++){
        for(int j = 0; j < 3 & full; j++){
            if (board[i][j].getText().equals("")){
                full = false;
            }
        }
    }

    if (full) {

        if (board == this.board){
            hasWinner = true;
        }

        return 3; //tie
    }
    else return 0; //incomplete

}

1 个答案:

答案 0 :(得分:0)

您的minimax不算任何东西 如您所见,它只是从第一个ifs的函数返回:

 if (getResult(boardCopy) == 2) {
        return new Move(10);
    } else if (getResult(boardCopy) == 1){
        return new Move(-10);
    } else if (getResult(boardCopy) == 3) {
        return new Move(0);
    }