This program demonstrates the use and manipulation of a 3-dimensional string array, N$(R,C,9), by tracking occupancy with a numeric array, P(R,C). The program runs in slow mode; you can see blocks move.
Both ZX/TS and TS2068 versions operate in the same way, but the following analysis refers to line numbers in the ZX/TS program. You can find the equivalent lines in the color version, which has added commands to enhance the colors and different graphics that use TS2068 color to advantage.
Line 60 reserves a grid of 230 spaces (P), (10 rows (R) of 23 columns (C)). If P=1, the position is occupied. P=0 means vacant. In N$, position R,C can be occupied by nine-character strings, shown by the third dimension of N$, (line 40). Five strings (A$–the blocks) will be used (line 50) and designated 1-5 (lines 70-110).
Lines 130-200 build, and 270-320 label, three platforms. Our first platform is topped with five blocks (lines 210-240). The others remain empty, for now.
FROM loop F (in lines 450-580) removes the top block (lines 490-510) (P=1) and empties the space by resetting variable P=0.
TO loop T (lines 590-710) seeks an empty space (P=0) from the bottom up, deposits the block there (line 650) and makes P=1 at the new position (line 680).
S (line 480) tabulates moves. You need at least 31. How few can you use to move all the blocks?
Enter the program listing as shown, then SAVE by using GOTO 820 for the ZX/TS version or GOTO 760 on your TS2068.
When program displays, press any key to make the game start. Exit the program with BREAK. Restart with RUN.
SOLUTION
MOVE PIECE FROM TO 1 1 1 3 2 2 1 2 3 1 3 2 4 3 1 3 5 1 2 1 6 2 2 3 7 1 1 3 8 4 1 2 9 1 3 2 10 2 3 1 11 1 2 1 12 3 3 2 13 1 1 3 14 2 1 2 15 1 3 2 16 5 1 3 17 1 2 1 18 2 2 3 19 1 1 3 20 3 2 1 21 1 3 2 22 2 3 1 23 1 2 1 24 4 2 3 25 1 1 3 26 2 1 2 27 1 3 2 28 3 1 3 29 1 2 1 30 3 2 3 31 1 1 3
This program tests your moves to see if you placed a larger piece atop a smaller one. If so, you are chided and told to move it back. If you ignore the instruction, you can still cheat. In addition, it fiendishly refuses to let you put a piece back on the pile you took it from (line 440). From 1 to 3, 5 times, lets you win in 5 moves, despite the fact that the tower is completely inverted.
Most of the error-checking in this program can be defeated or ignored easily. But the program does a good job for an honest, cooperative user.
Many programs offer this style of writing to work. You should now add error traps so that the program can not fail. Be creative in your testing–try moving from 1 to 2, then from 2 to 1–this should not win the game! Also try from 1 to 2, 2 to 3, 2 to 3… Hmmmm!
See if you can find ways to make these programs use less array space without changing the 10 by 23 part of the DIM statements. Does N$ need 9-character strings? Does P need to be a number (7 bytes for each location)?
Can you make the anti-cheating loop put the piece back and charge you two moves?–KO
Enter ZX/TS Graphics as follows:
70 3 spaces graphic eight inverse
one graphic five 3 spaces
80 3 spaces inverse space
inverse two inverse space 3
spaces
90 2 spaces 2 inverse spaces
inverse three 2 inverse spaces 2
spaces
100 space 3 inverse spaces inverse
four 3 inverse spaces space
110 4 inverse spaces inverse five
4 inverse spaces
160 graphic eight 7 graphic H's
graphic five
170 graphic eight 3 graphic H's...
3 graphic H's graphic five
180 graphic eight 7 graphic F's
graphic five
930 17 graphic sixes
940 inverse(space TOWERS space OF
space HANOI space)
950 17 graphic sevens
990 5 space inverse * space...
space inverse *... 5 space *
inverse(space NO space NO
space) * space
2068
First, remember the light part of the key symbol means INK. Also, we use SS to mean SYMBL SHIFT–hit the black & 3 keys in graphics mode to get “SS graphic three.”
70 3 SS graphic eights 1 SS
graphic five...1 SS graphic
five 3 SS graphic eights
80 3 SS graphic eights space...
space 3 SS graphic eights
90 2 SS graphic eights 2 spaces..
2 spaces 2 SS graphic eights
100 SS graphic eight 3 spaces...
3 spaces SS graphic eight
110 4 spaces... 4 spaces
160 SS graphic eight 7 graphic
threes SS graphic eight
170 SS graphic eight 3 spaces...
3 spaces SS graphic eight
180 SS graphic eight 7 SS graphic
threes SS graphic eight
930 17 SS graphic three
940 inverse(space TOWERS space OF
space HANOI space)
990 17 graphic three
ZX/TS VERSION
10 CLS
20 FAST
30 LET E$=" "
40 DIM N$(10,23,9)
50 DIM A$(5,9)
60 DIM P(10,23)
70 LET A$(1)=" ▐1▌ "
80 LET A$(2)=" 2 "
90 LET A$(3)=" 3 "
100 LET A$(4)=" 4 "
110 LET A$(5)=" 5 "
120 LET S=0
130 LET N=1
140 LET R=11
150 FOR C=1 TO 23 STEP 11
160 PRINT AT R,C;"▐▒▒▒▒▒▒▒▌"
170 PRINT AT R+1,C;"▐▒▒▒";CHR$ (N+156);"▒▒▒▌"
180 PRINT AT R+2,C;"▐▓▓▓▓▓▓▓▌"
190 LET N=N+1
200 NEXT C
210 LET C=1
220 FOR R=6 TO 10
230 LET P(R,C)=1
240 NEXT R
250 GOSUB 930
260 FOR R=6 TO 10
270 FOR C=1 TO 23 STEP 11
280 IF P(R,C)=1 THEN LET N$(R,C)=A$(R-5)
290 PRINT AT R,C;N$(R,C)
300 NEXT C
310 NEXT R
320 SLOW
330 PRINT AT 15,14;"MOVE"
340 PRINT AT 17,8;"FROM- ";
350 IF INKEY$="" THEN GOTO 350
360 LET F$=INKEY$
370 IF F$<>"1" AND F$<>"2" AND F$<>"3" THEN GOTO 350
380 LET F=VAL F$
390 PRINT F;" TO- ";
400 IF INKEY$="" THEN GOTO 400
410 LET T$=INKEY$
420 IF T$<>"1" AND T$<>"2" AND T$<>"3" THEN GOTO 400
430 LET T=VAL T$
440 IF T=F THEN GOTO 400
450 PRINT T
460 PRINT AT 15,9;" "
470 PRINT AT 15,14;"MOVE"
480 LET S=S+1
490 IF F=1 THEN LET C=1
500 IF F=2 THEN LET C=12
510 IF F=3 THEN LET C=23
520 FOR R=6 TO 10
530 IF P(R,C)=1 THEN PRINT AT R,C;E$
540 IF P(R,C)=1 THEN LET Z$=N$(R,C)
550 IF P(R,C)=0 THEN GOTO 580
560 LET P(R,C)=0
570 GOTO 590
580 NEXT R
590 IF T=1 THEN LET C=1
600 IF T=2 THEN LET C=12
610 IF T=3 THEN LET C=23
620 FOR R=10 TO 6 STEP -1
630 IF P(R,C)=1 THEN GOTO 710
640 IF P(R,C)=0 THEN LET N$(R,C)=Z$
650 PRINT AT R,C;N$(R,C)
660 IF R>=10 THEN GOTO 680
670 IF Z$(5)>N$(R+1,C,5) THEN GOSUB 970
680 LET P(R,C)=1
690 IF R=6 THEN GOTO 720
700 GOTO 800
710 NEXT R
720 PRINT AT 15,14;" ";AT 17,5;"YOU DID IT IN ";S;" MOVES"
730 PRINT AT 21,4;"WANT TO TRY AGAIN? (Y/N)"
740 IF INKEY$="" THEN GOTO 740
750 IF INKEY$="Y" THEN RUN
760 CLS
770 PRINT AT 10,14;"BYE-"
780 PAUSE 4E4
790 STOP
800 PRINT AT 17,8;" "
810 GOTO 340
820 SAVE "TOWERS"
830 CLS
840 GOSUB 930
850 PRINT AT 3,0;"OBJECT OF THIS GAME IS TO MOVE"
860 PRINT ,,"FIVE BLOCKS FROM TOWER 1 TO",,,"TOWER 3 IN THE SAME ORDER."
870 PRINT ,,"A SMALLER BLOCK MAY BE PLACED ON",,"A LARGER BLOCK BUT NEVER A LARGE",,"ONE ATOP A SMALLER ONE.----THAT"
880 PRINT ,,"COSTS EXTRA MOVES TO RESTORE THE",,"PROPER ORDER."
890 PRINT ,,"NUMBER OF MOVES WILL BE SHOWN",,,"WHEN YOU FINISH THE GAME."
900 PAUSE 4E4
910 CLS
920 RUN
930 PRINT AT 0,8;"▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄"
940 PRINT AT 1,8;" TOWERS OF HANOI "
950 PRINT AT 2,8;"▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀"
960 RETURN
970 FOR N=1 TO 16
980 PRINT AT 15,14;" "
990 PRINT AT 17,7;" * NO-NO *";AT 17,7;" * NO NO * "
1000 NEXT N
1010 PAUSE 90
1020 PRINT AT 17,9;"THAT/S NOT LEGAL"
1030 PAUSE 150
1040 PRINT AT 15,9;" MOVE IT BACK "
1050 PRINT AT 17,9;" "
1060 RETURN
SYNTACTIC SUM: 27054, 8K ROM
Includes inverse S in line 820.
2068 VERSION
4 PAPER 6
6 FOR N=0 TO 21
7 PRINT INK 6;AT N,0;" "
8 NEXT N
10 BORDER 4
30 LET E$=" "
40 DIM N$(10,23,9)
50 DIM A$(5,9)
60 DIM P(10,23)
70 LET A$(1)="███▌1▐███"
80 LET A$(2)="███ 2 ███"
90 LET A$(3)="██ 3 ██"
100 LET A$(4)="█ 4 █"
110 LET A$(5)=" 5 "
120 LET S=0
130 LET N=1
140 LET R=11
150 FOR C=1 TO 23 STEP 11
160 PRINT PAPER 5;AT R,C;"█▀▀▀▀▀▀▀█"
170 PRINT PAPER 5;AT R+1,C;"█ ";N;" █"
180 PRINT PAPER 5;AT R+2,C;"█▄▄▄▄▄▄▄█"
190 LET N=N+1
200 NEXT C
210 LET C=1
220 FOR R=6 TO 10
230 LET P(R,C)=1
240 NEXT R
260 GO SUB 870
270 FOR R=6 TO 10
280 FOR C=1 TO 23 STEP 11
290 IF P(R,C)=1 THEN LET N$(R,C)=A$(R-5)
300 PRINT INVERSE 1; INK 1;AT R,C;N$(R,C)
305 IF P(R,C)=0 THEN PRINT INK 6;AT R,C;" "
310 NEXT C
320 NEXT R
330 PRINT AT 15,11;" "
340 PRINT FLASH 1;AT 15,14;"MOVE"
350 PRINT AT 17,8;"FROM- ";
360 PAUSE 0
370 LET F$=INKEY$
372 IF F$<>"1" AND F$<>"2" AND F$<>"3" THEN GO TO 360
375 LET F=VAL F$
390 PRINT F;" TO- ";
400 PAUSE 0
410 LET T$=INKEY$
412 IF T$<>"1" AND T$<>"2" AND T$<>"3" THEN GO TO 400
415 LET T=VAL T$
420 IF T>3 OR T=F THEN GO TO 400
430 PRINT T
440 LET S=S+1
450 IF F=1 THEN LET C=1
460 IF F=2 THEN LET C=12
470 IF F=3 THEN LET C=23
480 FOR R=6 TO 10
490 IF P(R,C)=1 THEN PRINT AT R,C;E$
500 IF P(R,C)=1 THEN LET Z$=N$(R,C)
510 IF P(R,C)=0 THEN GO TO 540
520 LET P(R,C)=0
530 GO TO 550
540 NEXT R
550 IF T=1 THEN LET C=1
560 IF T=2 THEN LET C=12
570 IF T=3 THEN LET C=23
580 FOR R=10 TO 6 STEP -1
590 IF P(R,C)=1 THEN GO TO 650
600 IF P(R,C)=0 THEN LET N$(R,C)=Z$
610 PRINT INVERSE 1; INK 1;AT R,C;N$(R,C): INVERSE 0
612 IF R>=10 THEN GO TO 620
615 IF Z$(5)>N$(R+1,C,5) THEN GO SUB 1000
620 LET P(R,C)=1
630 IF R=6 THEN GO TO 660
640 GO TO 740
659 NEXT R
660 BORDER 2
665 PRINT AT 15,14;" ";AT 17,5;"YOU DID IT IN "; FLASH 1;S; FLASH 0;" MOVES"
670 PRINT AT 21,4;"WANT TO TRY AGAIN? (Y/N)"
680 IF INKEY$="" THEN GO TO 680
690 IF INKEY$="Y" THEN RUN
700 CLS
710 PRINT AT 10,14;"BYE-"
720 PAUSE 0
730 STOP
740 PRINT AT 17,8;" "
759 GO TO 330
760 SAVE "TOWERS" LINE 780
780 CLS
785 GO SUB 870
790 PRINT AT 4,0;"OBJECT OF THIS GAME IS TO MOVE"
800 PRINT ,,"FIVE BLOCKS FROM TOWER 1 TO",,,"TOWER 3 IN THE SAME ORDER."
810 PRINT ,,"A SMALLER BLOCK MAY BE PLACED ON",,"A LARGER BLOCK BUT NEVER A LARGE",,"ONE ATOP A SMALLER ONE."
820 PRINT ,,"THAT COSTS EXTRA MOVES."
830 PRINT ,,,,"NUMBER OF MOVES WILL BE SHOWN",,,"WHEN YOU FINISH THE GAME."
849 PAUSE 0
850 CLS
860 GO TO 4
870 PRINT INK 2;AT 0,8;"▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄"
880 PRINT INK 2;AT 1,8;" TOWERS OF HANOI "
890 PRINT INK 2;AT 2,8;"▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀"
900 RETURN
1010 PRINT AT 15,14;" ";AT 17,7;" "
1020 PRINT INK 2; FLASH 1;AT 15,13;"*NO-NO*"
1030 PAUSE 150
1040 PRINT AT 17,9;"THAT'S NOT LEGAL!"
1050 PAUSE 150
1060 PRINT AT 15,11;"MOVE IT BACK"
1070 PRINT AT 17,9;" "
1080 PAUSE 150
1090 RETURN
SYNTACTIC SUM: 18328, TS2068
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.