Wednesday, January 16, 2008

Tower of Hanoi

A classic puzzle taught in many computer science courses is the Tower of Hanoi puzzle. The concept usually being taught is recursion. Because most video game developers also have a background in computer science, it's not surprising to find this puzzle included in some video games. A popular game studio, Bioware, apparently has some developers who really enjoy this one as it's appeared in more than one of their games.

As a programmer I didn't find the puzzle all that difficult, but I was surprised to learn when browsing the game forums on Xbox.com that many gamers out there found this puzzle extremely difficult. While playing Mass Effect I actually thought the puzzle was too easy. I mean, if Bioware really wanted to challenge gamers to figure it out, they should have put more than just four discs in the puzzle. Four discs only requires 15 moves to solve.

The puzzle could easily have been used as a checkpoint for gamers. Make it difficult enough that only a select few can get by it by solving it, then (as they do) provide a way to bypass the puzzle using something readily available elsewhere in the game. In Mass Effect you can use omni-gel, a substance obtained by breaking down items you find, to bypass the puzzle. If only Bioware had made the puzzle have six or seven discs.... Then the puzzle would have taken 63 and 127 moves, respectively, to solve. It could have been used as a way to make gamers pursue the side quests in the game if they didn't have the patience to solve the puzzle.

Number of Discs:4
Move from First to Second
Move from First to Third
Move from Second to Third
Move from First to Second
Move from Third to First
Move from Third to Second
Move from First to Second
Move from First to Third
Move from Second to Third
Move from Second to First
Move from Third to First
Move from Second to Third
Move from First to Second
Move from First to Third
Move from Second to Third
# of moves = 15


Extra:
Here's my C# algorithm to solve it, for those that want to know
public long hanoi(int discs, Pegs from, Pegs to)
{
   if (discs == 1)
   {
      Console.WriteLine(string.Format("Move from {0} to {1}", Peg2String(from), Peg2String(to)));
      return 1;
   }
   Pegs other = (from == Pegs.First) ?
      (to == Pegs.Second) ?
         Pegs.Third : Pegs.Second
      : (from == Pegs.Second) ?
         (to == Pegs.First) ?
            Pegs.Third : Pegs.First
         : (to == Pegs.First) ?
            Pegs.Second : Pegs.First;
   long moves = 0;
   moves += hanoi(discs - 1, from, other);
   moves += hanoi(1, from, to);
   moves += hanoi(discs - 1, other, to);
   return moves;
}

Labels: , ,

0 Comments:

Post a Comment

<< Home