
Our example program, ZX TIC TAC TOE (the listing appeared in May/June ’87 on pages 21 & 22), has the LOAD name “TTT”. When storing a program on a cassette, it is better to give it a name, especially when the tape contains other different programs. The procedure is, of course, to use a REM statement with the name of the program within a pair of quotations (see the chapter in the User Manual regarding LOAD/SAVE).
ZX TIC TAC TOE is self-running and this is possible because of line number 5. This line permits the program to begin execution immediately after it is loaded. It also eliminates the danger of the user accidentally erasing any data contained in the variable store by entering RUN. Any program written with a SAVE command, after it is loaded, will begin to execute from the first line after this command.
Lines 7 through 12 are in charge of initializing the character array C$(9). This array is initialized before the beginning of each game with the numbers 1 through 9. These values are what first appear in each square of the game board and permit a player to make a move during a game. The way the loop is structured is interesting from a programming viewpoint. This way of initializing a character array is faster ans consumes less memory, than say, using LET and listing each individual array element in consecutive order.
In lines 16 through 18 the number array N(9) is initialized with zeroes before each new game. This array appears in the driver by the name of COUNTR. This array is used by this routine to store how many times during a game a particular move is made. This is important because it prohibits any one square in the game board from being used more than once during an actual game.
Line 34 initializes the number variable CTR, which is used to keep track of the total number of moves made during a game. When CTR equals nine, it cues the driver called DRW. This routine does what its name says: given 9 consecutive moves and no winner, the game must end in a draw.
Line 35 is really interesting. This line lets the character variable F$, be used further down the program by pre-assigning it at the beginning of each game. F$ in this program acts as a “flag”–it tells the computer that a game has finished in either a win or a draw.
Lines 112 through 120 compose what is actually the main program within my overall program. It has 5 subroutines and two drivers (a clearer example of this will be shown later when we list the program “declarations”). After the last driver at line 119, terminates execution (i.e., when a game being played ends either in a win or a draw), then line 120 permits the start of a new game by letting the program re-execute again from line 7. This way of structuring a program, which involves integrated multi-functional routines, is convenient in that it first allows a programmer to outline the main areas of his/her program, then the rest of the time can be devoted to writing each specific section of the overall work. (I credit this suggestion to Dr. William T. Kraynek, Associate Professor, Mathematical Sciences Department, Florida International University, Tamiami Campus, Miami, Florida.)
From lines 1001 to 1019, two things happen. Subroutine MSG prints a message on the screen for three seconds urging the player to beat the computer. After clearing the screen another set of instructions appear for 12 seconds informing the user how to stop and restart the game. The length of time that each message appears can be reduced by pressing any key, except the space key, while each message is being displayed. These messages are displayed once per each new game.
Lines 2001 through 2012 is the subroutine BRD. This section prints on the screen, once per game, the playing board. The vertical and horizontal lines of the board are made of the characters “I” and “-” respectively. The routine is made of two FOR/NEXT loops and a “counter” by the name of C. The variable C is what informs the computer that three sets of vertical lines and two sets of horizontal lines have been printed and that it may proceed with its next instructions.
Lines 3001 through 3015 is the subroutine PSTN. This name, is actually an abbreviation for the word “position”. This routine is continually called upon to display the actual moves that are either made or remain to be made before and during a game. The row coordinates from top to bottom are 2, 7 and 11; the column coordinates from left to right are 2,7 and 13. This routine prints the content of each element in the array C$(9) at pre-determined locations on the game board in row form, beginning with the highest and ending with the lowest row.
Lines 4003 through 4010 is the subroutine INSTRCS, which stands for “instructions”. Once per game, this section displays on the screen the name of the game, the year it was copyrighted, its author’s name, who get to play with either of the characters (in inverse video) “O” and “X” and who gets the first move. The player who is assigned “O”, is the one who always gets to make the first move. Once a game begins, the instructions at the bottom of the screen (which report who moves with what and who makes the first move) are erased.
Lines 5000 through 5017 compose the subroutine RDAPRV. The basic functions of this routine are to read in the move made by a player during a game, then to determine if the move is included within the only acceptable kind of moves that should be made during a game. Only the numbers 1 through 9 are acceptable moves. This routine is what is called a “search” routine. It seeks to match what is entered with what can only be an acceptable move. If the move is acceptable, the game proceeds with the rest of the instructions in the program. If the move entered is unacceptable, then three things happen: an error message is displayed for two seconds, it self-erases and the player is allowed another move. Whether or not a move that is made is acceptable…what is entered by a player is always displayed on the screen to let him/her know exactly what was entered.
The first driver in the program appears in lines 6000 through 6018. Its name is COUNTR. Its function is to NOT allow a player to make the same move, he/she or the computer makes, more than once. It too acts like a search routine by determining which acceptable move was entered, then it stores in the number array N(9) the number of times that move was made. If a move in a game is made twice or more, and error message is displayed for two seconds, self-erases and allows another move by calling subroutine RDAPRV. If the move that was made has not been made before, the program then continues.
We’ll wrap up our explanation of the example program next issue.