Nim game

 The nim game requires 2 players, and 20 stones. The players take turns removing 1 to 3 stones at a time, and the person to remove the last stones is the winner. 

There is a method of playing that allows you to win the game if you go second. Whatever the number of stones the opponent takes, take the number of stones so that the stones taken by you and your opponent for that exchange of turns become 4. That way, it will eventually reach 4 stones left on your opponents turn, meaning that no matter how many stones they take, you will win. For example:

20(starting)

1 - 18(2)

2 - 16(2)

1 - 13(3)

2 - 12(1)

1 - 9(3)

2 - 8(1)

1 - 7(1)

2 - 4(3)(checkmate)

stones = 20
print(stones, "stones")
while True:
player1 = int(input("Player1: enter the number of stones to take, 1~3: "))
stones -= player1
print("Remaining stones:", stones)
if stones <= 0:
print("player1 wins")
break
player2 = int(input("Player2: enter the number of stones to take, 1~3: "))
stones -= player2
print("Remaining stones:", stones)
if stones <= 0:
print("player2 wins")
break

Comments