Python 搜索二维列表所有方向的更简单方法?

Python 搜索二维列表所有方向的更简单方法?,python,python-3.x,Python,Python 3.x,我正在尝试为我正在编写的《奥赛罗/逆转》游戏实现一个函数,我认为这是非常低效的 因此,基本上,有一个游戏板,有用户设置的行数和列数(行1从顶部开始,列1从左侧开始) B表示黑色,并保持整数1。 W表示白色,并保持整数2。 一个空格包含整数0 因此boardArray[7][7]将返回值1。(第8行第8列) 我正在编写一个函数来检查用户输入的移动的有效性。假设玩家布莱克想把他的作品插入第9行第5列。从该位置开始,程序必须从各个方向(北、东北、东、东南等)检查是否发现黑色碎片。如果发现,它将检查两个

我正在尝试为我正在编写的《奥赛罗/逆转》游戏实现一个函数,我认为这是非常低效的

因此,基本上,有一个游戏板,有用户设置的行数和列数(行1从顶部开始,列1从左侧开始)

B表示黑色,并保持整数1。 W表示白色,并保持整数2。 一个空格包含整数0

因此boardArray[7][7]将返回值1。(第8行第8列)

我正在编写一个函数来检查用户输入的移动的有效性。假设玩家布莱克想把他的作品插入第9行第5列。从该位置开始,程序必须从各个方向(北、东北、东、东南等)检查是否发现黑色碎片。如果发现,它将检查两个黑色块之间是否有白色块。如果发现一个白色的碎片,那个白色的碎片会变成黑色的碎片

目前,我正以一种极其低效的方式尝试这一点

    #north
    x = 1
    while True:
        try:
            if boardArray[row-x][col] == 0:
                break
            elif boardArray[row-x][col] == self._playerTurn:
                #function that flips all pieces in between the user inputted location and the location of the same-color piece found
        except IndexError:
            break
        x += 1


    #northeast
    x = 1
    while True:
        try:
            if boardArray[row-x][col+x] == 0:
                break
            elif boardArray[row-x][col+x] == self._playerTurn:
                #function that flips all pieces in between the user inputted location and the location of the same-color piece found
        except IndexError:
            break
        x += 1


    #east
    x = 1
    while True:
        try:
            if boardArray[row][col+x] == 0:
                break
            elif boardArray[row][col+x] == self._playerTurn:
                #function that flips all pieces in between the user inputted location and the location of the same-color piece found
        except IndexError:
            break
        x += 1
等等

  • 有谁能给我一个更有效的方法来实现这一点
  • 存储我们必须翻转的工件位置的好方法是什么
  • 希望这篇文章有意义!如果你知道奥赛罗的游戏规则,那就更容易理解了


    事先谢谢你Python新手

    我建议在指定行和列坐标增量的方向向量列表(可能是2元组)上使用循环,而不是为需要搜索的8个方向写8次循环。您还可以在
    范围上使用
    for
    循环,而不是
    while
    循环来处理沿方向向量移动的距离

    下面是一些应该查找并翻转所有适当空格的代码。我没有实际测试代码,因为我没有游戏的其他逻辑来包装它,但它应该已经接近正常工作了:

    toflip = []
    for x, y in [(0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1)]: # directions
        try:
            potential_flips = []
            for d in range(1,10):  # distances
                if boardArray[row+y*d][col+x*d] == self._playerTurn: # friendly piece found
                    toflip.extend(potential_flips)
                    break
                elif boardArray[row+y*d][col+x*d] == 3-self._playerTurn: # other player's piece
                    potential_flips.append((col+x*d, row+y*d))
                else: # empty square found, give up on this direction
                    break
        except IndexError: # ran off an edge of the board before finding a friendly piece
            pass
    if toflip: # valid move, causes some of the opponents pieces to be flipped
        for x, y in toflip:
            boardArray[y][x] = self._playerTurn
    else: # invalid move, doesn't flip anything
        raise ValueError("invalid move") # or report an error code some other way
    
    您可能会更改逻辑,在找到友好的条目时直接从
    潜在翻转列表中翻转条目,而不是将它们存储在顶级列表中,然后再全部翻转。然而,您将需要不同的逻辑来检测无效的移动(不会翻转任何东西)

    toflip = []
    for x, y in [(0,1), (1,1), (1,0), (1,-1), (0,-1), (-1,-1), (-1,0), (-1,1)]: # directions
        try:
            potential_flips = []
            for d in range(1,10):  # distances
                if boardArray[row+y*d][col+x*d] == self._playerTurn: # friendly piece found
                    toflip.extend(potential_flips)
                    break
                elif boardArray[row+y*d][col+x*d] == 3-self._playerTurn: # other player's piece
                    potential_flips.append((col+x*d, row+y*d))
                else: # empty square found, give up on this direction
                    break
        except IndexError: # ran off an edge of the board before finding a friendly piece
            pass
    if toflip: # valid move, causes some of the opponents pieces to be flipped
        for x, y in toflip:
            boardArray[y][x] = self._playerTurn
    else: # invalid move, doesn't flip anything
        raise ValueError("invalid move") # or report an error code some other way