Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Minimax连接4 AI故障_Java_Artificial Intelligence_Minimax - Fatal编程技术网

Java Minimax连接4 AI故障

Java Minimax连接4 AI故障,java,artificial-intelligence,minimax,Java,Artificial Intelligence,Minimax,我正在制作connect 4 AI,但游戏将继续,直到所有42个空间都被填满。 分数保持不变,每4分一行得1分 public int[] Max_Value(GameBoard playBoard, int depth){ GameBoard temp = new GameBoard(playBoard.playBoard); int h = 0, tempH = 999, tempCol=0; int myDepth = depth - 1; int[] tem

我正在制作connect 4 AI,但游戏将继续,直到所有42个空间都被填满。
分数保持不变,每4分一行得1分

public int[] Max_Value(GameBoard playBoard, int depth){
    GameBoard temp = new GameBoard(playBoard.playBoard);
    int h = 0, tempH = 999, tempCol=0;
    int myDepth = depth - 1;
    int[] tempH2 = new int[2];
    boolean noChildren = true;
    if(myDepth != -1){
        for(int i = 0; i < 7; i++){
            if(temp.isValidPlay(i)){
                count++;
                temp.playPiece(i);
                noChildren = false;
                tempH2 = Min_Value(temp, myDepth);
                if(tempH2[1] < tempH){
                    tempH=tempH2[1];
                    tempCol = i;
                }
                temp.removePiece(i);
            }
        }
    }   
    int[] x = new int[2];
    if(noChildren){
        h = temp.getHeuristic();
    }
    else{
        h = tempH;
        x[0]=tempCol;
    }
    x[1]=h;
    return x; 
}

public int[] Min_Value(GameBoard playBoard, int depth){
    GameBoard temp = new GameBoard(playBoard.playBoard);
    int h = 0, tempH = -999, tempCol=0;
    int myDepth = depth - 1;
    int[] tempH2 = new int[2];
    boolean noChildren = true;
    if(myDepth != -1){
        for(int i = 0; i < 7; i++){
            if(temp.isValidPlay(i)){
                count++;
                temp.playPiece(i);
                noChildren = false;
                tempH2 = Max_Value(temp, myDepth);
                if(tempH2[1] > tempH){
                    tempH=tempH2[1];
                    tempCol = i;
                }
                temp.removePiece(i);
            }
        }
    }   
    int[] x = new int[2];
    if(noChildren){
        h = temp.getHeuristic();
    }
    else{
        h = tempH;
        x[0]=tempCol;
    }
    x[1]=h;
    return x; 
}
public int[]最大值(游戏板、游戏板、int深度){
游戏板温度=新游戏板(playBoard.playBoard);
inth=0,tempH=999,tempCol=0;
int myDepth=深度-1;
int[]tempH2=新的int[2];
布尔noChildren=true;
如果(myDepth!=-1){
对于(int i=0;i<7;i++){
if(温度有效显示(i)){
计数++;
临时玩具(一);
无子女=假;
tempH2=最小值(temp,myDepth);
if(tempH2[1]tempH){
tempH=tempH2[1];
tempCol=i;
}
拆卸件温度(i);
}
}
}   
int[]x=新的int[2];
国际单项体育联合会(无儿童){
h=温度(0);
}
否则{
h=tempH;
x[0]=tempCol;
}
x[1]=h;
返回x;
}
我觉得我只是跌跌撞撞地完成了所有的事情,感觉就像是糟糕的代码。然而,我以前从未尝试过类似的事情,希望您能提供任何意见。我不知道我哪里出了问题。我的求值函数对于任何给定的状态,一行中的每4个只给出1分。main函数调用minu Value函数以10为深度开始


我试图返回列以及启发式的值。我希望我已经提供了足够的信息。谢谢你的见解。

尽管问题中没有说明,但我认为你的搜索没有取得好的进展,对吗

如果不看你的while代码,我已经可以说你的程序只会在游戏的最后10步(最后10个空位或10次强制获胜)中运行。否则,程序将返回其计算的最后一步或第一步。这是因为您的评估功能,其中您只处理一个赢(分别为一行4个),而不是一行2个、陷阱、一行3个,等等)。如果它不能强制获胜,它会认为所有的动作都是平等的

这是一个问题,因为从一个空场开始,只有先发球员才能获胜,而最后的第二个棋子要放在棋盘上。(在您的版本4中,连续强制)

由于您的搜索深度(10)小于游戏的最大移动数(42),您的程序将始终执行第一步


如果算法的其余部分实现正确,您可以通过简单地改进求值函数来解决此问题,这样就可以区分“好”和“坏”的游戏位置。

好吧,在实现了未显示的方法(如求值、playmove、remove等)后,我能够调试此方法。假设这些函数在您的版本中以某种正确的方式实现,错误在于,如果深度为-1,您实际上从未调用求值函数:

你有这个:

[...]if(myDepth != -1)
{/*restofthecode*/}[...]
但你需要的是这样的东西:

[...]if(myDepth == -1)
{
return temp.getHeuristic();
}
/*restofthecode*/
[...]
这样,每当您达到深度-1(minimax树中的一片叶子)时,就会对电路板进行评估并返回值(这正是minimax中需要的)


在这两个部分(最小值和最大值)进行此修改,一切都将正常。如果还有其他问题,请随时询问。

谢谢。我刚开始使用糟糕的评估函数,以确保它至少可以工作,因为它更容易检查。我只是在最上面一行是空的(只剩下7步)板上检查它。然而,即使有一个connect 4可用,它仍然不会做出提供connect 4的举动。另外,当我试图在一块空板上运行它时,程序会停止,即使在深度5,我认为它不应该停止。我想时间应该是7^5,在深度5的一块空木板上。再次感谢。@user2587878好吧,最酷的事情是我可以将其复制粘贴到visual studio中,并使用优秀的调试器进行调试。今天晚些时候我会尝试解决这个问题,如果我能找到bug,我会在这里发布另一个答案。编辑:请注意,您的MethodName是反向的,您的Min方法计算最大化玩家值=>可能会引起混乱