【问题标题】:How to fix random number generator in Tic-Tac-Toe如何修复井字游戏中的随机数生成器
【发布时间】:2019-01-29 21:25:31
【问题描述】:

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

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

我遇到的另一个问题,可以解决我的问题,是如何实现函数的异常,该函数随机化已经包含某些内容的字段的数字?

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一次
  • 请注意,在某些时候return 需要返回类型为非void 的函数。 int pcTurn () 不返回值并且具有未定义的行为。

标签: c++ tic-tac-toe


【解决方案1】:

我修复了你的输入函数,递归调用太多了 * 对更改进行了评论* 剩下的就是确定游戏的结束 :) + 如上所述 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;


            }

【讨论】:

    猜你喜欢
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多