Artificial Intelligence Game

Artificial intelligence (AI) is the ability of a computer program or a machine to think and learn. It is also a field of study which tries to make computers "smart".  What we will try to do in this experiment is to make a game that works using artificial intelligence. 

The game Nim, is an ancient game with many different variants. For your own home made artificial intelligence, we will have to play the simplest version of it. The idea of the game is as follows: You line up 10 pencils, (or, coins, or acorns or whatever you have handy) and on each players turn you may take one, two or three of the items at once. Who ever takes the last item looses. 

The goal is to build an artificial intelligence that will always win the game provided the AI is the first player. 

What you need:

  • 9 cups
  • 26 small pieces of paper
  • 10 pens
  • A willing family member who will help you train the AI.

Take a look at the detailed instructions here, including a video tutorial.

Step 1: Prepare the AI

- Label all the 9 cups with numbers between 2 and 10

  • Create 8 times 3 pieces of paper each labelled with 1,2,3 and two additional pieces of paper labelled with 1,2 
  • Place in each cup a piece labelled, one, a piece, labelled 2 and a piece lablled 3. Apart from the  cup labelled 2, which will only take two pieces of paper labelled 1 and 2. 
  • You are now ready to train your AI

Step 2: Train you AI

In order to train the AI you need to play the game many times, until there is exactly one piece of paper left in each cup. Ideally find yourself someone in your family to play with. 

You take turns as the AI. When it is your turn look how many items (e.g. pens) are left and then take one piece of paper out of the cup that corresponds to the number of pens left. E.g. If you have 7 pens left and it is the AIs turn you take a piece of paper from cup 7. The number on the pice of paper is the number of pens you will then take. Place the piece of paper in front of the cup. Once a winner is determined it depends on the outcome what happens next.

  •  If you, as the AI won, you place all the pieces of paper back into the cups. 
  •  If the other person won, eliminate the last piece of paper you placed in front of a cup and carry on playing the next round. 
  • However, if a cup only has one piece of paper in it, put it back and discard the piece of paper from the previous action instead. Cups should always have at least one piece of paper. 

After playing enough times, each cup will end with only one piece of paper: the perfect move in that situation.  Now your 9 cups will win against any player, as long as you make sure you get to go first!

Want to learn more about AI, why not check out the virtual interactive exhibition on machine learning and artificial intelligence at

By Toni