如何修复 Tic-Tac-Toe 中的随机数生成器

How to fix random number generator in Tic-Tac-Toe

首先我想说我是编程的初学者(而且我的英语很烂)。我正在构建一款您可以在计算机上玩的井字游戏(使用随机数生成器),但我遇到了一个奇怪的错误,我找不到出路。

我没有实现 "end game" 条件,但有时,当电脑卡在一个选择中时,游戏就结束了。有趣的是,如果我添加 2 行代码只是为了查看 pc 选择发生了什么,它不会崩溃,直到游戏的 "end"("end" 因为还没有获胜条件)

我的另一个问题是如何为函数实现一个异常,该函数对已经有内容的字段的数字进行随机化?

pcTurn();
    cout << "\nComputer chosed field " << field << endl;
    cout << endl;

以上是我讲的台词。函数"input ()"开头的那几行让程序读取时运行不一样的是"pcTurn()"下的那两行"cout"。我尝试将它们放入和取出代码,将“//”放在它前面,然后,程序更频繁地运行到最后,没有它,它总是在结束前崩溃,我不知道为什么。

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

char matrix [3][3] = {'.','.','.','.','.','.','.','.','.',};
char player = 'X';
int field = 0;
int pcChoice = 0;
bool endGame = false;


//matrix
void draw (){

        for (int i=0; i<3; i++){
            for (int j=0; j<3; j++){
                cout << matrix [i][j] << " ";
        }
        cout << endl;
    }   
}

//pc turn - choice
int pcTurn (){

    srand(time(0));
    pcChoice = 1+ (rand () %9);
    field = pcChoice;


}

//input
void input (){

    if (player == 'X')
    {
        cout << "\nplayer " << player << " chose your field: ";
        cin >> field;
        cout << endl;
    }
    else
    {
        pcTurn();
        cout << "\nComputer chosed field " << field << endl;
        cout << endl;
    }   


    if (field == 1)
    {
        if (matrix [0][0] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [0][0] = player;
        }
    }
    else if (field == 2)
    {
        if (matrix [0][1] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [0][1] = player;
        }
    }
    else if (field == 3)
    {
        if (matrix [0][2] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [0][2] = player;
        }
    }
    else if (field == 4)
    {
        if (matrix [1][0] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [1][0] = player;
        }
    }
    else if (field == 5)
    {
        if (matrix [1][1] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [1][1] = player;
        }
    }
    else if (field == 6)
    {
        if (matrix [1][2] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [1][2] = player;
        }
    }
    else if (field == 7)
    {
        if (matrix [2][0] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [2][0] = player;
        }
    }
    else if (field == 8)
    {
        if (matrix [2][1] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [2][1] = player;
        }
    }
    else if (field == 9)
    {
        if (matrix [2][2] != '.')
        {
            if (player == 'X')
            {
                cout << "\ninvalid choice!" << endl;
            } 
            input ();
        }
        else
        {
            matrix [2][2] = player;
        }
    }
}




//toggle player
void togglePlayer (){
    if (player == 'X')
        player = 'O';
    else
        player = 'X';
}

int main (){

    draw ();

    do {
    input ();
    draw ();
    togglePlayer ();

    } while (endGame == false);
    return 0;
}

我希望,就像现在一样,能够完成游戏中的所有可用空间,然后并且只有这样,才能让程序进入无限循环“因为我还没有实现很多功能, 包括游戏结束条件)

好吧,我修复了你的输入函数递归调用太多了 * 对更改进行了评论* 剩下的就是确定游戏的结束 :) + 如上所述,当您启动程序时,srand 会被使用一次

void input() {

    if (player == 'X')
    {
        cout << "\nplayer " << player << " chose your field: ";
        cin >> field;
        cout << endl;
    }
    else
    {
        pcTurn();
        cout << endl;
    }


    // instead of testing each field by its self you can just do it like this for example
    // field = 4 (filed-1)/3 is 1 & field-1 %3 is 0 so second line first column that would make it much shorter

    // now you ask the used for its choice till its an empty place
    // same thing for the pc play it will generate randoms till it finds an empty place 

    while(matrix[(field - 1) / 3][((field - 1) % 3)] != '.')
        {
            if (player == 'X')
            {

                cout << "\ninvalid choice!" << endl;
                cout << "\nplayer " << player << " chose your field: ";
                cin >> field;
                cout << endl;
            }
            else {

                pcTurn();

            }

        }

            if (player!= 'X')
            {

                cout << "\nComputer chosed field " << field << endl;
                cout << endl;
                }

            matrix[(field - 1) / 3][((field - 1) % 3)] = player;


            }