kata

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

2020-05-11  本文已影响0人  Lacia

题目

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,妙啊

上一篇下一篇

猜你喜欢

热点阅读