如何在运行另一个再次调用它的函数之前停止运行该函数?

时间:2018-06-09 23:29:12

标签: python python-3.x recursion

我正在用国际象棋ai制作国际象棋程序。这是一些函数逻辑的简化版本。

def make_move(self, board, piece, old_x, old_y, new_x, new_y, ai=False):
    self.board = self.move_piece(self.board, piece, old_x, old_y, new_x, new_y, ai=ai,
                                 check_for_check=True, actual_move=True)
    self.previous_moves.append((piece, (self.index_to_num_x[old_x], self.index_to_num_y[old_y]),
                                (self.index_to_num_x[new_x], self.index_to_num_y[new_y])))
    self.side_to_move = self.opposite_side[self.side_to_move]
    self.attack_y = self.attack_x = None
    if self.check_for_check(self.side_to_move, self.board):
        self.attack_x, self.attack_y = self.check_for_check(self.side_to_move, self.board)
    self.previous_move_start = (old_x, old_y)
    self.previous_move_end = (new_x, new_y)

    self.display_board_to_move()
    if self.check_for_check_mate(self.side_to_move, self.board):
        winner = "White" if self.opposite_side[self.side_to_move] == "W" else "Black"
        self.win_label = Label(text="{} wins by checkmate!".format(winner))
        self.win_label.place(x=750, y=10)
    elif self.check_for_stalemate(self.side_to_move, self.board):
        self.stalemate_label = Label(text="Draw by stalemate!")
        self.stalemate_label.place(x=750, y=10)
    else:
        if self.ai_players[self.side_to_move] and not self.check_for_check_mate(self.side_to_move, self.board):
            self.ai_move()

def ai_move(self):
    self.ai_still_moving = True
    self.display_board_to_move(ai=True)
    self.update()
    piece, old_x, old_y, new_x, new_y = self.ai.make_move(self.board, self.side_to_move, self.previous_moves, 4)
    if self.ai_still_moving:
        self.make_move(self.board, piece, old_x, old_y, new_x, new_y, ai=True)

问题在于,如果我将ai运行到verse本身(因此make_move调用ai然后ai调用make_move然后重复该过程)python返回以下递归错误:RecursionError: maximum recursion depth exceeded while calling a Python object。我该如何解决这个问题?

0 个答案:

没有答案