#include<bits/stdc++.h>
using namespace std;
bool shuying = false;
char a[3][3];
void pan(char a[3][3])
{
// Judge the winner or loser vertically
for(int j=0;j<3;j++)
{
if(a[1][j]!='O')
{
char PAN = a[1][j];
if(a[0][j]==PAN&&a[2][j]==PAN)
{
if(PAN=='B')
{
cout<<"------ black wins -----";
shuying = true;
}
if(PAN=='W')
{
cout<<"------ white wins -----";
shuying = true;
}
}
}
}
//Win or lose by line
for(int i=0;i<3;i++)
{
if(a[i][1]!='O')
{
char PAN = a[i][1];
if(a[i][0]==PAN&&a[1][2]==PAN)
{
if(PAN=='B')
{
cout<<"------ black wins -----";
shuying = true;
}
if(PAN=='W')
{
cout<<"------ white wins -----";
shuying = true;
}
}
}
}
//Diagonally judge wins and loses
if(a[1][1]!='O')
{
char PAN = a[1][1];
if(a[0][0]==PAN&&a[2][2]==PAN||a[0][2]==PAN&&a[2][0]==PAN)
{
if(PAN=='B')
{
cout<<"------ black wins -----";
shuying = true;
}
if(PAN=='W')
{
cout<<"------ white wins -----";
shuying = true;
}
}
}
}
void shuchu(char a[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<a[i][j]<<' ';
}
cout<<endl;
}
}
int main()
{
cout<<"It's a tic-tac-toe program"<<endl<<endl;
cout<<"'O' for 'empty' 'B' for 'black' 'W' for 'white'"<<endl<<endl;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j] = 'O';
cout<<"This is the initial state:"<<endl<<endl;
shuchu(a);
cout<<endl;
cout<<"Okay, now the game begins"<<endl<<endl<<endl;
for(int ci=0;ci<9;ci++)
{
if(ci%2==0)
{
cout<<"For black players, please enter rows and columns"<<endl;
shuchu(a);
cout<<endl;
int i,j;
char P_i,P_j;
cin>>P_i>>P_j;
if(P_i>='1'&&P_i<='3'&&P_j>='1'&&P_j<='3')
{
i = P_i-48;
j = P_j-48;
}
else
{
cout<<endl<<"Typing error"<<endl<<endl;
ci--;
continue;
}
if(a[i-1][j-1]=='O') a[i-1][j-1] = 'B';
else
{
cout<<endl<<"The position has been subordinated"<<endl<<endl;
ci--;
continue;
}
cout<<endl;
}
if(ci%2==1)
{
cout<<"For white players, please enter rows and columns"<<endl;
shuchu(a);
cout<<endl;
int i,j;
char P_i,P_j;
cin>>P_i>>P_j;
if(P_i>='1'&&P_i<='3'&&P_j>='1'&&P_j<='3')
{
i = P_i-48;
j = P_j-48;
}
else
{
cout<<endl<<"Typing error"<<endl<<endl;
ci--;
continue;
}
if(a[i-1][j-1]=='O') a[i-1][j-1] = 'W';
else
{
cout<<endl<<"The position has been subordinated"<<endl<<endl;
ci--;
continue;
}
cout<<endl;
}
pan(a);
if(shuying) return 0;
}
cout<<"-----Well, this matchup didn't decide the winner-----";
return 0;
}
c++的井字棋程序(免费版)
于 2024-09-04 20:37:09 首次发布