关闭

CF 3C Tic-tac-toe

476人阅读 评论(0) 收藏 举报

考虑全面!!

#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;

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72723次
    • 积分:1571
    • 等级:
    • 排名:千里之外
    • 原创:92篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    最新评论