Tower+Of+Hanoi

This is a translation of the Tower of Hanoi puzzle that ships with Liberty BASIC. (hanoi.bas) Feel free to modify and improve this program and be sure to take credit for your improvements.

Here are some screenshots.

Winning the game.


code format="vbnet" ' Hanoi.bas

' This simple Run BASIC program lets the user ' play the puzzle "Tower of Hanoi".

' Prepare the variables that hold the disk data [restart] cls tiny = 1 small = 1 medium = 1 large = 1 answer$ = ""

graphic #hanoi, 410, 280 gosub [drawPuzzle]

Input "Keep all moves displayed (Y/[N])"; answer$

[inputLoop] if instr("YESyes", answer$) = 0 then cls

if fromPeg$ <> "" and toPeg$ <> "" and isIllegalMove = 0 then print "Moved disk from peg ";fromPeg$;" to peg ";toPeg$ end if   if isNoDisk = 1 then print "There are no disks on that pin!" isNoDisk = 0 end if   if isIllegalMove = 1 then html " That move is not allowed! " isIllegalMove = 0 end if

fromPeg$ = "" toPeg$ = ""

render #hanoi input "Remove from peg (1,2, or 3) > ";fromPeg$ if instr("123", fromPeg$) = 0 then goto [inputLoop] fromPeg$ = "" end if   peg = val(fromPeg$) if (tiny <> peg) and (small <> peg) and (medium <> peg) and (large <> peg) then fromPeg$ = "" isNoDisk = 1 goto [inputLoop] end if

input "Move to peg (1,2, or 3) > ";toPeg$ if instr("123", toPeg$) = 0 then goto [inputLoop] toPeg$ = "" end if

[endMove] ' Finish moving a disk ontoPeg = val(toPeg$) if ontoPeg = peg then goto [inputLoop]

' Determine which disk to move if large = peg then disk$ = "large" if medium = peg then disk$ = "medium" if small = peg then disk$ = "small" if tiny = peg then disk$ = "tiny"

' Determine if move is legal moveOnto$ = "nothing" if large = ontoPeg then moveOnto$ = "large" if medium = ontoPeg then moveOnto$ = "medium" if small = ontoPeg then moveOnto$ = "small" if tiny = ontoPeg then moveOnto$ = "tiny"

if disk$ = "large" and moveOnto$ = "medium" then goto [illegalMove] if disk$ = "large" and moveOnto$ = "small" then goto [illegalMove] if disk$ = "large" and moveOnto$ = "tiny" then goto [illegalMove] if disk$ = "medium" and moveOnto$ = "small" then goto [illegalMove] if disk$ = "medium" and moveOnto$ = "tiny" then goto [illegalMove] if disk$ = "small" and moveOnto$ = "tiny" then goto [illegalMove]

if disk$ = "large" then large = ontoPeg if disk$ = "medium" then medium = ontoPeg if disk$ = "small" then small = ontoPeg if disk$ = "tiny" then tiny = ontoPeg

gosub [drawPuzzle] if (tiny + small + medium + large = 12) then goto [youWin] goto [inputLoop]

[illegalMove] isIllegalMove = 1 goto [inputLoop]

[youWin] gosub [drawPuzzle] cls render #hanoi html "You win! "

Input "Do you like to play again (Y/[N])"; answer$ if instr("YESyes", answer$) > 0 then goto [restart] end

[drawPuzzle] ' Draw the pegs #hanoi cls("black") #hanoi Color("white") #hanoi Place(5,20) #hanoi "\Move disks one at a time from one peg to another." #hanoi "\Move all disks to the third peg." #hanoi "\No disk may be placed on a smaller disk." #hanoi Color(100,100,100) #hanoi Size(10) #hanoi line(100, 120, 100, 220) #hanoi line(200, 120, 200, 220) #hanoi line(300, 120, 300, 220) #hanoi Color(190,190,190) #hanoi Size(15) #hanoi Line(10,220,400,220)

[drawDisks] ' Draw the disks peg1 = 207 peg2 = 207 peg3 = 207 #hanoi Color("red") #hanoi Size(10)

if large <> 1 then goto [largeIsNotOne] #hanoi line(60, peg1, 140, peg1) peg1 = peg1 - 10 goto [drawMedium]

[largeIsNotOne] if large <> 2 then goto [largeIsNotTwo] #hanoi line(160, peg2, 240, peg2) peg2 = peg2 - 10 goto [drawMedium]

[largeIsNotTwo] #hanoi line(260, peg3, 340, peg3) peg3 = peg3 - 10

[drawMedium] ' Draw the medium size disk #hanoi Color("green") #hanoi Size(10) if medium <> 1 then goto [mediumIsNotOne] #hanoi line(70, peg1, 130, peg1) peg1 = peg1 - 10 goto [drawSmall]

[mediumIsNotOne] if medium <> 2 then goto [mediumIsNotTwo] #hanoi line(170, peg2, 230, peg2) peg2 = peg2 - 10 goto [drawSmall]

[mediumIsNotTwo] #hanoi line(270, peg3, 330, peg3) peg3 = peg3 - 10

[drawSmall] ' Draw the small size disk #hanoi Color("blue") #hanoi Size(10) if small <> 1 then [smallIsNotOne] #hanoi line(80, peg1, 120, peg1) peg1 = peg1 - 10 goto [drawTiny]

[smallIsNotOne] if small <> 2 then goto [smallIsNotTwo] #hanoi line(180, peg2, 220, peg2) peg2 = peg2 - 10 goto [drawTiny]

[smallIsNotTwo] #hanoi line(280, peg3, 320, peg3) peg3 = peg3 - 10

[drawTiny] ' Draw the tiny size disk #hanoi Color("yellow") #hanoi Size(10) if tiny <> 1 then [tinyIsNotOne] #hanoi line(90, peg1, 110, peg1) peg1 = peg1 - 10 goto [finishDrawing]

[tinyIsNotOne] #hanoi Color("yellow") #hanoi Size(10) if tiny <> 2 then goto [tinyIsNotTwo] #hanoi line(190, peg2, 210, peg2) peg2 = peg2 - 10 goto [finishDrawing]

[tinyIsNotTwo] #hanoi line(290, peg3, 310, peg3) peg3 = peg3 - 10

[finishDrawing] #hanoi flush html " " return code