home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lb091.zip / HANOIPM.BAS < prev    next >
BASIC Source File  |  1995-08-11  |  6KB  |  248 lines

  1.  
  2.     ' Hanoi.bas
  3.  
  4.     ' This simple Liberty BASIC program let's the user
  5.     ' play the puzzle "Tower of Hanoi".
  6.  
  7.     ' We don't want a main window
  8.     nomainwin
  9.  
  10.     ' Prepare the variables that hold the disk data
  11.     tiny = 1
  12.     small = 1
  13.     medium = 1
  14.     large = 1
  15.  
  16.  
  17.     gosub [openWindow]
  18.     gosub [drawPuzzle]
  19.  
  20.     print #hanoi, "when leftButtonUp [beginMove]" ;
  21.  
  22. [inputLoop]
  23.  
  24.     if hanoiStarted = 0 then print #hanoi, "redraw" ; : hanoiStarted = 1
  25.     input r$
  26.     goto [inputLoop]
  27.  
  28.  
  29.  
  30.  
  31. [openWindow]    'Open the puzzle's window
  32.  
  33.     WindowWidth = 410
  34.     WindowHeight = 300
  35.  
  36.     open "Tower of Hanoi" for graphics_nsb as #hanoi
  37.     print #hanoi, "trapclose [quitHanoi]" ;
  38.  
  39.     return
  40.  
  41.  
  42. [drawPuzzle]    ' Draw the pegs
  43.  
  44.     print #hanoi, "cls ; fill black ; down ; backcolor black" ;
  45.     print #hanoi, "color darkgray ; size 10 ; line 100 120 100 220" ;
  46.     print #hanoi, "line 200 120 200 220" ;
  47.     print #hanoi, "line 300 120 300 220" ;
  48.     print #hanoi, "color lightgray ; size 15 ; line 0 220 400 220" ;
  49.  
  50.  
  51. [drawDisks]     ' Draw the disks
  52.  
  53.     peg1 = 207
  54.     peg2 = 207
  55.     peg3 = 207
  56.  
  57.     if large <> 1 then [largeIsNotOne]
  58.     print #hanoi, "size 10 ; color red" ;
  59.     print #hanoi, "line 60 "; peg1; " 140 "; peg1 ;
  60.     peg1 = peg1 - 10
  61.     goto [drawMedium]
  62.  
  63. [largeIsNotOne]
  64.  
  65.     if large <> 2 then [largeIsNotTwo]
  66.     print #hanoi, "size 10 ; color red" ;
  67.     print #hanoi, "line 160 "; peg2; " 240 "; peg2 ;
  68.     peg2 = peg2 - 10
  69.     goto [drawMedium]
  70.  
  71. [largeIsNotTwo]
  72.  
  73.     print #hanoi, "size 10 ; color red" ;
  74.     print #hanoi, "line 260 "; peg3; " 340 "; peg3 ;
  75.     peg3 = peg3 - 10
  76.  
  77.  
  78. [drawMedium]    ' Draw the medium size disk
  79.  
  80.     if medium <> 1 then [mediumIsNotOne]
  81.     print #hanoi, "size 10 ; color green" ;
  82.     print #hanoi, "line 70 "; peg1; " 130 "; peg1 ;
  83.     peg1 = peg1 - 10
  84.     goto [drawSmall]
  85.  
  86. [mediumIsNotOne]
  87.  
  88.     if medium <> 2 then [mediumIsNotTwo]
  89.     print #hanoi, "size 10 ; color green" ;
  90.     print #hanoi, "line 170 "; peg2; " 230 "; peg2 ;
  91.     peg2 = peg2 - 10
  92.     goto [drawSmall]
  93.  
  94. [mediumIsNotTwo]
  95.  
  96.     print #hanoi, "size 10 ; color green" ;
  97.     print #hanoi, "line 270 "; peg3; " 330 "; peg3 ;
  98.     peg3 = peg3 - 10
  99.  
  100. [drawSmall]    ' Draw the small size disk
  101.  
  102.     if small <> 1 then [smallIsNotOne]
  103.     print #hanoi, "size 10 ; color blue" ;
  104.     print #hanoi, "line 80 "; peg1; " 120 "; peg1 ;
  105.     peg1 = peg1 - 10
  106.     goto [drawTiny]
  107.  
  108. [smallIsNotOne]
  109.  
  110.     if small <> 2 then [smallIsNotTwo]
  111.     print #hanoi, "size 10 ; color blue" ;
  112.     print #hanoi, "line 180 "; peg2; " 220 "; peg2 ;
  113.     peg2 = peg2 - 10
  114.     goto [drawTiny]
  115.  
  116. [smallIsNotTwo]
  117.  
  118.     print #hanoi, "size 10 ; color blue" ;
  119.     print #hanoi, "line 280 "; peg3; " 320 "; peg3 ;
  120.     peg3 = peg3 - 10
  121.  
  122.  
  123. [drawTiny]    ' Draw the tiny size disk
  124.  
  125.     if tiny <> 1 then [tinyIsNotOne]
  126.     print #hanoi, "size 10 ; color yellow" ;
  127.     print #hanoi, "line 90 "; peg1; " 110 "; peg1 ;
  128.     peg1 = peg1 - 10
  129.     goto [finishDrawing]
  130.  
  131. [tinyIsNotOne]
  132.  
  133.     if tiny <> 2 then [tinyIsNotTwo]
  134.     print #hanoi, "size 10 ; color yellow" ;
  135.     print #hanoi, "line 190 "; peg2; " 210 "; peg2 ;
  136.     peg2 = peg2 - 10
  137.     goto [finishDrawing]
  138.  
  139. [tinyIsNotTwo]
  140.  
  141.     print #hanoi, "size 10 ; color yellow" ;
  142.     print #hanoi, "line 290 "; peg3; " 310 "; peg3 ;
  143.     peg3 = peg3 - 10
  144.  
  145.  
  146. [finishDrawing]
  147.  
  148.     print #hanoi, "flush" ;
  149.  
  150.     return
  151.  
  152.  
  153. [beginMove]     ' Start to move a disk
  154.  
  155.     if MouseX < 60 or MouseX > 340 then notice "Oops! No pin there!" : goto [inputLoop]
  156.     if MouseY > 215 or MouseY < 120 then notice "Oops! No pin there!" : goto [inputLoop]
  157.  
  158.     peg = int((MouseX + 50) / 100)
  159.  
  160.     noDisks$ = "There are no disk on that pin!"
  161.     if tiny <> peg and small <> peg and medium <> peg and large <> peg then notice noDisks$ : goto [inputLoop]
  162.  
  163.     print #hanoi, "place 10 10 ; color white ; backcolor black" ;
  164.     print #hanoi, "\\ Peg "; peg; " has been selected.\ Please select a destination peg." ;
  165.  
  166.     print #hanoi, "when leftButtonUp [endMove]" ;
  167.  
  168.     goto [inputLoop]
  169.  
  170.  
  171. [endMove]   ' Finish moving a disk
  172.  
  173.     if MouseX < 60 or MouseX > 340 then notice "Oops! No pin there!" : goto [inputLoop]
  174.     if MouseY > 215 or MouseY < 120 then notice "Oops! No pin there!" : goto [inputLoop]
  175.  
  176.     ontoPeg = int((MouseX + 50) / 100)
  177.     if ontoPeg = peg then [resetSelection]
  178.  
  179.     ' Determine which disk to move
  180.     if large = peg then disk$ = "large"
  181.     if medium = peg then disk$ = "medium"
  182.     if small = peg then disk$ = "small"
  183.     if tiny = peg then disk$ = "tiny"
  184.  
  185.     ' Determine if move is legal
  186.     moveOnto$ = "nothing"
  187.     if large = ontoPeg then moveOnto$ = "large"
  188.     if medium = ontoPeg then moveOnto$ = "medium"
  189.     if small = ontoPeg then moveOnto$ = "small"
  190.     if tiny = ontoPeg then moveOnto$ = "tiny"
  191.  
  192.     if moveOnto$ = "nothing" then [finishMove]
  193.  
  194.     if disk$ = "large" and moveOnto$ = "medium" then [illegalMove]
  195.     if disk$ = "large" and moveOnto$ = "small" then [illegalMove]
  196.     if disk$ = "medium" and moveOnto$ = "small" then [illegalMove]
  197.     if disk$ = "medium" and moveOnto$ = "tiny" then [illegalMove]
  198.     if disk$ = "small" and moveOnto$ = "tiny" then [illegalMove]
  199.  
  200. [finishMove]
  201.  
  202.     print #hanoi, "when leftButtonUp [beginMove]"
  203.  
  204.     if disk$ = "large" then large = ontoPeg
  205.     if disk$ = "medium" then medium = ontoPeg
  206.     if disk$ = "small" then small = ontoPeg
  207.     if disk$ = "tiny" then tiny = ontoPeg
  208.  
  209.     gosub [drawPuzzle]
  210.     if tiny = 3 and small = 3 and medium = 3 and large = 3 then [youWin]
  211.  
  212.     goto [inputLoop]
  213.  
  214.  
  215. [illegalMove]
  216.  
  217.     notice "That move is not allowed!"
  218.  
  219.  
  220. [resetSelection]
  221.  
  222.     print #hanoi, "when leftButtonUp [beginMove]" ;
  223.     print #hanoi, "place 10 10 ; color black ; backcolor black" ;
  224.     print #hanoi, "\\ Peg "; peg; " has been selected.\ Please select a destination peg." ;
  225.     goto [inputLoop]
  226.  
  227.  
  228. [youWin]
  229.  
  230.     beep
  231.     notice "You win!"
  232.     confirm "Play another?"; answer$
  233.     if answer$ = "no" then [quitHanoi]
  234.  
  235.     tiny = 1
  236.     small = 1
  237.     medium = 1
  238.     large = 1
  239.     gosub [drawPuzzle]
  240.     goto [inputLoop]
  241.  
  242.  
  243. [quitHanoi]
  244.  
  245.     print #hanoi, "trapclose"
  246.     close #hanoi
  247.     end
  248.