Classic Computer Magazine Archive COMPUTE! ISSUE 58 / MARCH 1985 / PAGE 65

Bearmath
Bearmath


    Gary West & Jim Bryan


This program tests youngsters on any of the four mathematical operations and has three levels of difficulty. After each set of ten problems, the program calculates a score and gives the option of another round. The article also explains two useful programming techniques: detecting a keypress in response to a screen prompt, and page flipping on the PCjr. The program requires an IBM PC with BASICA and the color/graphics adapter or a PCjr with Cartridge BASIC.


"Bearmath" is a helpful math drill program if you have school-age youngsters, but the program also serves another purpose-it demonstrates a couple of handy programming techniques. It shows how to trap keystrokes in response to screen prompts (that is, menus), and how to instantly flip between two alternate screens on the PCjr. First we'll explain how to use the program for those who aren't interested in programming.
    If you have a PCjr, type in Program 1. If you have a PC, type in Program 1 and add the changes shown in Program 2. The modifications are required because the PC lacks the special screen-flipping commands found in PCjr Cartridge BASIC.
    When you run Bearmath, it first asks you (or the youngster) to type your name. Don't type in a long name (more than about nine characters), because later in the program the screen might scroll an extra line and mess up the screen formatting. Next, the program draws the face of a friendly bear on the screen. The picture is also copied to an alternate screen hidden in a safe place in memory. After you press the space bar to continue, a menu appears. By pressing a single key, you can choose to practice with addition, subtraction, multiplication, or division. Then the program offers a choice of three skill levels.
    Once the drill begins, the problems are presented one by one. After each correct answer, the friendly bear appears. If the answer was wrong, you get a second chance. If the second response is also incorrect, the program gives the correct answer.
    Bearmath continues in this way through a set of ten problems, maintaining the score at the top of the screen. After the tenth problem, the program presents three options: You can press the space bar for another set of ten problems; press E to end the program and return to BASIC; or press P to print a score report. Before pressing P, make sure a printer is connected, powered up, and on-line. Otherwise, the program ends and returns to BASIC. If you hit P by accident, you can exit the printing mode and return to the previous screen by pressing E.
    If you press the space bar for another set of problems, the program restarts from the beginning.

Trapping Keystrokes
There are several parts of this program which may interest those who want to learn a few BASIC programming techniques. One of the most common techniques used by programmers is a routine which waits for the user to press a key to either continue the program or select a menu option.
    The first menu in Bearmath is displayed by lines 32-48:

[Your name],
Press the number you want:
1 for addition
2 for subtraction
3 for multiplication
4 for division

    IBM BASIC (and nearly any extensive Microsoft BASIC) offers two general ways to detect keypresses: INPUT and INKEY$. In this case, since each menu option can be selected with one keystroke, it's easier to process the response with INKEY$ instead of INPUT. With INKEY$ only one keystroke is needed, while INPUT requires, at minimum, two keystrokes-the menu choice and the Enter key. INKEY$ simply takes the next key pressed (or the next key in the keyboard buffer, if you pressed several keys in succession) and goes on.
    INKEY$ requires that you test for the absence of a keypress, too-because, if no key is pressed, the INKEY$ function allows the program to continue as though no INKEY$ were there.
    Line 54 in Bearmath contains one example of trapping for the absence of a keypress so that only the keys you want will be accepted. The menu selections are numbered 1, 2, 3, and 4. If you press any other key, line 54 loops back to itself to prevent the program from continuing:

54 A$=INKEY$:IF A$<>"1" AND A$<>"2"
AND A$<>"3" AND A$<>"4" THEN 54

    Let's translate line 54 into English. This is a two-statement line, separated by a colon. The first statement, A$=INKEY$, tells the computer to read the keyboard and store any keystroke in the string variable A$. (You could use any string variable, of course, as long as you modified the rest of the line to agree.)
    The second statement tests for which key was pressed. Remember that <> is the BASIC symbol for inequality, the opposite of =. In other words, line 54 tests for the negative - keypresses which aren't allowed. If it detects such a key, line 54 sends control back to line 54-repeating itself endlessly until one of the proper keys is pressed. If the key is acceptable (1, 2, 3, or 4), the program continues.
    Another example of a menu with the appropriate traps can be seen in lines 61 (the menu) and 69 (the trap).

Checking For Letters
A special example of trapping is seen in lines 300, 920, and 9230. Here, the program waits until the space bar is pressed before going on to the next part of the program. For example:

