Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This request has been answered previously except the disk.py class is missing. This program plays the Towers of Hanoi game with different numbers of disks

This request has been answered previously except the disk.py class is missing.

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 is 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 containing three Towers objects. 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]

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions

Question

3. Identify cultural universals in nonverbal communication.

Answered: 1 week ago