您好我的程序有问题。编程对我来说是新的,我不知道在哪里我将重置矩阵的值,因为它的值的声明是全局的。要了解更多,我在下面有一个评论,其中如果游戏已经完成,程序将询问用户是否想要再次运行游戏。无论如何通过向给定代码添加一些代码或修改来解决这个问题。我试图使用函数goto Start;在if条件内,在main函数中有一个Start:write。但它没有用。请提前帮助我。
#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;
}
答案 0 :(得分:0)
您必须重置游戏变量。我认为你的游戏变量重置值是:
LoadIncrementalHFiles loader = new LoadIncrementalHFiles(conf);
loader.doBulkLoad(new Path(outputPath), admin, hTable, regionLocator);
虽然您可以通过分配重置int rear = 0;
player = 'X';
char matrix[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
和rear
,但player
必须“重新生成”。重新matrix
的一种方法是(有关详情,请参阅how to assign an array from an initializer list):
matrix
最后,你必须回到 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
无法在Linux上运行答案 1 :(得分:0)
最简单的解决方案是创建一个执行它的函数(void InitGame()
)并在需要时运行它。
注意:矩阵索引可以通过几个简单算法matrix[(position - 1)/3][(position - 1) % 3]
从位置计算出来。这对于初始化矩阵也很方便。
如果每个位置等于每个玩家,也不是检查每一行,只是检查每个位置的值是否相等,然后该值代表胜利者就更简单了。