MiniMax TicTacToe不起作用(c)

时间:2015-01-22 21:10:46

标签: c tic-tac-toe minimax

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

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;

};

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;
}

};

1 个答案:

答案 0 :(得分:0)

我相信您的代码中存在错误。您将得分初始化为10,将临时变量初始化为任意高和低数字。这是倒退。无论如何,TempCircle和TempCross都被覆盖了我对minimax的调用。必须像这样设置得分变量。取代

int score = 10;
int temp_cross = -9876543;
int temp_circle = 9876543;

int score;
if(pl==cross){
    score = 9876543
}
else{
    score = -9876543
}
int temp_cross;
int temp_circle;

你的nextMove函数似乎还有另一个错误。我假设其目的是迭代所有可能的移动,找到具有最高极小极大值的那个,并返回该移动(如果我错了,请纠正我)。这不是功能的作用。它迭代所有的动作,但不会做出任何动作。除了更新移动之外,x和y从未被使用过。你本质上是多次调用相同的minimax。相反,我要么彻底摆脱这个功能,因为有一种方法可以在minimax函数中执行此操作,或者修复此函数。要修复该功能,我会将其更改为:

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以跟踪对该功能的调用。如果它是第一个递归调用(根),则跟踪移动本身及其值。然后返回移动。

相关问题