gpt4 book ai didi

c# - 连接四(Unity3D 和 C#)的 Minimax : Problems

转载 作者:行者123 更新时间:2023-11-30 14:32:43 26 4
gpt4 key购买 nike

我正在使用 Unity3D (C#) 中的 AI 开发四连胜游戏。为此,我根据此(德语)Pseudocode 使用 MiniMax 算法.

AI 的表现仍然很糟糕。它试图连续获得 4 个,尽管只有三个空闲字段。 AI 总是遍历行并仅在需要时才阻止。不幸的是,它并不总是会阻塞。

哪里隐藏问题?我忘记了什么?

当下一步没有人赢或输时,我如何整合 AI 的随机 Action 。

这是我的源代码:

最小最大深度 = 4

函数调用:Max(minimaxDepth, fieldCopy);

int Max (int depth, int[,] fieldCopy)
{
int maxValue = -9999;
int moveValue;
bool winAI = false;
bool winHuman = false;
bool isStoneBelow = false;

for (int y = 0; y < 6; y++) {
for (int x = 0; x < 7; x++) {
if (y > 0) {
//is a stone under it?
if (fieldCopy [x, y - 1] == 1 || fieldCopy [x, y - 1] == 2) {
isStoneBelow = true;
} else {
isStoneBelow = false;
}
} else {
isStoneBelow = true;
}

// possible move?
if (fieldCopy [x, y] != 1 && fieldCopy [x, y] != 2 && isStoneBelow == true) {
isStoneBelow = false;
fieldCopy [x, y] = 2; //simulate move
winAI = false;
winHuman = false;

//Is there a winner?
if (CheckWin (x, y, 2, fieldCopy)) {
winAI = true;
winHuman = false;
}

//No more moves possible?
if (depth <= 1 || winAI == true) {
moveValue = evaluationFunction (winAI, winHuman); //evaluate the move
} else {
moveValue = Min (depth - 1, fieldCopy);
}

fieldCopy [x, y] = 0; //Reset simulated move

if (moveValue > maxValue) {
maxValue = moveValue;
if (depth == minimaxDepth) {
aiMoveX = x; // next move
}
}
}
}
}
return maxValue;
}

int Min (int depth, int[,] fieldCopy)
{
int minValue = 9999;
int moveValue;
bool winAI = false;
bool winHuman = false;
bool isStoneBelow = false;
bool loopBreak = false;

for (int y = 0; y < 6; y++) {
for (int x = 0; x < 7; x++) {
if (y > 0) {
//is a stone under it?
if (fieldCopy [x, y - 1] == 1 || fieldCopy [x, y - 1] == 2) {
isStoneBelow = true;
} else {
isStoneBelow = false;
}
} else {
isStoneBelow = true;
}

// possible move?
if (fieldCopy [x, y] != 1 && fieldCopy [x, y] != 2 && isStoneBelow == true) {
isStoneBelow = false;
fieldCopy [x, y] = 1; //simulate move
winHuman = false;
winAI = false;

//Is there a winner?
if (CheckWin (x, y, 1, fieldCopy)) {
winHuman = true;
winAI = false;
}

//No more moves possible?
if (depth <= 1 || winHuman == true) {
moveValue = evaluationFunction (winAI, winHuman); //evaluate the move
} else {
moveValue = Max (depth - 1, fieldCopy);
}

fieldCopy [x, y] = 0; //Reset simulated move

if (moveValue < minValue) {
minValue = moveValue;
}
}
}
}
return minValue;
}


int evaluationFunction (bool winAI, bool winHuman)
{
if (winAI) {
return 1;
} else if (winHuman) {
return -1;
} else {
return 0;
}
}

感谢您的帮助!

最佳答案

我认为问题在于您的评估功能。您不应评估其中一名玩家是否获胜,而应评估他们在游戏中的状态。一个可能的考虑因素是该玩家棋子的最长连续链的长度。这准确地显示了每个玩家距离获胜还有多远,而不是在玩家即将获胜但尚未获胜时返回相同的值。

为清楚起见,这里是伪代码:

int evaluationFunction (int depth)
{
// max_ai_chain and max_human_chain are global variables that
// should be updated at each piece placement

if (depth % 2 == 0) // assuming AI gets calculated on even depth (you mentioned your depth is 4)
{
return max_ai_chain;
} else {
return max_human_chain;
}
}

关于c# - 连接四(Unity3D 和 C#)的 Minimax : Problems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18058852/

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