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
X0X .0. .X.
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相关推荐
- python二维游戏示例_Python实现的井字棋(Tic Tac Toe)游戏示例
本文实例讲述了Python实现的井字棋(Tic Tac Toe)游戏.分享给大家供大家参考,具体如下: 说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码 ...
- python游戏代码运行不了_无法使我的tic tac toe游戏在python中正确运行
转不到"玩家1"的原因是你的支票中缺少一个空格.你也没有正确地检查一个玩家何时获胜,这就是为什么你会有这种奇怪的行为.你需要检查每个位置,而不仅仅是最后一个.我还添加了对用户输入的 ...
- 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 ...
- 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 ...
- amazon.设计1. tic tac toe
//不觉中 已经全力找工作好久好久了.大概有1年半了.身心疲惫,不要放弃.曙光快来了. 1.tic tac toe //http://www.ntu.edu.sg/home/ehchua/progra ...
- python井字棋ai,python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- python井字棋游戏代码_Python实现的井字棋(Tic Tac Toe)游戏示例
Python实现的井字棋(Tic Tac Toe)游戏示例 来源:中文源码网 浏览: 次 日期:2018年9月2日 [下载文档: Python实现的井字棋(Tic Tac Toe)游戏示 ...
- C++ 很有趣:编写一个井字游戏 (Tic Tac Toe)
英文原文:C++ is fun: Writing a Tic Tac Toe Game 这个有趣的C++系列打算展示一下使用C++写代码可以和其他主流语言一样高效而有趣.在第二部分,我将向你展示使用C ...
- 圈叉游戏 java_【炫光圈叉棋】炫光圈叉棋 Tic Tac Toe Glow 1.8.1下载_安卓(android)软件下载-魅族溜...
一款炫光风格的圈叉棋游戏,支持单/双人模式.圈叉棋,英文:tic-tac-toe,别名:圈叉游戏.是一种游戏,3*3的9个方格子,先下者画圈,后下者画叉,每人可以在任意没有对方棋子的封闭方格里下一次, ...
- java tic tac toe_请问我这个tic tac toe的游戏代码的问题在哪里
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 Scanner in = new Scanner(System.in); final int SIZE =3; int[][] board = new i ...
最新文章
- 跨域问题的前后端解决方案
- C语言循环队列的实现方法,C语言循环队列(数组实现)
- xen虚拟机克隆命令
- shell中用grep查找并且不输出_shell中grep命令详解
- Halcon:区域特征:select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )
- jQuery 基础教程 (五)之使用jQuery创建动画效果
- 火爆全网的《李焕英》通过 AI 还原“复活”了
- 第十九章:李丽质入狱
- python调试利器pysnooper
- java jar包 资源_一个小坑:java如何访问依赖jar包中的资源文件
- 为什么webview中网页字体大小跟样式设置的不同?
- 鱼塘钓鱼(贪心算法)--算法设计
- Micrometer中文文档
- 学php应该怎么学习数学,数学难学,数学到底该怎么学?
- 为什么受伤的总是我,赢的总是她?
- C++编程验证费马小定律
- 2020 CES“高尖精”机器人回顾!
- L1-031 到底是不是太胖了 (10分)
- 一秒获取朋友位置(火绒抓取ip)
- 成龙在北大的演讲:值得每一个中国人看