C语言 我如何让我的“AI”做一个动作,直到满足某些标准?

tvmytwxo  于 2023-10-16  发布在  其他
关注(0)|答案(1)|浏览(105)

这是一个玩家与人工智能井字游戏在C。如果AI滚到一个被占领的位置,我如何让它再次滚到一个未被占领的位置?

char boardchar[3][3] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'} }; 

void Turn()
{
    /*Player's Turn*/

    AIrandomizer();
    AI = AI_roll();

        if (AI == 1 && boardchar[0][0] == '1')
        {
            boardchar[0][0] = symbAI;
        }

        else if (AI == 2 && boardchar[0][1] == '2')
        {
            boardchar[0][1] = symbAI;                   
        }

        else if (AI == 3 && boardchar[0][2] == '3')
        {
            boardchar[0][2] = symbAI;               
        }

        else if (AI == 4 && boardchar[1][0] == '4')
        {
            boardchar[1][0] = symbAI;       
        }

        else if (AI == 5 && boardchar[1][1] == '5')
        {
            boardchar[1][1] = symbAI;               
        }

        else if (AI == 6 && boardchar[1][2] == '6')
        {
            boardchar[1][2] = symbAI;               
        }

        else if (AI == 7 && boardchar[2][0] == '7')
        {
            boardchar[2][0] = symbAI;               
        }

        else if (AI == 8 && boardchar[2][1] == '8')
        {
            boardchar[2][1] = symbAI;               
        }

        else if (AI == 9 && boardchar[2][2] == '9')
        {
            boardchar[2][2] = symbAI;           
        }

}

我已经尝试了开关(情况)和多个if和else if,但我仍然无法找到解决这个问题的方法。

v09wglhw

v09wglhw1#

你可以使用while或do-while语句。
也做一个3x 3的二维数组来跟踪斑点:0 =未填充,1 =人类,2 = AI(最好使用枚举)。
在伪代码中:
全局变量:

int spots[3][3]; // 3x3 array

代码:

Setup(); // Setup of board, sets 0's to spots.

while (!endOfGame())
{
    Player(); // Handle player, assuming it plays first, sets a 1 in one spot

    if (!endOfGame())
    {
         AI();
    }
}

void AI() 
{
    do
    {
       square = AIChoice();
    } while (spots[square] == 0);

    // Now we know spots[square] == 0, thus unfilled
    spots[square] = 2; // AI
}

相关问题