人人对战五子棋 C++
#include<iostream>
#include<cstring>
#include<windows.h>
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<<endl;
for(int i=1;i<=Size;i++)
{
printf("%2d",i);
for (int j = 1; j <= Size; j++)
{
cout << "| " << chessboard[i][j] << " ";
}
cout<<"|";
cout<<endl<<" ";
for(int j=1;j<=Size;j++)
cout<<" ---";
cout<<endl;
}
}
int judge(string s1,string s2)
{
for(int i=0;i<s1.size();i++)
if(s1[i]<'0'||s1.size()>'9')
return 2;
for(int i=0;i<s2.size();i++)
if(s2[i]<'0'||s2.size()>'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;
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");
}