Data Structure & Algorithm: Level-1 Problem #53. Tic-tac-toe Game

Tic-tac-toe is a paper-and-pencil game for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three of their marks in a diagonal, horizontal, or vertical row is the winner. It is a solved game with a forced draw assuming best play from both players.

Given a status for the game, check whether there is a winner. The left lower corner position is (0, 0) and the top right corner position is (2, 2). The first player A draws X, and the second plaer B draws O.

If no winner and game is over then output TIE. If no winner and game is not over then output PENDING.

Sample Input:

moves =[ (1, 1), (0, 0), (2, 0), (0, 2), (0, 1), (2, 1) ]

Sample Output:

PENDING

Tic-tac-toe2

Based on the sample input, the above game is not over yet. So the output is PENDING.


数据结构和算法:初级练习题 #53 – 井字棋


井字棋 – 中国大陆、台湾又称为井字游戏、圈圈叉叉;另外也有打井游戏、○×棋的称呼,香港多称井字过三关、过三关,是种纸笔游戏,另有多种派生变化玩法。

两个玩家,一个打叉(✗),一个打圈(◯),轮流在3乘3的格上打自己的符号,最先以横、直、斜连成一线则为胜。如果双方都下得正确无误,将得和局。这种游戏实际上是由第一位玩家所控制,第一位玩家是攻,第二位玩家是守。

给定一个井字棋的棋谱,判断是否有赢家。棋盘左下角坐标为(0,0),右上角坐标为(2,2)。先手玩家A下叉,后手玩家B下圈。如果平局就输出 TIE,如果棋局未结束就输出 PENDING。

输入样例:

moves =[ (1, 1), (0, 0), (2, 0), (0, 2), (0, 1), (2, 1) ]

输出样例:

PENDING

根据输入样例棋谱,当前棋局没有赢家也没有结束,所以输出 PENDING。


Python Solution

Tic-tac-toe Solution