The Candy Computer

The activity is not related to any subject other than computer science. It requires some preparation, but it has always proven to be engaging while also leading to a deep discussion about what it really means for a computer to be intelligent and to learn.

If we want to explain more background, it makes sense to introduce the intelligent paper. The Candy Computer answers the question we ask students about the “intelligent” paper: would the paper be intelligent if the instructions weren’t written by humans, but if it learned to play the game by itself?

The activity originates from CS4FN: The Sweet Learning Computer. The CS4FN project was started by Paul Curzon and Peter McOwan from Queen Mary University of London.

Rules of the game

Introduce the rules of the game Hexapawn using a board on the table or a school board.

  1. Set up the initial layout as shown in the image.

  2. The pieces move similarly to pawns in chess: with each move, a player can:

    • move one of their pieces forward by one square, but only if the square ahead is empty, or
    • capture the opponent’s piece diagonally.

    The image shows the possible moves the player can make after the opponent moves the left circle forward: the middle and right crosses can move forward, and additionally, the middle cross can capture the circle diagonally in front of it. The left cross cannot move.

  3. The game ends when one of the players succeeds in:

    • placing one of their pieces in the last row, or
    • capturing all of the opponent’s pieces, or
    • preventing the opponent from making any moves.

Explain the game by playing a few rounds with the students.

Preparing game boards and computers

  1. Divide the students into groups of about six.

  2. Assign a table to each group where they will play. On the table, draw the game board using masking tape, or prepare it in another way. Each group receives their pieces.

  3. Each group receives figures with all possible positions in when it is computer’s turn to make a move. Arrange them neatly on the table.

    • Explain that the game will always start with the human player, who will play with circles.
    • Additionally, agree that the human player will never start by moving the left circle (the right one from the computer’s perspective). Explain that this is only to reduce the number of possible positions. If they wish to move the left circle, they can move the right one instead, and the game can proceed the same way, just mirrored.
  4. Place a plastic cup next to each paper with an image of a position. The possible computer’s moves are marked with different colors. Every cup should contain candies of the same colors as those used on the corresponding board. For the board with yellow and red moves, the cup must contain one yellow and one red candy. Instead of candies, you can use pebbles, beads, or other objects, though in that case, the computer will be less tasty.

Instructions for the computer

  1. Explain to the students how they will “control” the computer. The student representing the computer will need to do the following each time it’s the computer’s turn:

    • find the picture that corresponds to the current position on the board;
    • randomly draw a candy from the cup next to that picture;
    • look at the color of the candy and make the move that corresponds to that color;
    • put the cup back and place the drawn candy next to the cup, where it will stay until the end of the game.

    If it happens that the cup corresponding to the current position has no more candies, the computer surrenders and the game ends.

    If there’s only one candy in a cup, we’ll always draw that candy and place it next to the cup.

  2. It will be obvious to the students that the computer initially knows nothing and makes random moves. How will we teach it? By punishing it for incorrect moves.

    Explain that at the end of each game, we will do the following:

    • If the computer loses, we punish it by eating the candy that corresponds to the last move it made. (If we’re using marbles instead of candies, we won’t eat them, but just remove them instead.) All other candies next to the cups that correspond to the earlier moves should be placed back into the cups. For example, if the computer made three moves, we’ll only eat the third candy and return the first two.
    • If the computer wins, we place all the candies back into the cups.

If you’re worried that the children won’t understand how the computer works, play a few games with them.

The game

The groups begin playing the game. It’s important not to interrupt the game, even when the position is clearly lost: the computer will initially make foolish mistakes and lose games even in situations in which it would win with a single correct move.

To make it more interesting, have the students keep the score. It will turn out that the computer will start by losing game after game, but then begin playing better and better.

Continue repeating the game until it becomes clear that the computer has become unbeatable. This will happen after less than a dozen games.

Discussion after the game

Did we give the computer instructions on how to play the game in order to win, or did it learn this on its own? It learned on its own: through trial and error, it discovered which moves it should avoid. To prevent making these wrong moves again, it “remembers” by losing a candy it could have drawn.

Although our computer is made of paper, cups, and candies, we could also write a program to do the same thing, but instead of candies, it would have ones that would turn into zeros upon defeat. A real computer wouldn’t need a person to look for papers or draw candies for it. However, the program would work the same way.

In the case of Intelligent paper, we might have concluded that we would consider a computer intelligent when it can learn to play the game on its own. Now we have such a computer. Is it intelligent?

Children might object: this computer isn’t intelligent either, since it still only operates based on completely “mechanical” pre-determined rules invented by someone else. That’s correct: if we think like that, computers will never be truly intelligent. But they can continue to get better at pretending.

This perspective is entirely valid. That’s why we call it artificial (and not “real”) intelligence.

  • Subject: computer science
  • Duration: 1-2 hours
  • Age: Primary school
  • AI topic:
  • Author of idea: CS4FN
  • Materials
  • Preparation for the lesson
    • masking tape for drawing game boards on tables (if the boards are not made otherwise)
    • three game pieces in the shape of crosses and circles for each table; you can print the provided PDF and cut them out
    • optional: pieces for displaying the game on the board. You can also print and cut these out, and add magnets if desired
    • printed position boards
    • 24 plastic cups
    • a lot of candies or other items (e.g. pebbles) in different colors. For each table, 5 red candies and about 13 yellow, purple, brown, and blue. Smartie or M&M candies are examples based on colors. Also, keep in mind that some candies will be eaten during each game. Have extra candies on hand for disappointed children.