!--------------------------------------------------
!- HANOI 16    venerdì 14 agosto 2026 21:50:56
!- Import of : HANOI-16.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 3780:REM DISEGNA I PIOLI E LA BASE
1310 IF GM$="s" THEN 3890
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 5150:GOTO1670:REM SELEZIONO P1
1600 IF(J AND 16)=0 AND PC=1 AND PI=1 THEN PI=0:gosub 5150:REM DESELEZIONO P1
1610 IF(J AND 16)=0 AND PC=2 AND PI=0 THEN PI=2:gosub 5150:GOTO1670:REM SELEZIONO P2
1620 IF(J AND 16)=0 AND PC=2 AND PI=2 THEN PI=0:gosub 5150:GOTO1670:REM DESELEZIONO P2
1630 IF(J AND 16)=0 AND PC=3 AND PI=0 THEN PI=3:gosub 5150:GOTO1670:REM SELEZIONO P3
1640 IF(J AND 16)=0 AND PC=3 AND PI=3 THEN PI=0:gosub 5150: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 1720
1670 IF(PI=0) THEN POKE56145,1:POKE56155,1:POKE56165,1
1680 IF(PI=1) THEN POKE56145,5
1690 IF(PI=2) THEN POKE56155,5
1700 IF(PI=3) THEN POKE56165,5
1710 GOTO 1370
1720 IF PI=1 AND PF=2 AND Q1>0 AND (P2=0 OR P2>P1) THEN 1920
1730 IF PI=1 AND PF=3 AND Q1>0 AND (P3=0 OR P3>P1) THEN 2210
1740 IF PI=2 AND PF=1 AND Q2>0 AND (P1=0 OR P1>P2) THEN 2490
1750 IF PI=2 AND PF=3 AND Q2>0 AND (P3=0 OR P3>P2) THEN 2770
1760 IF PI=3 AND PF=1 AND Q3>0 AND (P1=0 OR P1>P3) THEN 3050
1770 IF PI=3 AND PF=2 AND Q3>0 AND (P2=0 OR P2>P3) THEN 3330
1780 POKE 53280,2:PRINT "{home}{down*4}{right*9}{red}* mossa non possibile *""
1790 rem for i=1 to 255:next i:for i=1 to 255:next i
1800 REM ERROR SOUND
1810 S=54272:FOR I=0 TO 24:POKE S+I,0:NEXT I:POKE S+24,15
1820 POKE S+1,8:POKE S+0,0:REM LOW FREQUENCY NOTE
1830 POKE S+5,0:POKE S+6,240
1840 POKE S+4,65:REM PULSE WAVE ON
1850 FOR PW=1 TO 15
1860 POKE 53280,PW
1870 POKE S+3,PW:POKE S+2,0:REM SWEEP HIGH BYTE OF PULSE WIDTH
1880 FOR T=1 TO 40:NEXT T
1890 NEXT PW
1900 POKE S+4,64:REM PULSE OFF
1910 print "{home}{down*4}{right*9}{red}{space*24}":poke 53280,15:goto 1340
1920 rem ******************************************
1930 rem * spostamento disco da piolo 1 a piolo 2 *
1940 rem ******************************************
1950 rem p1 = valore del disco piu in alto,che corrisponde allo sprites p1-1
1960 for y=peek(v+1+(2*(p1-1))) to y0 step -2
1970 poke (v+1+(2*(p1-1))),y:rem sollevo disco
1980 next y
1990 for x=peek(v+(2*(p1-1))) to x2 step 2
2000 poke (v+(2*(p1-1))),x:rem sposto disco a dx
2010 next x
2020 rem appoggio disco:
2030 rem y1= coord y di partenza  y2 = coord y di arrivo dello sprite
2040 y1=peek(v+1+(2*(p1-1))):rem altrimenti y di arrivo e' appena sopra disco
2050 if p2=0 then y2=y9:goto 2070
2060 y2=y9-(ad*q2)
2070 for y=y1 to y2 step 2
2080 poke (v+1+(2*(p1-1))),y
2090 next y
2100 gosub 4200:rem riproduci suono
2110 rem aggiorno peso pila di destinazione 2
2120 for i=q2+1 to 1 step -1:p2(i)=p2(i-1):next i:rem scalo in alto i pesi
2130 p2(1)=p1:p2=p1:q2=q2+1
2140 rem aggiorno presenza dischi pila di partenza 1
2150 if q1>1 then:for i=1 to q1-1:p1(i)=p1(i+1):next i:rem scalo in basso i pesi
2160 p1(q1)=0:q1=q1-1
2170 if q1>=1 then p1=p1(1)
2180 if q1=0 then p1(1)=0:p1=0
2190 if gm$="g" then 3590
2200 return:rem else if gm$="s"
2210 rem ******************************************
2220 rem * spostamento disco da piolo 1 a piolo 3 *
2230 rem ******************************************
2240 for y=peek(v+1+(2*(p1-1))) to y0 step -2
2250 poke (v+1+(2*(p1-1))),y:rem sollevo disco
2260 next y
2270 for x=peek(v+(2*(p1-1))) to x3 step 2
2280 poke (v+(2*(p1-1))),x:rem sposto disco a dx
2290 next x
2300 rem appoggio disco:
2310 rem y1= y di partenza  y2 = y di arrivo
2320 y1=peek(v+1+(2*(p1-1)))
2330 if p3=0 then y2=y9:goto 2350
2340 y2=y9-(ad*q3):rem altrimenti y di arrivo e' appena sopra disco
2350 for y=y1 to y2 step 2
2360 poke (v+1+(2*(p1-1))),y
2370 next y
2380 gosub 4200:rem riproduci suono
2390 rem aggiorno peso pila di destinazione 3
2400 for i=q3+1 to 1 step -1:p3(i)=p3(i-1):next i:rem scalo in basso i pesi
2410 p3(1)=p1:p3=p1:q3=q3+1
2420 rem aggiorno presenza dischi pila di partenza 1
2430 if q1>1 then:for i=1 to q1-1:p1(i)=p1(i+1):next i:rem scalo in alto i pesi
2440 p1(q1)=0:q1=q1-1
2450 if q1>=1 then p1=p1(1)
2460 if q1=0 then p1(1)=0:p1=0
2470 if gm$="g" then 3590
2480 return:rem else if gm$="s"
2490 rem *******************************************
2500 rem * spostamento disco da piolo 2 a piolo 1  *
2510 rem *******************************************
2520 for y=peek(v+1+(2*(p2-1))) to y0 step -2
2530 poke (v+1+(2*(p2-1))),y:rem sollevo disco
2540 next y
2550 for x=peek(v+(2*(p2-1))) to x1 step -2
2560 poke (v+(2*(p2-1))),x:rem sposto disco a sx
2570 next x
2580 rem appoggio disco:
2590 rem y1= coord y di partenza  y2 = coord y di arrivo dello sprite
2600 y1=peek(v+1+(2*(p2-1))):rem altrimenti y di arrivo e' appena sopra disco
2610 if p1=0 then y2=y9:goto 2630
2620 y2=y9-(ad*q1)
2630 for y=y1 to y2 step 2
2640 poke (v+1+(2*(p2-1))),y
2650 next y
2660 gosub 4200:rem riproduci suono
2670 rem aggiorno peso pila di destinazione 1
2680 for i=q1+1 to 1 step -1:p1(i)=p1(i-1):next i:rem scalo in basso i pesi
2690 p1(1)=p2:p1=p2:q1=q1+1
2700 rem aggiorno presenza dischi pila di partenza 2
2710 if q2>1 then:for i=1 to q2-1:p2(i)=p2(i+1):next i:rem scalo in alto i pesi
2720 p2(q2)=0:q2=q2-1
2730 if q2>=1 then p2=p2(1)
2740 if q2=0 then p2(1)=0:p2=0
2750 if gm$="g" then 3590
2760 return:rem else if gm$="s"
2770 rem *******************************************
2780 rem * spostamento disco da piolo 2 a piolo 3  *
2790 rem *******************************************
2800 for y=peek(v+1+(2*(p2-1))) to y0 step -2
2810 poke (v+1+(2*(p2-1))),y:rem sollevo disco
2820 next y
2830 for x=peek(v+(2*(p2-1))) to x3 step 2
2840 poke (v+(2*(p2-1))),x:rem sposto disco a sx
2850 next x
2860 rem appoggio disco:
2870 rem y1= coord y di partenza  y2 = coord y di arrivo dello sprite
2880 y1=peek(v+1+(2*(p2-1))):rem altrimenti y di arrivo e' appena sopra disco
2890 if p3=0 then y2=y9:goto 2910
2900 y2=y9-(ad*q3)
2910 for y=y1 to y2 step 2
2920 poke (v+1+(2*(p2-1))),y
2930 next y
2940 gosub 4200:rem riproduci suono
2950 rem aggiorno peso pila di destinazione 3
2960 for i=q3+1 to 1 step -1:p3(i)=p3(i-1):next i:rem scalo in basso i pesi
2970 p3(1)=p2:p3=p2:q3=q3+1
2980 rem aggiorno presenza dischi pila di partenza 2
2990 if q2>1 then:for i=1 to q2-1:p2(i)=p2(i+1):next i:rem scalo in alto i pesi
3000 p2(q2)=0:q2=q2-1
3010 if q2>=1 then p2=p2(1)
3020 if q2=0 then p2(1)=0:p2=0
3030 if gm$="g" then 3590
3040 return:rem else if gm$="s"
3050 rem ******************************************
3060 rem * spostamento disco da piolo 3 a piolo 1 *
3070 rem ******************************************
3080 for y=peek(v+1+(2*(p3-1))) to y0 step -2
3090 poke (v+1+(2*(p3-1))),y:rem sollevo disco
3100 next y
3110 for x=peek(v+(2*(p3-1))) to x1 step -2
3120 poke (v+(2*(p3-1))),x:rem sposto disco a dx
3130 next x
3140 rem appoggio disco:
3150 rem y1= y di partenza  y2 = y di arrivo
3160 y1=peek(v+1+(2*(p3-1)))
3170 if p1=0 then y2=y9:goto 3190
3180 y2=y9-(ad*q1):rem altrimenti y di arrivo e' appena sopra disco
3190 for y=y1 to y2 step 2
3200 poke (v+1+(2*(p3-1))),y
3210 next y
3220 gosub 4200:rem riproduci suono
3230 rem aggiorno peso pila di destinazione 1
3240 for i=q1+1 to 1 step -1:p1(i)=p1(i-1):next i:rem scalo in basso i pesi
3250 p1(1)=p3:p1=p3:q1=q1+1
3260 rem aggiorno presenza dischi pila di partenza 3
3270 if q3>1 then:for i=1 to q3-1:p3(i)=p3(i+1):next i:rem scalo in alto i pesi
3280 p3(q3)=0:q3=q3-1
3290 if q3>=1 then p3=p3(1)
3300 if q3=0 then p3(1)=0:p3=0
3310 if gm$="g" then 3590
3320 return:rem else if gm$="s"
3330 rem ******************************************
3340 rem * spostamento disco da piolo 3 a piolo 2 *
3350 rem ******************************************
3360 for y=peek(v+1+(2*(p3-1))) to y0 step -2
3370 poke (v+1+(2*(p3-1))),y:rem sollevo disco
3380 next y
3390 for x=peek(v+(2*(p3-1))) to x2 step -2
3400 poke (v+(2*(p3-1))),x:rem sposto disco a sx
3410 next x
3420 rem appoggio disco:
3430 rem y1= y di partenza  y2 = y di arrivo
3440 y1=peek(v+1+(2*(p3-1)))
3450 if p2=0 then y2=y9:goto 3470
3460 y2=y9-(ad*q2):rem altrimenti y di arrivo e' appena sopra disco
3470 for y=y1 to y2 step 2
3480 poke (v+1+(2*(p3-1))),y
3490 next y
3500 gosub 4200:rem riproduci suono
3510 rem aggiorno peso pila di destinazione 2
3520 for i=q2+1 to 1 step -1:p2(i)=p2(i-1):next i:rem scalo in basso i pesi
3530 p2(1)=p3:p2=p3:q2=q2+1
3540 rem aggiorno presenza dischi pila di partenza 3
3550 if q3>1 then:for i=1 to q3-1:p3(i)=p3(i+1):next i:rem scalo in alto i pesi
3560 p3(q3)=0:q3=q3-1
3570 if q3>=1 then p3=p3(1)
3580 if q3=0 then p3(1)=0:p3=0
3590 poke56145,1:poke56155,1:poke56165,1:if gm$="s" then return
3600 rem debug
3610 if q3<>d then 3750:rem ho finito il gioco (q3=d ?)
3620 rem print"{clear}":gosub 3500:poke 646,5:poke 53280,5
3630 print "{home}{space*32}":poke 646,5:poke 53280,5
3640 print"{up*24}{down}{space*3}complimenti,hai risolto il gioco!"
3650 if(m=mm) then print "{space*3}nel numero minimo di mosse ("str$(mm)")"
3660 poke 646,1:if(m<>mm) then print "{space*3}hai fatto"str$(m)" mosse su"str$(mm)" previste"
3670 poke 1872,32:poke 1874,32
3680 poke 1882,32:poke 1884,32
3690 poke 1892,32:poke 1894,32
3700 poke 1919,62:poke 1927,60
3710 gosub 4960:rem victory sound
3720 if(peek(56320) and 16)=0 goto 410
3730 goto 3720
3740 print "{clear}":poke 53269,0:end
3750 m=m+1:rem incremento mosse fatte
3760 goto 1340
3770 rem disegno i pioli e la base
3780 print "{down*5}":poke 646,1
3790 for i=1 to 13
3800 print "{space*9}{reverse on} {reverse off}{space*9}{reverse on} {reverse off}{space*9}{reverse on} "
3810 next i
3820 rem print "{space*2}{reverse on}{space*36}"
3830 print "{space*2}{reverse on}{space*36}"
3840 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}"
3850 print "{space*2}{reverse on}{space*13}{reverse off} termina {reverse on}{space*14}"
3860 print "{space*2}{reverse on} sposta tutti i dischi sul piolo #3 "
3870 return
3880 rem ***********************
3890 rem * soluzione ricorsiva:*
3900 rem ***********************
3910 poke 1919,62:poke 1927,60:rem per terminare premi fuoco su >TERMINA<
3920 n=d:sp=49151:a=1:b=2:c=3:m=0
3930 rem salvo le variabili nello stack a partire dalla locazione 49152:
3940 sp=sp+1:poke sp,n:sp=sp+1:poke sp,a:sp=sp+1:poke sp,b:sp=sp+1:poke sp,c
3950 gosub 3970:rem 1a chiamata  hanoi($n, $a, $b ,$c);
3960 goto 3610
3970 if n>1 then 4050:rem ramo1:e' come dire if n=1 esegui le righe seguenti
3980 m=m+1:a$=mid$(str$(a),2,1):b$=mid$(str$(c),2,1):pi=a:pf=c
3990 print "{home}{down*2}mossa "str$(m)": #"a$" -> #"b$:gosub 1720
4000 rem ritorno dalla ricorsione,ripristino le variabili dallo stack
4010 c=peek(sp):sp=sp-1:b=peek(sp):sp=sp-1:a=peek(sp):sp=sp-1:n=peek(sp):sp=sp-1
4020 rem print "ritorno dalla ricorsione ramo 1"
4030 return
4040 rem else (n>1): ramo 2
4050 n=n-1
4060 rem salvo le variabili nello stack:
4070 sp=sp+1:poke sp,n:sp=sp+1:poke sp,a:sp=sp+1:poke sp,b:sp=sp+1:poke sp,c
4080 x=b:b=c:c=x:rem scambio b con c  hanoi($n, $a, $c ,$b);
4090 gosub 3970
4100 m=m+1:a$=mid$(str$(a),2,1):b$=mid$(str$(c),2,1):pi=a:pf=c
4110 print "{home}{down*2}mossa "str$(m)": #"a$" -> #"b$:gosub 1720
4120 rem salvo le variabili nello stack:
4130 sp=sp+1:poke sp,n:sp=sp+1:poke sp,a:sp=sp+1:poke sp,b:sp=sp+1:poke sp,c
4140 x=a:a=b:b=x:rem scambio a con b  hanoi($n, $b, $a ,$c);
4150 gosub 3970
4160 rem ritorno dalla ricorsione,ripristino le variabili dallo stack
4170 c=peek(sp):sp=sp-1:b=peek(sp):sp=sp-1:a=peek(sp):sp=sp-1:n=peek(sp):sp=sp-1
4180 rem print "ritorno dalla ricorsione ramo 2"
4190 return
4200 rem *** suono dischi ***
4210 poke54296,15
4220 poke54277,0
4230 poke54278,240
4240 poke54272,100
4250 poke54273,20
4260 poke54276,129
4270 fort=1to20:next
4280 poke54276,128
4290 if gm$="s" and (peek(56320) and 16)=0 then 410
4300 return
4310 rem *******************
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,0,247,0,1,231
4380 data 128,1,247,128,1,247,128,1,247,128
4390 data 0,255,0
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,1,231,128,3,219
4460 data 192,3,247,192,3,239,192,3,195,192
4470 data 1,255,128
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,3,231,192,7,219
4540 data 224,7,247,224,7,219,224,7,231,224
4550 data 3,255,192
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,7,247,224,15,239
4620 data 240,15,215,240,15,195,240,15,247,240
4630 data 7,255,224
4640 rem
4650 data 0,0,0,0,0,0,0,0,0,0
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,15,199,240,31,223
4700 data 248,31,199,248,31,251,248,31,199,248
4710 data 15,255,240
4720 rem
4730 data 0,0,0,0,0,0,0,0,0,0
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,31,231,248,63,223
4780 data 252,63,199,252,63,219,252,63,231,252
4790 data 31,255,248
4800 rem
4810 data 0,0,0,0,0,0,0,0,0,0
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,63,195,252,127,251
4860 data 254,127,247,254,127,239,254,127,239,254
4870 data 63,255,252
4880 rem
4890 data 0,0,0,0,0,0,0,0,0,0
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,127,231,254,255,219
4940 data 255,255,231,255,255,219,255,255,231,255
4950 data 127,255,254
4960 REM ********* VICTORY SOUND ********
4970 Q=54272:FOR I=0 TO 24:POKE Q+I,0:NEXT I:POKE Q+24,15
4980 POKE Q+5,15:POKE Q+6,240
4990 POKE Q+1,25:POKE Q+0,243:POKE Q+4,17
5000 FOR T=1 TO 50:NEXT T
5010 POKE Q+4,16:FOR T=1 TO 15:NEXT T
5020 POKE Q+1,25:POKE Q+0,243:POKE Q+4,17
5030 FOR T=1 TO 50:NEXT T
5040 POKE Q+4,16:FOR T=1 TO 15:NEXT T
5050 POKE Q+1,25:POKE Q+0,243:POKE Q+4,17
5060 FOR T=1 TO 50:NEXT T
5070 POKE Q+4,16:FOR T=1 TO 15:NEXT T
5080 POKE Q+1,34:POKE Q+0,164:POKE Q+4,17
5090 FOR T=1 TO 180:NEXT T
5100 POKE Q+4,16:FOR T=1 TO 15:NEXT T
5110 POKE Q+1,51:POKE Q+0,230:POKE Q+4,17
5120 FOR T=1 TO 350:NEXT T
5130 POKE Q+4,16
5140 RETURN
5150 REM ************ SUONO BLEEP **************
5160 POKE 54296,15
5170 POKE 54277,17
5180 POKE 54278,150
5190 POKE 54273,100
5200 POKE 54272,100
5210 POKE 54276,17
5220 FOR T=1 TO 12:NEXT T
5230 POKE 54273,70
5240 POKE 54272,100
5250 FOR T=1 TO 12:NEXT T
5260 POKE 54273,40
5270 POKE 54272,100
5280 FOR T=1 TO 18:NEXT T
5290 POKE 54276,16
5300 POKE 54276,0
5310 return