所以我写了“tic tac toe”主机游戏。它读取播放器的名称并将它们存储在文本文件中,并显示(或应该显示)文本文件中所有播放器的列表。但它不会显示所有玩家的名字。如果我直接选择显示名称选项(即案例 2),它不会显示任何内容。如果我玩游戏一次并按两个它只会显示当前 2 个玩家的名字。我希望它显示该列表中所有玩家的姓名!这是我的代码(查看 main () 中的案例 2),案例 3 也显示了意想不到的结果。请帮助
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
void user_info ();
void gameplay();
using namespace std;
int turn[9],Row,Col;
const int row=3,col=3;
string player1;
string player2;
void main()
{
int x;
cout<<endl;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$ Press 1 to play game $\n";
cout<<"$ Press 2 to view the list of all players $\n";
cout<<"$ Press 3 to EXIT $\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n";
cin>>x;
switch(x)
{
case 1:
{
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$ $\n";
cout<<"$ LETS PLAY TIC TAC TOE $\n";
cout<<"$ $\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n\n";
user_info ();
gameplay ();
cout<<endl<<endl<<endl;
main ();
}
case 2:
{
ifstream infile("file.txt",ios::in);
cout<<"The name of players are "<<endl<<player1<<"\t"<<player2<<endl;;
cout<<endl<<endl<<endl;
main ();
}
case 3:
{
cout<<"Goodbye"<<endl;
break;
}
default:
{
cout<<"Invalid choice\n";
main();
}
}
}
void user_info ()
{
ofstream infile("file.txt",ios::app);
cout<<"player 1 enter your name :";
cin>>player1;
cout<<"\n\nplayer 2 enter your name :";
cin>>player2;
cout<<endl<<endl;
cout<<player1<<"'s symbol is 'O'\n\n";
cout<<player2<<"'s symbol is 'X'\n\n";
cout<<endl<<endl;
infile<<player1<<"\t"<<player2<<endl;
}
void gameplay ()
{
char arr[row][col]={'_','_','_','_','_','_','_','_','_'};
cout<<endl<<endl;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl<<endl;
}
for(int k=1;k<=9;k++)
{
turn : cout<<endl;
if(k%2!=0)
{
cout<<player1<<" will enter row and column :";
cin>>turn[k];
if(turn[k]<=33)
{
Row=turn[k]/10-1;
Col=turn[k]%10-1;
}
else
{
cout<<"wrong input...";
goto turn;
}
arr[Row][Col]= 'O';
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl<<endl;
}
if (arr[0][0]=='O' && arr[0][0]==arr[1][1] && arr[1][1]==arr[2][2]) {cout<<player1<<" wins"<<endl;break;}
if (arr[2][0]=='O' && arr[2][0]==arr[1][1] && arr[1][1]==arr[0][2]) {cout<<player1<<" wins"<<endl;break;}
if (arr[0][0]=='O' && arr[0][0]==arr[1][0] && arr[1][0]==arr[2][0]) {cout<<player1<<" wins"<<endl;break;}
if (arr[0][1]=='O' && arr[0][1]==arr[1][1] && arr[1][1]==arr[2][1]) {cout<<player1<<" wins"<<endl;break;}
if (arr[0][2]=='O' && arr[0][2]==arr[1][2] && arr[1][2]==arr[2][2]) {cout<<player1<<" wins"<<endl;break;}
if (arr[0][0]=='O' && arr[0][0]==arr[0][1] && arr[0][1]==arr[0][2]) {cout<<player1<<" wins"<<endl;break;}
if (arr[1][0]=='O' && arr[1][0]==arr[1][1] && arr[1][1]==arr[1][2]) {cout<<player1<<" wins"<<endl;break;}
if (arr[2][0]=='O' && arr[2][0]==arr[2][1] && arr[2][1]==arr[2][2]) {cout<<player1<<" wins"<<endl;break;}
if (k== 9){cout<<"The game is DRAW!!"<<endl; break;} //sum=9 because there are only 9 boxes in the game
}
//big
else
{
turn2 : cout<<endl;
cout<<player2<<" will enter row and column :";
cin>>turn[k];
if(turn[k]<=33)
{
Row=turn[k]/10-1;
Col=turn[k]%10-1;
}
else
{
cout<<"wrong input...";
goto turn2;
}
arr[Row][Col]= 'X';
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<arr[i][j]<<"\t";
}
cout<<endl<<endl;
}
}
if (arr[0][0]=='X' && arr[0][0]==arr[1][1] && arr[1][1]==arr[2][2]) {cout<<player2<<" wins"<<endl;break;}
if (arr[2][0]=='X' && arr[2][0]==arr[1][1] && arr[1][1]==arr[0][2]) {cout<<player2<<" wins"<<endl;break;}
if (arr[0][0]=='X' && arr[0][0]==arr[1][0] && arr[1][0]==arr[2][0]) {cout<<player2<<" wins"<<endl;break;}
if (arr[0][1]=='X' && arr[0][1]==arr[1][1] && arr[1][1]==arr[2][1]) {cout<<player2<<" wins"<<endl;break;}
if (arr[0][2]=='X' && arr[0][2]==arr[1][2] && arr[1][2]==arr[2][2]) {cout<<player2<<" wins"<<endl;break;}
if (arr[0][0]=='X' && arr[0][0]==arr[0][1] && arr[0][1]==arr[0][2]) {cout<<player2<<" wins"<<endl;break;}
if (arr[1][0]=='X' && arr[1][0]==arr[1][1] && arr[1][1]==arr[1][2]) {cout<<player2<<" wins"<<endl;break;}
if (arr[2][0]=='X' && arr[2][0]==arr[2][1] && arr[2][1]==arr[2][2]) {cout<<player2<<" wins"<<endl;break;}
if (k== 9){cout<<"The game is DRAW!!!"<<endl; break;} //sum=9 because there are only 9 boxes in the game
}
}