题意:XO游戏,现在给你一局游戏,让你判断是否合法,谁获胜,如果还没获胜,则输出下一步由谁走
思路:模拟..细节很多
#include<bits/stdc++.h>
using namespace std;
string s[3];
int check(char c)
{
for(int i=0;i<3;i++)
{
int win = 1;
for(int j=0;j<3;j++)
if(s[i][j]!=c)
win=0;
if(win==1)return win;
}
for(int i=0;i<3;i++)
{
int win=1;
for(int j=0;j<3;j++)
if(s[j][i]!=c)
win=0;
if(win==1)return win;
}
if(s[0][0]==c&&s[1][1]==c&&s[2][2]==c)return 1;
if(s[0][2]==c&&s[1][1]==c&&s[2][0]==c)return 1;
return 0;
}
int main()
{
for(int i=0;i<3;i++)
cin>>s[i];
int X=0,O=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(s[i][j]=='X')X++;
else if(s[i][j]=='0')O++;
int Win1 = check('X');
int Win2 = check('0');
if(Win1&&Win2)return puts("illegal"),0;
if(Win1&&X-O!=1)return puts("illegal"),0;
if(Win2&&X!=O)return puts("illegal"),0;
if(X-O>1||O>X)return puts("illegal"),0;
if(Win1)return puts("the first player won"),0;
if(Win2)return puts("the second player won"),0;
if(X+O==9)return puts("draw"),0;
if(X==O)return puts("first"),0;
if(O==X-1)return puts("second"),0;
}