【问题标题】:How i can reset the values of my Tic Tac Toe Game我如何重置井字游戏的值
【发布时间】:2018-03-25 03:14:45
【问题描述】:

您好,我的程序有问题。编程对我来说是新的,我不知道我将在哪里重置矩阵的值,因为它的值的声明是全局的。为了了解更多,我在下面有一个评论,如果游戏已经完成,程序会询问用户如果他或她想再次运行游戏。无论如何如何通过向给定代码添加一些代码或修改来解决此问题。我尝试使用 goto Start 功能;在 if 条件和 main 函数中有一个 Start: 编写。但它没有用。请帮助我提前谢谢。

#include <iostream>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
int rear; 


void Draw()
{
    system("cls");
    cout << "\n\n\tTic Tac Toe Game \n\n";
    for (int i = 0; i < 3; i++)
    {
        cout<<" \t     "; 
        for (int j = 0; j < 3; j++)
        {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

void Input()
{
    int a;
    ++rear; 
    cout << "\n    Press the number of the field: ";
    cin >> a;

    if (a == 1)
        matrix[0][0] = player;
    else if (a == 2)
        matrix[0][1] = player;
    else if (a == 3)
        matrix[0][2] = player;
    else if (a == 4)
        matrix[1][0] = player;
    else if (a == 5)
        matrix[1][1] = player;
    else if (a == 6)
        matrix[1][2] = player;
    else if (a == 7)
        matrix[2][0] = player;
    else if (a == 8)
        matrix[2][1] = player;
    else if (a == 9)
        matrix[2][2] = player;
    else {
    cout<<"\n [ERROR: Invalid Range of Input is only 1-9 ]\n\n "; 
    system("pause"); 
    rear--; 
    }
}

void TogglePlayer()
{
    if (player == 'X')
        player = 'O';
    else
        player = 'X';
}
char Win()
{
    //first player
    if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X')
        return 'X';
    if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X')
        return 'X';
    if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X')
        return 'X';

    if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X')
        return 'X';
    if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X')
        return 'X';
    if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X')
        return 'X';

    if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')
        return 'X';
    if (matrix[2][0] == 'X' && matrix[1][1] == 'X' && matrix[0][2] == 'X')
        return 'X';

    //second player
    if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O')
        return 'O';
    if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O')
        return 'O';
    if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O')
        return 'O';

    if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O')
        return 'O';
    if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O')
        return 'O';
    if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O')
        return 'O';

    if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O')
        return 'O';
    if (matrix[2][0] == 'O' && matrix[1][1] == 'O' && matrix[0][2] == 'O')
        return 'O';

    return '/';
}

int main()
{
    char choice; 
    Draw();
    Start: 
    while (1)
    {
        Input();
        Draw();

        if (Win() == 'X')
        {
            cout << "\n    [Message: Player 'X' Wins The Game]  ";
            break;
        }
        else if (Win() == 'O')
        {
            cout << "\n    [Message: Player 'O' Wins The Game ]" << endl;
            break;
        }
        else if(rear==9)
        {
            cout<<" Draw"<<endl; 
            break; 
        }
        TogglePlayer();
    }

    cout<<"\n\n   ";
    cout<<"Do you want to run the game again [Y/N]? ";
    cin>>choice;
    if(choice=='Y' || choice=='y')
    {
        // i don't know where i can reset the values again; 

    }
    else 
    {

    }
    return 0;
}

【问题讨论】:

    标签: c++ tic-tac-toe


    【解决方案1】:

    您必须重置游戏变量。我认为您的游戏变量重置值是:

    int rear = 0;
    player = 'X';
    char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    

    虽然您可以通过分配重置rearplayer,但必须“重新生成”matrix。重新生成matrix 的一种方法是(有关更多信息,请参阅how to assign an array from an initializer list):

        for (int i=0; i<3; i++) {
            for (int j=0; j<3; j++) {
                matrix[i][j] = i*3+(j+1) + 48;
            }
        }
    

    最后,你必须回到main函数的开头。您已经在此处添加了Start: 标签,所以我假设您可能正在尝试使用goto?标签需要移到Draw() 之前。所以这里是你的重置代码:

    if(choice=='Y' || choice=='y')
    {
        // i don't know where i can restart the values again; 
        for (int i=0; i<3; i++) {
            for (int j=0; j<3; j++) {
                matrix[i][j] = i*3+(j+1) + 48;
            }
        }
        rear = 0;
        player = 'X';
        goto Start;
    }
    

    一些问题:

    • 您不应该依赖goto,尽管在这种情况下它仍然可以。
    • system("cls") 在 Linux 上不起作用

    【讨论】:

    • 对不起@tinstaafl :(我认为你的观点也很重要,所以保留你的答案就好了
    • 谢谢我尝试了这些并添加到我的代码中它工作得很好顺便说一句 + 48 的重要性是什么?
    • 如果我们不添加 48,我们将绘制 ASCII 码 0 到 9 的字符。但是,我们想要打印 characters '0' 到 ' 9',这些具有 ASCII 码 48-57。 (参见例如en.wikipedia.org/wiki/ASCII#Character_set
    【解决方案2】:

    最简单的解决方案是创建一个执行此操作的函数 (void InitGame()) 并在需要时运行它。

    附带说明:矩阵索引可以通过几个简单的算法matrix[(position - 1)/3][(position - 1) % 3] 从位置计算出来。这在初始化矩阵时也很方便。

    此外,如果每个位置等于每个玩家,而不是检查每一行,只需检查每个位置的值是否相等,然后该值代表获胜者。

    【讨论】:

    • 这是个好主意。感谢您的评论。
    猜你喜欢
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 2015-01-08
    相关资源
    最近更新 更多