Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Towers of Hanoi in Python with Classes. toh_main.py tower_of_hanoi.py tower.py disk.py This program plays the Towers of Hanoi game with different numbers of disks ranging

Towers of Hanoi in Python with Classes.

toh_main.py

tower_of_hanoi.py

tower.py

disk.py

This program plays the Towers of Hanoi game with different numbers of disks ranging from 3 to 24. Each time the game is played, the number of moves are recorded. The mainline code is in the toh_main.py file. The mainline code plays each of the Towers of Hanoi games with numbers of disks ranging from 3 to 24. This program must create three classes: Tower, Disk and TowersOfHanoi. The class definitions are placed in the files, towers_of_hanoi.py, tower.py, and disk.py, respectively. The mainline code is in the toh_main.py file. The mainline code has a main function which uses a loop to play each of the 22 games with number of disks ranging from 3 to 24. A Disk object must have a size, which is displayed when a Tower containing the Disk is displayed. Use a __str__ method to display the size of the Disk. The Disk is an immutable class no setters. You simulate a Tower by making it a list, in fact, you must inherit from the list class. So, in OOP terms, we say that a Tower is a list. In fact, the Tower is a list of Disk objects. This implies that there is an aggregation association between Towers and Disks. The Disk objects are added to and removed from the three Tower objects as the game is played. A Tower object must have a name, either A, B, or C, which is displayed when the Tower is displayed. A Tower must have a method that can be called to move a Disk from itself to a destination Tower. Use a __str__ method to display the Tower. The display must follow the required output given below. The Tower only adds Disks to the end of itself and takes them off at the same end, simulating a stack. So, to initialize the beginning Tower, you need to add the Disks in order from largest to smallest. The Disk sizes are numbered 1, 2, 3, ., num_of_disks. The TowersOfHanoi object is the one which plays one round of the game. This object must have a list of three Towers. The Towers only exist within this TowersOfHanoi class object. This implies that there is a composition association between the TowersOfHanoi and Tower objects. The constructor for the class must accept the number of Disks being used for that round. It must instantiate the three Towers and add them to a Tower list. It must also store the number of moves. This TowersOfHanoi class must have a recursive method to move the Disks from Tower to Tower. There must be a helper method to start the game which calls the recursive method the first time. There must be a display method to display the three Towers after every move. When playing the game with 5 disks, you must display the towers to see the movement between them. Do this only when playing with 5 disks. You may code up these classes and the main function, as you wish, but you must follow these specifications. You must also display the Towers, when the number of disks is 5, exactly as shown below in the Required Output. For help with coding the classes, please refer to the Appendix. There you can find the UML class diagrams for a version of this program that follows the specifications.

Required Output Towers of Hanoi

===============

Moving 3 completed in 7 moves!

Moving 4 completed in 15 moves!

Towers:

Tower A: [5, 4, 3, 2, 1]

Tower B: []

Tower C: []

Towers:

Tower A: [5, 4, 3, 2]

Tower B: [1]

Tower C: []

Towers:

Tower A: [5, 4, 3]

Tower B: [1]

Tower C: [2]

Towers:

Tower A: [5, 4, 3]

Tower B: []

Tower C: [2, 1]

Towers:

Tower A: [5, 4]

Tower B: [3]

Tower C: [2, 1]

Towers:

Tower A: [5, 4, 1]

Tower B: [3]

Tower C: [2]

Towers:

Tower A: [5, 4, 1]

Tower B: [3, 2]

Tower C: []

Towers:

Tower A: [5, 4]

Tower B: [3, 2, 1]

Tower C: []

Towers:

Tower A: [5]

Tower B: [3, 2, 1]

Tower C: [4]

Towers:

Tower A: [5]

Tower B: [3, 2]

Tower C: [4, 1]

Towers:

Tower A: [5, 2]

Tower B: [3]

Tower C: [4, 1]

Towers:

Tower A: [5, 2, 1]

Tower B: [3]

Tower C: [4]

Towers:

Tower A: [5, 2, 1]

Tower B: []

Tower C: [4, 3]

Towers:

Tower A: [5, 2]

Tower B: [1]

Tower C: [4, 3]

Towers:

Tower A: [5]

Tower B: [1]

Tower C: [4, 3, 2]

Towers:

Tower A: [5]

Tower B: []

Tower C: [4, 3, 2, 1]

Towers:

Tower A: []

Tower B: [5]

Tower C: [4, 3, 2, 1]

Towers:

Tower A: [1]

Tower B: [5]

Tower C: [4, 3, 2]

Towers:

Tower A: [1]

Tower B: [5, 2]

Tower C: [4, 3]

Towers:

Tower A: []

Tower B: [5, 2, 1]

Tower C: [4, 3]

Towers: Tower A: [3]

Tower B: [5, 2, 1]

Tower C: [4]

Towers:

Tower A: [3]

Tower B: [5, 2]

Tower C: [4, 1]

Towers:

Tower A: [3, 2]

Tower B: [5]

Tower C: [4, 1]

Towers:

Tower A: [3, 2, 1]

Tower B: [5]

Tower C: [4]

Towers:

Tower A: [3, 2, 1]

Tower B: [5, 4]

Tower C: []

Towers:

Tower A: [3, 2]

Tower B: [5, 4, 1]

Tower C: []

Towers:

Tower A: [3]

Tower B: [5, 4, 1]

Tower C: [2]

Towers:

Tower A: [3]

Tower B: [5, 4]

Tower C: [2, 1]

Towers:

Tower A: []

Tower B: [5, 4, 3]

Tower C: [2, 1]

Towers:

Tower A: [1]

Tower B: [5, 4, 3]

Tower C: [2]

Towers:

Tower A: [1]

Tower B: [5, 4, 3, 2]

Tower C: []

Towers:

Tower A: []

Tower B: [5, 4, 3, 2, 1]

Tower C: []

Moving 5 completed in 31 moves!

Moving 6 completed in 63 moves!

Moving 22 completed in 4194303 moves!

Moving 23 completed in 5388607 moves! Moving 24 completed in 16777215 moves!

Appendix:

Disk

-size: int

+__init__(size: int)

+get_size(): string or int

+__str__(): string

Disks are initialized to their size in the constructor and the __str__ method returns the size as a str.

Tower(list)

-name: string

+__init__(name: string)

+get_name(): string

+move(dest_tower: Tower)

+__str__(): string

Towers are initialized with their name in the constructor. The move method pops the top disk from itself and appends it to the destination Tower. The __str__ method returns a string that is displayed as follows:

For empty Towers:

Tower A: [ ]

For Towers with Disks:

Tower A: [5, 4, 1]

You must follow the output given. The length of the Tower is the number of disks it has on it. This can be obtained with len(self). The individual Disks can be accessed with self[index], where index is the position on the Tower, a list.

TowersOfHanoi

-num_of_disks: int

-num_of_moves: int

-towers: list

+__init__(num_disks: int)

+get_num_of_moves(): int

+move_disks(num_disks_to_move: int, beg_tow: Tower, aux_tow: Tower, end_tow: Tower)

+play()

+display_towers()

The move_disks method is recursive. The play method is the recursion helper method that calls move_disks.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 9th International Conference Dasfaa 2004 Jeju Island Korea March 2004 Proceedings Lncs 2973

Authors: YoonJoon Lee ,Jianzhong Li ,Kyu-Young Whang

2004th Edition

3540210474, 978-3540210474

More Books

Students also viewed these Databases questions