I have written a code for "Towers of Hanoi" in python and I am trying to add a counter to show how many times it has run. I tried several things like a while loop and for loops etc. but it doesn't work. I am sure that the answer is pretty easy but my brain is running on the lowest setting right now. My code looks like this:
def Hanoi(n, src, dst, tmp):
if n > 0:
Hanoi(n - 1, src, tmp, dst)
print "Move disc", chr(64 + n), "From tower", src, "to tower", dst
Hanoi(n - 1, tmp, dst, src)
Hanoi(4,0,2,1) #Just an example
I know that the code has to run (2^n)-1 times, but I just cant implement it. Because the way I wrote the code the value n changes so that would work for me. (I tried something like this:
def Hanoi(n, src, dst, tmp):
a = 0
while (a < (2**n)-1)
a+=1
if n > 0:
Hanoi(n - 1, src, tmp, dst)
print a, "Move disc", chr(64 + n), "From tower", src, "to tower", dst
Hanoi(n - 1, tmp, dst, src)
but as I've said, the value n changes and I don't know how to fix it.
Edit:
To clarify, I want it to shop the number of steps like this: (If I call Hanoi(3,0,2,1))
1. Move disc A From tower 0 to tower 2
2. Move disc B From tower 0 to tower 1
3. Move disc A From tower 2 to tower 1
4. Move disc C From tower 0 to tower 2
5. Move disc A From tower 1 to tower 0
6. Move disc B From tower 1 to tower 2
7. Move disc A From tower 0 to tower 2