BASIC

Author Topic: Towers of hanoi  (Read 2395 times)

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1579
  • Particle Hawk
    • View Profile
Towers of hanoi
« on: 2012-Oct-16 »
Beta test for Towers of Hanoi, brainteaser puzzle.
This program will solve the game for you with a recursive algorithm, displaying all the moves step by step.
There are many videos on youtube showing the brainteaser.
Im not taking credit for the original algorithm, it is converted from a python snipet, i found during the recursive lecture on MiTx 6.00x at Edx.org.
With a bit of tweaking, i was able to put it into glbasic in about 10 minutes.
I dont actually have the puzzle, so i dont know if this is properly working, but it looks like the same output from python.

If you have the puzzle....try it with an even number of stacks, then an odd number...
Test:
Try with 4 stacks, then 5 stacks.

Enjoy, have fun!
- regards Neil



[attachment deleted by admin]
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: Towers of hanoi
« Reply #1 on: 2012-Oct-27 »
Tried with 20 stacks from the outset. Had to remove the key press wait command in the print function because there were so many moves. After that I removed the showscreen command in the print function because even refreshing the screen every so many moves was taking too long. It runs out there are 1,038,575 moves needed to solve the puzzle with 20 stacks. Imagine attempting that in a lifetime but messing up a move 2/3 into the solution.  :S

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4146
    • View Profile
    • Portfolio
Re: Towers of hanoi
« Reply #2 on: 2012-Oct-28 »
 :o

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1579
  • Particle Hawk
    • View Profile
Re: Towers of hanoi
« Reply #3 on: 2012-Oct-28 »
LOL, if you mess up the first move, i think you end up on the wrong tower.

Its interesting, because the number of moves can be predicted with a formula, BASE 2 to the power of stacks(n) plus one.

(2n)+1 = minimum number of moves to solve.

 :bed:

Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline Wampus

  • Prof. Inline
  • *****
  • Posts: 1004
    • View Profile
Re: Towers of hanoi
« Reply #4 on: 2012-Oct-28 »
Huh. Maybe I made a typo then. Does happen.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1579
  • Particle Hawk
    • View Profile
Re: Towers of hanoi
« Reply #5 on: 2012-Oct-28 »
I know, i wasnt laughing at that..

Your joke was funny, the million move puzzle.

Goodluck with THAT! haha

Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)