home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Basic / MAXONB32.DMS / in.adf / Beispiele_1.3 / Examples / Hanoi.bas < prev    next >
Encoding:
BASIC Source File  |  1994-04-14  |  2.0 KB  |  95 lines

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