Storing 3-Letter Words in an Array

See all articles from SYNC v2 n3

I wanted to develop a method of storing as many three-letter words as possible in my 1K ZX80, using as little memory as possible. The solution is based on the fact that character numbers of the letters can be compounded into a single number, which can be stored in an array element. Thus three letters can be stored in two bytes.

Listing 1, The Word Storing Routine, sets up arrays which are used in Listing 2, The WORDY Program.

The technique takes the character numbers of the letters in a word; subtracts 36, then multiplies by 1 for the first letter, 30 for the second letter, and 900 for the third. Then the three are added together so that they can be stored as a single number, which must not exceed 32767, hence the -36 in line 14. Line 18 ensures that a space keeps its code number of 0, and a nul string is treated as a space. This allows one- or two-letter words to be stored if desired.

Line 20 does the multiplication and addition, and the numbers representing the words are stored in Array S(). The maximum number of words this program can hold is 175.

When the words have been stored, the whole of the word storing routine can be deleted. But remember never use RUN, or the words will be lost. The words could be stored at this stage, but it is better to get as much as possible of the main program entered first.

Having entered Listing 1 and checked the SYNCSUM, which should be 18, enter Listing 2, starting at Line 32. The first part of Listing 2 cannot be entered until you are finished with Listing 1. Check the SYNCSUM again at the end of Listing 2. It should be 57.

Now you are almost ready to store your 175 words, but first the SYNCSUM Routine must be removed as we need the 27 bytes it occupies. We also need to store the program on tape at this stage, so we can kill two birds with one stone, as they say. When you have it safely on tape, enter NEW and then reLOAD from the tape.

Enter RUN and the screen will display:

ENTER WORDS
0

Now enter a 3-letter word (e.g., AND) and the screen will display:

0         AND
1

Continue entering words until all 175 have been entered. Then the error code 9/26 will be displayed. Press any key and the program listing will appear. Delete lines 1 to 26 and enter lines 10 to 30 of Listing 2.

When you have saved the completed program on tape, you are ready to start playing “WORDY,” which is a 3-letter word version of “Mastermind.” Enter GOTO any number up to 10, but not RUN or the words will be lost. Enter your word and NEWLINE. If you think the answer might be a 2-letter word, enter a space for the third letter.

The program uses the multiplication sign to indicate a direct match and the zero for an indirect match. It allows up to 10 tries and prints out the correct answer and the number of tries needed, showing 11 if you have not solved it by the tenth try.

The word selection and unscrambling routines are contained in lines 10 to 26. The selected number is divided by 900, i.e., 30**2, to find the character number of the third letter. Then the remainder is divided by 30 for the second. The final remainder gives the first—when they have each had 36 added at line 24. The rest of the program is, I think, fairly conventional, but note the Boolean Logic in lines 52 and 74.

There are a lot more than 175 3-letter words in the English language, so you might like to make several versions.

Besides showing a method of storing 3 characters in 2 bytes, this program also shows that once arrays have been set up, the routine which established them can be deleted so long as CLEAR and RUN are avoided.

Listing 1: The Word Storing Routine

 1 DIM A(2)
2 DIM B(2)
3 DIM C(2)%
4 DIM S(174)
5 PRINT "ENTER WORDS"
6 FOR I=0 TO 174
7 PRINT I
8 INPUT U$
9 CLS
10 PRINT I, U$
12 FOR J=0 TO 2
14 LET A(J)=CODE(U$)-36
16 LET U$=TL$(U$)
18 IF A(J)< 2 THEN LET A(J)=0
20 LET S(I)=S(I)+A(J)*30**J
22 NEXT J
24 NEXT I
26 STOP
SYNCSUM = 18

Listing 2: The WORDY Program

10 LET W=RND(175)-1
12 LET W=S(W)
14 FOR I=0 TO 2
16 LET J=2-I
18 LET K=30**J
20 LET A(J)=W/K
22 LET W=W-A(J)*K
24 IF A(J)>1 THEN LET A(J)=A(J)+36
26 NEXT I
28 PRINT "WORDY"
30 PRINT
32 FOR J=1 TO 10
34 LET D=0
36 INPUT U$
38 PRINT U$;"#";
40 FOR I=0 TO 2
42 LET C(I)=CODE(U$)
44 LET U$=TL$(U$)
46 LET B(I)=A(I)
48 NEXT I
50 FOR I=0 TO 2
52 IF C(I)-B(I) THEN GOTO 62
54 LET D=D+1
56 LET C(I)=3
58 LET B(I)=2
60 PRINT "*";
62 NEXT I
70 FOR I=0 TO 2
72 FOR K=0 TO 2
74 IF C(K)-B(I) THEN GOTO 82
76 LET C(K)=3
78 LET B(I)=2
80 PRINT 0;
82 NEXT K
84 NEXT I
86 PRINT
87 IF D=3 THEN GOTO 90
88 NEXT J
90 PRINT
92 FOR I=0 TO 2
94 PRINT CHR$(A(I));
96 NEXT I
98 PRINT "#"; J

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.