NEFU要崛起第2场 C - Tic-tac-toe

本文详细介绍了C-Tic-tac-toe游戏的规则,并通过代码实现来解决如何判断当前玩家是谁,游戏是否合法,谁赢得了游戏或是否平局的问题。代码实现中包含了对游戏布局的输入解析,以及对游戏状态的逻辑判断。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C - Tic-tac-toe
Crawling in process... Crawling failed Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

Description

Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts). The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is announced.

You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:

  • illegal — if the given board layout can't appear during a valid game;
  • the first player won — if in the given board layout the first player has just won;
  • the second player won — if in the given board layout the second player has just won;
  • draw — if the given board layout has just let to a draw.

Input

The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).

Output

Print one of the six verdicts: first, second, illegal, the first player won, the second player won or draw.

Sample Input

Input
X0X
.0.
.X.
Output
second

表示很纠结,弱爆了…………n次wa后,看了若干测试样例后终于A了。各种情况,考虑不周啊。

#include<iostream>
#include<stdio.h>
using namespace std;
char map[3][3];
int main()
{
    while(scanf("%s",&map[0])!=EOF)
    {
        int flag=0,flag2=0;
        int num1=0,num2=0,num3=0;
        scanf("%s",&map[1]);
        scanf("%s",&map[2]);
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++)
            {
                if(map[i][j]=='X')
                    num1++;
                if(map[i][j]=='0')
                    num2++;
                if(map[i][j]=='.')
                    num3++;
            }
        if(map[0][0]=='X')
        {
            if((map[0][1]=='X'&&map[0][2]=='X')||(map[1][0]=='X'&&map[2][0]=='X')||(map[1][1]=='X'&&map[2][2]=='X'))
                flag=1;
        }
        if(map[0][0]=='0')
        {
            if((map[0][1]=='0'&&map[0][2]=='0')||(map[1][0]=='0'&&map[2][0]=='0')||(map[1][1]=='0'&&map[2][2]=='0'))
                flag2=1;
        }
        if(map[0][1]=='X')
        {
            if((map[0][0]=='X'&&map[0][2]=='X')||(map[1][1]=='X'&&map[2][1]=='X'))
                flag=1;
        }
        if(map[0][1]=='0')
        {
            if((map[0][0]=='0'&&map[0][2]=='0')||(map[1][1]=='0'&&map[2][1]=='0'))
                flag2=1;
        }
        if(map[0][2]=='X')
        {
            if((map[0][1]=='X'&&map[0][0]=='X')||(map[1][1]=='X'&&map[2][0]=='X')||(map[1][2]=='X'&&map[2][2]=='X'))
                flag=1;
        }
        if(map[0][2]=='0')
        {
            if((map[0][1]=='0'&&map[0][0]=='0')||(map[1][1]=='0'&&map[2][0]=='0')||(map[1][2]=='0'&&map[2][2]=='0'))
                flag2=1;
        }
        if(map[1][0]=='X')
        {
            if((map[0][0]=='X'&&map[2][0]=='X')||(map[1][1]=='X'&&map[1][2]=='X'))
                flag=1;
        }
        if(map[1][0]=='0')
        {
            if((map[0][0]=='0'&&map[2][0]=='0')||(map[1][1]=='0'&&map[1][2]=='0'))
                flag2=1;
        }
        if(map[1][1]=='X')
        {
            if((map[0][0]=='X'&&map[2][2]=='X')||(map[0][1]=='X'&&map[2][1]=='X')||(map[0][2]=='X'&&map[2][0]=='X')||
                    (map[1][0]=='X'&&map[1][2]=='X'))
                flag=1;
        }
        if(map[1][1]=='0')
        {
            if((map[0][0]=='0'&&map[2][2]=='0')||(map[0][1]=='0'&&map[2][1]=='0')||(map[0][2]=='0'&&map[2][0]=='0')||
                    (map[1][0]=='0'&&map[1][2]=='0'))
                flag2=1;
        }
        if(map[1][2]=='X')
        {
            if((map[1][0]=='X'&&map[1][1]=='X')||(map[0][2]=='X'&&map[2][2]=='X'))
                flag=1;
        }
        if(map[1][2]=='0')
        {
            if((map[1][0]=='0'&&map[1][1]=='0')||(map[0][2]=='0'&&map[2][2]=='0'))
                flag2=1;
        }

        if(map[2][2]=='X')
        {
            if((map[2][1]=='X'&&map[2][0]=='X')||(map[1][2]=='X'&&map[0][2]=='X')||(map[1][1]=='X'&&map[0][0]=='X'))
                flag=1;
        }
        if(map[2][2]=='0')
        {
            if((map[2][1]=='0'&&map[2][0]=='0')||(map[1][2]=='0'&&map[0][2]=='0')||(map[1][1]=='0'&&map[0][0]=='0'))
                flag2=1;
        }
        if(map[2][1]=='X')
        {
            if((map[2][0]=='X'&&map[2][2]=='X')||(map[1][1]=='X'&&map[0][1]=='X'))
                flag=1;
        }
        if(map[2][1]=='0')
        {
            if((map[2][0]=='0'&&map[2][2]=='0')||(map[1][1]=='0'&&map[0][1]=='0'))
                flag2=1;
        }
        if(map[2][0]=='X')
        {
            if((map[1][0]=='X'&&map[0][0]=='X')||(map[1][1]=='X'&&map[0][2]=='X')||(map[2][1]=='X'&&map[2][2]=='X'))
                flag=1;
        }
        if(map[2][0]=='0')
        {
            if((map[1][0]=='0'&&map[0][0]=='0')||(map[1][1]=='0'&&map[0][2]=='0')||(map[2][1]=='0'&&map[2][2]=='0'))
                flag2=1;
        }
        if(num1-num2>1||num1<num2)
        cout<<"illegal"<<endl;
        else if(flag==1&&!flag2)
        {
            if(num1==num2)
            cout<<"illegal"<<endl;
            else
            cout<<"the first player won"<<endl;
        }

        else if(!flag&&flag2==1)
        {
            if(num1-num2==1)
            cout<<"illegal"<<endl;
            else
            cout<<"the second player won"<<endl;
        }
        else if(flag==1&&flag2==1)
            cout<<"illegal"<<endl;
        else if(!flag&&!flag2)
        {
            if(num3>0)
            {
                if(num1-num2==1)
                cout<<"second"<<endl;
                else if(num1==num2)
                cout<<"first"<<endl;
            }
            else
            {
                if(num1-num2==1)
                cout<<"draw"<<endl;
                else
                cout<<"illegal"<<endl;
            }
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值