我正在做一个简单的游戏,涉及两个玩家。每个玩家都有两个计数器,通过选择它们可以单独移动。玩家1有计数器1和1,而玩家2有计数器3和4。为了防止玩家移动对手的计数器,我编写了以下递归函数。在
如果没有人“作弊”的话,效果很好。在
如果玩家作弊,函数会让他们重新输入正确的计数器号码。如预期,这将一直持续到最终的收益表。在
然而,此时它并没有返回正确的计数器编号,而是采取了另一个步骤,并将其更改为初始的错误值。这就好像代码记住了在递归过程中尝试过的变量计数器的所有值,并在最后循环这些值,然后返回第一个值。在
我错过了什么?在def get_counter(current_player):
counter = int(input("Select which counter you want to move."))
if counter != 1 and counter != 2 and current_player == 1:
print("This is not your counter")
print("Your counters are 1 or 2")
get_counter(current_player)
elif counter != 3 and counter != 4 and current_player == 2:
print("This is not your counter")
print("Your counters are 3 or 4")
get_counter(current_player)
return counter