首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MiniMax TicTacToe不工作(c)

MiniMax TicTacToe不工作(c)
EN

Stack Overflow用户
提问于 2015-01-22 21:10:46
回答 1查看 435关注 0票数 0

我用TicTacToe实现了一个MiniMax算法,但问题是计算机总是把'x‘放在下一个可能的位置,而不是对游戏进行评估。有人知道为什么吗?(问题只能出现在MiniMax函数或nextMove函数中。)提前谢谢!下面是代码:

代码语言:javascript
运行
复制
int MiniMax(struct Game g, enum Symbol pl){

int score;
if (pl==CROSS)
{
    score = -98765;
}
else score = 98765;


int temp_cross =0;
int temp_circle =0;



//base case
if (game_over(g) == CROSS)
    return 10;
else if (game_over(g) == CIRCLE)
    return -10;
else if (game_over(g) == FULL)
    return 0;


int x,y;
for (y=0; y<SIZE_Y_AXIS; y++)
{
    for (x=0; x<SIZE_X_AXIS; x++)
    {
        if (g.board.fields[x][y] == NONE)
        {
            if (pl == CROSS)
                g.board.fields[x][y] = CROSS;
            else  g.board.fields[x][y] = CIRCLE;
            if (pl == CROSS)
                temp_cross= MiniMax(g, CIRCLE);
            else temp_circle = MiniMax(g, CROSS);
            g.board.fields[x][y] = NONE;


            if ((pl == CROSS) && (temp_cross > score))
                score = temp_cross;
            else if ((pl == CIRCLE) && (temp_circle < score))
                score = temp_circle;


        }
    }
}

return score;

};

代码语言:javascript
运行
复制
int nextMove(struct Game g, enum Symbol player){

int score_cross = -865435;
int score_cross_temp = 0;
int cross_position = 1;
int score_circle = 876545;
int score_circle_temp = 0;
int circle_position = 1;
int x,y;
for (y=0; y<SIZE_Y_AXIS; y++)
{
    for (x=0; x<SIZE_X_AXIS; x++)
    {
        if (g.board.fields[x][y] == NONE)
        {
            if (player == CROSS)
            {
                score_cross_temp = MiniMax(g, CROSS);
                printf("%d ",MiniMax(g, CROSS));
                if (score_cross_temp > score_cross)
                {
                    score_cross = score_cross_temp;
                    cross_position = (y)*3 + x+1;
                }

            }
            else if (player == CIRCLE)
            {
                score_circle_temp = MiniMax(g, CIRCLE);
                if (score_cross_temp < score_circle)
                {
                    score_circle = score_circle_temp;
                    circle_position = (y)*3 + x+1;
                }
            }
        }
    }
}


if (player == CROSS)
{
    //printf("%d",cross_position);
    return cross_position;

}
else
{
    //printf("%d",circle_position);
    return circle_position;
}

};

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-23 03:24:54

我相信你的代码里有个bug。将分数初始化为10分,将临时变量设为任意高和低的数字。这是倒过来的。无论如何,TempCircle和TempCross都被覆盖了--我对minimax的调用。必须这样设置分数变量。替换

代码语言:javascript
运行
复制
int score = 10;
int temp_cross = -9876543;
int temp_circle = 9876543;

使用

代码语言:javascript
运行
复制
int score;
if(pl==cross){
    score = 9876543
}
else{
    score = -9876543
}
int temp_cross;
int temp_circle;

您的nextMove函数中似乎还存在另一个bug。我假设它的目的是迭代所有可能的移动,找到具有最高极小值的移动,并返回该移动(如果我错了请纠正我)。这不是该函数所做的。它迭代了所有的动作,但没有做任何动作。除了更新移动之外,X和y甚至从未使用过。您实际上是多次调用相同的极小值。相反,我要么完全摆脱这个函数,因为在minimax函数中有一种方法可以做到这一点,要么修复这个函数。要修复该函数,我会将其更改为:

代码语言:javascript
运行
复制
int nextMove(struct Game g, enum Symbol player){

int score_cross = -865435;
int score_cross_temp = 0;
int cross_position = 1;
int score_circle = 876545;
int score_circle_temp = 0;
int circle_position = 1;
int x,y;
for (y=0; y<SIZE_Y_AXIS; y++)
{
    for (x=0; x<SIZE_X_AXIS; x++)
    {
        if (g.board.fields[x][y] == NONE)
        {
            if (player == CROSS)
            {
                g.board.fields[x][y] = CROSS;
                score_cross_temp = MiniMax(g, CIRCLE);
                printf("%d ",MiniMax(g, CROSS));
                g.board.fields[x][y] = NONE;
                if (score_cross_temp > score_cross)
                {
                    score_cross = score_cross_temp;
                    cross_position = (y)*3 + x+1;
                }

            }
            else if (player == CIRCLE)
            {
                g.board.fields[x][y] = CIRCLE;
                score_circle_temp = MiniMax(g, CROSS);
                g.board.fields[x][y] = NONE;
                if (score_cross_temp < score_circle)
                {
                    score_circle = score_circle_temp;
                    circle_position = (y)*3 + x+1;
                }
            }
        }
    }
}


if (player == CROSS)
{
    //printf("%d",cross_position);
    return cross_position;

}
else
{
    //printf("%d",circle_position);
    return circle_position;
}
};

或者您可以编辑minimax以跟踪对函数的调用。如果它是第一个递归调用(根),跟踪移动本身以及它的值。那就把行动还给我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28098745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档