QL Corner: Guess the number game

Author(s): Bob Gilder
Publication: LISTing Newsletter
Date: December 1997
Pages: 2-4

All of a sudden, I have realized that I have been working exclusively with text for at least four years and I have not programmed anything at all. I am currently writing an article for QLtoday and I’ve decided that I need a little rest. Within this article I have two menus which are in my BOOT disk: One has 3 different printer drivers and the other is two different HELP files for The Editor. The main ingredients are two procedures, SELect ON and END SELect. The following documentation will help understanding both procedures.

SELect ON

SELect is an alternative form of decision making. It is used when it is required to make one choice from a list of possibilities. This is done by testing the value of a variable by a series of ON statements, each of which may be followed by a series of program lines. When the test following an ON is true, all the statements between it and the next ON line are executed.

A SELect structure is started with a SELect ON variable_name statement, and concluded with an END SELect statement.

The ON statements allow the variable to be tested for equality to a value, or for a range of values, using the familiar TO notation (i.e. ON rest_of_file=30 TO 50). Other tests, such as not equal, greater than, less than, etc. are not acceptable. There is also an ON REMAINDER statement, which will execute a series of statements if no other ON conditions have been satisfied, This can be thought of as the equivalent of ELSE in IF … THEN structure.

The following listing is a guess-the-number game which uses a large number of conditional statements. A random number is generated, and you get a clue as to the size. You then have to enter guesses, and the computer tells you how you are doing.

Lines 130 and 140 set the windows. Here we are creating 2 windows of our own. The channel 3 window is for output only, so we use the scr_device name. The channel 4 window is for input and output so it is a console device. The extra parameter at the end sets the size of a buffer to hold the characters typed ahead.

The random number is selected by a loop (lines 270 to 300) which repeatedly selects random numbers until a key is pressed. This is a good way of producing a truly random value. Lines 310 to 390 provide the clues. An IF … THEN statement is used first for low values, followed by a SELect structure testing for ranges of intermediate values, and then another IF … THEN statement for the high values.

The main part of the game is between lines 410 and 570. Input of a guess is invited. If this is out of range, a suitable message is printed, and a NEXT statement is used (here in a REPeat loop) to go back for another entry. The difference between the guess and the random number is calculated, and this is then tested in a similar way to the original random number to tell you how you are doing.

The variable diff is compared to a variable old_diff, and by this means you are told if your new guess is less accurate than the last.

At line 450, if you enter the correct number, the program branches out of the loop to line 580, prints CORRECT!!! and invites you to have another go. If you decline, lines 660 to the end reset the computer. Note that these lines reset for use with a monitor. END.

