gpt4 book ai didi

java - Minimax Connect 4 AI 麻烦

转载 作者:行者123 更新时间:2023-12-02 04:30:33 24 4
gpt4 key购买 nike

我正在制作 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;
}

我觉得我只是偶然发现了一切,而且感觉代码很糟糕。然而,我以前从未尝试过这样的事情,并且希望得到任何意见。我不知道我哪里出了问题。我的评估函数对于任何给定状态连续找到的每 4 个点只给出 1 分。 main 函数调用 Min_Value 函数以深度 10 开始。

我正在尝试返回该列以及启发式的值。我希望我已经提供了足够的信息。感谢您的任何见解。

最佳答案

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

你有这个:

[...]if(myDepth != -1)
{/*restofthecode*/}[...]

但是你需要的是这样的:

[...]if(myDepth == -1)
{
return temp.getHeuristic();
}
/*restofthecode*/
[...]

这样,每当你到达深度 -1(极小极大树中的一片叶子)时,都会对棋盘进行评估并返回值(这正是极小极大树中你所需要的)。

对两个部分(最小和最大)进行此修改,一切都应该没问题。如果还有其他问题,请随时询问。

关于java - Minimax Connect 4 AI 麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31528913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com