Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中重用代码来跟踪三角形板上的片段运行_Python - Fatal编程技术网

如何在Python中重用代码来跟踪三角形板上的片段运行

如何在Python中重用代码来跟踪三角形板上的片段运行,python,Python,我正在用Python做一个项目,其中一部分涉及在游戏中对棋盘状态进行排名。这个游戏,鬼鬼祟祟的雕像,类似于连接四,在其中你赢得了四件在一排。我对棋盘的天真估计是一行中有多少块(1、2、3或4)。棋盘是三角形的,所以你可以在水平方向或对角方向上有一行。这就是我现在用来查找片段运行的函数 def score(player): player_x = sorted(player, key=lambda statue: statue.x) #player's pieces sorted by x

我正在用Python做一个项目,其中一部分涉及在游戏中对棋盘状态进行排名。这个游戏,鬼鬼祟祟的雕像,类似于连接四,在其中你赢得了四件在一排。我对棋盘的天真估计是一行中有多少块(1、2、3或4)。棋盘是三角形的,所以你可以在水平方向或对角方向上有一行。这就是我现在用来查找片段运行的函数

def score(player):

    player_x = sorted(player, key=lambda statue: statue.x) #player's pieces sorted by x coordinate
    player_y = sorted(player, key=lambda statue: statue.y)
    max_score = [0]

    count = 1
    #pieces are in a horizontal line if they share a y coord and have sequential x coords
    for cur_s, next_s in zip(player_x, player_x[1:]):
        if cur_s.x + 1 == next_s.x and cur_s.y == next_s.y:
            count += 1
        else:
            max_score.append(count)
            count = 1
    max_score.append(count)

    count = 1
    #pieces are diagonal if they share an x and have sequential y's
    for cur_s, next_s in zip(player_y, player_y[1:]):
        if cur_s.y + 1 == next_s.y and cur_s.x == next_s.x:
            count += 1
        else:
            max_score.append(count)
            count = 1
    max_score.append(count)

    count = 1
    #they are diagonal if both x's and y's are sequential
    for cur_s, next_s in zip(player_y, player_y[1:]):
        if cur_s.y + 1 == next_s.y and cur_s.x + 1 == next_s.x:
            count += 1                                                                                                                                                               
        else:
            max_score.append(count)
            count = 1
    max_score.append(count)

    return max(max_score)

据我所知,它是有效的,但我基本上是重复自己三次。我的问题是,对于我来说,编写此函数以减少重复次数的最佳方式是什么?

这可能不是最好的方式,但乍一看,我发现可以将所有循环组合到一个具有三个参数的函数中:

def score(player):

    player_x = sorted(player, key=lambda statue: statue.x) #player's pieces sorted by x coordinate
    player_y = sorted(player, key=lambda statue: statue.y)
    max_score = [0]

    def take_count(player, x_offset, y_offset):
        count = 1
        for cur_s, next_s in zip(player, player[1:]):
            if cur_s.x + x_offset == next_s.x and cur_s.y + y_offset == next_s.y:
                count += 1
            else:
                max_score.append(count)
                count = 1
        max_score.append(count)

    #pieces are in a horizontal line if they share a y coord and have sequential x coords
    take_count(player_x, 1, 0)

    #pieces are diagonal if they share an x and have sequental y's
    take_count(player_y, 0, 1)

    #they are diagonal if both x's and y's are sequential
    take_count(player_y, 1, 1)

    return max(max_score)