Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(PYTHON 3) Code up a medium-sized, multi-function program, with the following goals: For this program: 1) Please follow the background material and design specifications for

(PYTHON 3) Code up a medium-sized, multi-function program, with the following goals:

For this program:

1) Please follow the background material and design specifications for this Lab.

2) Code up the traditional Towers of Hanoi puzzle game using recursion.

3) Create three classes, as specified in the System Design:

One to represent a Disk

One to represent a Tower

One to play the game, TowersOfHanoi

4) Use a driver module containing the main function.

5) Each of the three classes has one or more special OOP properties, as noted in the design specifications. One class is immutable, one uses inheritance, one uses aggregation and one uses composition.

You are to code a program to solve the Towers of Hanoi puzzle. The game driver program must call a recursive function that does all the work

The following is a slight revision of the hanoi function found in the article. The one in the article is explained step-by-step. Please modify the following code slightly for your move_disks (toh -> move_disks) method of the TowersOfHanoi class for your program.

def toh(n, source, helper, target):

if n == 1:

# The following code is performed in the # Tower move method in your Tower class

target.append(source.pop())

# Display the towers here # Increment the count of moves at this location

else: # move tower of size n-1 to helper: toh(n-1, source, target, helper) # move largest disk from source peg to target peg toh(1, source, helper, target) # move tower of size n-1 from helper to target toh(n-1, helper, source, target)

# These three lines of code are not used in your program # Your program must use Tower objects, not vanilla lists source = [4,3,2,1] target = [] helper = []

# The following line of code is placed in the # TowersOfHanoi recursion helper method: play() # In your code, each of these arguments is a # instance method of the TowersOfHanoi class

toh(len(source), source, helper, target) # You are responsible for doing this differently # in the display_towers() method print("Source: {} Helper: {} Target: {}" .format(source, helper, target))

The Python class names, and Python file names must be exactly as specified below: Folder: (PyCharm project): Lab3 Python main driver file: toh_main.py Python class file names: towers_of_hanoi.py for class TowersOfHanoi tower.py for class Tower disk.py for class Disk

System Design

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, and the state of Towers is displayed with every move. The mainline code is in the toh_main.py file. It plays each of the 22 TowersOfHanoi games.

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.

Main: toh_main module:

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. For each game a TowersOfHanoi object is created and its play method is called. In addition, the number of moves for the game is displayed at the end of each game from this main function. Please follow the sample output which shows the program output. The Tower contents are displayed only when the game uses four 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 4, exactly as shown below in the Required Output.

Required Output Towers of Hanoi =============== Moving 3 disks completed in 7 moves! Towers: Tower A: [4, 3, 2, 1] Tower B: [] Tower C: []

Towers: Tower A: [4, 3, 2] Tower B: [1] Tower C: []

Towers: Tower A: [4, 3] Tower B: [1] Tower C: [2]

Towers: Tower A: [4, 3] Tower B: [] Tower C: [2, 1]

Towers: Tower A: [4] Tower B: [3] Tower C: [2, 1]

Towers: Tower A: [4, 1] Tower B: [3] Tower C: [2]

Towers: Tower A: [4, 1] Tower B: [3, 2] Tower C: []

Towers: Tower A: [4] Tower B: [3, 2, 1] Tower C: []

Towers: Tower A: [] Tower B: [3, 2, 1] Tower C: [4]

Towers: Tower A: [] Tower B: [3, 2] Tower C: [4, 1]

Towers: Tower A: [2] Tower B: [3] Tower C: [4, 1]

Towers: Tower A: [2, 1] Tower B: [3] Tower C: [4]

Towers: Tower A: [2, 1] Tower B: [] Tower C: [4, 3]

Towers: Tower A: [2] Tower B: [1] Tower C: [4, 3]

Towers: Tower A: [] Tower B: [1] Tower C: [4, 3, 2]

Towers: Tower A: [] Tower B: [] Tower C: [4, 3, 2, 1]

Moving 4 disks completed in 15 moves! Moving 5 disks completed in 31 moves! Moving 6 disks completed in 63 moves! Moving 23 disks completed in 8388607 moves! Moving 24 disks completed in 16777215 moves!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions