home *** CD-ROM | disk | FTP | other *** search
/ Amiga Computing 57 / ac057a.adf / Demos / hanoi.bas < prev    next >
BASIC Source File  |  1988-12-02  |  2KB  |  93 lines

  1. 'The Towers of Hanoi program in HiSoft BASIC
  2.  
  3. DEFINT a-z
  4.  
  5. CONST max_rings=20
  6. CONST left=1,middle=2,right=3
  7. CONST pole1=110,pole2=320,pole3=530
  8. CONST space=50
  9. CONST max_width=200
  10. CONST gap=10
  11. CONST full_height=200
  12.  
  13. SUB draw_ring(which_pole,start,size,type)
  14. SHARED ring_height
  15. STATIC xstart,ystart
  16.  
  17. SELECT CASE which_pole
  18.     CASE=1
  19.         xstart=pole1-(size)\2
  20.     CASE=2
  21.         xstart=pole2-(size)\2
  22.     CASE=3
  23.         xstart=pole3-(size)\2
  24. END SELECT
  25.  
  26. ystart=full_height-space-start*ring_height
  27.  
  28. IF type=0 THEN
  29.   LINE(xstart,ystart)-STEP(size,ring_height-2),0,bf
  30. ELSE
  31.   LINE(xstart,ystart)-STEP(size,ring_height-2),,bf
  32. END IF      
  33.           
  34. END SUB
  35.  
  36. SUB realmove(val source, val destination)
  37. SHARED poles(2), highest(3)
  38. STATIC ring_width,ystart
  39.  
  40. ring_width=poles(source,highest(source))
  41.  
  42. 'erase source ring
  43. draw_ring source,highest(source),ring_width,0
  44.  
  45. poles(source,highest(source))=0
  46. DECR highest(source)
  47.  
  48. 'draw destination ring
  49. INCR highest(destination)
  50. poles(destination,highest(destination))=ring_width
  51.  
  52. draw_ring destination,highest(destination),ring_width,2
  53.  
  54. END SUB
  55.  
  56. SUB move(val howmany, val source, val work, val destination)
  57.     IF howmany=1 THEN
  58.        realmove source,destination
  59.     ELSE
  60.        move howmany-1,source,destination,work
  61.        realmove source,destination
  62.        move howmany-1,work,source,destination
  63.     END IF
  64. END SUB
  65.  
  66. 'The actual start
  67. DO
  68.     LOCATE 1,2
  69.     INPUT "Number of rings to move: ",num_rings
  70. LOOP UNTIL num_rings>1 AND num_rings<=max_rings
  71.  
  72. ring_height=(full_height-2*space)\max_rings
  73.  
  74. WINDOW(1),"The Towers of Hanoi in HiSoft BASIC"
  75.  
  76. DIM poles(3,num_rings)
  77. DIM highest(3)
  78.  
  79. highest(1)=num_rings
  80.  
  81. 'initialise first pole
  82. FOR i=1 TO num_rings
  83.     poles(1,i)=max_width-(i-1)*(max_width\num_rings)
  84. NEXT i
  85.  
  86. 'draw first pole
  87. FOR i=1 TO num_rings
  88.     draw_ring 1,i,poles(1,i),2
  89. NEXT i
  90.  
  91. move num_rings,left,middle,right
  92.  
  93.