!--------------------------------------------------
!- sabato 15 agosto 2026 16:33:58
!- Import of : HANOI-3.PRG
!- From Disk : c:\users\alessandro\documents\d64\hanoi-tower.d64
!- Commodore 64
!--------------------------------------------------
10 rem ***************     la torre di hanoy        ***************
20 rem ***************   by alessandro scola 2024   ***************
30 rem ***************   www.alessandroscola.com    ***************
40 rem ************************************************************************
50 rem ********************    variabili  principali  *************************
60 rem sd       :sprite definiti (si/no)
70 rem dm       :numero dichi massimi che il programma gestisce
80 rem d        :numero dischi scelti dal giocatore per la partita
90 rem mm       :numero mosse minime per vincere = 2^d-1
100 rem pi       :piolo iniziale selezionato
110 rem pf       :piolo finale selezionato
120 rem pc       :piolo corrente (selettore piolo da joystick)
130 rem p1       :peso disco in alto su piolo 1
140 rem p2       :peso disco in alto su piolo 2
150 rem p3       :peso disco in alto su piolo 3
160 rem p1(dm)   :situazione pesi dischi su piolo 1
170 rem p2(dm)   :situazione pesi dischi su piolo 2
180 rem p3(dm)   :situazione pesi dischi su piolo 3
190 rem q1,q2,q3 :numero dischi presenti su ogni piolo
200 rem x1,x2,x3 :coordinate x degli sprites su ogni piolo
210 rem y0       :coordinata y minima sprites (il punto piu in alto)
220 rem y9       :coordinata y massima sprites (il punto piu in basso)
230 rem ad       :altezza sprite di un singolo disco in pixels
240 rem sn       :sprite number
250 rem sp       :stack pointer (per la soluzione ricorsiva)
260 rem v        :registro del vic di partenza per gestion sprites
270 rem b        :banco memoria di partenza per gli sprite
280 rem ************************************************************************
290 rem ************************************************************************
300 sd=0:rem sprites non ancora definiti
310 dm=8:rem fino a 8 dischi massimo
320 dim p1(dm):rem stato presenza dei pioli su piolo 1, da 1 a 7
330 dim p2(dm):rem stato presenza dei pioli su piolo 2, da 1 a 7
340 dim p3(dm):rem stato presenza dei pioli su piolo 3, da 1 a 7
350 x1=76:x2=156:x3=236:rem coordinate x degli sprites su pioli
360 y0=60:rem y mimina (punto piu in alto quando muovo disco)
370 y9=168:rem y massima (coord y pputno di appoggio dei dischi al piano)
380 ad=12:rem altezza disco in pixels(sprite espanso =6 x 2 = 12)
390 v=53248:rem registro del vic di partenza per gestion sprites
400 rem ************************************************************************
410 poke 53280,15:poke 53281,0:poke 646,1:rem colore sfondo,bordo scritte
420 poke 53269,0:rem spegne tutti gli sprites eventualmente visibili
430 if peek(44)=64 then 520
440 print "{clear}attenzione!!"
450 print "{down}per eseguire questo programma"
460 print "devi prima eseguire il seguente comando:"
470 print "{down}{red}poke 43,1:poke 44,64:poke 16384,0{down*2}"
480 print "{white}poi ricarica il programma da disco"
490 print "con ,8 e non con ,8,1{space*3}!! "
500 print "{down}alla fine eseguilo con il comando run"
510 end
520 rem ***********************************************************************
530 print "{clear}"spc(9)"{down*3}{red}la{space*2}torre{space*2}di{space*2}hanoi"
540 print spc(6)"{down*4}{cyan}by alessandro scola (c)2024"
550 print spc(8)"{white}www.alessandroscola.com"
560 print spc(6)"{down*2}{blue}gioca alla torre di hanoi"
570 print spc(6)"o{space*2}falla risolvere al tuo"
580 print spc(6)"commodore 64 con il metodo"
590 print spc(6)"ricorsivo."
600 if sd=1 goto 730
610 rem ***************************
620 rem * ridefinisce gli sprites *
630 rem ***************************
640 print "{down*4}{yellow}{space*2}inizializzazione sprites, attendi..."
650 b=245:rem banco memoria di partenza per gli sprite 245
660 rem legge i data per i dischi che utilizzero
670 for sn=0 to dm-1
680 poke 1862,56-sn:rem visualizza conto alla rovescia
690 poke 2040+sn,b+sn:rem setta banco di memoria per ogni sprite
700 for i=0 to 62
710 read a:poke 64*b+(sn*64)+i,a:next i
720 next sn
730 sd=1:rem sprites definiti
740 print "{yellow}{up*3}""
750 PRINT "{yellow}{home}{down*20}{space*3}premi fire in porta 2 per giocare{space*3}"
760 IF(PEEK(56320) AND 16)=0 GOTO 780:REM ATTENTE PRESSIONE FIRE
770 GOTO 760
780 POKE 53269,0:POKE 53280,15:POKE 53281,0:POKE 646,1:REM COLORE SFONDO,BORDO SCRITTE
790 D=3:PRINT"{clear}{down*11}{right*9}{green}quanti dischi ? (1-8)"
800 PRINT "{yellow}{right*11}{down*3}(joystick up/down)"
810 PRINT"{home}{down*11}{right*31}{space*2}<{up}"
820 PRINT"{home}{down*10}":PRINT TAB(31);D:J=PEEK(56320)
830 J=PEEK(56320)
840 IF((J AND 1)<>0 AND (J AND 2)<>0  AND (J AND 16)<>0) GOTO 830
850 IF(D<=7 AND (J AND 1)=0) THEN D=D+1
860 IF(D>=2 AND (J AND 2)=0) THEN D=D-1
870 IF(J AND 16)=0 THEN 910
880 FOR I=1 TO 50:NEXTI:GOTO 820
890 REM FOR I=1 TO 25:NEXT I
900 POKE 56320,255:GOTO 820
910 GM$="g":PRINT "{clear}"
920 POKE 56320,255
930 IF(GM$="g") THEN PRINT"{home}{down*9}{space*11}{yellow}>{reverse on}{space*4}giocare{space*5}{reverse off}<"
940 IF(GM$="g") THEN PRINT"{home}{down*11}{space*11}{green}>vedere soluzione<"
950 IF(GM$="s") THEN PRINT"{home}{down*9}{space*11}{green}>{space*4}giocare{space*5}<"
960 IF(GM$="s") THEN PRINT"{home}{down*11}{space*11}{yellow}>{reverse on}vedere soluzione{reverse off}<"
970 PRINT "{yellow}{right*11}{down*3}(joystick up/down)"
980 POKE 56320,255:J=PEEK(56320)
990 IF(GM$="s" AND (J AND 1)=0) THEN GM$="g":GOTO 920
1000 IF(GM$="g" AND (J AND 2)=0) THEN GM$="s":GOTO 920
1010 IF(J AND 16)=0 THEN 1040
1020 FOR I=1 TO 50:NEXT I
1030 GOTO 980
1040 REM
1050 REM MEMORIZZA IN UN ARRAY LO STATO PRESENZA DISCHI PESATI SU PIOLO 1
1060 FOR I=1 TO D:P1(I)=I:NEXT I
1070 P1=1:P2=0:P3=0:REM PESO DEL DISCO IN ALTO SU UN DATO PIOLO
1080 Q1=D:Q2=0:Q3=0:REM Q1, Q2, Q3 = QUANTI DISCHI HA CIASCUN PIOLO
1090 REM POSIZIONA GLI SPRITES DEI DISCHI UNO SULL'ALTRO SU PIOLO 1
1100 REM DISEGNA LA BASE DEL GIOCO
1110 I=0:REM VARIABILE OFFSET RESGISTRI SPRITES
1120 OC=0: REM OFFFSET COLORE PER SALTARE IL NUMERO 8
1130 FOR SN=D-1 TO 0 STEP -1:REM SETTA POSIZIONE X E Y DISCHI
1140 POKE V+(SN*2),X1:I=I+1:POKE V+((SN*2)+1),Y9-((I-1)*AD/2)
1150 I=I+1
1160 IF (SN+4)=7 THEN OC=1:REM PER SALTARE IL COLORE GIALLO
1170 POKE 53287+SN,SN+4-OC:REM SETTA I COLORI DEI DISCHI
1180 NEXT SN
1190 REM ABILITA SPRITES E ESPANDILI IN LARGHEZZA E ALTEZZA
1200 VA=0:FOR N=0 TO D-1:VA=VA+(2^N):NEXT
1210 POKE 53269,VA:REM ABILITA SPRITES
1220 POKE 53277,VA:REM ESPANSIONE IN LARGHEZZA
1230 POKE 53271,VA:REM ESPANSIONE IN ALTEZZA
1240 REM *************************************************
1250 REM *************************************************
1260 MM=2^D-1:M=1:REM MM=NUMERO MOSSE MINIME X RISOLVERE, M=NUMERO MOSSA
1270 REM SPOSTA IL DISCO PIU IN ALTO DAL PIOLO SELEZIONATO
1280 PRINT "{clear}{cyan}si risolve in "+STR$(MM)+" mosse.{white}"
1290 POKE 646,1:POKE 53280,15:PRINT "{home}{cyan}si risolve in "+STR$(MM)+" mosse.{white}"
1300 GOSUB 3790:REM DISEGNA I PIOLI E LA BASE
1310 IF GM$="s" THEN 3900
1320 REM GM$="g" GIOCARE
1330 PI=0:PF=0:PC=1:REM PIOLO INIZIO,FINE,CORRENTE
1340 POKE 646,1:POKE 214,2:SYS 58732:PRINT "fai la mossa n."+STR$(M)
1350 POKE56145,1:POKE56155,1:POKE56165,1
1360 PI=0:PF=0:REM PIOLO INIZIO,FINE
1370 IF(PC=1) THEN POKE 1872,62:POKE1874,60
1380 IF(PC=1) THEN POKE 1882,32:POKE1884,32
1390 IF(PC=1) THEN POKE 1892,32:POKE1894,32
1400 IF(PC=1) THEN POKE 1919,32:POKE1927,32
1410 IF(PC=2) THEN POKE 1872,32:POKE1874,32
1420 IF(PC=2) THEN POKE 1882,62:POKE1884,60
1430 IF(PC=2) THEN POKE 1892,32:POKE1894,32
1440 IF(PC=2) THEN POKE 1919,32:POKE1927,32
1450 IF(PC=3) THEN POKE 1872,32:POKE1874,32
1460 IF(PC=3) THEN POKE 1882,32:POKE1884,32
1470 IF(PC=3) THEN POKE 1892,62:POKE1894,60
1480 IF(PC=3) THEN POKE 1919,32:POKE1927,32
1490 IF(PC=4) THEN POKE 1872,32:POKE1874,32
1500 IF(PC=4) THEN POKE 1882,32:POKE1884,32
1510 IF(PC=4) THEN POKE 1892,32:POKE1894,32
1520 IF(PC=4) THEN POKE 1919,62:POKE1927,60
1530 J=PEEK(56320)
1540 IF((J AND 4)<>0 AND (J AND 8)<>0 AND (J AND 16)<>0) AND (J AND 2)<>0 AND (J AND 1)<>0 GOTO 1530
1550 IF(J AND 4)=0 AND PC>1 THEN PC=PC-1:REM JOY A SINISTRA
1560 IF(J AND 8)=0 AND PC<3 THEN PC=PC+1:REM JOY A DESTRA
1570 IF(J AND 2)=0 THEN PC=4:REM GIU
1580 IF(J AND 1)=0 THEN PC=2:REM SU
1590 IF(J AND 16)=0 AND PC=1 AND PI=0 THEN PI=1:GOSUB 5160:GOTO1670:REM SELEZIONO P1
1600 IF(J AND 16)=0 AND PC=1 AND PI=1 THEN PI=0:GOSUB 5160:REM DESELEZIONO P1
1610 IF(J AND 16)=0 AND PC=2 AND PI=0 THEN PI=2:GOSUB 5160:GOTO1670:REM SELEZIONO P2
1620 IF(J AND 16)=0 AND PC=2 AND PI=2 THEN PI=0:GOSUB 5160:GOTO1670:REM DESELEZIONO P2
1630 IF(J AND 16)=0 AND PC=3 AND PI=0 THEN PI=3:GOSUB 5160:GOTO1670:REM SELEZIONO P3
1640 IF(J AND 16)=0 AND PC=3 AND PI=3 THEN PI=0:GOSUB 5160:GOTO1670:REM DESELEZIONO P3
1650 IF(J AND 16)=0 AND PC=4 THEN 410
1660 IF(J AND 16)=0 AND PI<>0 AND PC<>PI THEN PF=PC:GOTO 1730
1670 IF(PI=0) THEN POKE56145,1:POKE56155,1:POKE56165,1
1680 IF(PI=0) THEN PRINT "{home}{down*4}"SPC(9)"{space*21}"
1690 IF(PI=1) THEN POKE56145,3:PRINT "{cyan}{home}{down*4}"SPC(9)"da piolo 1"
1700 IF(PI=2) THEN POKE56155,3:PRINT "{cyan}{home}{down*4}"SPC(9)"da piolo 2"
1710 IF(PI=3) THEN POKE56165,3:PRINT "{home}{down*4}"SPC(9)"da piolo 3"
1720 GOTO 1370
1730 if pf=1 and GM$="g" then PRINT "{light blue}{home}{down*4}"SPC(20)" a piolo 1"
1731 if pf=2 and GM$="g" then PRINT "{light blue}{home}{down*4}"SPC(20)" a piolo 2" 
1732 if pf=3 and GM$="g" then PRINT "{light blue}{home}{down*4}"SPC(20)" a piolo 3" 
1740 IF PI=1 AND PF=2 AND Q1>0 AND (P2=0 OR P2>P1) THEN 1930
1741 IF PI=1 AND PF=3 AND Q1>0 AND (P3=0 OR P3>P1) THEN 2220
1742 IF PI=2 AND PF=1 AND Q2>0 AND (P1=0 OR P1>P2) THEN 2500
1743 IF PI=2 AND PF=3 AND Q2>0 AND (P3=0 OR P3>P2) THEN 2780
1744 IF PI=3 AND PF=1 AND Q3>0 AND (P1=0 OR P1>P3) THEN 3060
1745 IF PI=3 AND PF=2 AND Q3>0 AND (P2=0 OR P2>P3) THEN 3340
1790 POKE 53280,2:PRINT "{home}{down*4}{right*9}{red}* mossa non possibile *""
1800 rem for i=1 to 255:next i:for i=1 to 255:next i
1810 rem error sound
1820 s=54272:for i=0 to 24:poke s+i,0:next i:poke s+24,15
1830 poke s+1,8:poke s+0,0:rem low frequency note
1840 poke s+5,0:poke s+6,240
1850 poke s+4,65:rem pulse wave on
1860 for pw=1 to 15
1870 poke 53280,pw
1880 poke s+3,pw:poke s+2,0:rem sweep high byte of pulse width
1890 for t=1 to 40:next t
1900 next pw
1910 poke s+4,64:rem pulse off
1920 print "{home}{down*4}{right*9}{red}{space*24}":poke 53280,15:goto 1340
1930 rem ******************************************
1940 rem * spostamento disco da piolo 1 a piolo 2 *
1950 rem ******************************************
1960 rem p1 = valore del disco piu in alto,che corrisponde allo sprites p1-1
1970 for y=peek(v+1+(2*(p1-1))) to y0 step -2
1980 poke (v+1+(2*(p1-1))),y:rem sollevo disco
1990 next y
2000 for x=peek(v+(2*(p1-1))) to x2 step 2
2010 poke (v+(2*(p1-1))),x:rem sposto disco a dx
2020 next x
2030 rem appoggio disco:
2040 rem y1= coord y di partenza  y2 = coord y di arrivo dello sprite
2050 y1=peek(v+1+(2*(p1-1))):rem altrimenti y di arrivo e' appena sopra disco
2060 if p2=0 then y2=y9:goto 2080
2070 y2=y9-(ad*q2)
2080 for y=y1 to y2 step 2
2090 poke (v+1+(2*(p1-1))),y
2100 next y
2110 gosub 4210:rem riproduci suono
2120 rem aggiorno peso pila di destinazione 2
2130 for i=q2+1 to 1 step -1:p2(i)=p2(i-1):next i:rem scalo in alto i pesi
2140 p2(1)=p1:p2=p1:q2=q2+1
2150 rem aggiorno presenza dischi pila di partenza 1
2160 if q1>1 then:for i=1 to q1-1:p1(i)=p1(i+1):next i:rem scalo in basso i pesi
2170 p1(q1)=0:q1=q1-1
2180 if q1>=1 then p1=p1(1)
2190 if q1=0 then p1(1)=0:p1=0
2200 if gm$="g" then 3600
2210 return
2220 rem ******************************************
2230 rem * spostamento disco da piolo 1 a piolo 3 *
2240 rem ******************************************
2250 for y=peek(v+1+(2*(p1-1))) to y0 step -2
2260 poke (v+1+(2*(p1-1))),y:rem sollevo disco
2270 next y
2280 for x=peek(v+(2*(p1-1))) to x3 step 2
2290 poke (v+(2*(p1-1))),x:rem sposto disco a dx
2300 next x
2310 rem appoggio disco:
2320 rem y1= y di partenza  y2 = y di arrivo
2330 y1=peek(v+1+(2*(p1-1)))
2340 if p3=0 then y2=y9:goto 2360
2350 y2=y9-(ad*q3):rem altrimenti y di arrivo e' appena sopra disco
2360 for y=y1 to y2 step 2
2370 poke (v+1+(2*(p1-1))),y
2380 next y
2390 gosub 4210:rem riproduci suono
2400 rem aggiorno peso pila di destinazione 3
2410 for i=q3+1 to 1 step -1:p3(i)=p3(i-1):next i:rem scalo in basso i pesi
2420 p3(1)=p1:p3=p1:q3=q3+1
2430 rem aggiorno presenza dischi pila di partenza 1
2440 if q1>1 then:for i=1 to q1-1:p1(i)=p1(i+1):next i:rem scalo in alto i pesi
2450 p1(q1)=0:q1=q1-1
2460 if q1>=1 then p1=p1(1)
2470 if q1=0 then p1(1)=0:p1=0
2480 if gm$="g" then 3600
2490 return
2500 rem *******************************************
2510 rem * spostamento disco da piolo 2 a piolo 1  *
2520 rem *******************************************
2530 for y=peek(v+1+(2*(p2-1))) to y0 step -2
2540 poke (v+1+(2*(p2-1))),y:rem sollevo disco
2550 next y
2560 for x=peek(v+(2*(p2-1))) to x1 step -2
2570 poke (v+(2*(p2-1))),x:rem sposto disco a sx
2580 next x
2590 rem appoggio disco:
2600 rem y1= coord y di partenza  y2 = coord y di arrivo dello sprite
2610 y1=peek(v+1+(2*(p2-1))):rem altrimenti y di arrivo e' appena sopra disco
2620 if p1=0 then y2=y9:goto 2640
2630 y2=y9-(ad*q1)
2640 for y=y1 to y2 step 2
2650 poke (v+1+(2*(p2-1))),y
2660 next y
2670 gosub 4210:rem riproduci suono
2680 rem aggiorno peso pila di destinazione 1
2690 for i=q1+1 to 1 step -1:p1(i)=p1(i-1):next i:rem scalo in basso i pesi
2700 p1(1)=p2:p1=p2:q1=q1+1
2710 rem aggiorno presenza dischi pila di partenza 2
2720 if q2>1 then:for i=1 to q2-1:p2(i)=p2(i+1):next i:rem scalo in alto i pesi
2730 p2(q2)=0:q2=q2-1
2740 if q2>=1 then p2=p2(1)
2750 if q2=0 then p2(1)=0:p2=0
2760 if gm$="g" then 3600
2770 return
2780 rem *******************************************
2790 rem * spostamento disco da piolo 2 a piolo 3  *
2800 rem *******************************************
2810 for y=peek(v+1+(2*(p2-1))) to y0 step -2
2820 poke (v+1+(2*(p2-1))),y:rem sollevo disco
2830 next y
2840 for x=peek(v+(2*(p2-1))) to x3 step 2
2850 poke (v+(2*(p2-1))),x:rem sposto disco a sx
2860 next x
2870 rem appoggio disco:
2880 rem y1= coord y di partenza  y2 = coord y di arrivo dello sprite
2890 y1=peek(v+1+(2*(p2-1))):rem altrimenti y di arrivo e' appena sopra disco
2900 if p3=0 then y2=y9:goto 2920
2910 y2=y9-(ad*q3)
2920 for y=y1 to y2 step 2
2930 poke (v+1+(2*(p2-1))),y
2940 next y
2950 gosub 4210:rem riproduci suono
2960 rem aggiorno peso pila di destinazione 3
2970 for i=q3+1 to 1 step -1:p3(i)=p3(i-1):next i:rem scalo in basso i pesi
2980 p3(1)=p2:p3=p2:q3=q3+1
2990 rem aggiorno presenza dischi pila di partenza 2
3000 if q2>1 then:for i=1 to q2-1:p2(i)=p2(i+1):next i:rem scalo in alto i pesi
3010 p2(q2)=0:q2=q2-1
3020 if q2>=1 then p2=p2(1)
3030 if q2=0 then p2(1)=0:p2=0
3040 if gm$="g" then 3600
3050 return
3060 rem ******************************************
3070 rem * spostamento disco da piolo 3 a piolo 1 *
3080 rem ******************************************
3090 for y=peek(v+1+(2*(p3-1))) to y0 step -2
3100 poke (v+1+(2*(p3-1))),y:rem sollevo disco
3110 next y
3120 for x=peek(v+(2*(p3-1))) to x1 step -2
3130 poke (v+(2*(p3-1))),x:rem sposto disco a dx
3140 next x
3150 rem appoggio disco:
3160 rem y1= y di partenza  y2 = y di arrivo
3170 y1=peek(v+1+(2*(p3-1)))
3180 if p1=0 then y2=y9:goto 3200
3190 y2=y9-(ad*q1):rem altrimenti y di arrivo e' appena sopra disco
3200 for y=y1 to y2 step 2
3210 poke (v+1+(2*(p3-1))),y
3220 next y
3230 gosub 4210:rem riproduci suono
3240 rem aggiorno peso pila di destinazione 1
3250 for i=q1+1 to 1 step -1:p1(i)=p1(i-1):next i:rem scalo in basso i pesi
3260 p1(1)=p3:p1=p3:q1=q1+1
3270 rem aggiorno presenza dischi pila di partenza 3
3280 if q3>1 then:for i=1 to q3-1:p3(i)=p3(i+1):next i:rem scalo in alto i pesi
3290 p3(q3)=0:q3=q3-1
3300 if q3>=1 then p3=p3(1)
3310 if q3=0 then p3(1)=0:p3=0
3320 if gm$="g" then 3600
3330 return
3340 rem ******************************************
3350 rem * spostamento disco da piolo 3 a piolo 2 *
3360 rem ******************************************
3370 for y=peek(v+1+(2*(p3-1))) to y0 step -2
3380 poke (v+1+(2*(p3-1))),y:rem sollevo disco
3390 next y
3400 for x=peek(v+(2*(p3-1))) to x2 step -2
3410 poke (v+(2*(p3-1))),x:rem sposto disco a sx
3420 next x
3430 rem appoggio disco:
3440 rem y1= y di partenza  y2 = y di arrivo
3450 y1=peek(v+1+(2*(p3-1)))
3460 if p2=0 then y2=y9:goto 3480
3470 y2=y9-(ad*q2):rem altrimenti y di arrivo e' appena sopra disco
3480 for y=y1 to y2 step 2
3490 poke (v+1+(2*(p3-1))),y
3500 next y
3510 gosub 4210:rem riproduci suono
3520 rem aggiorno peso pila di destinazione 2
3530 for i=q2+1 to 1 step -1:p2(i)=p2(i-1):next i:rem scalo in basso i pesi
3540 p2(1)=p3:p2=p3:q2=q2+1
3550 rem aggiorno presenza dischi pila di partenza 3
3560 if q3>1 then:for i=1 to q3-1:p3(i)=p3(i+1):next i:rem scalo in alto i pesi
3570 p3(q3)=0:q3=q3-1
3580 if q3>=1 then p3=p3(1)
3590 if q3=0 then p3(1)=0:p3=0
3600 poke56145,1:poke56155,1:poke56165,1:if gm$="s" then return
3610 rem debug
3620 if q3<>d then 3760:rem ho finito il gioco (q3=d ?)
3630 rem print"{clear}":gosub 3500:poke 646,5:poke 53280,5
3640 print "{home}{space*32}":poke 646,5:poke 53280,5
3650 print"{up*24}{down}{space*3}complimenti,hai risolto il gioco!"
3660 if(m=mm) then print "{space*3}nel numero minimo di mosse ("str$(mm)")"
3670 poke 646,1:if(m<>mm) then print "{space*3}hai fatto"str$(m)" mosse su"str$(mm)" previste"
3675 PRINT "{home}{down*4}"SPC(9)"{space*21}"
3680 poke 1872,32:poke 1874,32
3690 poke 1882,32:poke 1884,32
3700 poke 1892,32:poke 1894,32
3710 poke 1919,62:poke 1927,60
3720 gosub 4970:rem victory sound
3730 if(peek(56320) and 16)=0 goto 410
3740 goto 3730
3750 print "{clear}":poke 53269,0:end
3760 m=m+1:rem incremento mosse fatte
3770 goto 1340
3780 rem disegno i pioli e la base
3790 print "{down*5}":poke 646,1
3800 for i=1 to 13
3810 print "{space*9}{reverse on} {reverse off}{space*9}{reverse on} {reverse off}{space*9}{reverse on} "
3820 next i
3830 rem print "{space*2}{reverse on}{space*36}"
3840 print "{space*2}{reverse on}{space*36}"
3850 print "{space*2}{reverse on}{space*6}{reverse off} 1 {reverse on}{space*7}{reverse off} 2 {reverse on}{space*7}{reverse off} 3 {reverse on}{space*7}"
3860 print "{space*2}{reverse on}{space*13}{reverse off} termina {reverse on}{space*14}"
3870 print "{space*2}{reverse on} sposta tutti i dischi sul piolo #3 "
3880 return
3890 rem ***********************
3900 rem * soluzione ricorsiva:*
3910 rem ***********************
3920 poke 1919,62:poke 1927,60:rem per terminare premi fuoco su >termina<
3930 n=d:sp=49151:a=1:b=2:c=3:m=0
3940 rem salvo le variabili nello stack a partire dalla locazione 49152:
3950 sp=sp+1:poke sp,n:sp=sp+1:poke sp,a:sp=sp+1:poke sp,b:sp=sp+1:poke sp,c
3960 gosub 3980:rem 1a chiamata  hanoi($n, $a, $b ,$c);
3970 goto 3620
3980 if n>1 then 4060:rem ramo1:e' come dire if n=1 esegui le righe seguenti
3990 m=m+1:a$=mid$(str$(a),2,1):b$=mid$(str$(c),2,1):pi=a:pf=c
4000 print "{home}{down*2}mossa "str$(m)": #"a$" -> #"b$:gosub 1730
4010 rem ritorno dalla ricorsione,ripristino le variabili dallo stack
4020 c=peek(sp):sp=sp-1:b=peek(sp):sp=sp-1:a=peek(sp):sp=sp-1:n=peek(sp):sp=sp-1
4030 rem print "ritorno dalla ricorsione ramo 1"
4040 return
4050 rem else (n>1): ramo 2
4060 n=n-1
4070 rem salvo le variabili nello stack:
4080 sp=sp+1:poke sp,n:sp=sp+1:poke sp,a:sp=sp+1:poke sp,b:sp=sp+1:poke sp,c
4090 x=b:b=c:c=x:rem scambio b con c  hanoi($n, $a, $c ,$b);
4100 gosub 3980
4110 m=m+1:a$=mid$(str$(a),2,1):b$=mid$(str$(c),2,1):pi=a:pf=c
4120 print "{home}{down*2}mossa "str$(m)": #"a$" -> #"b$:gosub 1730
4130 rem salvo le variabili nello stack:
4140 sp=sp+1:poke sp,n:sp=sp+1:poke sp,a:sp=sp+1:poke sp,b:sp=sp+1:poke sp,c
4150 x=a:a=b:b=x:rem scambio a con b  hanoi($n, $b, $a ,$c);
4160 gosub 3980
4170 rem ritorno dalla ricorsione,ripristino le variabili dallo stack
4180 c=peek(sp):sp=sp-1:b=peek(sp):sp=sp-1:a=peek(sp):sp=sp-1:n=peek(sp):sp=sp-1
4190 rem print "ritorno dalla ricorsione ramo 2"
4200 return
4210 rem *** suono dischi ***
4220 poke54296,15
4230 poke54277,0
4240 poke54278,240
4250 poke54272,100
4260 poke54273,20
4270 poke54276,129
4280 fort=1to20:next
4290 poke54276,128
4300 if gm$="s" and (peek(56320) and 16)=0 then 410
4310 return
4320 rem *******************
4330 rem
4340 data 0,0,0,0,0,0,0,0,0,0
4350 data 0,0,0,0,0,0,0,0,0,0
4360 data 0,0,0,0,0,0,0,0,0,0
4370 data 0,0,0,0,0,0,0,0,0,0
4380 data 0,0,0,0,0,0,247,0,1,231
4390 data 128,1,247,128,1,247,128,1,247,128
4400 data 0,255,0
4410 rem
4420 data 0,0,0,0,0,0,0,0,0,0
4430 data 0,0,0,0,0,0,0,0,0,0
4440 data 0,0,0,0,0,0,0,0,0,0
4450 data 0,0,0,0,0,0,0,0,0,0
4460 data 0,0,0,0,0,1,231,128,3,219
4470 data 192,3,247,192,3,239,192,3,195,192
4480 data 1,255,128
4490 rem
4500 data 0,0,0,0,0,0,0,0,0,0
4510 data 0,0,0,0,0,0,0,0,0,0
4520 data 0,0,0,0,0,0,0,0,0,0
4530 data 0,0,0,0,0,0,0,0,0,0
4540 data 0,0,0,0,0,3,231,192,7,219
4550 data 224,7,247,224,7,219,224,7,231,224
4560 data 3,255,192
4570 rem
4580 data 0,0,0,0,0,0,0,0,0,0
4590 data 0,0,0,0,0,0,0,0,0,0
4600 data 0,0,0,0,0,0,0,0,0,0
4610 data 0,0,0,0,0,0,0,0,0,0
4620 data 0,0,0,0,0,7,247,224,15,239
4630 data 240,15,215,240,15,195,240,15,247,240
4640 data 7,255,224
4650 rem
4660 data 0,0,0,0,0,0,0,0,0,0
4670 data 0,0,0,0,0,0,0,0,0,0
4680 data 0,0,0,0,0,0,0,0,0,0
4690 data 0,0,0,0,0,0,0,0,0,0
4700 data 0,0,0,0,0,15,199,240,31,223
4710 data 248,31,199,248,31,251,248,31,199,248
4720 data 15,255,240
4730 rem
4740 data 0,0,0,0,0,0,0,0,0,0
4750 data 0,0,0,0,0,0,0,0,0,0
4760 data 0,0,0,0,0,0,0,0,0,0
4770 data 0,0,0,0,0,0,0,0,0,0
4780 data 0,0,0,0,0,31,231,248,63,223
4790 data 252,63,199,252,63,219,252,63,231,252
4800 data 31,255,248
4810 rem
4820 data 0,0,0,0,0,0,0,0,0,0
4830 data 0,0,0,0,0,0,0,0,0,0
4840 data 0,0,0,0,0,0,0,0,0,0
4850 data 0,0,0,0,0,0,0,0,0,0
4860 data 0,0,0,0,0,63,195,252,127,251
4870 data 254,127,247,254,127,239,254,127,239,254
4880 data 63,255,252
4890 rem
4900 data 0,0,0,0,0,0,0,0,0,0
4910 data 0,0,0,0,0,0,0,0,0,0
4920 data 0,0,0,0,0,0,0,0,0,0
4930 data 0,0,0,0,0,0,0,0,0,0
4940 data 0,0,0,0,0,127,231,254,255,219
4950 data 255,255,231,255,255,219,255,255,231,255
4960 data 127,255,254
4970 rem ********* victory sound ********
4980 q=54272:for i=0 to 24:poke q+i,0:next i:poke q+24,15
4990 poke q+5,15:poke q+6,240
5000 poke q+1,25:poke q+0,243:poke q+4,17
5010 for t=1 to 50:next t
5020 poke q+4,16:for t=1 to 15:next t
5030 poke q+1,25:poke q+0,243:poke q+4,17
5040 for t=1 to 50:next t
5050 poke q+4,16:for t=1 to 15:next t
5060 poke q+1,25:poke q+0,243:poke q+4,17
5070 for t=1 to 50:next t
5080 poke q+4,16:for t=1 to 15:next t
5090 poke q+1,34:poke q+0,164:poke q+4,17
5100 for t=1 to 180:next t
5110 poke q+4,16:for t=1 to 15:next t
5120 poke q+1,51:poke q+0,230:poke q+4,17
5130 for t=1 to 350:next t
5140 poke q+4,16
5150 return
5160 rem ************ suono bleep **************
5170 poke 54296,15
5180 poke 54277,17
5190 poke 54278,150
5200 poke 54273,100
5210 poke 54272,100
5220 poke 54276,17
5230 for t=1 to 12:next t
5240 poke 54273,70
5250 poke 54272,100
5260 for t=1 to 12:next t
5270 poke 54273,40
5280 poke 54272,100
5290 for t=1 to 18:next t
5300 poke 54276,16
5310 poke 54276,0
5320 return