AI正在忽略Connect4中的设置参数 我现在90%完成了我的C++连接游戏,但是我遇到了一个相当恼人的错误。游戏玩得很好,除了一件事之外,一切都正常;玩家与电脑对抗,但电脑会忽略我为其设置的参数,并将其棋子放在棋盘上的任何位置。这可能是我的逻辑错误吗?我知道这不是人工智能的一个例子,但这是总结它的最好方式。 以下是函数: void columnChoiceComputer(int computer) { int number = 0; srand(time(0)); cout << (rand() % 6 + 1) << endl; if (cin.fail()) { cout << "Error!"; srand(time(0)); cout << (rand() % 6 + 1) << endl; } while ((rand() % 6 + 1) > WIDTH || (rand() % 6 + 1) <= 0) { cout << "Please select a different column."; srand(time(0)); cout << (rand() % 6 + 1) << endl; } while (boardMatrix[(HEIGHT - 1) - number][((rand() % 6 + 1) - 1)] != 0) { number++; if (number > (HEIGHT - 1)) { cout << "Please select a different column."; srand(time(0)); cout << (rand() % 6 + 1) << endl; number = 0; } }; boardMatrix[(HEIGHT - 1) - number][(rand() % 6 + 1) - 1] = computer; lastMoveY = (HEIGHT - 1) - number; lastMoveX = ((rand() % 6 + 1)) - 1; (system("cls")); void列选择计算机(int计算机) { 整数=0; srand(时间(0)); cout

AI正在忽略Connect4中的设置参数 我现在90%完成了我的C++连接游戏,但是我遇到了一个相当恼人的错误。游戏玩得很好,除了一件事之外,一切都正常;玩家与电脑对抗,但电脑会忽略我为其设置的参数,并将其棋子放在棋盘上的任何位置。这可能是我的逻辑错误吗?我知道这不是人工智能的一个例子,但这是总结它的最好方式。 以下是函数: void columnChoiceComputer(int computer) { int number = 0; srand(time(0)); cout << (rand() % 6 + 1) << endl; if (cin.fail()) { cout << "Error!"; srand(time(0)); cout << (rand() % 6 + 1) << endl; } while ((rand() % 6 + 1) > WIDTH || (rand() % 6 + 1) <= 0) { cout << "Please select a different column."; srand(time(0)); cout << (rand() % 6 + 1) << endl; } while (boardMatrix[(HEIGHT - 1) - number][((rand() % 6 + 1) - 1)] != 0) { number++; if (number > (HEIGHT - 1)) { cout << "Please select a different column."; srand(time(0)); cout << (rand() % 6 + 1) << endl; number = 0; } }; boardMatrix[(HEIGHT - 1) - number][(rand() % 6 + 1) - 1] = computer; lastMoveY = (HEIGHT - 1) - number; lastMoveX = ((rand() % 6 + 1)) - 1; (system("cls")); void列选择计算机(int计算机) { 整数=0; srand(时间(0)); cout,c++,artificial-intelligence,C++,Artificial Intelligence,首先,我非常感谢那些帮助我的人。解决方案非常简单,我不需要每次调用函数时都调用(rand()%6+1),只需将随机数的第一次迭代设置为一个名为“computerChoice”的变量,然后简单地调用该变量。下面是更新的代码: int number = 0; srand(time(0)); int computerChoice = (rand() % 6 + 1); while (computerChoice > WIDTH || computerChoice <= 0) {

首先,我非常感谢那些帮助我的人。解决方案非常简单,我不需要每次调用函数时都调用(rand()%6+1),只需将随机数的第一次迭代设置为一个名为“computerChoice”的变量,然后简单地调用该变量。下面是更新的代码:

int number = 0;
srand(time(0));
int computerChoice = (rand() % 6 + 1);

while (computerChoice > WIDTH || computerChoice <= 0)
{
    cout << "Please select a different column.";
    srand(time(0));
    cout << (rand() % 6 + 1) << endl;
}
while (boardMatrix[(HEIGHT - 1) - number][(computerChoice - 1)] != 0)
{
    number++;
    if (number > (HEIGHT - 1))
    {
        cout << "Please select a different column.";
        srand(time(0));
        cout << (rand() % 6 + 1) << endl;
        number = 0;
    }
};

boardMatrix[(HEIGHT - 1) - number][computerChoice - 1] = computer;
lastMoveY = (HEIGHT - 1) - number;
lastMoveX = (computerChoice) - 1;
(system("cls"));
int number=0;
srand(时间(0));
int computerChoice=(rand()%6+1);

而(computerChoice>WIDTH | | computerChoice)问题的核心是
rand()
每次调用都会获得一个新号码,因此使用
(rand()%6+1)
更新电路板的步骤与所有其他
(rand()%6+1)完全不同
s,可能会引起各种混乱。您对
srand()的使用
也可能有问题。你应该包括你的AI应该做什么,以便有人能提供更明确的答案。感谢kmdreko的回复。AI的目标是简单地玩connect4,它的播放方式与用户的相同,即它选择一个列,在其中放置一个片段;但是,我让它使用一个随机选项用于数字1-6的数字生成器,而不是像用户功能那样从键盘获取输入。