In a simplified game of Nim where two players take turns to take either 1 or 2 stones from a pile of n stones, and the player to take the last stone wins, a bit of thought or experimentation shows that where n % 3 == 0, the first player can't win, but for n % 3 == 1 or n % 3 == 2, they can.
I noticed in arriving at the above solution that I was using a recursive thought process, so rather than writing a program that just used the modulo operator, I want to write a recursive version that tells me whether I can win, and also what move to make at each step.
I've made a start with the Python code below, but am stuck on how to print the instruction for how many stones to take at each step. It's possible I've made a big conceptual error in that I haven't considered what player 2 will do at each step - I can't tell if this information is essential of if I can just use the the observations I have already made. Any help completing my program much appreciated.
def last_stone(n):
# Base cases that guarantees player 1 will lose
if n == 0:
print("You can't win.")
return False
# Base cases that guarantees player 1 will win
elif n == 1 or n == 2:
return True
else:
return last_stone(n - 3)
for i in range(10):
print(last_stone(i))