9230 Q$=INKEY$:IF Q$<>" " THEN 9230

    Again, the first part of this multiplestatement line reads the keyboard with INKEY$ and stores the keypress in a string variable, Q$. The second statement in the line checks to see if the keypress was not equal (<>) to a space. If the space bar is not pressed, the program repeats line 9230 endlessly. When the space bar is pressed, the program continues.
    There aren't many complications when trapping for numbers or spaces. However, when trapping for letters, you must be more careful in building your traps. An example can be seen in lines 299-305. Line 299 gives you the option of pressing the E key to end the program, after line 296 gives you the option of pressing the space bar to continue. Line 300 accepts whatever key you press and compares it to the acceptable responses. If the pressed key is not a space bar or an E or a P, the program goes back to look for another key:

300 Z$=INKEY$:IF Z$<>" " AND Z$<>"E"
AND Z$<>"e" AND Z$<>"P" AND Z$<>"p"
THEN 300

    Notice that the trap includes tests for both the uppercase and lowercase letters so that either will be accepted. That way, it doesn't matter if the user's keyboard is in Caps Lock or standard mode.
    The proper use of menus and traps can allow others who have no knowledge of the program to use it with little difficulty.

special screen-flipping
Using a special screen-flipping technique, "Bearmath"
instantly displays the face of a friendly bear to reward
correct answers.

Screen Flipping
One of the features of Bearmath is that it displays the bear's friendly face as a small reward after each correct answer. But normally it takes the computer a few seconds to draw that face. If you had to wait for it to be drawn each time, you could easily become bored with the program. So Bearmath uses a different technique.
    When you first run Bearmath, you'll see the face being drawn (by lines 9000-9140). After it's drawn, it is copied onto another video page. On the PCjr, Cartridge BASIC has a command that can flip to the alternate screen page instantly so the bear's face can be displayed without redrawing it each time.
    Since the equivalent command is missing from BASICA on the PC, a short machine language routine was written to do the same thing, except it takes a fraction of a second longer. That's why PC users need to add the modifications in Program 2 to Program 1. The machine language routine is created in the subroutine starting at line 10000 in Program 2. Unfortunately, an explanation of how this routine works is beyond the scope of this article.
    On the PCjr the technique is much easier and can be done entirely in Cartridge BASIC. First, to flip back and forth between two or more screens, the program must set aside enough video memory to hold the screens you want to use. In this particular graphics mode (SCREEN 1), each screen requires 16K of memory. (Some graphics modes require as much as 32K.) So to use a second page, Bearmath must tell the computer to set aside another 16K of video memory. Look at line 2 in Program 1:

2 CLEAR,,,32768!:GOTO 8000

    The CLEAR statement reserves a total of 32K of video memory so the program can use two pages for displays. The next statement branches to line 8000, where a subroutine asks for the user's name and then draws the bear.

Active & Visual Screens
The PCjr has two types of screen pages-the active page and the visual page. The active page is the one affected by BASIC commands which output to the screen-such as PRINT, LINE, CIRCLE, and so on. The visual page is the screen you're seeing at any moment-the screen actually displayed on the monitor.
    Most of the time, the active page and the visual page are the same. But they don't have to be. When they are separated, the program can print messages or draw graphics on the active page without tipping off the user. The commands are taking effect, but invisibly to anyone looking at the monitor.
    By adding extra parameters to the SCREEN statement, you can designate the active and visual pages. Line 8002 in Program 1 is an example:

8002 SCREEN 1,0,0,0

    This statement sets the screen to graphics mode 1 (as in SCREEN 1) and turns on the color (the first 0). The next two zeros set the active page equal to the visual page. Thus, you can see the bear's face as it's being drawn for the first time when the program starts.
    After the face is drawn, line 9159 in Program 1 copies it from page 0 to page 1:

9159 PCOPY 0,1

    The rest is simple. When a math problem is answered correctly, the PCjr version of Bearmath displays the bear's face by just copying page 1 back to page 0:

9300 PCOPY 1,0

    The PCOPY command, by the way, is the one that's missing from Advanced BASIC (BASICA) on the PC.

Checking The Answer
Bearmath processes your answer to a math problem in line 170 and checks it in line 180. The correct answer was calculated and assigned to the variable Q earlier, in lines 500-820. Your answer, assigned to the variable E, is subtracted from the correct answer. If the difference between your answer and the correct one is no more than .01, you are given credit for the problem. Then the program branches to the routine which copies the bear's face from the alternate screen page. You're told your answer was right and are given the option of pressing the space bar to continue.
    Here's a brief outline of Program 1:

Lines

Description
8000-8050 Input user's name.
9000-9240 Draw bear's face and copy it to other page.
2-30 Setup
32-70 Print menus for operations and levels.
100-111 Set up work screen.
120-140 Make up problems.
150-153 Branch to routine for right answer.
500-520 Calculate addition answer.
600-630 Calculate subtraction answer.
700-720 Calculate multiplication answer.
800-820 Calculate division answer.
160-190 Display problem, accept and check answer.
191-201 Branch to routine for right answer.
9300-9350 Report that answer is correct.
202-215 Give a second chance if first answer was wrong.
241-250 Report if second answer was also wrong.
280-310 Report score and option to continue or end.
900-930 Print various prompts on the.screen.


Program 1: Bearmath (PCjr Version)

JB 1 REM BEARMATH/JR
GE 2 CLEAR,,,327681:GOTO 8000
LB 6 COLOR 1,3
DD 7 RANDOMIZE TIMER
HL 10 CLS
LK 20 BLANK$="
                ":REM 34 SPACES
JJ 30 LET F=0
IB 32 LOCATE 3,5:PRINT NME$;" "
GG 35 LOCATE 5,5
BC 40 PRINT "Press the number you want
      :":PRINT
LD 45 PRINT TAB(5);"l for addition"
MI 46 PRINT TAB(5);"2 for subtraction"
DB 47 PRINT TAB(5);"3 for multiplicati
      on"
HM 48 PRINT TAB(5);"4 for division"
LF 50 LINE (1,1)-(305,100),2,B
CI 54 A$=INKEY$:IF A$<>"1" AND A$<>"2"
       AND A$<>"3" AND A$<>"4" THEN 5
      4
NJ 55 A=VAL(A$)
CM 60 LOCATE 16.5
CN 61 PRINT TAB(5);"Level 1, 2, or 7,
OL 64 LINE (1,105)-(300,144)),I,B
IF 69 B$=INKEY$:IF B$<>"1" AND B$<>"2"
       AND B$<>"3" THEN 69
NP 70 B=VAL (B$)
JI 100 FOR N=1 TO 10
HH 101 CLS: LOCATE 2,10
KL 102 PRINT F;:PRINT " correct so far
       "
HK 103 LINE (1,1)-(300,20),2,B:PRINT
LM 104 LOCATE 5,15:PRINT "Problem ";N
QJ 105 LINE (1,23)-(300,43),1.B:PRINT
MM 106 LOCATE 17,2-PRINT NME$;","
NK 107 LINE (1,115)-(300,140),1,B
QI 110 LOCATE 20,2:PRINT "Type your an
       swer and press Enter."
LN 111 LINE (1,145)-(300,165),2,B
EJ 120 LET W=10-^B
FO 130 LET C=INT(RND(1)*W)+1
GK 131 LET D=INT(RND(1)*W)+1
FG 135 TRY=1
DB 140 IF A>2 THEN LET D=(INT(D/10"(B-
       1)))+1
LO 150 IF A=1 THEN GOTO 500
NB 151 IF A=2 THEN GOTO 600
PE 152 IF A=3 THEN GOTO 700
BH 153 IF A=4 THEN GOTO 800
KO 160 LOCATE 10,10:PRINT C;" ";S$;" "
       ;D;" = ";
CP 170 INPUT " ",E
FK 180 IF ABS(Q-E)>.01 THEN GOTO 202
JC 190 PRINT
PC 191 GOTO 9300
GO 200 LET F=F+1
FM 201 CCOTO 260
CJ 202 PRINT:PRINT:IF TRY=2 THEN 241
NE 203 PRINT TAB(15);"Incorrect"
HP 204 PRINT:PRINT:TRY=2
KC 205 LINE (1,90)-(300,108),1,B
CE 207 PRINT TAB(15);NME$;",":PRINT TA
       B(15);"try again.".
PK 208 LINE (1,115)-(300,140),2,B
QJ 210 LOCATE 20,2:PRINT "Type your an
       swer and press Enter."
QM 212 LOCATE 10,2:PRINT BLANK$
FA 215 GOTO 160
NG 241 PRINT TAB(15);"Incorrect"
IH 243 PRINT:PRINT:PRINT TAB(2);BLANK$
       :PRINT TAB(2);BLANK$
CH 244 LOCATE 16.10.-PRINT "The correct
        answer is":PRINT TAB(18);Q