110 REMark Guess-the-Number Game *
120 MODE 256
130 OPEN#3,scr_512x100a0x0
140 OPEN#4,con_512x100a0x100_8
150 BORDER#3,16,5:BORDER#4,16,5
160 CLOSE#1: CLOSE#2
170 INK#3,1: PAPER#3,7
180 INK#4,7: PAPER#4,1
190 CLS#3: CLS#4
200 CSIZE#3,2,0: CSIZE#4,2,0
210 LET tries=1: old_diff=5000
220 REMark ** main program starts **
230 REPeat games
240 CLS#3
250 PRINT#3, 'I am thinking of a number (1 to 5000)'
260 PRINT#3,'(Press a key )'
270 REPeat setnumber
280 number=RND(1 TO 5000)
290 IF INKEY$(#4,20)<>"" THEN EXIT setnumber
300 END REPeat setnumber
310 PRINT #3,'Here is a clue ...'
320 PRINT #3,'It could be ';
330 IF number<100 THEN PRINT#3,!' someones age.'!
340 SELect ON number
350 ON number = 100 TO 400: PRINT#3,!'a cricket score.'!
360 ON number = 401 TO 1000: PRINT#3,!'the pages in a book'!
370 ON number = 1001 TO 3000: PRINT#3,!'the size of a car engine'!
380 END SELect
390 IF number>3000 THEN PRINT#3,!'a telephone number'!
400 PRINT#4,'Try no. ';tries
410 REPeat guesses
420 INPUT#4,'Your guess?'!guess%
430 IF guess%<1 OR guess%>5000 THEN PRINT#4, 'SILLY!!!': NEXT guesses
440 diff=ABS(number-guess%)
450 IF diff=0 THEN EXIT guesses
460 IF number<guess% THEN PRINT#4,'Too big';
470 IF number>guess% THEN PRINT#4,'Too small ';
480 IF diff<10 THEN PRINT#4;"but you are very close"
490 SELect ON diff
500 ON diff=10 TO 50: PRINT#4;"but you are quite close"
510 ON diff=51 TO 200: PRINT#4;'.'
520 END SELect
530 IF diff>200 THEN PRINT#4;'by a long chalk!'
540 IF diff>old_diff THEN PRINT#4;'You are further out than last time!'
550 tries=tries+1: old_diff=diff
560 PRINT#4;'Last guess ';guess%
570 END REPeat guesses
580 CLS#3
590 FOR lines=1 TO 5
600 PRINT#3;'CORRECT!!!'
610 END FOR lines
620 CLS#4
630 PRINT#4;'Another go?'
640 IF INKEY$(-1)<>'y' THEN EXIT games
650 END REPeat games
660 CLOSE#3: CLOSE#4
670 OPEN#1,con_256x200a256x0_255: INK#1,7:PAPER#1,2
680 OPEN#2,scr_256x200a0x0: INK#2,2: INK#2,2: PAPER#2,7
690 MODE 512
700 CLEAR: STOP

PRINTER DRIVER MENU (used with Taskmaster)

451 INPUT #0;"Select a PRINTER DRIVER; Load The EDITOR; or BASIC?
(P/E/B) ";A$
452 IF A$="e" OR A$="E" THEN GO TO 660
453 IF A$="p" OR A$="P" THEN GO TO 455
454 IF A$="b" OR A$="B" THEN GO TO 680
455 CLS: PRINT \\\"Select printer driver"
456 PRINT " 1 - ITOH 8510A PRINTER DRIVER"
457 PRINT " 2 - PANASONIC KXP1124 PRINTER DRIVER"
458 PRINT " 3 - EPSON LQ-2500 PRINTER DRIVER"
459 INPUT #0;"Your choice? "; pr
460 DELETE flp1_printer_dat
461 SELect ON pr
462 =1: COPY flp1_ITOHpr_dat TO flp1_printer_dat
463 =2: COPY flp1_PANpr_dat TO flp1_printer_dat
464 =3: COPY flp1_EPLQpr_dat TO flp1_printer_dat
465 END SELect
660 LRUN flp1_BOOT_edt
670 LRUN FLP1_PIP_boot
680 STOP

Two HELP files for the EDITOR SE

455 CLS: PRINT \\\"Select EDT_HELP_file menu"
456 PRINT " 1 - EDT1_help"
457 PRINT " 2 - EDT2_help"
458 PRINT " 3 - Non_available"
459 INPUT #0;"Your choice? "; pr
460 DELETE flp1_EDT_help
461 SELect ON pr
462 =1: COPY flp1_EDT1_help TO flp1_EDT_help
463 =2: COPY flp1_EDT2_help TO flp1_EDT_help
464 =3: COPY flp1_EDT3_help TO flp1_N/A
465 END SELect

This is another short program using SELect ON and END SELect

1 REMark *** Ski holiday tariffs ***
5 CLS
100 PRINT "Ski Holidy tariffs"
110 PRINT "Enter 1...for Westerndorf"
120 PRINT" 2...for Adelboden"
130 PRINT" 3...for La Plagne"
140 PRINT" 4...for Verbier"\\
150 INPUT "your selection"!n
160 SELect ON n
170 ON n=1
180 PRINT "High-season $250.00"
190 PRINT "Low-season $195.00"
200 ON n=2
210 PRINT "High-season $320; low $286"
220 ON n=3
230 PRINT "Special terms $200 self-cater"
240 ON n=4
250 PRINT "Dec, Jan $312;Feb,MAR $324"
260 ON n=REMAINDER
270 PRINT "Incorrect number entry"
280 END SELect

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