每日kata~09~Tic-Tac-Toe Checker

题目

https://www.codewars.com/kata/525caa5c1bf619d28c000335

f we were to set up a Tic-Tac-Toe game, we would want to know whether the board's current state is solved, wouldn't we? Our goal is to create a function that will check that for us!

Assume that the board comes in the form of a 3x3 array, where the value is 0 if a spot is empty, 1 if it is an "X", or 2 if it is an "O", like so:

[[0, 0, 1],
[0, 1, 2],
[2, 1, 0]]
We want our function to return:

-1 if the board is not yet finished (there are empty spots),
1 if "X" won,
2 if "O" won,
0 if it's a cat's game (i.e. a draw).
You may assume that the board passed in is valid in the context of a game of Tic-Tac-Toe.

吃完感康一下午昏昏沉沉脑子不转弯,想了半天只能想出最笨的解法
大神的解法

def isSolved(board):
    for sign in [1, 2]:
        win = [sign] * 3
        if (win in board or
            win in zip(*board[::-1]) or
            win in [[board[x][0], board[1][1], board[2-x][2]] for x in [0, 2]]):
                return sign
    return -1 if 0 in sum(board, []) else 0

win=[sign]*3,妙啊

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容