【问题标题】:Easier way to search all directions of a 2D list?搜索二维列表所有方向的更简单方法?
【发布时间】:2015-11-20 22:08:22
【问题描述】:

我正在尝试为我正在编写的黑白棋/黑白棋游戏实现一个功能,我认为它的效率非常低。

所以基本上,有一个游戏板具有用户设置的行数和列数(第 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. 什么是存储我们必须翻转的棋子位置的好方法?

希望这篇文章有意义!如果你知道奥赛罗游戏的规则会更容易理解。

先谢谢了! - Python新手

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-13
      • 1970-01-01
      • 2014-05-17
      • 2015-05-07
      相关资源
      最近更新 更多