!--------------------------------------------------
!- venerdì 14 agosto 2026 11:03:49
!- Import of : HANOI-11.PRG
!- From Disk : c:\users\alessandro\documents\d64\hanoi-tower.d64
!- Commodore 64
!--------------------------------------------------
10 rem ***************     la torre di hanoy        ***************
40 rem ***************   by alessandro scola 2024   ***************
70 rem ***************   www.alessandroscola.com    ***************
100 rem ************************************************************************
130 rem ********************    variabili  principali  *************************
160 rem sd       :sprite definiti (si/no)
190 rem dm       :numero dichi massimi che il programma gestisce
220 rem d        :numero dischi scelti dal giocatore per la partita
250 rem mm       :numero mosse minime per vincere = 2^d-1
280 rem p1       :peso disco in alto su piolo 1
310 rem p2       :peso disco in alto su piolo 2
340 rem p3       :peso disco in alto su piolo 3
370 rem p1(dm)   :situazione pesi dischi su piolo 1
400 rem p2(dm)   :situazione pesi dischi su piolo 2
430 rem p3(dm)   :situazione pesi dischi su piolo 3
460 rem q1,q2,q3 :numero dischi presenti su ogni piolo
490 rem x1,x2,x3 :coordinate x degli sprites su ogni piolo
520 rem y0       :coordinata y minima sprites (il punto piu in alto)
550 rem y9       :coordinata y massima sprites (il punto piu in basso)
580 rem ad       :altezza sprite di un singolo disco in pixels
610 rem sn       :sprite number
640 rem sp       :stack pointer (per la soluzione ricorsiva)
670 rem v        :registro del vic di partenza per gestion sprites
700 rem b        :banco memoria di partenza per gli sprite
730 rem ************************************************************************
760 rem ************************************************************************
790 sd=0:rem sprites non ancora definiti
820 dm=8:rem fino a 8 dischi massimo
850 dim p1(dm):rem stato presenza dei pioli su piolo 1, da 1 a 7
880 dim p2(dm):rem stato presenza dei pioli su piolo 2, da 1 a 7
910 dim p3(dm):rem stato presenza dei pioli su piolo 3, da 1 a 7
940 x1=76:x2=156:x3=236:rem coordinate x degli sprites su pioli
970 y0=60:rem y mimina (punto piu in alto quando muovo disco)
1000 y9=168:rem y massima (coord y pputno di appoggio dei dischi al piano)
1030 ad=12:rem altezza disco in pixels(sprite espanso =6 x 2 = 12)
1060 v=53248:rem registro del vic di partenza per gestion sprites
1090 rem ************************************************************************
1120 poke 53280,15:poke 53281,0:poke 646,1:rem colore sfondo,bordo scritte
1150 poke 53269,0:rem spegne tutti gli sprites eventualmente visibili
1180 if peek(44)=64 then 1450
1210 print "{clear}attenzione!!"
1240 print "{down}per eseguire questo programma"
1270 print "devi prima eseguire il seguente comando:"
1300 print "{down}{red}poke 43,1:poke 44,64:poke 16384,0{down*2}"
1330 print "{white}poi ricarica il programma da disco"
1360 print "con ,8 e non con ,8,1{space*3}!! "
1390 print "{down}alla fine eseguilo con il comando run"
1420 end
1450 rem ***********************************************************************
1480 print "{clear}"spc(9)"{down*3}{red}la{space*2}torre{space*2}di{space*2}hanoi"
1510 print spc(6)"{down*4}{cyan}by alessandro scola (c)2024"
1540 print spc(8)"{white}www.alessandroscola.com"
1570 print spc(6)"{down*2}{blue}gioca alla torre di hanoi"
1600 print spc(6)"o{space*2}falla risolvere al tuo"
1630 print spc(6)"commodore 64 con il metodo"
1660 print spc(6)"ricorsivo."
1690 if sd=1 goto 2080
1720 rem ***************************
1750 rem * ridefinisce gli sprites *
1780 rem ***************************
1810 print "{down*4}{yellow}{space*2}inizializzazione sprites, attendi..."
1840 b=245:rem banco memoria di partenza per gli sprite 245
1870 rem legge i data per i dischi che utilizzero
1900 for sn=0 to dm-1
1930 poke 1862,56-sn:rem visualizza conto alla rovescia
1960 poke 2040+sn,b+sn:rem setta banco di memoria per ogni sprite
1990 for i=0 to 62
2020 read a:poke 64*b+(sn*64)+i,a:next i
2050 next sn
2080 sd=1:rem sprites definiti
2090 print "{yellow}{up*3}""
2110 print "{yellow}{down*1}{space*3}premi fire in porta 2 per giocare{space*3}"
2140 if(peek(56320) and 16)=0 goto 2200:rem attente pressione fire
2170 goto 2140
2200 poke 53269,0:poke 53280,15:poke 53281,0:poke 646,1:rem colore sfondo,bordo scritte
2230 d=2:print"{clear}{down*11}{right*9}{green}quanti dischi ? (1-8)"
2260 print "{yellow}{right*11}{down*3}(joystick up/down)"
2290 print"{home}{down*11}{right*31}{space*2}<{up}"
2320 print"{home}{down*10}":print tab(31);d:j=peek(56320)
2350 if(d<=7 and (j and 1)=0) then d=d+1
2380 if(d>=2 and (j and 2)=0) then d=d-1
2410 if(j and 16)=0 then 2500
2440 for i=1 to 25:next i
2470 poke 56320,255:goto 2320
2500 gm$="g":print "{clear}"
2530 poke 56320,255
2560 if(gm$="g") then print"{home}{down*9}{space*11}{yellow}>{reverse on}{space*4}giocare{space*5}{reverse off}<"
2590 if(gm$="g") then print"{home}{down*11}{space*11}{green}>vedere soluzione<"
2620 if(gm$="s") then print"{home}{down*9}{space*11}{green}>{space*4}giocare{space*5}<"
2650 if(gm$="s") then print"{home}{down*11}{space*11}{yellow}>{reverse on}vedere soluzione{reverse off}<"
2680 print "{yellow}{right*11}{down*3}(joystick up/down)"
2710 poke 56320,255:j=peek(56320)
2740 if(gm$="s" and (j and 1)=0) then gm$="g":goto 2530
2770 if(gm$="g" and (j and 2)=0) then gm$="s":goto 2530
2800 if(j and 16)=0 then 2890
2830 for i=1 to 50:next i
2860 goto 2710
2890 rem
2920 rem memorizza in un array lo stato presenza dischi pesati su piolo 1
2950 for i=1 to d:p1(i)=i:next i
2980 p1=1:p2=0:p3=0:rem peso del disco in alto su un dato piolo
3010 q1=d:q2=0:q3=0:rem q1, q2, q3 = quanti dischi ha ciascun piolo
3040 rem posiziona gli sprites dei dischi uno sull'altro su piolo 1
3070 rem disegna la base del gioco
3100 i=0:rem variabile offset resgistri sprites
3130 oc=0: rem offfset colore per saltare il numero 8
3160 for sn=d-1 to 0 step -1:rem setta posizione x e y dischi
3190 poke v+(sn*2),x1:i=i+1:poke v+((sn*2)+1),y9-((i-1)*ad/2)
3220 i=i+1
3250 if (sn+4)=7 then oc=1:rem per saltare il colore giallo
3280 poke 53287+sn,sn+4-oc:rem setta i colori dei dischi
3310 next sn
3340 rem abilita sprites e espandili in larghezza e altezza
3370 va=0:for n=0 to d-1:va=va+(2^n):next
3400 poke 53269,va:rem abilita sprites
3430 poke 53277,va:rem espansione in larghezza
3460 poke 53271,va:rem espansione in altezza
3490 rem *************************************************
3520 rem *************************************************
3550 mm=2^d-1:m=1:rem mm=numero mosse minime x risolvere, m=numero mossa
3580 rem sposta il disco piu in alto dal piolo selezionato
3610 print "{clear}{cyan}si risolve in "+str$(mm)+" mosse.{white}"
3640 poke 646,1:poke 53280,15:print "{home}{cyan}si risolve in "+str$(mm)+" mosse.{white}"
3670 gosub 10150:rem disegna i pioli e la base
3700 if gm$="s" then 10420
3730 rem gm$="g" giocare
3760 pi=0:pf=0:pc=1:rem piolo inizio,fine,corrente
3790 poke 646,1:poke 214,2:sys 58732:print "fai la mossa n."+str$(m)
3820 pi=0:pf=0:rem piolo inizio,fine
3850 if(pc=1) then poke 1872,62:poke1874,60
3880 if(pc=1) then poke 1882,32:poke1884,32
3910 if(pc=1) then poke 1892,32:poke1894,32
3940 if(pc=2) then poke 1872,32:poke1874,32
3970 if(pc=2) then poke 1882,62:poke1884,60
4000 if(pc=2) then poke 1892,32:poke1894,32
4030 if(pc=3) then poke 1872,32:poke1874,32
4060 if(pc=3) then poke 1882,32:poke1884,32
4090 if(pc=3) then poke 1892,62:poke1894,60
4120 j=peek(56320):if(j and 4)=0 and pc>1 then pc=pc-1:rem joy a sinistra
4150 if(j and 8)=0 and pc<3 then pc=pc+1:rem joy a destra
4180 if(j and 16)=0 and pc=1 and pi=0 then pi=1:goto4390:rem seleziono p1
4210 if(j and 16)=0 and pc=1 and pi=1 then pi=0:rem deseleziono p1
4240 if(j and 16)=0 and pc=2 and pi=0 then pi=2:goto4390:rem seleziono p2
4270 if(j and 16)=0 and pc=2 and pi=2 then pi=0:goto4390:rem deseleziono p2
4300 if(j and 16)=0 and pc=3 and pi=0 then pi=3:goto4390:rem seleziono p3
4330 if(j and 16)=0 and pc=3 and pi=3 then pi=0:goto4390:rem deseleziono p3
4360 if(j and 16)=0 and pi<>0 and pc<>pi then pf=pc:goto 4630
4390 if(pi=0) then poke56145,1:poke56155,1:poke56165,1
4420 if(pi=1) then poke56145,4
4450 if(pi=2) then poke56155,4
4480 if(pi=3) then poke56165,4
4510 goto 3850
4540 rem if a$="f" then 9910
4570 rem if b$="f" then 9940
4630 if pi=1 and pf=2 and q1>0 and (p2=0 or p2>p1) then 4900
4660 if pi=1 and pf=3 and q1>0 and (p3=0 or p3>p1) then 5740
4690 if pi=2 and pf=1 and q2>0 and (p1=0 or p1>p2) then 6550
4720 if pi=2 and pf=3 and q2>0 and (p3=0 or p3>p2) then 7360
4750 if pi=3 and pf=1 and q3>0 and (p1=0 or p1>p3) then 8170
4780 if pi=3 and pf=2 and q3>0 and (p2=0 or p2>p3) then 8980
4810 poke 53280,2:print "{home}{down*4}{right*9}{red}* mossa non possibile *""
4840 FOR I=1 TO 255:NEXT I:FOR I=1 TO 255:NEXT I
4870 print "{home}{down*4}{right*9}{red}                       "":GOTO 3550
4900 REM ******************************************
4930 REM * SPOSTAMENTO DISCO DA PIOLO 1 A PIOLO 2 *
4960 REM ******************************************
4990 REM P1 = VALORE DEL DISCO PIU IN ALTO,CHE CORRISPONDE ALLO SPRITES P1-1
5020 FOR Y=PEEK(V+1+(2*(P1-1))) TO Y0 STEP -2
5050 POKE (V+1+(2*(P1-1))),Y:REM SOLLEVO DISCO
5080 NEXT Y
5110 FOR X=PEEK(V+(2*(P1-1))) TO X2 STEP 2
5140 POKE (V+(2*(P1-1))),X:REM SPOSTO DISCO A DX
5170 NEXT X
5200 REM APPOGGIO DISCO:
5230 REM Y1= COORD Y DI PARTENZA  Y2 = COORD Y DI ARRIVO DELLO SPRITE
5260 Y1=PEEK(V+1+(2*(P1-1))):REM ALTRIMENTI Y DI ARRIVO E' APPENA SOPRA DISCO
5290 IF P2=0 THEN Y2=Y9:GOTO 5350
5320 Y2=Y9-(AD*Q2)
5350 FOR Y=Y1 TO Y2 STEP 2
5380 POKE (V+1+(2*(P1-1))),Y
5410 NEXT Y
5440 REM AGGIORNO PESO PILA DI DESTINAZIONE 2
5470 FOR I=Q2+1 TO 1 STEP -1:P2(I)=P2(I-1):NEXT I:REM SCALO IN ALTO I PESI
5500 P2(1)=P1:P2=P1:Q2=Q2+1
5530 REM AGGIORNO PRESENZA DISCHI PILA DI PARTENZA 1
5560 IF Q1>1 THEN:FOR I=1 TO Q1-1:P1(I)=P1(I+1):NEXT I:REM SCALO IN BASSO I PESI
5590 P1(Q1)=0:Q1=Q1-1
5620 IF Q1>=1 THEN P1=P1(1)
5650 IF Q1=0 THEN P1(1)=0:P1=0
5680 IF GM$="g" THEN 9730
5710 RETURN:REM ELSE IF GM$="s"
5740 REM ******************************************
5770 REM * SPOSTAMENTO DISCO DA PIOLO 1 A PIOLO 3 *
5800 REM ******************************************
5830 FOR Y=PEEK(V+1+(2*(P1-1))) TO Y0 STEP -2
5860 POKE (V+1+(2*(P1-1))),Y:REM SOLLEVO DISCO
5890 NEXT Y
5920 FOR X=PEEK(V+(2*(P1-1))) TO X3 STEP 2
5950 POKE (V+(2*(P1-1))),X:REM SPOSTO DISCO A DX
5980 NEXT X
6010 REM APPOGGIO DISCO:
6040 REM Y1= Y DI PARTENZA  Y2 = Y DI ARRIVO
6070 Y1=PEEK(V+1+(2*(P1-1)))
6100 IF P3=0 THEN Y2=Y9:GOTO 6160
6130 Y2=Y9-(AD*Q3):REM ALTRIMENTI Y DI ARRIVO E' APPENA SOPRA DISCO
6160 FOR Y=Y1 TO Y2 STEP 2
6190 POKE (V+1+(2*(P1-1))),Y
6220 NEXT Y
6250 REM AGGIORNO PESO PILA DI DESTINAZIONE 3
6280 FOR I=Q3+1 TO 1 STEP -1:P3(I)=P3(I-1):NEXT I:REM SCALO IN BASSO I PESI
6310 P3(1)=P1:P3=P1:Q3=Q3+1
6340 REM AGGIORNO PRESENZA DISCHI PILA DI PARTENZA 1
6370 IF Q1>1 THEN:FOR I=1 TO Q1-1:P1(I)=P1(I+1):NEXT I:REM SCALO IN ALTO I PESI
6400 P1(Q1)=0:Q1=Q1-1
6430 IF Q1>=1 THEN P1=P1(1)
6460 IF Q1=0 THEN P1(1)=0:P1=0
6490 IF GM$="g" THEN 9730
6520 RETURN:REM ELSE IF GM$="s"
6550 REM *******************************************
6580 REM * SPOSTAMENTO DISCO DA PIOLO 2 A PIOLO 1  *
6610 REM *******************************************
6640 FOR Y=PEEK(V+1+(2*(P2-1))) TO Y0 STEP -2
6670 POKE (V+1+(2*(P2-1))),Y:REM SOLLEVO DISCO
6700 NEXT Y
6730 FOR X=PEEK(V+(2*(P2-1))) TO X1 STEP -2
6760 POKE (V+(2*(P2-1))),X:REM SPOSTO DISCO A SX
6790 NEXT X
6820 REM APPOGGIO DISCO:
6850 REM Y1= COORD Y DI PARTENZA  Y2 = COORD Y DI ARRIVO DELLO SPRITE
6880 Y1=PEEK(V+1+(2*(P2-1))):REM ALTRIMENTI Y DI ARRIVO E' APPENA SOPRA DISCO
6910 IF P1=0 THEN Y2=Y9:GOTO 6970
6940 Y2=Y9-(AD*Q1)
6970 FOR Y=Y1 TO Y2 STEP 2
7000 POKE (V+1+(2*(P2-1))),Y
7030 NEXT Y
7060 REM AGGIORNO PESO PILA DI DESTINAZIONE 1
7090 FOR I=Q1+1 TO 1 STEP -1:P1(I)=P1(I-1):NEXT I:REM SCALO IN BASSO I PESI
7120 P1(1)=P2:P1=P2:Q1=Q1+1
7150 REM AGGIORNO PRESENZA DISCHI PILA DI PARTENZA 2
7180 IF Q2>1 THEN:FOR I=1 TO Q2-1:P2(I)=P2(I+1):NEXT I:REM SCALO IN ALTO I PESI
7210 P2(Q2)=0:Q2=Q2-1
7240 IF Q2>=1 THEN P2=P2(1)
7270 IF Q2=0 THEN P2(1)=0:P2=0
7300 IF GM$="g" THEN 9730
7330 RETURN:REM ELSE IF GM$="s"
7360 REM *******************************************
7390 REM * SPOSTAMENTO DISCO DA PIOLO 2 A PIOLO 3  *
7420 REM *******************************************
7450 FOR Y=PEEK(V+1+(2*(P2-1))) TO Y0 STEP -2
7480 POKE (V+1+(2*(P2-1))),Y:REM SOLLEVO DISCO
7510 NEXT Y
7540 FOR X=PEEK(V+(2*(P2-1))) TO X3 STEP 2
7570 POKE (V+(2*(P2-1))),X:REM SPOSTO DISCO A SX
7600 NEXT X
7630 REM APPOGGIO DISCO:
7660 REM Y1= COORD Y DI PARTENZA  Y2 = COORD Y DI ARRIVO DELLO SPRITE
7690 Y1=PEEK(V+1+(2*(P2-1))):REM ALTRIMENTI Y DI ARRIVO E' APPENA SOPRA DISCO
7720 IF P3=0 THEN Y2=Y9:GOTO 7780
7750 Y2=Y9-(AD*Q3)
7780 FOR Y=Y1 TO Y2 STEP 2
7810 POKE (V+1+(2*(P2-1))),Y
7840 NEXT Y
7870 REM AGGIORNO PESO PILA DI DESTINAZIONE 3
7900 FOR I=Q3+1 TO 1 STEP -1:P3(I)=P3(I-1):NEXT I:REM SCALO IN BASSO I PESI
7930 P3(1)=P2:P3=P2:Q3=Q3+1
7960 REM AGGIORNO PRESENZA DISCHI PILA DI PARTENZA 2
7990 IF Q2>1 THEN:FOR I=1 TO Q2-1:P2(I)=P2(I+1):NEXT I:REM SCALO IN ALTO I PESI
8020 P2(Q2)=0:Q2=Q2-1
8050 IF Q2>=1 THEN P2=P2(1)
8080 IF Q2=0 THEN P2(1)=0:P2=0
8110 IF GM$="g" THEN 9730
8140 RETURN:REM ELSE IF GM$="s"
8170 REM ******************************************
8200 REM * SPOSTAMENTO DISCO DA PIOLO 3 A PIOLO 1 *
8230 REM ******************************************
8260 FOR Y=PEEK(V+1+(2*(P3-1))) TO Y0 STEP -2
8290 POKE (V+1+(2*(P3-1))),Y:REM SOLLEVO DISCO
8320 NEXT Y
8350 FOR X=PEEK(V+(2*(P3-1))) TO X1 STEP -2
8380 POKE (V+(2*(P3-1))),X:REM SPOSTO DISCO A DX
8410 NEXT X
8440 REM APPOGGIO DISCO:
8470 REM Y1= Y DI PARTENZA  Y2 = Y DI ARRIVO
8500 Y1=PEEK(V+1+(2*(P3-1)))
8530 IF P1=0 THEN Y2=Y9:GOTO 8590
8560 Y2=Y9-(AD*Q1):REM ALTRIMENTI Y DI ARRIVO E' APPENA SOPRA DISCO
8590 FOR Y=Y1 TO Y2 STEP 2
8620 POKE (V+1+(2*(P3-1))),Y
8650 NEXT Y
8680 REM AGGIORNO PESO PILA DI DESTINAZIONE 1
8710 FOR I=Q1+1 TO 1 STEP -1:P1(I)=P1(I-1):NEXT I:REM SCALO IN BASSO I PESI
8740 P1(1)=P3:P1=P3:Q1=Q1+1
8770 REM AGGIORNO PRESENZA DISCHI PILA DI PARTENZA 3
8800 IF Q3>1 THEN:FOR I=1 TO Q3-1:P3(I)=P3(I+1):NEXT I:REM SCALO IN ALTO I PESI
8830 P3(Q3)=0:Q3=Q3-1
8860 IF Q3>=1 THEN P3=P3(1)
8890 IF Q3=0 THEN P3(1)=0:P3=0
8920 IF GM$="g" THEN 9730
8950 RETURN:REM ELSE IF GM$="s"
8980 REM ******************************************
9010 REM * SPOSTAMENTO DISCO DA PIOLO 3 A PIOLO 2 *
9040 REM ******************************************
9070 FOR Y=PEEK(V+1+(2*(P3-1))) TO Y0 STEP -2
9100 POKE (V+1+(2*(P3-1))),Y:REM SOLLEVO DISCO
9130 NEXT Y
9160 FOR X=PEEK(V+(2*(P3-1))) TO X2 STEP -2
9190 POKE (V+(2*(P3-1))),X:REM SPOSTO DISCO A SX
9220 NEXT X
9250 REM APPOGGIO DISCO:
9280 REM Y1= Y DI PARTENZA  Y2 = Y DI ARRIVO
9310 Y1=PEEK(V+1+(2*(P3-1)))
9340 IF P2=0 THEN Y2=Y9:GOTO 9400
9370 Y2=Y9-(AD*Q2):REM ALTRIMENTI Y DI ARRIVO E' APPENA SOPRA DISCO
9400 FOR Y=Y1 TO Y2 STEP 2
9430 POKE (V+1+(2*(P3-1))),Y
9460 NEXT Y
9490 REM AGGIORNO PESO PILA DI DESTINAZIONE 2
9520 FOR I=Q2+1 TO 1 STEP -1:P2(I)=P2(I-1):NEXT I:REM SCALO IN BASSO I PESI
9550 P2(1)=P3:P2=P3:Q2=Q2+1
9580 REM AGGIORNO PRESENZA DISCHI PILA DI PARTENZA 3
9610 IF Q3>1 THEN:FOR I=1 TO Q3-1:P3(I)=P3(I+1):NEXT I:REM SCALO IN ALTO I PESI
9640 P3(Q3)=0:Q3=Q3-1
9670 IF Q3>=1 THEN P3=P3(1)
9700 IF Q3=0 THEN P3(1)=0:P3=0
9730 IF GM$="s" THEN RETURN
9760 REM DEBUG
9790 IF Q3<>D THEN 10060:REM HO FINITO IL GIOCO (Q3=D ?)
9820 PRINT"{clear}":GOSUB 10150:POKE 646,5:POKE 53280,5
9850 PRINT"{up*24}{down}{space*3}complimenti,hai risolto il gioco!"
9880 IF(M=MM) THEN PRINT "{space*3}nel numero minimo di mosse ("STR$(MM)")"
9910 POKE 646,1:IF(M<>MM) THEN PRINT "{space*3}hai fatto"STR$(M)" mosse su"STR$(MM)" previste"
9940 IF(PEEK(56320) AND 16)=0 GOTO 1120
9970 GOTO 9940
10000 IF A$="s" THEN 2290
10030 PRINT "{clear}":POKE 53269,0:END
10060 M=M+1:REM INCREMENTO MOSSE FATTE
10090 GOTO 3790
10120 REM DISEGNO I PIOLI E LA BASE
10150 PRINT "{down*5}":POKE 646,1
10180 FOR I=1 TO 13
10210 PRINT "{space*9}{reverse on} {reverse off}{space*9}{reverse on} {reverse off}{space*9}{reverse on} "
10240 NEXT I
10270 PRINT "{space*2}{reverse on}{space*36}"
10300 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}"
10330 PRINT "{space*2}{reverse on}{space*36}"
10360 RETURN
10390 REM ***********************
10420 REM * SOLUZIONE RICORSIVA:*
10450 REM ***********************
10480 N=D:SP=49151:A=1:B=2:C=3:M=0
10510 REM SALVO LE VARIABILI NELLO STACK A PARTIRE DALLA LOCAZIONE 49152:
10540 SP=SP+1:POKE SP,N:SP=SP+1:POKE SP,A:SP=SP+1:POKE SP,B:SP=SP+1:POKE SP,C
10570 GOSUB 10630:REM 1A CHIAMATA  HANOI($N, $A, $B ,$C);
10600 GOTO 9790
10630 IF N>1 THEN 10870:REM RAMO1:E' COME DIRE IF N=1 ESEGUI LE RIGHE SEGUENTI
10660 M=M+1:A$=MID$(STR$(A),2,1):B$=MID$(STR$(C),2,1)
10690 PRINT "{home}{down*2}mossa "STR$(M)": #"A$" -> #"B$:GOSUB 4630
10720 REM RITORNO DALLA RICORSIONE,RIPRISTINO LE VARIABILI DALLO STACK
10750 C=PEEK(SP):SP=SP-1:B=PEEK(SP):SP=SP-1:A=PEEK(SP):SP=SP-1:N=PEEK(SP):SP=SP-1
10780 REM PRINT "ritorno dalla ricorsione ramo 1"
10810 RETURN
10840 REM ELSE (N>1): RAMO 2
10870 N=N-1
10900 REM SALVO LE VARIABILI NELLO STACK:
10930 SP=SP+1:POKE SP,N:SP=SP+1:POKE SP,A:SP=SP+1:POKE SP,B:SP=SP+1:POKE SP,C
10960 X=B:B=C:C=X:REM SCAMBIO B CON C  HANOI($N, $A, $C ,$B);
10990 GOSUB 10630
11020 M=M+1:A$=MID$(STR$(A),2,1):B$=MID$(STR$(C),2,1)
11050 PRINT "{home}{down*2}mossa "STR$(M)": #"A$" -> #"B$:GOSUB 4630
11080 REM SALVO LE VARIABILI NELLO STACK:
11110 SP=SP+1:POKE SP,N:SP=SP+1:POKE SP,A:SP=SP+1:POKE SP,B:SP=SP+1:POKE SP,C
11140 X=A:A=B:B=X:REM SCAMBIO A CON B  HANOI($N, $B, $A ,$C);
11170 GOSUB 10630
11200 REM RITORNO DALLA RICORSIONE,RIPRISTINO LE VARIABILI DALLO STACK
11230 C=PEEK(SP):SP=SP-1:B=PEEK(SP):SP=SP-1:A=PEEK(SP):SP=SP-1:N=PEEK(SP):SP=SP-1
11260 REM PRINT "ritorno dalla ricorsione ramo 2"
11290 RETURN
11320 REM
11350 DATA 0,0,0,0,0,0,0,0,0,0
11380 DATA 0,0,0,0,0,0,0,0,0,0
11410 DATA 0,0,0,0,0,0,0,0,0,0
11440 DATA 0,0,0,0,0,0,0,0,0,0
11470 DATA 0,0,0,0,0,0,247,0,1,231
11500 DATA 128,1,247,128,1,247,128,1,247,128
11530 DATA 0,255,0
11560 REM
11590 DATA 0,0,0,0,0,0,0,0,0,0
11620 DATA 0,0,0,0,0,0,0,0,0,0
11650 DATA 0,0,0,0,0,0,0,0,0,0
11680 DATA 0,0,0,0,0,0,0,0,0,0
11710 DATA 0,0,0,0,0,1,231,128,3,219
11740 DATA 192,3,247,192,3,239,192,3,195,192
11770 DATA 1,255,128
11800 REM
11830 DATA 0,0,0,0,0,0,0,0,0,0
11860 DATA 0,0,0,0,0,0,0,0,0,0
11890 DATA 0,0,0,0,0,0,0,0,0,0
11920 DATA 0,0,0,0,0,0,0,0,0,0
11950 DATA 0,0,0,0,0,3,231,192,7,219
11980 DATA 224,7,247,224,7,219,224,7,231,224
12010 DATA 3,255,192
12040 REM
12070 DATA 0,0,0,0,0,0,0,0,0,0
12100 DATA 0,0,0,0,0,0,0,0,0,0
12130 DATA 0,0,0,0,0,0,0,0,0,0
12160 DATA 0,0,0,0,0,0,0,0,0,0
12190 DATA 0,0,0,0,0,7,247,224,15,239
12220 DATA 240,15,215,240,15,195,240,15,247,240
12250 DATA 7,255,224
12280 REM
12310 DATA 0,0,0,0,0,0,0,0,0,0
12340 DATA 0,0,0,0,0,0,0,0,0,0
12370 DATA 0,0,0,0,0,0,0,0,0,0
12400 DATA 0,0,0,0,0,0,0,0,0,0
12430 DATA 0,0,0,0,0,15,199,240,31,223
12460 DATA 248,31,199,248,31,251,248,31,199,248
12490 DATA 15,255,240
12520 REM
12550 DATA 0,0,0,0,0,0,0,0,0,0
12580 DATA 0,0,0,0,0,0,0,0,0,0
12610 DATA 0,0,0,0,0,0,0,0,0,0
12640 DATA 0,0,0,0,0,0,0,0,0,0
12670 DATA 0,0,0,0,0,31,231,248,63,223
12700 DATA 252,63,199,252,63,219,252,63,231,252
12730 DATA 31,255,248
12760 REM
12790 DATA 0,0,0,0,0,0,0,0,0,0
12820 DATA 0,0,0,0,0,0,0,0,0,0
12850 DATA 0,0,0,0,0,0,0,0,0,0
12880 DATA 0,0,0,0,0,0,0,0,0,0
12910 DATA 0,0,0,0,0,63,195,252,127,251
12940 DATA 254,127,247,254,127,239,254,127,239,254
12970 DATA 63,255,252
13000 REM
13030 DATA 0,0,0,0,0,0,0,0,0,0
13060 DATA 0,0,0,0,0,0,0,0,0,0
13090 DATA 0,0,0,0,0,0,0,0,0,0
13120 DATA 0,0,0,0,0,0,0,0,0,0
13150 DATA 0,0,0,0,0,127,231,254,255,219
13180 DATA 255,255,231,255,255,219,255,255,231,255
13210 DATA 127,255,254