Is my Python Script to play Hanoi Tower respectful of OOP spirit?
I have difficulties to create meaningful classes so I started an easy project. Here is one class at the plateau level and then a class to handle towers.
How can it be improved (on the OOP level and on script level)?
I want then a GUI with TKinter, a third class GUI would be suited ?
Thank you
import numpy as np
NB_DISKS = 3
class Plateau:
"""Class for the whole plateau"""
def __init__(self):
self.init_plateau()
def init_plateau(self):
"""List of towers,first tower full"""
self.nb_disks = NB_DISKS
self.towers = [Tower(self.nb_disks), Tower(0), Tower(0)]
def motion(self, tower_from, tower_to):
"""Motion from one tower to another one with checking"""
from_tower = self.towers[tower_from-1]
to_tower = self.towers[tower_to-1]
if from_tower.get_last_disk()>to_tower.get_last_disk(): #Receive 0 if to_tower is empty
disk = from_tower.get_last_disk()
from_tower.delete_disk()
to_tower.add_disk(disk)
self.check_victory()
self.print_towers()
else:
print('Last disk number from origin has to be bigger than reception one')
def check_victory(self):
"""Check if last tower has an ordered list or not"""
last_tower = self.towers[2].get_whole_tower()
if len(last_tower) == self.nb_disks:
diff_last_tower = np.diff(last_tower)
if np.unique(diff_last_tower) == 1:
print('victory:')
self.print_towers()
print('new plateau:')
self.init_plateau()
def print_towers(self):
"""Print the towers"""
for elt in self.towers:
elt.print_tower()
print('\n')
class Tower:
"""Class for a tower"""
def __init__(self,nb_disks):
"""Creation of a tower"""
if nb_disks !=0:
self.tower = list(range(1, nb_disks+1))
else:
self.tower = []
def get_last_disk(self):
"""Return last element of the tower"""
if self.tower == []:
return 0
else:
return self.tower[-1]
def delete_disk(self):
"""Delete last disk of the tower"""
self.tower.pop()
def add_disk(self,disk):
"""Add a disk to the top of the tower"""
self.tower.append(disk)
def get_whole_tower(self):
"""Return the complete tower"""
return(self.tower)
def print_tower(self):
"""Print the element of the tower"""
print(self.tower)
if __name__ == '__main__':
game = Plateau()
game.motion(1,3)
game.motion(1,3) # Try to move a big disk on a small one
game.motion(1,2)
game.motion(3,2)
game.motion(1,3)
game.motion(2,1)
game.motion(2,3)
game.motion(1,3)