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

C - Tic-tac-toe相关推荐

  1. python二维游戏示例_Python实现的井字棋(Tic Tac Toe)游戏示例

    本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏.分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码 ...

  2. python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行

    转不到"玩家1"的原因是你的支票中缺少一个空格.你也没有正确地检查一个玩家何时获胜,这就是为什么你会有这种奇怪的行为.你需要检查每个位置,而不仅仅是最后一个.我还添加了对用户输入的 ...

  3. react中使用构建缓存_通过在React中构建Tic Tac Toe来学习ReasonML

    react中使用构建缓存 3. 7. 2018: UPDATED to ReasonReact v0.4.2 3. 7. 2018:更新为ReasonReact v0.4.2 You may have ...

  4. Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

    1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...

  5. amazon.设计1. tic tac toe

    //不觉中 已经全力找工作好久好久了.大概有1年半了.身心疲惫,不要放弃.曙光快来了. 1.tic tac toe //http://www.ntu.edu.sg/home/ehchua/progra ...

  6. python井字棋ai,python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  7. python井字棋游戏代码_Python实现的井字棋(Tic Tac Toe)游戏示例

    Python实现的井字棋(Tic Tac Toe)游戏示例 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  Python实现的井字棋(Tic Tac Toe)游戏示 ...

  8. C++ 很有趣:编写一个井字游戏 (Tic Tac Toe)

    英文原文:C++ is fun: Writing a Tic Tac Toe Game 这个有趣的C++系列打算展示一下使用C++写代码可以和其他主流语言一样高效而有趣.在第二部分,我将向你展示使用C ...

  9. 圈叉游戏 java_【炫光圈叉棋】炫光圈叉棋 Tic Tac Toe Glow 1.8.1下载_安卓(android)软件下载-魅族溜...

    一款炫光风格的圈叉棋游戏,支持单/双人模式.圈叉棋,英文:tic-tac-toe,别名:圈叉游戏.是一种游戏,3*3的9个方格子,先下者画圈,后下者画叉,每人可以在任意没有对方棋子的封闭方格里下一次, ...

  10. java tic tac toe_请问我这个tic tac toe的游戏代码的问题在哪里

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 Scanner in = new Scanner(System.in); final int SIZE =3; int[][] board = new i ...

最新文章

  1. 跨域问题的前后端解决方案
  2. C语言循环队列的实现方法,C语言循环队列(数组实现)
  3. xen虚拟机克隆命令
  4. shell中用grep查找并且不输出_shell中grep命令详解
  5. Halcon:区域特征:select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )
  6. jQuery 基础教程 (五)之使用jQuery创建动画效果
  7. 火爆全网的《李焕英》通过 AI 还原“复活”了
  8. 第十九章:李丽质入狱
  9. python调试利器pysnooper
  10. java jar包 资源_一个小坑:java如何访问依赖jar包中的资源文件
  11. 为什么webview中网页字体大小跟样式设置的不同?
  12. 鱼塘钓鱼(贪心算法)--算法设计
  13. Micrometer中文文档
  14. 学php应该怎么学习数学,数学难学,数学到底该怎么学?
  15. 为什么受伤的总是我,赢的总是她?
  16. C++编程验证费马小定律
  17. 2020 CES“高尖精”机器人回顾!
  18. L1-031 到底是不是太胖了 (10分)
  19. 一秒获取朋友位置(火绒抓取ip)
  20. 成龙在北大的演讲:值得每一个中国人看

热门文章

  1. 60分钟轻松搞定树莓派 AI 服务开发
  2. P6474 [NOI Online #2 入门组] 荆轲刺秦王 题解--zhengjun
  3. openGemini正式加入openEuler DB SIG,携手开展全方面技术创新
  4. 基于飞桨PP-PicoDet的钢铁缺陷检测
  5. docker恢复redis备份文件rdb被覆盖
  6. CSS写一个网页烟花特效并使用
  7. JS数组(最全的数组最详解包括es6)
  8. Linux入门第三讲(完结)
  9. 金佩姗 - 决 歌词
  10. css label 居中布局_详解 CSS 居中布局技巧