` COMPUTE! ISSUE 55 / DECEMBER 1984 / PAGE 197`

 PROGRAMMING THE TI C. Regena Multiplication Maze First, a correction. In "Alphabet Song," which appeared in this column in the August issue, change line 1910 GOTO 330 to 1910 ON SP GOTO 330,340 so the program will work properly whether you have the speech synthesizer or not. Readers have been sending quite a few letters about the "Simple Math" program in the July column. Many of you want to know how to rewrite the program to add higher numbers or modify it for subtraction, multiplication, or division. That particular program used numbers less than five so the sum would be less than ten, and the answer would be one digit. CALL KEY was used to get the answer. To use higher numbers or receive an answer that can be two digits, use two CALL KEY loops. It is better to avoid INPUT wherever possible because INPUT is so easy to crash. This month's program illustrates how to receive an answer that may be either one or two digits long. Some of the following tips may be useful to you. For subtraction, choose a random number A from one to nine, then a random number B from one to nine. The total of the two numbers is A + B. For the subtraction problem, use A + B for the top number, and B for the number to be subtracted. The answer will be A, which is a one-digit number. For multiplication, choose a random number A from one to nine, then a random number B from one to nine. The answer is A*B and can be a one- or two-digit answer. For division, choose a random number A from one to nine, then a random number B from one to nine. The product is A*B. To write a division problem, use A*B for the dividend and B for the divisor. The answer will be A. This procedure makes sure you will have whole numbers for the answers, and the answers will be one-digit numbers. The Faster The Better This month's program, "Multiplication Maze," is another example of a math drill. First, the program draws a maze. Within the maze are the numbers from one to nine. A random factor or multiplier is chosen and appears in the upper-left corner of the maze. The player uses the arrow keys (on E, S, D, and X) to move, and must go to each number on the maze and type the product of his factor times the number. The faster the player goes around the maze and gets all nine answers, the lower the time score will be. The player should try to get as low a time as possible. (The best score around our house was under 200.) The answer must be correct to continue, so if the student misses answers, it takes up valuable time. Lines 100–240 clear the screen, then print the title and instructions. Lines 250 and 260 define graphics character 96 to be a solid white square for the design of the maze. If you want to economize, CALL COLOR(9,16,16) will also make a solid square, and you do not need to define character 96. The first method is used in case you want to add other objects in the maze and use other character numbers in color set 9. Lines 270–280 change the colors for the numbers to be printed in the maze so they will be black with a white background. Line 290 uses DEF to define a function R(X) as a random number from 0 to X—1. This simplifies programming in later statements wherever random numbers are needed. For example, line 590 uses R(3) and R(5) to generate random numbers from 0 to 2 and from 0 to 4, respectively. Line 660 uses R(9) + 1, which gives a random number from 1 to 9. Lines 300–350 READ values from DATA to limit nine areas for placing the numbers in the maze. The numbers are placed randomly, but this makes sure the numbers are spread throughout the maze. Each area goes from column XA(I) to XB(I) and from row YA(I) to YB(I). As you type the DATA statements, notice that there are three groups of four numbers for each DATA statement. Be sure you get the commas right and don't put an extra comma at the end of a line. Keeping Track Of Data Lines 360–440 define characters and symbols for the black-on-yellow color set. The RESTORE statement tells the computer to start reading the next data with line 410. Although this line is not necessary in this program, in general the RESTORE statement can help you keep track of which DATA statements go with which READ statements. In this case, if you happen to make a typing error in lines 330–350, it won't affect the data for the next READ statements, which need data in lines 410–420. These lines define the numbers in order, starting with character 104 as zero and continuing to character 104 + 9 as nine. Line 450 initializes the lowest time or low score to be 99999. Later games will use whatever score has previously been the lowest score. Lines 460–480 wait for the player to press a key to start the game. In the CALL KEY statement, if the status S is 0 or -1, either the same key is being pressed or no keys have been pressed. When a key is pressed, S will be 1. Lines 490–500 clear the screen, then change the screen color to magenta. You can use whatever color you want (darker colors will look better with the white maze), but I've always liked purple. Lines 510–560 draw a grid of white lines for the base of the maze. Lines 570–610 randomly erase some of the white squares to create the maze. The loop goes from row 3 to row 21, using only the odd-numbered rows. The CALL HCHAR statements pick a column from 4 to 8 and from 18 to 22 and draw a random number of spaces from 2 to 10. This automatically leaves some vertical paths throughout the maze so it is always possible to reach every point. Lines 620–640 define the nine possible multipliers, the numbers from 1 to 9, in the array FF(I). Lines 650–750 randomly place the nine multipliers in the nine areas of the screen, making sure the number has not been used before and that the number is on a white square. Game Setup Lines 760–800 initialize the variables which are used to move the player's factor. The player's factor always starts in the upper-left corner of the maze, row 2 and column 4. NR and NC are used to calculate the new row and new column when the factor moves. P is the character number of the previous spot, or the white square. Lines 810–820 randomly choose the player's factor, which is a number from 2 to 9. GR is then calculated, which will be the graphics character number for the factor with the yellow background. Line 830 initializes the time T, which is used for scoring. T is incremented within the CALL KEY loops as the computer is waiting for the player to press an acceptable key. Line 840 repeats the main game loop nine times, so the player needs to go to nine multipliers and give the answers. Lines 850–890 place the player's factor on the maze and increment the time T. Lines 900–920 detect the player's keypress, which must be an arrow key. CALL KEY(1, K, S) checks the left half of the keyboard. If a key is not pressed, or the key pressed is not an arrow key, the program branches back to line 880 to increment the time. In line 910, the first check is K + 1 < 1 because checking for zero does not always work with some TI-99/4A computers. Line 920 saves several IF-THEN statements by using an ON-GOTO statement. If an arrow key is pressed, K equals 0, 2, 3, or 5 and the program branches to the appropriate direction. Checking For Valid Moves Lines 930–1030 define DR and DC depending on the arrow key pressed. DR is the change in row number, and DC is the change in column number. Line 1040 calculates the possible new position on row NR and column NC. Lines 1060-1070 make sure the new position is still within the boundaries of the maze. Line 1080 checks character G in the new position. In line 1090, if G is 96 or a white square, the move is valid, and the program branches back to line 850 to move the player's factor. But in lines 1100–1120, if G is 32 or a space, the player cannot move and the computer sounds a low beep. Then the program branches back to increment the time and get another keypress. Line 1130 starts the procedure which results if the player's factor has hit another number. Line 1130 changes the number to an asterisk, and line 1140 sounds a prompting tone. Lines 1150–1190 print the multiplication problem on row 23. Since G is the character number of the number hit on the maze, G-48 is the number, AM. The number to be printed with a yellow background will be 104 + AM. The answer will be AM times the player's factor, M. Lines 1200-1240 blink a question mark and increment the time while waiting for the student to press a number. This time, zero is used in the CALL KEY statement to detect a key pressed anywhere on the keyboard. Line 1240 makes sure the key pressed is a number from 1 to 9 to be accepted. Line 1250 prints the number the player presses. Line 1260 calculates the correct answer B, and line 1270 defines B\$ as the player's answer. Line 1280 checks the length of the correct answer (which can be one or two digits). If the length is 2, then lines 1290-1350 get the player's second digit, which may be a number from 0 to 9. If the answer is only a one-digit number, the program branches to line 1360. Sound Effects Line 1360 checks the answer, and if the answer is incorrect, lines 1370–1420 play an "uh-oh" sound, clear the player's answer B\$, and branch back for another answer. The answer must be correct to continue the game. Lines 1430-1460 play a musical arpeggio for the correct answer, then line 1470 clears the problem, and line 1480 continues the game for nine multipliers. When all nine multipliers have been answered correctly, lines 1490–1510 play a tune of 30 random notes. Lines 1520-1530 clear the screen and print the score. Lines 1540-1560 calculate and print the lowest score. Lines 1570–1610 print the option to try again and branch according to the player's keypress of Y or N. Line 1620 ends the program. You can try this program as listed or adapt it to other types of problems. To modify it for addition, simply change all * signs to +. To change to division, you can use a factor M, then put all the possible quotients in the maze. To change to a nonmath subject, define some objects in the maze. Then whenever the player hits an object, print a history question, vocabulary word, or whatever. If you want to save typing time and effort, I'll make you a copy of this program if you send a self-addressed, stamped envelope, a blank cassette or disk, plus a \$3 copying fee to: C. RegenaP.O. Box 1502Cedar City, UT 84720 Please be sure to specify the title of the program and that you need the TI version.