CF 3C Tic-tac-toe

考虑全面!!

#include <iostream>
using namespace std;
char m[3][3];
bool fwin()
{
    if((m[0][0] == m[0][1]&&m[0][1]==m[0][2]&&m[0][0] == 'X')||
       (m[1][0] == m[1][1]&&m[1][1]==m[1][2]&&m[1][0] == 'X')||
       (m[2][0] == m[2][1]&&m[2][1]==m[2][2]&&m[2][0] == 'X')||
       (m[0][0] == m[1][0]&&m[1][0]==m[2][0]&&m[0][0] == 'X')||
       (m[0][1] == m[1][1]&&m[1][1]==m[2][1]&&m[0][1] == 'X')||
       (m[0][2] == m[1][2]&&m[1][2]==m[2][2]&&m[0][2] == 'X')||
       (m[0][0] == m[1][1]&&m[1][1]==m[2][2]&&m[0][0] == 'X')||
       (m[0][2] == m[1][1]&&m[1][1]==m[2][0]&&m[0][2] == 'X'))
       return 1;
    else return 0;
}
bool swin()
{
    if((m[0][0] == m[0][1]&&m[0][1]==m[0][2]&&m[0][0] == '0')||
       (m[1][0] == m[1][1]&&m[1][1]==m[1][2]&&m[1][0] == '0')||
       (m[2][0] == m[2][1]&&m[2][1]==m[2][2]&&m[2][0] == '0')||
       (m[0][0] == m[1][0]&&m[1][0]==m[2][0]&&m[0][0] == '0')||
       (m[0][1] == m[1][1]&&m[1][1]==m[2][1]&&m[0][1] == '0')||
       (m[0][2] == m[1][2]&&m[1][2]==m[2][2]&&m[0][2] == '0')||
       (m[0][0] == m[1][1]&&m[1][1]==m[2][2]&&m[0][0] == '0')||
       (m[0][2] == m[1][1]&&m[1][1]==m[2][0]&&m[0][2] == '0'))
       return 1;
    else return 0;
}
int main()
{
    int i,j,f = 0,s = 0;
    bool full = 1;
    for(i = 0;i < 3;i ++)
    {
        for(j = 0;j < 3;j ++)
        {
            cin>>m[i][j];
            if(m[i][j] =='.') full = 0;
            else if(m[i][j] == 'X') f ++;
            else s ++;
        }
    }
    if(f == s||f == s + 1)
    {
        if(full)
        {
            if(fwin()) {if(f == s+1){if(swin())cout<<"illegal"<<endl;else  cout<<"the first player won"<<endl;}else cout<<"illegal"<<endl;}
            else if(swin()) {if(f==s+1)cout<<"illegal"<<endl;else cout<<"the second player won"<<endl;}
            else cout<<"draw"<<endl;
        }
        else
        {
            if(fwin())  {if(f == s+1){if(swin())cout<<"illegal"<<endl;else  cout<<"the first player won"<<endl;}else cout<<"illegal"<<endl;}
            else if(swin()) {if(f==s+1)cout<<"illegal"<<endl;else cout<<"the second player won"<<endl;}
            else if(f == s) cout<<"first"<<endl;
            else cout<<"second"<<endl;
        }
    }
    else cout<<"illegal" <<endl;

}


你可能感兴趣的:(c)