带字典的乐曲乐章

2024-06-25 22:58:54 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我现在有一个字典,它有多个棋子作为值,位置作为键。我现在可以根据国际象棋规则移动每一个棋子,但我正在努力确保棋子不能相互跳过

这是我现在的一段代码,允许女王移动

QueenRow = int(self.UI_entry2.get()) #mighht not be needed
QueenColumn = int(self.UI_entry3.get())
QueenMoves = self.QueenMoves(QueenRow,QueenColumn)
QueenTuple.append(QueenRow) # Add to list to tuple
QueenTuple.append(QueenColumn) # Add to list to tuple
QueenRowColumn = tuple(QueenTuple) # Tuple the numbers
for piece,position in list(self.pieces.items()): # Get the Key from the values
       if position == QueenRowColumn: # if the tupled numbers equals the key
              print("Piece:",piece,"|","Old Position:",position)   # Print the key and position
              MoveRow = int(self.UI_entry4.get()) #then get the row and column they can move to
              MoveColumn = int(self.UI_entry5.get())
              QueenTuple2.append(MoveRow)
              QueenTuple2.append(MoveColumn)
              TheComparison = tuple(QueenTuple2)
              for TheQueenMoves in QueenMoves: #loop through all the kings moves
                    for a,b in list(self.pieces.items()):    
                      # Move Piece!
                      if TheComparison == TheQueenMoves and TheComparison[0] >= 0 and TheComparison[1] <= 7 and TheComparison[1] >= 0 and TheComparison[0] <= 7:
                      self.placepiece(piece, row = MoveRow, column = MoveColumn)
                      print("Piece:",piece,"|","Current Position:",TheComparison)                                                   print("Possible Moves in current position:",QueenMoves)
                      break
                      del QueenTuple[:]
                      del QueenTuple2[:]

(我没有添加完整的代码,因为它太长、太多,无法通读)

这个代码允许皇后移动,但也允许棋子跳过其他棋子。我有办法阻止这一切发生吗


Tags: andthetoselfuigetpositionlist