有没有一种方法可以调用self.bestSpot(...),直到返回True。我正在玩井字游戏,我希望computerTurn()调用bestSpot(),直到bestSpots()之一工作为止,此时计算机将轮到他,并且人类可以采取下一步行动。 / p>
def computerTurn(self):
botPawn = self.getCurrentPlayerPawn()
if botPawn == 'O':
enemyPawn = 'X'
elif botPawn == 'X':
enemyPawn = 'O'
while True:
self.bestSpot(enemyPawn, 0, 1, 2)
self.bestSpot(enemyPawn, 3, 4, 5)
self.bestSpot(enemyPawn, 6, 7, 8)
def bestSpot(self, enemyPawn, num0, num1, num2):
if self.placeHolder[num0] == enemyPawn and self.placeHolder[num2] == enemyPawn:
self.playerTurn(str(num1))
elif self.placeHolder[num0] == enemyPawn and self.placeHolder[num1] == enemyPawn:
self.playerTurn(str(num2))
elif self.placeHolder[num1] == enemyPawn and self.placeHolder[num2] == enemyPawn:
self.playerTurn(str(num0))
else:
return(False)
def playerTurn(self, button_name):
for i in self.buttons:
if button_name == i.objectName() and int(button_name) in self.placeHolder:
self.moves = self.moves + 1
player = self.getCurrentPlayerPawn()
self.buttons[int(i.objectName())].setText(player)
self.placeHolder[:] = [player if x == int(i.objectName()) else x for x in self.placeHolder]
self.buttons[int(i.objectName())].setEnabled(False)
self.checkWin(player)
self.changePlayer()