Tuesday, December 8, 2015

Tower of Hanoi Flash Game

Tower of Hanoi Flash Game
Code this classic puzzle game, "Tower of Hanoi" with Flash AS3 in 2011.

The Tower of Hanoi or Towers of Hanoi is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod.

Game rules

The game start with 3 rods, A,B and C. Initially, all disks are stack on rod A, and you have to move all the disk in ascending order on rod C to win.

tower of hanoi
There are few criteria when moving the disk around.
  • Only 1 disk can be move at a time.
  • Only the top most disk can be move.
  • Disk can only be placed on top of a larger disk.

tower of hanoi

Gameplay Programming

To code this game, we can use 3 arrays to represent the 3 rods, which contain list of values that represent all the disks. The bigger the disk, the larger the number (or the other way round, depend on how you do the condition statement later).

Then, use array PUSH and POP function to "move the disk around". In this case, the PUSH and POP function work exactly like what we want, removing and adding items from the top of the array/stack.

Last, use a condition statement to check to see if a disk can be move on to another rod base on target rod top most disk size. To check the top most disk size, just POP it. tower of hanoi

Recursive Solution

The puzzle can be solve automatically by using a recursive function too, which you can read more in this page, "Writing a Towers of Hanoi program".

tower of hanoi

Play The Game

You can play "Tower of Hanoi" in my deviantart. I have include the recursive puzzle solving function as well.

tower of hanoi flash game


Tower of Hanoi Wikipedia
Writing a Towers of Hanoi program
Tower of Hanoi Game Deviantart

No comments: