home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Computing 57
/
ac057a.adf
/
Demos
/
hanoi.bas
< prev
next >
Wrap
BASIC Source File
|
1988-12-02
|
2KB
|
93 lines
'The Towers of Hanoi program in HiSoft BASIC
DEFINT a-z
CONST max_rings=20
CONST left=1,middle=2,right=3
CONST pole1=110,pole2=320,pole3=530
CONST space=50
CONST max_width=200
CONST gap=10
CONST full_height=200
SUB draw_ring(which_pole,start,size,type)
SHARED ring_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),,bf
END IF
END SUB
SUB realmove(val source, val destination)
SHARED poles(2), highest(3)
STATIC ring_width,ystart
ring_width=poles(source,highest(source))
'erase source ring
draw_ring source,highest(source),ring_width,0
poles(source,highest(source))=0
DECR highest(source)
'draw destination ring
INCR highest(destination)
poles(destination,highest(destination))=ring_width
draw_ring destination,highest(destination),ring_width,2
END SUB
SUB move(val howmany, val source, val work, val 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(1),"The Towers of Hanoi in HiSoft BASIC"
DIM poles(3,num_rings)
DIM highest(3)
highest(1)=num_rings
'initialise first pole
FOR i=1 TO num_rings
poles(1,i)=max_width-(i-1)*(max_width\num_rings)
NEXT i
'draw first pole
FOR i=1 TO num_rings
draw_ring 1,i,poles(1,i),2
NEXT i
move num_rings,left,middle,right