![]() If sum(tower.height for tower in self._towers) = self._towers.height: If the player has achieved victory, reset the game. """Check if all disks have moved to the last tower (victory condition). # don't drop the disk! Put it back where we got it and reraise. # Make the move, print exception if it fails.ĭisk = self._towers.pop_top_disk() Prints an error if the move is invalid.""" """Move from one tower to another (towers specified as an int from 1-3). Self._towers = ĭef move(self, from_tower: int, to_tower: int) -> None: Super()._init_("Can't put a disk of size %d on a disk of size %d" % (top, bottom))ĭef _init_(self, nb_disks: int) -> None:ĭef _init_plateau(self, nb_disks: int) -> None: Here's the final code I came up with after implementing the above suggestions: from typing import NewTypeĭef _init_(self, top: int, bottom: int) -> None: This is more an English thing than a Python thing, but the verb form of "motion" is "move" so it would be better to name your method move. The int values of your disks should be reversed so that bigger numbers represent bigger disks. The Pythonic way of making a "print" function is to implement the magic function _repr_ to return a string. Rather than making NB_DISKS a global and having Plateau initialize itself from that, Plateau's initializer should take the number of disks as a parameter. Note also that in general you should distinguish between "private" and "public" members of your classes. Get_whole_tower seems bad because it's exposing the internal data structure of the tower this violates your entire OOP abstraction. Since you always use get_last_disk and delete_disk together, why not combine them? The thing you really want is a pop_top_disk operation that removes and returns the last disk (that's already how you're implementing delete_disk anyway).Īdd_disk could implement the "no larger disk on a smaller one" rule. If it doesn't need any methods, you could just make it a subtype of int: from typing import NewType Game.motion(1,3) # Try to move a big disk on a small oneįrom the OOP/typing level, you might want to consider having Disk be a type. """Check if last tower has an ordered list or not""" Print('Last disk number from origin has to be bigger than reception one') If from_tower.get_last_disk()>to_tower.get_last_disk(): #Receive 0 if to_tower is empty """Motion from one tower to another one with checking""" I want then a GUI with TKinter, a third class GUI would be suited ? How can it be improved (on the OOP level and on script level)? ![]() Here is one class at the plateau level and then a class to handle towers. I have difficulties to create meaningful classes so I started an easy project. ![]() Is my Python Script to play Hanoi Tower respectful of OOP spirit? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |