Towers¶
-
class
towers.core.towers.
Towers
(height=1, rods=None, moves=0, verbose=False)[source]¶ A representation of the towers including all logic.
-
__call__
()[source]¶ Run the towers. Convenience method.
Raises: See Towers.move_tower()
.
-
__contains__
(x)[source]¶ Does this
Towers
contain the givenRod
.Parameters: x (Rod) – The Rod
to find.Return type: bool
-
__deepcopy__
(*d)[source]¶ Return a deep copy of this instance.
Parameters: d (dict) – Memoisation dict. Return type: Towers
-
__enter__
()[source]¶ Context-Manager entry, validate our entry state for towers-start conditions.
Raises: See Towers.validate_start()
.
-
__eq__
(other)[source]¶ Compare Towers instances for equivalence.
Parameters: other (Towers) – The other Towers
to compare.Return type: bool
-
__exit__
(*args, **kwargs)[source]¶ Context-Manager exit, validate our exit state for towers-end conditions.
Raises: See Towers.validate_end()
.
-
__getitem__
(index)[source]¶ Get the
Rod
at the given index.Parameters: index (int) – The index to get the Rod
at.Return type: Rod
-
context
(*args, **kwds)[source]¶ Create a temp context for performing moves. The state of this instance will be reset at context exit.
Parameters: - reset_on_success (bool) – Reset this instance’s state on exit from the context if no error occurred. Default = True.
- reset_on_error (bool) – Reset this instance’s state on exit from the context if an error occurred. Default = False.
-
classmethod
from_json
(d)[source]¶ Return a class instance from a json serializable representation.
Parameters: d (str|dict) – The json or decoded-json from which to create a new instance. Return type: Towers Raises: See Towers
.__new__.
-
height
¶ Obtain the height of the
Towers
(ie: max number of disks each one rod can hold).Return type: int
-
move_disk
(start, end)[source]¶ Move the Disk from one Rod to another.
Note: Generator, yields Move instances.
Parameters:
-
moves
¶ Determine how many moves have occurred so far.
Return type: int
-
static
moves_for_height
(height)[source]¶ Determine the max number of moves required to solve the puzzle for the given height
Parameters: height (int) – The height of the Rods
(number ofDisk
on aRod
).Return type: int
-
validate
()[source]¶ Perform self validation.
Raises: - InvalidTowerHeight – The height of the tower is invalid
- DuplicateDisk – This
Rod
already contains thisDisk
. - CorruptRod – A
Disk
is on top of aDisk
of smaller size.
-
validate_end
()[source]¶ Validate the end conditions for this towers.
Raises: - InvalidTowerHeight – The height of the tower is invalid
- DuplicateDisk – This
Rod
already contains thisDisk
. - CorruptRod – A
Disk
is on top of aDisk
of smaller size. - InvalidEndingConditions – End conditions are invalid.
-
validate_start
()[source]¶ Validate the start conditions for this towers
Raises: - InvalidTowerHeight – The height of the
Towers
is invalid - DuplicateDisk – This
Rod
already contains thisDisk
. - CorruptRod – A
Disk
is on top of aDisk
of smaller size. - InvalidStartingConditions – Initial conditions are invalid.
- InvalidTowerHeight – The height of the
-
verbose
¶ Obtain this instance’s verbose flag.
Return type: bool
-