压缩多个while循环

时间:2018-10-18 13:04:36

标签: python while-loop chess

我正在进行(有点雄心勃勃的)项目,编写使用Mac终端机玩的国际象棋游戏,使用代数符号输入来实现动作。

我将董事会写成一个列表列表,每个嵌套列表都用作董事会上的等级(水平线):

board = [['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
['bP', 'bP', 'bP', 'bP', 'bP', 'bP', 'bP', 'bP'],
['e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e '],
['e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e '],
['e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e '],
['e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e ', 'e '],
['wP', 'wP', 'wP', 'wP', 'wP', 'wP', 'wP', 'wP'],
['wR', 'wN', 'wB', 'wQ', 'wK', 'wB', 'wN', 'wR']
]

然后,我编写了一个(相当长的)过程,以仅根据用户希望进行的期望动作给出的输入来检查可用于某件作品的合法动作。

我正在尝试减少代码的重复性,但是在生成主教,菜鸟和女王的可能举动列表时遇到了麻烦。例如,对于主教,我首先使用给定的输入来定位可以移动到该特定正方形并存储其位置的主教。然后,我使用while循环沿每个对角线的方向生成主教可以移动到的可能正方形的列表。最终看起来像:

 elif piece == 'B':
      s,d = square[0] + square[1], square[0] - square[1]
      squares = [(r,f) for r in range(-8,0) for f in range(len(board)) if r-f == d or r+f==s]
      pos_bishop=[]
      for i in squares:
          pos = board[i[0]][i[1]]
          if pos == mover:
              pos_bishop.append(i[0])
              pos_bishop.append(i[1])
              break
      if len(pos_bishop) == 0:
          return False
      squares2 = []
      j,k = pos_bishop[0], pos_bishop[1]
      while j>-8 and k>0 and board[j-1][k-1] == 'e ':
          squares2.append((j-1,k-1))
          j-=1
          k-=1
      j,k = pos_bishop[0], pos_bishop[1]
      while j>-8 and k<8 and board[j-1][k+1] == 'e ':
          squares2.append((j-1,k+1))
          j-=1
          k+=1
      j,k = pos_bishop[0], pos_bishop[1]
      while j<-1 and k>0 and board[j+1][k-1] == 'e ':
          squares2.append((j+1,k-1))
          j+=1
          k-=1
      j,k = pos_bishop[0], pos_bishop[1]
      while j<-1 and k<8 and board[j+1][k+1] == 'e ':
          squares2.append((j+1,k+1))
          j+=1
          k+=1
      if square in squares2:
          return True

如您所见,有四个“ while”循环,在每个循环的开始,我必须重新定义位置变量。这似乎很笨拙,但是我很难想到以更有效的方式产生移动的另一种方法。 (顺便说一句,我还没在研究是否是招式,只是看正方形是否为空。)

我希望朝着正确的方向前进,而不仅仅是一个明确的解决方案;但任何建议都将受到欢迎。谢谢!

1 个答案:

答案 0 :(得分:0)

  

问题:思考以更有效的方式生成举动的另一种方式。

如果使用function将其概括,则也可以将其用于其他pices
添加其他四个方向以及“哪些动作对给定的香料有效” 的知识,此function可用于所有图片

例如:

def compute_moves(pb0, pb1):
    m = []

    def get_moves(j, k, direction):
        if direction == 'sw':
            while j > -8 and k > 0 and board[j - 1][k - 1] == 'e ':
                m.append((j - 1, k - 1))
                j -= 1
                k -= 1
        elif direction == 'nw':
            while j > -8 and k < 8 and board[j - 1][k + 1] == 'e ':
                m.append((j - 1, k + 1))
                j -= 1
                k += 1
        elif direction == 'se':
            while j < -1 and k > 0 and board[j + 1][k - 1] == 'e ':
                m.append((j + 1, k - 1))
                j += 1
                k -= 1
        elif direction == 'ne':
            while j < -1 and k < 8 and board[j + 1][k + 1] == 'e ':
                m.append((j + 1, k + 1))
                j += 1
                k += 1

    for direction in ['sw', 'nw', 'se', 'ne']:
        get_moves(pb0, pb1, direction)

    return m       


if __name__ in "__main__":
    moves = compute_moves(pos_bishop[0], pos_bishop[1])