LK 245 LINE (1,90)-(300 ,108),1,B
PM 246 LINE (1,115)-(300,140),2,B
JL 250 GOSUB 900
AG 260 NEXT N
BM 270 CLS
GN 275 LNTH=LEN(NME$):SPOT=20-(.5*LNTH
       ):LOCATE 2,SPOT:PRINT NME$
LP 280 LOCATE 3,12:PRINT "Your score i
       s"
KF 281 LOCATE 4,12:PRINT F;" OUT OF 10
       "
CK 282 LINE (1,1)-(300,40),1,B
PC 290 FOR M=1 TO 100
AF 291 NEXT M
DO 295 PRINT:PRINT:PRINT
LL 296 PRINT TAB(10):"Press space bar
       ":PRINT TAB(17);"for"
J0 297 PRINT TAB(10);"next 10 problems
       "
IM 298 LINE (1,50)-(300.85),2,B
MC 299 PRINT:PRINT:PRINT:PRINT TAB(10)
       "Press E to end.":LINE (1,95)-
       (300,120),1,B:PRINT:PRINT:PRINT
       :PRINT TAB(3); "Press P to prin
       t score report.":LINE (1,127)-(
       300,152),2,B
DB 300 Z$=INKEY$: IF Z$<>" " AND Z$<>"E
        " AND Z$<>"e" AND Z$<>"P" AND Z
        $<>"p" THEN 300
EH 305 IF Z$="e" OR Z$="E" THEN CLS:LO
       CATE 12,16:PRINT "Goodbye!":LOC
       ATE 22,20:END
HO 310 IF Z$=" " THEN RUN
JK 320 CLS: LOCATE 2,3:PRINT "Please be
        sure that printer is on.":LINE
        (1,1)-(300,20),1,B
FI 325 LOCATE 5,4:PRINT "Press P to co
       ntinue printing.":LOCATE 7,6:PR
       INT "Press E to exit printing."
       :LINE (1,25)-(300,65),2,B
GK 330 PT$=INKEY$:IF PT$<>"P" AND PT$<
       >"p" AND PT$<>"E" AND PT$<>"e"
       THEN 330
LN 335 IF PT$="E" OR PT$="e" THEN 270
CA 340 FOR X=1 TO 60:LPRINT "+";:NEXT
       X:LPRINT " ":LPRINT:LPRINT
HO 345 LPRINT TAB(SPOT+10);NME$:
PA 350 LPRINT TAB(20);"worked with ope
       ration ":LPRINT TAB(30-.5*LEN(O
       P$));OP$:LPRINT TAB(24);" at le
       vel ";B$
JL 360 LPRINT:LPRINT TAB(13);"and work
       ed ";F;" out of 10 problems.":L
       PRINT:LPRINT:LPRINT TAB(35):"Th
       e bear":LPRINT TAB(35);DATE$:LP
       RINT:FOR X=1 TO 60-LPRINT "+"::
       NEXT X:LPRINT " "
GD 370 GOTO 270
KA 500 LET S$="+"
PB 505 OP$="ADDITION"
KB 510 LET Q=C+D
EG 520 GOTO 160
ML 600 LET S$="-"
GH 605 OP$="SUBTRACTION"
DP 610 IF C<D THEN SWAP C,D
MM 620 LET Q=C-D
FJ 630 GOTO 160
JF 700 LET S$="*"
BN 705 OP$="MULTIPLICATION"
JH 710 LET Q=C*D
F1 720 GOTO 160
NH 809 LET S$="/"
BD 805 OP$="DIVISION"
NE 810 LET Q=C/D
BD 811 IF Q<>INT(C/D) THEN C=C+1:GOTO
       810
FJ 820 GOTO 160
JI 900 PRINT
DL 901 IF N<10 THEN LOCATE 20,2:PRINT
       "Press space bar for next probl
       em. "
JB 910 IF N=10 THEN LOCATE 20,2:PRINT
       "Press space bar for your score
       .     "
ON 915 LINE (1,145)-(300,165),3,B
FB 920 D$=INKEY$:IF D$<:>" " THEN 920
NJ 930 RETURN
MG 8000 CLS
HP 8001 KEY OFF:STRT=0
DN 8002 SCREEN 1,0,0,0
ON 8010 LOCATE 10,4-PRINT "Type your n
        ame and press Enter."
IP 8020 LINE (1,50)-(300,100),1,B
ON 8030 LINE (1,105)-(300,125),2,B
NM 8040 LOCATE 15,3
MP 8050 INPUT " ",NME$
GM 9000 PI=3.141593
NN 9020 CLS
GK 9030 COLOR 1,3
OG 9040 CIRCLE (120,50),10,1:PAINT (12
        0,50),1
