Classic Computer Magazine Archive COMPUTE! ISSUE 20 / JANUARY 1982 / PAGE 116

Word Search

Bob Jones

Cranbury, NJ

Basically this program creates two matrices. The first matrix, the A matrix, is the one we shall hide the words in. Since the ATARI and many other BASICs I have run across do not permit the use of strings in a matrix, I have found that the next best thing to do is use the ASCII value of the characters instead. In this case it seems to be even simpler. The A matrix is initialized in line 10 to a random set of numbers between 65 and 91, (the ASCII value of the letters A thru Z). The C matrix will be our control matrix and our answer key. In line 10, all locations in C are initialized to 42, (the ASCII value for the character "*").

Next the user is asked to input 12 words, (the subroutine called by line 15). Lines 3015 through 3130 simply set A$ equal to the word to be processed, selected by the variable I. Line 45 sets L equal to the length of the word and if it is too long, (greater than ten letters) asks the user to input a shorter word. In line 50 we convert letters of the word to their ASCII values and place them in the B array, (a numeric array also initialized to all zero's by line 10). This array is our workhorse. Line 60 serves two functions: first, to generate a random starting location within both matrices and, second, to generate a random direction for the word to go in.

Now comes the math. Line 70 directs the program to one of eight subroutines, each one representing a different possible direction for the word to travel in. I shall only go over the first one, (lines 500 to 550) as the others work the same way. Line 500 checks to see that the word will fit within the matrix, if not the program is directed back to line 60 to generate a new starting location and direction. In line 510 we check the position of the word against the C or control matrix for possible conflicts with words already placed within that matrix. If a conflict exists the program is again directed back to line 60. Line 520 checks for a crossover with a previous word and if there is one it sets a flag, (the variable F) equal to 1. Line 630 directs the program to lines 2000 to 2020, these lines would have been repeated 8 times, once for each direction subroutine so in order to save memory they are only listed once and called upon when needed. The use of the ‘GOTO’ instead of the ‘GOSUB’ command is necessary in order to conditionally return to other portions of the program without confusing the computer by jumping in and out of subroutines. In these lines, (2000 to 2020) we continue to process our word, if there is a crossover (F = 1), or we have tried 300 times to find one, (determined by the variable R) we continue, otherwise we go back to line 60. Line 2020 gets us back into our original subroutine. Line 550 is the last line of our subroutine, it places our word into the A and C matrix's and sends us on to get a new word.

Line 80 determines if we have processed all of our words, and if so sends us on. In line 100 we print our hidden word matrix by printing the letters represented by our ASCII values, and when we are ready, line 110 prints our C matrix which is now our answer key.

This program requires more than 8K of memory as stands to run, though it will load into 8K of memory. It is a simple matter to shorten it by cutting out some of the possible direction subroutines. Also you can ask for the words to be INPUT as they are needed rather than storing them in string arrays. This program can be run on almost any computer using BASIC as stands, the only possible modifications that might be needed are with the GOTO statements like ‘GOTO D*100’. These may be changed to ‘ON D GOTO 500,600, 700,800,900,1000,1100,1200’. Or you could use the ‘IF…THEN’ statements, though the program won't be as much fun. A ‘?’ is simply a PRINT command. The POKE statements are not necessary: they simply speed up the program. (Thanks to Ed Stewart, COMPUTE! #11.)