美文网首页kata
每日kata~09~Tic-Tac-Toe Checker

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

作者: Lacia | 来源:发表于2020-05-11 18:00 被阅读0次

    题目

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

    相关文章

      网友评论

        本文标题:每日kata~09~Tic-Tac-Toe Checker

        本文链接:https://www.haomeiwen.com/subject/uycpnhtx.html