更轻松地搜索2D列表的所有方向?

时间:2015-11-20 22:08:23

标签: python python-3.x

我正试图为我写的奥赛罗/黑白棋游戏实现一个功能,我认为这是非常低效的。

所以基本上,有一个游戏板,其中包含用户设置的行数和列数(第1行从顶部开始,第1列从左侧开始)。

 . . . . . . . . . .
 . . . . . . . . . .
 . . . . . . . . . .
 . . . . . . . . . .
 . . . W W W . . . .
 . . . . B B . . . .
 . . . . B B B W . .
 . . . . . . . B . .
 . . . . . . . . . .
 . . . . . . . . . .

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

  1. 有人能以更有效的方式告诉我吗?
  2. 什么是存储我们必须翻转的部件位置的好方法?
  3. 希望这篇文章有道理!如果你了解奥赛罗的游戏规则,就会更容易理解。

    事先谢谢你! - Python新手

1 个答案:

答案 0 :(得分:0)

不是为你需要搜索的8个方向写出8次循环,而是建议在方向向量列表(可能是2元组)上使用循环来指定增加的数量。行和列坐标。您还可以在for上使用range循环,而不是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

当您找到友好的片段时,您可能会更改逻辑以直接从potential_flips列表中翻转项目,而不是将它们存储在顶级列表中并稍后翻转它们。但是,您需要使用不同的逻辑来检测无效的移动(不要翻转任何内容)。

相关问题