人人对战五子棋 C++
#include
#include
#include
using namespace std;
char chessboard[55][55];
int X,Y,Size;
void Initialize_the_chessboard() //初始化棋盘
{
for (int i = 1; i <= Size; i++)
for (int j = 1; j <= Size; j++)
chessboard[i][j] = ' '; //先将所有棋盘初始化为空格
}
void Print_the_chessboard() //打印棋盘
{
system("cls"); //打印之前先清屏
//打印棋盘头部
printf("%20s", "Five in a row"); //打印题目
cout << 'n';
cout << "Player1 (X) - Player2 (O)"; //两位玩家
cout << 'n';
//打印棋盘主体, 用循环结构完成即可
cout<<" "; //空出列序号的位置
for(int i=1;i<=Size;i++)
printf("%4d",i); //按格式输出
cout<'9')
return 2; //输入的字符中存在违法的字符
for(int i=0;i'9')
return 2; //输入的字符中存在违法的字符
int x= stoi(s1),y=stoi(s2);
if(!(x>0&&x<=Size&y>0&&y<=Size))
return -1; //输入的数字超出了棋盘的范围
if(chessboard[x][y]!=' ')
return 0;
return 1;
}
void Player(int x)
{
if(x==1)
cout << "Player1, enter the location:";
else
cout<< "Player2, enter a number:";
string s1,s2;
cin >> s1>>s2; //输入
int a=judge(s1,s2); //判断输入是否合法
if(a==2)
{
cout << "Your enter is not a number, please try again.n";
Player(x);
}
else if(a==-1)
{
cout << "Your number is out of range, please try again.n";
Player(x);
}
else if(a==0)
{
cout << "Your number has been occupied, please try again.n";
Player(x);
}
else if(a == 1)
{
int i=stoi(s1),j= stoi(s2);
X=i,Y=j; //把最后一次落子的位置传入X,Y中
if(x==1)
chessboard[i][j]='X';
else
chessboard[i][j]='O';
}
} //游戏主程序
int check()
{
int flag = 0;
int count1;
char ch=' ';
int x=X,y=Y;
for (int i = 1; i < Size; i++)
for (int j = 1; j < Size; j++)
if (chessboard[i][j] == ' ')
flag++; //判断还有几个空格
count1=1;
for(int i=1;i<=Size-1;i++)
{
if(chessboard[X][i]==chessboard[X][i+1]&&chessboard[X][i]!=' ')
count1++;
else
count1=1;
if(count1==5)
{
ch=chessboard[X][i];
goto A; //判断最后落子所在行是否有相等的
}
}
count1=1;
for(int i=1;i<=Size-1;i++)
{
if(chessboard[i][Y]==chessboard[i+1][Y]&&chessboard[i][Y]!=' ')
count1++;
else
count1=1;
if(count1==5)
{
ch=chessboard[X][i];
goto A; //判断最后落子所在列是否有相等的
}
}
count1=1;
while(true)
{
x--,y--;
if(x<0||y<0)
break;
}
x++,y++;
for(int i=x,j=y;i+1<=Size||j+1<=Size;i++,j++)
{
if(chessboard[i][j]==chessboard[i+1][j+1]&&chessboard[i][j]!=' ')
count1++;
else
count1=1;
if(count1==5)
{
ch=chessboard[i][j];
goto A;
}
}
x=X,y=Y;
count1=1;
while(true)
{
x--,y++;
if(x<0||y>Size)
break;
}
x++,y--;
for(int i=x,j=y;i+1<=Size||j+1>0;i++,j--)
{
if(chessboard[i][j]==chessboard[i+1][j+1]&&chessboard[i][j]!=' ')
count1++;
else
count1=1;
if(count1==5)
{
ch=chessboard[i][j];
goto A;
}
}
A: if(ch==' '&&flag==0)
return -1; //棋盘被占满, 但没有五子出现
else if(ch=='X')
return 1; //表示一号选手获胜
else if(ch=='O')
return 2; //表示二号选手获胜
else
return 0;
} //判断输赢
int main()
{
while(true)
{
cout<<"请输入棋盘大小(<=50):";
cin>>Size;
if(Size>=5&&Size<=50)
break;
else
cout<<"Your enter is not correct, please try again.n";
}
Initialize_the_chessboard();
Print_the_chessboard();
int p = 1;
while (true)
{
Player(p % 2);
Print_the_chessboard();
p++;
int a = check();
if (a == 1)
{
cout << "The winner is Player1.";
break;
}
else if (a == 2)
{
cout << "The winner is Player2.";
break;
}
else if (a == -1)
{
cout << "The game is over. No one is a winner.";
break;
}
else
continue;
}
system("pause");
}