I am working on a simple game which involves 2 players. Each player has two counters which they can move individually by selecting them. Player 1 has counters 1 and 1, whilst player 2 has counters 3 and 4. In order to prevent a player from moving one of their opponents counters I have written the following recursive function.
It works fine if no one 'cheats'.
If a player cheats, the function gets them to re-enter a correct counter number. This is carried through to the final return statement as expected.
However instead of returning a correct counter number at this point it appears to take another step and change it to the initial wrong value. It is as if the code has remembered all the values of the variable counter that have been tried during the recursion and cycles through them at the end before returning whatever was the first one.
What am I missing?
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