2019-07-01 08:10:18 +00:00
|
|
|
"""Tower of Hanoi."""
|
|
|
|
|
2019-04-14 11:58:16 +00:00
|
|
|
# @author willx75
|
2019-07-01 08:10:18 +00:00
|
|
|
# Tower of Hanoi recursion game algorithm is a game, it consists of three rods
|
|
|
|
# and a number of disks of different sizes, which can slide onto any rod
|
2019-04-14 11:58:16 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
|
|
|
2019-07-01 08:10:18 +00:00
|
|
|
def Tower_Of_Hanoi(n, source, dest, by, movement):
|
|
|
|
"""Tower of Hanoi - Move plates to different rods."""
|
2019-04-14 11:58:16 +00:00
|
|
|
if n == 0:
|
|
|
|
return n
|
|
|
|
elif n == 1:
|
2019-07-01 08:10:18 +00:00
|
|
|
movement += 1
|
|
|
|
# no print statement
|
|
|
|
# (you could make it an optional flag for printing logs)
|
2019-04-14 11:58:16 +00:00
|
|
|
logging.debug('Move the plate from', source, 'to', dest)
|
2019-07-01 08:10:18 +00:00
|
|
|
return movement
|
2019-04-14 11:58:16 +00:00
|
|
|
else:
|
|
|
|
|
2019-07-01 08:10:18 +00:00
|
|
|
movement = movement + Tower_Of_Hanoi(n - 1, source, by, dest, 0)
|
2019-04-14 11:58:16 +00:00
|
|
|
logging.debug('Move the plate from', source, 'to', dest)
|
|
|
|
|
2019-07-01 08:10:18 +00:00
|
|
|
movement = movement + 1 + Tower_Of_Hanoi(n - 1, by, dest, source, 0)
|
|
|
|
return movement
|