PB 9045 CIRCLE (120,52),5,3:PAINT (120
        ,52),3
LF 9050 CIRCLE (200,50),10,1:PAINT (20
        0,50),1
MB 9055 CIRCLE (200,52),5,3:PAINT (200
        ,52),3
NM 9060 CIRCLE (120,50),20
MP 9070 CIRCLE (200,50),20
EM 9075 FOR K=148 TO 152
AA 9080 CIRCLE (160,0),K,2,1.4*PI,1.6*
        PI
KJ 9085 NEXT K
MN 9090 CIRCLE (160,52),50,,-1.4*PI,-1
        .6*PI
NB 9100 CIRCLE (160,86),100:PAINT (160
        ,86)
OM 9110 CIRCLE (160,100),50:PAINT (100
        ,100)
PB 9120 CIRCLE (75,25),20:PAINT (75,25
        )
BJ 9125 CIRCLE (75,28),10,2:PAINT (75,
        28),2
GE 9130 CIRCLE (245,25),20:PAINT (245,
        25)
OM 9135 CIRCLE (245,28),10,2:PAINT (24
        5,28),2
MA 9140 CIRCLE (160,52),50,0,-1.4*PI,-
        1.6*PI
HH 9142 LOCATE 4,2:PRINT "B":LOCATE 6,
        2:PRINT "E":LOCATE 8,2:PRINT "
        A":LOCATE 10,2:PRINT "R"
FB 9143 LOCATE 12,2:PRINT "M":LOCATE 1
        4,2:PRINT "A":LOCATE 16,2:PRIN
        T "T":LOCATE 18,2:PRINT "H"
PB 9150 PAINT (160,100),0
JB 9155 LINE (1,1)-(300,170),2,B
DC 9156 LINE (20,1)-(20,170),2
MP 9159 PCOPY 0,1
JN 9160 FOR Q=1 TO 5
F1 9170 COLOR Q,Q:FOR P=1 TO 150:NEXT
        P
BK 9180 BEEP
NM 9190 NEXT Q
FF 9200 COLOR 1,3
LE 9210 IF N<10 THEN LOCATE 23,7:PRINT
         "Press space bar to go on.
        "
GC 9211 IF N=10 THEN LOCATE 23,7:PRINT
         "Press space bar for score.
         "
EC 9220 LINE (1,173)-(300,187),2,B
PH 9230 (2$=INKEY$: IF Q$<>" " THEN 9230
CA 9235 IF STRT=0 THEN STRT=1:GOTO 6
PJ 9240 GOTO 200
30 9300 PCOPY 1,0
PE 9305 LOCATE 23,7:PRINT NME$;", you
        are right!"
FG 9306 LINE (1,173)-(300,187),2,B
JF 9315 FOR X=1 TO 2
AM 9320 BEEP
LM 9330 FOR Y=1 TO 150:NEXT Y
BO 9340 NEXT X
PD 9350 GOTO 9200


Program 2: Bearmath (Modifications For PC)

NO 2 GOSUB 10000:GOTO 8000
GB 6 CLS:COLOR 1,3
IJ 30 CLS: LET F=0
DG 9020 CALL Z:CLS
PF 9159 REM
CE 9235 IF STRT=0 THEN STRT=1:CALL Z:G
        OTO 6
JB 9240 CALL Z:GOTO 200
BC 9300 CALL Z
CM 9305 LOCATE 23,7:PRINT NME$;"  you
        are right.!       "
MH 10000 DEF SEG:ML$=SPACE$(39):V=VARP
         TR(ML$):DEF FNML!(DUMMY)=PEEK
         (V+1)+256*PEEK(V+2)
FL 10010 RESTORE 10040:Z=FNML!(0):FOR
         I=0 TO 38:READ A:CKSUM=CKSUM+
         A:POKE Z+I,A:NEXT
LE 10020 IF CKSUM=3842 THEN RETURN
HB 10030 SCREEN 0,0,0:COLOR 31:PRINT"E
         rror";:COLOR 7:PRINT" in DATA
          statements.":END
DN 10040 DATA 85,30,190,0,0,187,0,16,1
         42,219,139,4,197,0,184,142,21
         9,135,4,187,0,16,142,219
II 10050 DATA 137,4,70,70,129,254,0,64
         ,114,227,31,93,202,0,0