3

我目前正在尝试实现一个Minimax algorithmfor Tic Tac Toe。在当前版本中,在某些情况下,计算机会出现问题,我不太清楚为什么。例如,如果我(作为人类玩家)从左上角的 x 开始,计算机的反应是左下角的 ao(当然,这等于他输了)整个程序在MVC-Design.

问题:我是否正确纠正了 Minimax 算法,或者(如果没有)是什么导致了“坏”动作?

这是代码:(我省略了一些我测试正确的方法的代码)

 package tictactoe;

    import java.util.*;

    public class AIMinimax {

        Game game;
        Cell[][] cell = new Cell[3][3];
        int[] bestMove;
        public AIMinimax(Game game) {
              this.game=game;
              for(int i=0;i<3;i++)
              {
                  for(int j=0;j<3;j++)
                  {
                      cell[i][j]=game.getCell(i,j);
                  }
              }
           }

        //Calculates the best move.
        private int minimax(Cell player)
        {
            //Computer tries to maximize the bestscore, Player tries to minimize the bestscore.

            int bestScore = 0;
            if(player==Cell.CIRCLE) bestScore=-1; 
            if(player==Cell.CROSS) bestScore=+1;


            int currentScore;
            List<int[]> moves = identifyMoves();
            if(moves.isEmpty() || hasWon(Cell.CROSS) || hasWon(Cell.CIRCLE))
            {
                //System.out.println(hasWon(Cell.CROSS));
                //System.out.println(hasWon(Cell.CIRCLE));
                //System.out.println(moves.isEmpty());
                currentScore=evaluate();
                bestScore=currentScore;
            }
            else
            {
                for (int[] move : moves) {
                    if (player==Cell.CIRCLE)
                    {
                        cell[move[0]][move[1]]=Cell.CIRCLE;
                        currentScore=minimax(Cell.CROSS);
                        if(currentScore>bestScore)
                {
                    bestScore=currentScore;
                    bestMove = move;
                }
            }
            else if(player==Cell.CROSS)
            {
                cell[move[0]][move[1]]=Cell.CROSS;
                currentScore=minimax(Cell.CIRCLE);


                if(currentScore<bestScore  )
                {

                    bestScore=currentScore;
                    bestMove=move;
                }
            }   

            cell[move[0]][move[1]]=Cell.EMPTY;
            if((player==Cell.CIRCLE&&bestScore==1)||(player==Cell.CROSS&&bestScore==-1))
            {
                return bestScore;
            }
        }
        }
                System.out.println(bestScore);
        return bestScore;
        }



    //Compute all possible next moves.
    //@return List of int[2] arrays where int[0] indicates the column and int [1] indicates the row.
    private List<int[]> identifyMoves(){}


    //Evaluates the current board. 
    // -1 if the player wins, +1 if the computer wins, 0 for a draw
    //No other values needed as the depth of the minimax algorithm is maximal.


    private int evaluate() {
        int result=0;
        if(hasWon(Cell.CIRCLE)) result=1;
        if(hasWon(Cell.CROSS)) result=-1;

        return result;
    }
    //return true if the player has won and false if the other player won or there is
    // a draw.


    public boolean hasWon(Cell player){}

    //Returns the next move for the computer as a int[2] where int[0] indicates the column and 
    //int[1] indicates the row.
    public int[] nextMove(Cell player)
    {
        int minimaxResult= minimax(player);

        return bestMove

    }


}

    package tictactoe;


    public enum Cell {

        EMPTY,
        CROSS,
        CIRCLE;

    }

游戏类表示 Cell[][] 数组中的字段(与 AIMinimax 相同)并创建 AIMinimax 的实例并在此调用 nextMove,以生成计算机做出的下一个移动。默认情况下,人类玩家总是启动。

先感谢您!

4

0 回答 0