Starting from:

$30

SE127 Lab 3-Tower of Hanoi  Solved

                                                 

In Lab 3, you are required to design and implement a game: Tower of Hanoi.

0    The puzzle    
The Tower of Hanoi is a famous mathematical puzzle. It consists of three rods and a number of disks of different sizes. Initially, all disks are in the first rod in ascending order of size (i.e., the smallest is at the top). The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

1.    Only one disk can be moved at a time.

2.    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.

3.    No larger disk may be placed on top of a smaller disk.

The classical way to solve the puzzle is recursion. The algorithm could be described as the following pseudocode:

 

1    Lab Description   
Here are some concepts that you need to know first.

 Canvas: The UI of the game.

Rod: There are 3 rods (numbered 1, 2  3) in the canvas. Disks are pushed and popped among them. The source rod is 1 and the target rod is 2. Rod 3 acts as a temporary rod. Disk: Each disk is of different sizes. In the beginning, they are all pushed into Rod 1 in ascending order of size.

You should implement the Hanoi game program with the following functionality:

1.     Intial Phase: When the program starts, it firstly asks the player for the number of disks. The input should a integer in [1, 5].  If input is Q , quit the game. Other invalid input should be ignored.

2.     Normal Mode: Then, print the canvas in the console, and the game starts. The program continuously asks for commands until the player wins. The canvas will be printed after each move. Command format: from to . Invalid input should be ignored.

3.     Auto Play Mode: If the game receives command 0 0 . Auto-play mode will be activated. The game will automatically continue without player’s commands. The canvas, as well as the auto-generated command, will be printed after each move.

Requirements                                                                                                       
    You have to implement the game using Object-Oriented programming. Hope the following materials will help you if you're not familiar with OOP.

Object Oriented Design

Object Oriented Programming in C++

  Containers in STL( std::vector, std::stack, std::list, std::queue ) are NOT allowed to use. Please implement your own Stack or Queue if needed.

2    Guidance      
2.1     Drawing the Canvas       
 We have offered you a file, termio.h . It might be helpful for you to deal with the canvas printing work.

CANVAS_WIDTH and CANVAS_HEIGHT : the width and height of the canvas.

 Clear() helps you clear the screen. Using it before Draw() will improve the user experience.

Draw() helps you draw the buffer to console.

ResetBuffer() fills the buffer with spaces.

2.2     Auto Play Mode    
During the game, players may enter auto-play mode in any state. After entering into auto-play mode, you need to do two things.

 Restore to the beginning state (simple idea: use a stack to log the player's commands and undo these commands when entering auto-play mode). Auto-play the game from the beginning state.

2.3     Output format       
  For the sake of simplicity, the disk is represented by * .

   The number of * that you should draw is calculated by 2* disk_size + 1 .  For example, if there are five disks in rod1, then the number of *  is 3, 5, 7, 9, 11 . The middle * is vertical aligned with the rod .

 You should draw disks from the bottom of the rod. The two examples below show the canvas when there are 5, 3 disks at the beginning.

 The position of the rods should remain unchanged regardless of number of disks. We have defined  CANVAS_WIDTH and CANVAS_HEIGHT for you, you should not change the value.

 Upon wining the game (either from normal mode or auto-play mode),  first print  "Congratulations! You win!", and then start a new round of game.

 std::cout << "Congratulations! You win!" << std::endl; std::cout << "How many disks do you want? (1 ~ 5)" << std::endl; To improve user exprience, you don't have to clear screen under auto-play mode.

About other output formats, please see the example below.

3    Example       
3.1     Example1      
3.1.1    Initial Phase     
 

 

After telling the program about the number of disks, we come to the normal mode.

3.1.2    Normal Mode  
Then we give an ilegal command  1 3 (since it violate the rule: No larger disk may be placed on top of a smaller disk.),  it will be ignored, so the previous canvas is printed.

3.1.3    Auto Play Mode          
 

 

3.2     Example2      
Use normal-mode to win the game yourself.

 

 

 

4    Submission   
 Compress your source code into a 7z file, and rename it to lab3-{your studentid}.7z Upload it to canvas.

 

Feel free to ask teaching assistants if you have any questions.

More products