Introduction

tower hanoi

The Tower of Hanoi puzzle is a classic in studying planning in children and adults (see references).

In this puzzle, participants need to move discs from the left peg to the right peg (see image on the right). There are two constraints;

  1. Larger discs are not allowed to be placed on smaller discs.

  2. Discs shall be moved one at the time.

About this implementation

In this example, there are three pegs with only three discs. These three discs can be moved to the goal in as little as seven steps.

In this experiment, each "trial" is actually just one step. After each move by the participant, the time used so far as well as the total steps is save.

It is possible to save more information, but for simplicity that is not done here.

Run the demo

Data output file

In PsyToolkit, the data output file is simply a textfile. The save line of the PsyToolkit experiment script determines what is being saved in the data output file. Typically, for each experimental trial, you would have exactly one line in your text file, and each number/word on that line gives you the information you need for your data analysis, such as the condition, response speed, and whether an error was made.

Meaning of the columns in the output datafile. You need this information for your data analysis.

Colum Meaning

1

Time spent so far in milliseconds

2

Counter of steps (possible steps)

3

Number attempts so far to put bigger on smaller disc

4

Number of attempts to move disc somewhere where it cannot be (e.g., in the air, and it snaps back)

The last line in the data file will give you all the information you really want, namely the number of steps and the numbers of mistakes made. Alternatively, you can look for the maximum value of column 2, which gives you the number of steps of each participant.

PsyToolkit code

Click to expand the PsyToolkit script code (part of zip file below)
options
  mouse on

bitmaps
  titlescreen
  instructions
  gradient
  peg
  disc1
  disc2
  disc3
  welldone

## possible y positions; 84,120,158

task tower
  show bitmap gradient #1
  show rectangle 0 192 600 30    100 149 237 #2
  show bitmap peg -200 100 #3
  show bitmap peg    0 100 #4
  show bitmap peg  200 100 #5
  #################################################
  show bitmap disc1 &disc1_x &disc1_y # stimulus #6
  show bitmap disc2 &disc2_x &disc2_y # stimulus #7
  show bitmap disc3 &disc3_x &disc3_y # stimulus #8
  #
  # now determine which disc can be moved (only the top ones)
  #
  set &&topdiscs clear
  set $peg1size &&peg1 size
  set $peg2size &&peg2 size
  set $peg3size &&peg3 size
  if $peg1size > 0
     set $tmp &&peg1 use last
     set &&topdiscs append $tmp
  fi
  if $peg2size > 0
     set $tmp &&peg2 use last
     set &&topdiscs append $tmp
  fi
  if $peg3size > 0
     set $tmp &&peg3 use last
     set &&topdiscs append $tmp
  fi
  #
  #
  #
  drag option from &&topdiscs
  drag option snap center bottom no_overlap 6 7 8
  drag option to 3 4 5
  drag 999999
  #
  # now process data
  set $wrong 0
  if DRAG_CHANGED == 1 && STATUS == 1
    ## make copies of the 3 pegs in case the dragging was wrong
    set &&peg1copy &&peg1
    set &&peg2copy &&peg2
    set &&peg3copy &&peg3
    ## remove the DRAGGED value from whichever peg it was on
    set &&peg1 remove value DRAGGED
    set &&peg2 remove value DRAGGED
    set &&peg3 remove value DRAGGED
    ## now check on which peg the disc is
    set $newX getx DRAGGED ## the x position of the dragged stimulus
    set $newY gety DRAGGED ## the x position of the dragged stimulus
    set &stepcounter increase
    if $newX == -200
      set &&peg1 append DRAGGED
      set &&peg1 reverse
      if &&peg1[1] > &&peg1[2]
        set $wrong 1
      fi
      set &&peg1 reverse
    fi
    if $newX == 0
      set &&peg2 append DRAGGED
      set &&peg2 reverse
      if &&peg2[1] > &&peg2[2]
        set $wrong 1
      fi
      set &&peg2 reverse
    fi
    if $newX == 200
      set &&peg3 append DRAGGED
      set &&peg3 reverse
      if &&peg3[1] > &&peg3[2]
        set $wrong 1
      fi
      set &&peg3 reverse
    fi
    if $wrong == 1
      set &mistakes increase
      delay 200
      show text "Wrong, do not put disc on smaller one."
      delay 500
      relocate DRAGGED DRAG_OLD_X DRAG_OLD_Y
      ## reinstate pegs
      set &&peg1 &&peg1copy
      set &&peg2 &&peg2copy
      set &&peg3 &&peg3copy
      set &stepcounter decrease
    fi
    show text &stepcounter 300 250
    if $wrong == 0
      set &moves increase    
      ##
      ## set the new positions
      ##
      if DRAGGED == 6
	 set &disc1_x $newX
	 set &disc1_y $newY
      fi
      if DRAGGED == 7
	 set &disc2_x $newX
	 set &disc2_y $newY
      fi
      if DRAGGED == 8
	 set &disc3_x $newX
	 set &disc3_y $newY
      fi
    fi
  fi
  if DRAG_CHANGED == 1 && STATUS == 2
    set &outplaced increase
  fi
  ## now check if the pegs really changed
  delay 500
  timestamp the_time_now
  set &time_so_far timestamp-diff block_start_time the_time_now
  save &time_so_far &stepcounter &mistakes &outplaced
  set $peg3size &&peg3 size
  if $peg3size == 3
    if &&peg3[1] == 8 and &&peg3[2] == 7 and &&peg3[3] == 6
      show bitmap welldone 0 -100
      delay 1000
      end tasklist
    fi
  fi

message titlescreen mouse

message instructions mouse
  
block test
  timestamp block_start_time
  set &stepcounter 0
  set &disc1_y 82
  set &disc2_y 118
  set &disc3_y 157
  set &disc1_x -200
  set &disc2_x -200
  set &disc3_x -200
  set &&peg1 8 7 6
  tasklist
    tower 100  ## each trial is one step.
  end

Download

If you have a PsyToolkit account, you can upload the zipfile directly to your PsyToolkit account. Watch a video on how to do that. If you want to upload the zipfile into your PsyToolkit account, make sure the file is not automatically uncompressed (some browsers, especially Mac Safari, by default uncompress zip files). Read here how to easily deal with this.

Further reading

  • Byrnes, M.M. & Spitz, H.H. (1979). Developmental progression of performance on the Tower of Hanoi problem. Bulletin of the Psychonomic Society, 14 (5), 379-381. (Publisher link)

  • Davis, H.P., Klebe, K.J. (2001). A longitudinal study of the performance of the elderly and the young on the Tower of Hanoi Puzzle and Rey Recall. Brain and Cognition, 46, 95-99. (Publisher link)

  • Schiff, R. & Vakil, E. (2015). Age differences in cognitive skill learning, retention and transfer: The case of the Tower of Hanoi Puzzle. Learning and Individual Differences, 39, 164-171. (Publisher link)