当前位置: 首页 > 工具软件 > TAC > 使用案例 >

C - Tic-tac-toe

李嘉胜
2023-12-01
C - Tic-tac-toe

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 a3 × 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 ordraw.

Sample Input

Input
X0X
.0.
.X.
Output
second


题目大意:

给出一个只有“X”,“O”,“.”的tic-tac-toe 棋盘,先手的人是“X”,后手的人是“0”,根据给出的棋盘判断棋盘的结果:先手的人获胜;后手的人获胜;平局;轮到先手的人了;轮到后手的人了;棋盘非法。面。

解题思路:

暴力判断,注意分类时考虑全


#include <iostream>
#include <cstdio>
using namespace std;
int X,O;
char grid[3][3];
int win(char sym)
{
    int i;
    for(i=0;i<3;i++)
    {
        if(grid[i][0]==sym&&grid[i][1]==sym&&grid[i][2]==sym)
            return 1;
        if(grid[0][i]==sym&&grid[1][i]==sym&&grid[2][i]==sym)
            return 1;
    }
    if(grid[1][1]!=sym)
        return 0;
    if(grid[0][0]==sym&&grid[2][2]==sym)
        return 1;
    if(grid[0][2]==sym&&grid[2][0]==sym)
        return 1;
    return 0;
}
int legal()
{
    int i,j;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            X+=grid[i][j]=='X';
            O+=grid[i][j]=='0';
        }
    }
    if(X-O>1||X-O<0)
        return 0;
    if((win('X')&&X==O)||(win('0')&&X-O==1))
        return 0;
    return 1;
}
int main()
{
    int i,j;
    X=O=0;
    char stmp[4];
    for(i=0;i<3;i++)
    {
        scanf("%s",stmp);
        for(j=0;j<3;j++)
            grid[i][j]=stmp[j];
    }
     while (1)
    {
        if(!legal())
        {
            printf("illegal\n");
            break;
        }
        if(win('X'))
        {
            printf("the first player won\n");
            break;
        }
        if(win('0'))
        {
            printf("the second player won\n");
            break;
        }
        if(X+O==9)
        {
            printf("draw\n");
            break;
        }
        if(X==O)
        {
            printf("first\n");
            break;
        }
        if(X-O==1)
        {
            printf("second\n");
            break;
        }
    } 
    return 0;
}


 类似资料: