home *** CD-ROM | disk | FTP | other *** search
- ' The Towers of Hanoi program in HiSoft BASIC
-
- DEFINT A-Z
-
- CONST max_rings = 25
- CONST left = 1, middle = 2, right = 3
- CONST pole1 = 110, pole2 = 320, pole3 = 530
- CONST space = 50
- CONST max_width = 200
- CONST gap = 10
-
- full_height = 200
-
- DIM SHARED highest(3)
-
- SUB draw_ring(BYVAL which_pole, BYVAL size, BYVAL type, BYVAL start)
- SHARED ring_height, full_height
- STATIC xstart,ystart
-
- SELECT CASE which_pole
- CASE=1
- xstart = pole1 - size \ 2
- CASE=2
- xstart = pole2 - size \ 2
- CASE=3
- xstart = pole3 - size \ 2
- END SELECT
-
- ystart = full_height - space - start * ring_height
-
-
- IF type = 0 THEN
- LINE (xstart, ystart) - STEP (size, ring_height - 2), 0, BF
- ELSE
- LINE (xstart, ystart) - STEP (size, ring_height - 2), 1, BF
- END IF
- END SUB
-
- SUB realmove(BYVAL source, BYVAL destination)
- SHARED poles(2)
- STATIC ring_width,ystart
-
- ring_width=poles(source,highest(source))
-
- 'erase source ring
- draw_ring source,ring_width,0,highest(source)
-
- poles(source,highest(source))=0
- DECR highest(source)
-
- 'draw destination ring
- INCR highest(destination)
- poles(destination,highest(destination)) = ring_width
-
- draw_ring destination, ring_width, 1, highest(destination)
- END SUB
-
- SUB move(BYVAL howmany, BYVAL source, BYVAL work, BYVAL destination)
- IF howmany <=1 THEN
- realmove source,destination
- ELSE
- move howmany - 1, source, destination, work
- realmove source, destination
- move howmany - 1, work, source, destination
- END IF
- END SUB
-
- 'The actual start
- DO
- LOCATE 1,2
- INPUT "Number of rings to move: ",num_rings
- LOOP UNTIL num_rings>1 AND num_rings<=max_rings
-
- ring_height=(full_height-2*space)\max_rings
-
- WINDOW 2, "The Towers of Hanoi in HiSoft BASIC", (0, 18) - (640, full_height),256+31
-
- DIM SHARED poles(3,num_rings)
-
- 'initialise first pole
- FOR i = 1 TO num_rings
- poles(1,i) = max_width - (i - 1) * (max_width \ num_rings)
- NEXT i
-
- FOR i = 1 TO num_rings
- highest(1) = i
- draw_ring 1, poles(1,i), 2, highest(1)
- NEXT i
-
- tm! = TIMER
-
- move num_rings, left, middle, right
-
- PRINT num_rings; "rings moved in"; TIMER-tm!; "seconds"
-