【问题标题】:Reset the values after first loop tic tac toe c++?在第一个循环tic tac toe c ++之后重置值?
【发布时间】:2020-05-05 15:23:28
【问题描述】:

游戏不会重置上次游戏的输入。它具有第一个游戏的输入,并且不会在询问用户是否想再次玩的循环中重置。我尝试制作一个 void 函数,通过拥有两个相同的数组来重置代码的值,但它仍然不起作用。请帮忙,因为我对 c++ 还很陌生。

#include <iostream>
using namespace std;
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
const char matrix2[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char player = 'X';
int n;
int xscore = 0, oscore = 0, gamenum = 1;
void Draw()
{
system("cls");
cout << "T i c T a c T o e" << endl;
cout << "Game #" << gamenum << " Score: X - [" << xscore << "] O - [" << oscore << "]" << endl;
for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        cout << matrix[i][j] << "   ";
    }
    cout << endl;
}
}
void Input()
{
int a;
cout << "It's " << player << " turn. " <<"Press the number of the field: ";
cin >> a;

if (a == 1)
{
    if (matrix[0][0] == '1')
        matrix[0][0] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 2)
{
    if (matrix[0][1] == '2')
        matrix[0][1] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 3)
{
    if (matrix[0][2] == '3')
        matrix[0][2] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 4)
{
    if (matrix[1][0] == '4')
        matrix[1][0] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 5)
{
    if (matrix[1][1] == '5')
        matrix[1][1] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 6)
{
    if (matrix[1][2] == '6')
        matrix[1][2] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 7)
{
    if (matrix[2][0] == '7')
        matrix[2][0] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 8)
{
    if (matrix[2][1] == '8')
        matrix[2][1] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}
else if (a == 9)
{
    if (matrix[2][2] == '9')
        matrix[2][2] = player;
    else
    {
        cout << "Number is already filled try again." << endl;
        Input();
    }
}

}
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 '/';
}
void gamereset()
{
for (int g = 0; g < 3; g++)
{
    for (int h = 0; h < 3; h++)
    {
        matrix2[g][h] == matrix[g][h];
    }
}
}
int main()
{
char user;
do{
n = 0;
Draw();
while (1)
{
    n++;
    Input();
    Draw();
    if (Win() == 'X')
    {
        cout << "X wins!" << endl;
        xscore++;
        break;
    }
    else if (Win() == 'O')
    {
        cout << "O wins!" << endl;
        oscore++;
        break;
    }
    else if (Win() == '/' && n == 9)
    {
        cout << "It's a draw!" << endl;
        break;
    }
    TogglePlayer();
}
cout << "Do you want to play again[Y/N]:";
cin >> user;
gamenum++;
gamereset();
}
while(user == 'Y' or user == 'y');
}   

如您所见,gamreset() 函数不起作用。我尝试了很多方法来弄清楚如何重置它,但它仍然不起作用。

【问题讨论】:

  • 如果你在编译代码时启用了警告,你的编译器应该会告诉你matrix2[g][h] == matrix[g][h] 什么都不做。改为matrix[g][h] = matrix2[g][h]
  • 你知道'1' == '0' + amatrix[0][0] == matrix[(a-1) / 3][(a-1) % 3] 对应a = 1 吗?这适用于矩阵的所有字段。您可以使用它来删除 if-else 级联。这是一个例子:wandbox.org/permlink/Hu7lkbsDXOPeDIZM

标签: c++ loops


【解决方案1】:

matrix2[g][h] == matrix[g][h];替换为matrix[g][h] = matrix2[g][h];不起作用的原因是因为==if语句中使用的运算符,并根据左侧和右侧返回truefalse是否相等。而= 使左侧组件等于右侧组件的值,我可以看到您的matrix2const,这意味着它的值无法更改,因此您可能打算将matrix2 复制到matrix 和不是matrix 变成matrix2 你也可以使用memcpy 来实现。

【讨论】:

  • 好的,谢谢你的回答真的帮助我理解了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-12
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
相关资源
最近更新 更多