Dr. Nim

Dr. NIM is a marble powered plastic computer game from the 1960s. It is based of the mathematical puzzle, Nim, which is played like this:

  1. 12 sticks (marbles, or any other object) lay on the ground.
  2. Players take turns, taking 1, 2, or 3 sticks when it is their turn.
  3. The person who takes the last stick wins.

In the marble computer, the human player plays against a piece of plastic! Somehow, Dr. NIM always wins. He uses some clever mathematical tricks to guarantee a win, so as long as the human plays first. How is this possible? Try to figure it out yourself, and if you can’t, watch the YouTube video below.

Your job is to implement a Python version of Dr. NIM. Here is a sample interaction:

Welcome to Dr. NIM!
In this game, there are 12 sticks sitting on the ground.
Your goal is to pick up the last stick. You will play against
Dr. NIM, an expert at this game.

When it is your turn, you may pick up 1, 2, or 3 sticks.

It is your turn. There are 12 sticks on the ground.
How many would you like to pick up? 2

There are 10 sticks on the ground.
Dr. NIM picked up 2 sticks.

It is your turn. There are 8 sticks on the ground.
How many would you like to pick up? 3

There are 5 sticks on the ground.
Dr. NIM picked up 1 stick.

It is your turn. There are 4 sticks on the ground.
How many would you like to pick up? 3

There is 1 stick on the ground.
Dr. NIM picked up 1 stick.

There are no more sticks on the ground.
Dr. NIM picked up the last stick and has won. Try again later.

Feel free to change up the wording I used here, so as long as it plays the game I described, and always wins.

If you would like, you may play my version of the program by downloading it here and executing it as a normal Python program.

Extending your Program

Finished? Here are some things you can add:

  • Let the human specify the number of sticks or marbles.
  • Let the human specify who plays first.
  • Read the original manual and add features from the original game.
  • Come up with your own features to add!