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