The Tower of Hanoi (also called The problem of Benares Temple or Tower of Brahma or Lucas' Tower and sometimes pluralized as Towers, or simply pyramid puzzle) is a mathematical game or puzzle consisting of three rods and a number of disks of various diameters, which can slide onto any rod. The puzzle begins with the disks stacked on one rod in order of decreasing size, the smallest at the top, thus approximating a conical shape.
With 3 disks, the puzzle can be solved in 7 moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2^n − 1, where n is the number of disks. You can read more on wikipedia.
The objective of the puzzle is to move the entire stack to the last rod, obeying the following rules:
- Only one disk may be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
- No disk may be placed on top of a disk that is smaller than it.
The application was developed in .NET Framework 6.0 (C#) as a console application. At the begining the user must write the number of disks. Then the towers first state is drawn with ASCII characters. In order to use extended ASCII characters, at the begining of my console application I have changed the encoding Console.OutputEncoding = System.Text.Encoding.UTF8
. Then another menu will show where you can choose playing the game or the game to be played automatically. I used also text files for generating the logs (all of the moves made during the game) at the end of the game. There are 2 possible log files dd_M_yyyy_MovesLog_User.txt and dd_M_yyyy_MovesLog_Computer.txt. The header is generated with this text to ASCII generator website and the used font is called "Calvin S".
Screenshot of the sub-menu, when the user should choose who will play the game (towers with 7 disks):
Screenshot of the user before making the first move (towers with 7 disks):
Screenshot of the log file (with some errors) for towers with 3 disks:
The demo is how the computer play the game for towers with 4 disks: