如何在井字游戏中修复随机数生成器

时间:2019-01-29 21:25:31

标签: c++ tic-tac-toe

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

我没有实现“结束游戏”条件,但是有时候,当电脑陷入困境时,游戏结束。有趣的是,如果我添加两行代码只是为了查看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;
}

我现在希望能够完成游戏中的所有可用空间,然后直到那时,才使程序进入无限循环。“因为我还没有实现很多功能,包括游戏结束条件)

1 个答案:

答案 0 :(得分:-1)

好吧,我修复了您的输入函数,递归调用太多了 *注释已更改*剩下的就是确定游戏的结束:) 上面指出的srand会在您启动程序时使用一次

    void Screenshot::memoryManagement()
{
    ::delete bmpPtr;
    ReleaseDC(NULL, memdc);
    DeleteObject(fontdc);
    DeleteObject(memdc);
    DeleteObject(membit);
    GdiplusShutdown(gdiplusToken);
}