0% found this document useful (0 votes)
587 views226 pages

(Ebook - Commodore Computers) Impossible Routines For The c64

Commodore 64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
587 views226 pages

(Ebook - Commodore Computers) Impossible Routines For The c64

Commodore 64
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 226

BRAIN GAMES FOR

KIDS AND ADULT S


Using the Commo dore 64
Publishing Director: David Culverwell
Acquisitions Editor: Susan Love
Production Editor/Text Designer: Roberta Glencer
Art Director /Cover Design: Don Sellers
Assistant Art Director: Bernard Vervin
Cover Photography: George Dodson
Manufacturing Director: John A. Komsa

Typesetter: Sogitec, Inc., Lakewood, CA


Printer: R.R. Donnelley and Sons Company, Harrisonburg, VA
Typefaces: News Gothic (text), Antique (display), Eterna Monospace, (programs)
BRAIN GAMES FOR
KIDS AND ADULTS
Using the Commodore 64

John w. Stephenson, PhD


University of Saskatchewan
Saskatchewan, Canada
in association with

!iOFT§YNC
14 East 34th St.
New York, NY 10016

Prentice/Hall e
ENGLEWOOD CLIFFS, N.J. LONDON
International
NEW DELHI
RIO DE JANIERO
SINGAPORE
WELLINGTON
SYDNEY TOKYO TORONTO
Brain Games for Kids and Adults Using the Commodore 64

Copyright © 1984 by John W. Stephenson and Softsync, Inc.


All rights reserved. No part of this publication may be reproduced or transmitted
in any form or by any means, electronic or mechanical, including photocopying
and recording, or by any information storage and retrieval system, without
permission in writing from the publisher. For information, address Brady
Communications Company, Inc., Bowie, Maryland 20715

Library of Congress Cataloging in Publication Data

Stephenson, John W., 1940-


Brain games for kids and adults using the Commodore 64.

Includes index.
1. Computer games. 2. Commodore 64 (Computer)-
Programming. 3. Basic (Computer program language)
4. Mathematical recreations. I. Title.
GV1469.2.S7 4 1984 794.8'2 84-329
ISBN 0-13-0809 38-1

Prentice-Hall International, Inc., London


Prentice-Hall Canada, Inc., Scarborough, Ontario
Prentice-Hall of Australia, Pty., Ltd., Sydney
Prentice-Hall of India Private Limited, New Delhi
Prentice-Hall of Japan, Inc., Tokyo
Prentice-Hall of Southeast Asia Pte. Ltd., Singapore
Whitehall Books, Limited, Petone, New Zealand
Editora Prentice-Hall Do Brasil LTDA., Rio de Janeiro

Printed in the United States of America

84 85 86 87 88 89 90 91 92 93 94 1 2 3 4 5 6 7 8 9 10
This small book of fun and games is dedicated to
my children Margot, Blake, and James,
who have been a big help.
Limits of Liability and
Disclaimer of Warranty

The author and publisher of this book have used their best efforts in
preparing this book and the programs contained in it. These efforts include the
development, research, and testing of the programs to determine their
effectiveness. The author and the publisher make no warranty of any kind,
expressed or implied, with regard to these programs, the text, or the
documentation contained in this book. The author and the publisher shall not be
liable in any event for claims of incidental or consequential damages in
connection with, or arising out of, the furnishing, performance, or use of the text
or the programs. The programs contained in this book and on any diskettes are
intended for use of the original purchaser-user. The diskettes may be copied by
the original purchaser-user for backup purposes without requiring express
permission of the copyright holder.

Note to Authors
Do you have a manuscript or a software program related to personal
computers? Do you have an idea for developing such a project? If so, we
would like to hear from you. The Brady Co. produces a complete range of
books and applications software for the personal computer market. We
invite you to write to David Culverwell, Publishing Director, Brady
Communications Company, Inc., Bowie, Maryland 20715.

Trademarks of Material
Mentioned in This Text

Commodore 64 is a trademark of Commodore Business Machines, Inc.


CONTENTS
Preface ........ ........ ..... ..................... ................. .. ......... ix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Chapter I
Concentration Games............ ...................... ................... 1
Concentration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Flash.............. ...................... ...................... ....... 7
Knight............ ...................... ...................... ....... 11
Knightmare ........ ...................... ................ ..... ...... 15
Simon............ ...................... ...................... ........ 19
Simon (Long Version) .. .. .... .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. . 21
Chapter II
Word Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Anagrams I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Anagrams II (With Hints) .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. . 33
Hangman........ ...................... ...................... ........ 38
Find a Word.............. ...................... ................... 43
Enigma I.................... ...................... .................. 50
Enigma II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Chapter III
Logic and Strategy Games ...................... ............ ........... 57
Invader........... ...................... ...................... ....... 58
Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Sea Hunt............... ...................... ...................... . 66
Verify............ ...................... ...................... ........ 70
Loose Change . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Nim I.................... ..................... ..................... .. 79
Nim II................... ...................... ...................... 81
Nim III .................... ............. ................. ........... 85
Towers of Hanoi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Chapter IV
Arithmetic Games............ ...................... ...................... . 97
Barter............. ...................... ...................... ....... 98
Base............... ...................... ...................... ....... 101

vii
Your Number Is Up . .. .. .. .. .. .. .. .. .. .. .. .. . . . . .. .... .. .. . .. .. . 105
Cryptic..... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Human Arithmetic .. . . .. . . . .. .. . . . .. .. .. . . . . .. .. .. .. . .. . .. .. . .. .. . 112
Jugs.................................................................. 117
Chapter V
Geometry Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Detonation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Quest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Trek.................................................................. 132
U-Boat ............................................................... 138
Warp................................................................. 143
Chapter VI
Algebra Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Monkey Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Obscure Ages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
More Obscure Ages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Much More Obscure Ages .. .. .. . .. .. .. .. .. .. .. .. .. .. . . .. .. .. .. . 158
Product and Sums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Rally................................................................. 166
Chapter VII
Root Finders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Fixed-Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Bracket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Disk Load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Loader............................................................... 182
Loader (With Music)............................................. 186
Music Maker....................................................... 192
Index......................................................................... 197
Instructions For Operating The Prerecorded Floppy Disk...... 203
Instructions For Making A Back-up Copy Of Your Brain
Games Diskette . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

viii
PREFAC E
WARNING! These games may be hazardous to your peace of mind! You
will not find the usual ZAP, ZING, and BOOM of the arcade games here.
These are quiet, peaceful, challenging games of concentration and mental
skill that can be played between just you and your computer, or a group of
your friends can gather around and challenge the computer to battle. The
games can be a lot of fun, but are you up to the challenge?
Can you keep your cool when lost in a four-dimensional time warp
and your fuel supply is running low?
Will you be able to locate the pirates' treasure before the pirates find
it and use you to scrub the barnacles off their ship?
How are you at finding your way around a three-dimensional chess
board or cooly deducing the location of a bomb in a three-dimensional
building of one million rooms before the bomb detonates?
Can you outwit your computer at games of strategy and logic?
These games are meant to entertain you. While you are having fun,
you will learn something about programming and mathematics.
First of all, a lot of good stuff is crammed into very few lines of code.
The reason for keeping the codes so tight and concise is that it minimizes
the risk of typographical errors occurring while you enter the code on
your machine. At the same time, it is not a great chore to enter 30 or 40
lines, and yet where else can you derive so much pleasure from so little an
investment of your time?
Second, there are useful programming tips that illustrate short cuts
in programming. And last but not least, the solutions to the games
demonstrate some everyday, practical mathematics. If you are not
familiar with programming, then you will find that the short programs in
this book will serve as an introduction to programming techniques.
You don't have to use mathematics to solve the games. My children,
aged 10, 14 and 1 7 have enjoyed playing the games for hours. The
youngest uses the technique "by guess and by golly". The middle one
delights in "breaking" into the execution of a program, and since the
computer can not lie, he obtains the answers from the computer in
response to print commands. However, if you are studying mathematics
at the high school or university level, or you simply enjoy solving
mathematical puzzles, you will delight in the challenge of trying to solve
these games in the quickest and most elegant fashion.
In many cases, you are encouraged to go further and to seek answers
to questions or problems that are pointed out in the description of a
ix
program. It is my sincere hope that in these games, you will discover for
yourself something new and interesting in mathematics.

Prof. John Stephenson


Department of Mathematics
University of Saskatchewan
Saskatoon, Saskatchewan

X
INTRODUCT ION

This collection of short programs has been written in BASIC for the
COMMODORE 64. The object was to keep the programs short and
snappy. Sometimes as in "SIMON", "ANAGRAMS", "ENIGMA", and
"NIM", an additional listing of a longer version of the same program is
included when there is something to be gained through the use of the
longer program. The point is that you will be able to enter a short program
in less than 10 minutes. There is less chance of your making
typographical errors with short programs but if you do make an error, it
should not be too difficult to find the error and debug your code.
The programs in this book are generally made up of three parts.
Lines 1 to 99: Code for information about the game.
Lines 100 to 999: The game, or main part of the program.
Lines 1000 to 9999: Code for audio-visual effects.
In every case, lines 100 to 999 of the listings contain the essential
part of the programs. The additional lines before 100 give information on
how to play and contain a summary of the information given in the
sections AIM OF THE GAME and HOW TO PLAY, which are placed in this
book after each listing. The code in the lines following line 999 are for
audio-visual effects. These additions make the programs more exciting to
run, but are a pain to enter! To get started, you may want to enter the
main part of the programs first. You will need to read the section on THE
CODE to make sure that any references to the audio-visual coding is
suppressed. At a later stage, you could add the extra lines. You may also
leave out all the REM statements that are included in the listings to
explain what the code is doing.
In order to keep the main part of the programs short, the use of
screen displays and graphics had to be kept to the bare essentials of
prompts and messages. The listings given in this book are identical to the
screen displays of your entered programs. If your listing does not match
the one given, there is something wrong in the entry of your code. It may
or may not be crucial, but it will serve as a warning for you to check for
possible errors in your code. If you do not want to type in these programs
yourself, you can purchase a prerecorded floppy disk, and enjoy playing
the longer versions of the games immediately.
These programs will be quite useful to beginners in programming.
They will also give you a chance to become acquainted with analytic
geometry, linear equations, quadratic equations, systems of linear and
non-linear equations, conics, Lagrange Interpolation, probability and
xi
statistics, binomial probability, binomial expansion, Pascal's triangle, the
arithmetic of complex numbers, representation of numbers in different
bases, Cartesean and polar coordinates, distances measured by different
norms, the 1-norm and the Euclidean norm. There are also games of
logic, concentration, and of course, arithmetic and algebra.
Unlike arcade games, these games do not use moving graphics. For
the most part, the display is used for prompts, answers, and messages of
praise or doom! The essential features of the games are that they develop
concentration, imagination, logic and the ability to do mental arithmetic.
The games are collected into chapters which categorize them as to
their general content. You might find it helpful to refer to the following
where the games are categorized as to their specific nature or
mathematical content.

Content Game
Algebra: MONKEY PUZZLE, OBSCURE AGES,
MORE OBSCURE AGES, MUCH
MORE OBSCURE AGES, PRODUCT
AND SUMS, RALLY
Analytic geometry: DETONATION, QUEST, TREK, U-
BOAT, WARP
Arithmetic: BARTER, BASE, CRYPTIC, HUMAN
ARITHMETIC, JUGS, PRODUCT
AND SUMS, RALLY, YOUR NUM-
BER IS UP
Cartesian coordinates: DETONATION, QUEST, TREK, U-
BOAT, WARP
Complex numbers: HUMAN ARITHMETIC
Concentration and memory: CONCENTRATION, FLASH, KNIGHT,
KNIGHTMARE, LOOSE CHANGE,
SIMON, VERIFY
Cylindrical and polar coor- U-BOAT
dinates:
Distance measured by 1- QUEST, TREK, WARP
norm:
Euclidean distance: DETONATION, U-BOAT
Gauss elimination and lin- OBSCURE AGES, MORE OBSCURE
ear equations: AGES, MUCH MORE OBSCURE
AGES, RALLY, TREK, WARP
Lagrange interpolation: ANAGRAMS, HUMAN ARITHMETIC
xii
Content Game
Logic and strategy: INVADER, KNIGHT, KNIGHTMARE,
LOOSE CHANGE, MONKEY PUZZLE,
NIM, INVADER, SEA HUNT, TOW-
ERS OF HANOI
Number bases: BASE, NIM, TOWERS OF HANOI,
YOUR NUMBER IS UP
Probability and statistics: INVADER, PROBABILITY, SEA HUNT
Quadratic equations and DETONATION, PRODUCT AND SUMS,
non-linear equations: U-BOAT
Word games: ANAGRAMS, ENIGMA, HANGMAN,
FIND A WORD
Zeros or roots: FIXED-POINT, BRACKET

The experience gained from teaching mathematics for twenty years


has gone into these games. It is my belief that it is possible to learn
mathematics and the ability to think clearly and logically through the use
of games. This can be a lot of fun for children of all ages, including those
of us who are merely young in heart! The presentations and explanations
of the games are given in a tutorial fashion, as if I were by your side
explaining things to you as you go along. You should find this approach
less formal and stuffy than your usual mathematics textbook. My children
and their friends have enjoyed playing these games. It is my hope that you
will enjoy playing them as much as we did.

xiii
,-,_:"::-"~:_::'~-:F_:::?-:?,-'~"F~,,~:;--;;-,,::;,::~-i'A'::_:-:,_::;:,:;~'::,:::_:_;:;-;::;:,~>,:~;::_', :-;_;;--:-t-: -::,_:x::i:\1:;;: :~:,:,-:,:::'~('!'-:.,~- '::' :;~- -:~:

T ' <:,.; ' / ) < <,:) ; ::<

etif tER I
COill liJll'f lll\..'f iOll' ·
.··c;A.,MES
2 CONCENTRATION GAMES

''Concentration''
10 PRINT "a":PRINT:PRINT "INSTRUCTIONS?
PRESS Y OR N":PRINT
20 GET AS:IF A$="" THEN 20
30 IF AS<>"Y"THEN 100
40 PRINT "NUMBERS ARE HIDDEN IN A SQUARE
FULL OF *"
50 PRINT "YOU MUST FIND PAIRS OF MATCHIN
G NUMBERS."
60 PRINT "YOUR TRIES ARE COUNTED AND YOU
R GOAL IS H

70 PRINT "TO MATCH UP ALL OF THE PAIRS 0


F NUMBERSH

80 PRINT "IN THE FEWEST ATTEMPTS"


90 PRINT:INPUT "PRESS RETURN TO START";
100 LET H=0
105 LET T=0
110 PRINT"a":PRINT:PRINT"THERE ARE FOUR
LEVELS OF DIFFICULTY":PRINT
115 PRINT "WHICH DO YOU WANT?": PRINT:PR
INT "PRESS 1,2,3 OR 4"
120 GET SS:IF S$="" THEN 120
130 LET S=2*VAL(S$)
140 IF S<2 OR S>8 THEN 110
145 PRINT:PRINT "SETTING UP THE GAME"
150 FOR I=1 TO S
160 FOR J=1 TO S
170 LET A(I,J)=INT(S*RND(0))+1
180 IF J=1 THEN 200
190 FOR K=1 TO (J-1) :IF A(I,K)=A(I,J) TH
EN 170
195 NEXT K
200 NEXT J,I
210 FOR I=1 TO S:LET A$(I)=""
220 FOR J=1 TO S:LET AS(I)=AS(I)+" *
230 NEXT J,I
235 GOSUB 600
238 IF S=2*INT(S/2) AND H=S*S/2 THEN 900
240 PRINT:INPUT"ENTER ROW AND COLUMN- F
OR EXAMPLE 1,2";I,J
245 IF I=-1 THEN 910
''CONCENTR ATION'' 3

246 IF I<1 OR J<1 OR I>S OR J>S THEN 238


250 IF MID$(A $(I),2*J -1,2)<> " *"THEN 23
5
255 LET X=A(I,J )
260 GOSUB 400
265 PRINT:IN PUT"ENT ER ROW AND COLUMN -TR
Y FOR A MATCH" ;K,L
266 IF K=-1 THEN 910
270 IF K<1 OR L<1 OR K>S OR L>S OR (K=1
AND L=J) THEN 265
275 IF MID$(A $(K),2*L -1,2)<>" *"THEN 26
5
277 LET I=K:J=L
280 LET Y=A(I,J )
285 GOSUB 400
290 IF X=Y THEN PRINT "A MATCHING PAIR!"
:LET H=H+1:GOTO 238
295 PRINT "NOT MATCHING! GIVE IT ANOTHER
TRY MATE"
300 FOR I=1 TO S:LET A$(I)=B $(I) :NEXT I
305 FOR Z=1 TO 2000:NE XT Z
310 GOSUB 600
320 GOTO 238
390 REM:THI S SUBROUTINE REVEALS THE NUMB
ER BEHIND THE * AND COUNTS THE TURNS
400 LET T=T+.5
405 IF INT(T+ .5)=(T+ .5) THEN FOR K=1 TO
S:LET B$(K)=A $(K) :NEXT K
410 LET A$(I)=L EFT$(A $(I) ,2*J-2)+ STR$(A (
I ,J) )+RIGHT $ (A$ (I) ,2*S-2* J)
415 GOSUB 600
420 RETURN
590 REM:THI S SUBROUTINE DRAWS THE PICTUR
E OF THE GAME
600 PRINT"~ TO QUIT ENTER -1,-1"PR INT
TAB(3)
610 FOR M=1 TO S:PRINT STR$(M) ; :NEXT M
620 PRINT
630 FOR M=1 TO S:PRINT :PRINT M;A$(M) :NEX
T M
640 RETURN
900 PRINT:P RINT "YOU SOLVED THE GAME IN"
; T; "MOVES"
910 PRINT:P RINT "AGAIN? PRESS Y OR N"
4 CONCENTRATION GAMES

920 GET ZS:IF Z$="" THEN 920


930 IF ZS="Y" THEN 100
940 STOP
Note: The symbol t:J represents SHIFT CLEAR/HOME.
"CONCENTRATION'' 5

''Concentration''
AIM OF THE GAME
This is a game that tests your concentration and memory. An array of
S rows and S columns of asterisks(*) is set up. You decide on the size of
the array by entering a number 1 ,2,3 or 4 in response to a prompt. The
value of S is twice the number that you choose to enter. The numbers 1 to
S are randomly distributed in each of the rows, but are hidden behind the
asterisks. You are prompted to enter coordinates, (row I and column J) of
two asterisks. The numbers behind your chosen asterisks are displayed. If
you have selected a matching pair, they remain showing and you score a
hit. If you fail to match a pair, your selection is covered up by asterisks
and you try again. The object of the game is to match up all the pairs
possible in the fewest number of tries.
Luckily the computer does not get to have a turn, this is strictly a
game for you. The computer would be difficult to beat because like the
proverbial elephant, it never forgets!

HOW TO PLAY
When you run this program, you are first told

THERE ARE FOUR LEVELS OF DIFFICULTY


WHICH DO YOU WANT?
PRESS 1,2,3 OR 4
Suppose you press the key 2. Immediately the message appears

SETTING UP THE GAME


and after a short time there appears the array and prompt

TO QUIT ENTER -1,-1


1 2 3 4
1 * * * *
2 * * * *
3 * * * *
4 * * * *
ENTER ROW AND COLUMN-FOR EXAMPLE 1,2?
Suppose you enter 1 ,2. The number in the first row and second column is
revealed
6 CONCENTRATION GAMES

1 2 3 4
1 * 3 * *
2 * * * *
3 * * * *
4 * * * *
ENTER ROW AND COLUMN - TRY FOR A MATCH?
Suppose you enter 2, 1. The number in the second row first column is
revealed. If it is a pair, you get a message
A MATCHING PAIR!
ENTER ROW AND COLUMN- FOR EXAMPLE 1,2?
and you continue on while the matching pair remains showing. On the
other hand, if a pair does not match, you will be prompted to try again.
After a brief moment, while you memorize the numbers you turned up,
they are covered by asterisks and you are once again prompted to enter
your choice of coordinates.
When you have matched all the numbers that can be paired, (in this
case there are only eight pairs), you will be given the message
YOU SOLVED THE GAME IN T MOVES
where T will be the number of your tries. You are then invited to play
again or to quit. To quit at any time, enter the pair -1 ,-1.

THE CODE
The program checks that you enter two different number pairs. It
also checks that you enter the coordinates of a position that are not
already matched. These checks are in lines 246, 250, 270 and 275.
In setting up the game, the numbers 1 to S are jumbled up on each
row. The game would be more difficult if the numbers were randomly
located anywhere on the array. This would mean that a particular number
might occur more than once in any given row. This is a fairly difficult
programming exercise for a beginner, but I urge you to try it. Even if you
do not succeed, you will have learned something about programming. My
suggestion is to set up a routine to exchange pairs of numbers in the
array A, and then thoroughly mix them up.
One other challenge I would like to leave with you is to design a
subroutine in which the computer takes a turn when you miss. It would be
easy to design a few lines that cause the computer to look at random
locations. But what I had in mind was that the computer could use any
information revealed in previous turns. You will then be able to play
extremely challenging games with your computer, which would never miss
using any relevant information.
''FLASH'' 7

''Flash''
10 PRINT "~":PRINT:PRINT "INSTRUCTIONS?
PRESS Y OR N":PRINT
20 GET A$:IF A$="" THEN 20
30 IF AS<>"Y" THEN 100
40 PRINT "THIS GAME IMPROVES THE SPEED 0
F YOUR"
50 PRINT "READING. STARTING WITH A SINGL
E LETTER,"
50 PRINT "A STRING IS FLASHED ON THE SCR
EEN FOR A"
70 PRINT "MOMENT. YOU MUST ENTER THE SAM
E STRING."
80 PRINT "IF YOU ARE RIGHT, THEN A NEW S
TRING IS"
85 PRINT "FLASHED WITH ONE EXTRA CHARACT
ER."
90 PRINT "IF YOU PREFER, YOU MAY USE NUM
BERS.":PRINT
95 INPUT "PRESS RETURN TO GO ON"; :REM TH
IS WAITS WHILE THE MESSAGE IS READ
100 PRINT "tJ"
110 PRINT "NUMBERS OR LETTERS?"
120 PRINT:PRINT "PRESS NOR L KEY"
130 GET C$: IF C$="" THEN 130
140 FOR I=1 TO 24:REM THIS LIMITS THE LE
NGTH OF THE STRING TO 24
150 PRINT "WORKING ... ":REM FOR LONGER ST
RINGS THE SET UP CAN TAKE A MOMENT
155 LET A$="":REM STARTS OFF WITH AN EMP
TY STRING EACH TIME
160 FOR J=1 TO I
170 IF C$="N" THEN 210
180 LET S=INT(26*RND(0))+ASC("A")
190 LET A$=A$+CHR$(S) :REM CREATES A STRI
NG OF RANDOM LETTERS OF LENGTH I
200 GOTO 220
210 LET A$=A$+RIGHT$(STR$(INT(10*RND(0))
) ,1) :REM STRING OF NUMBERS, NO BLANKS
220 NEXT J
230 PRINT "~"
8 CONCENTRATION GAMES

235 PRINT "PRESS A KEY WHEN READY"


240 GET ZS:IF Z$="" THEN 240:REM GIVES Y
OU TIME TO GET READY
250 PRINT "C"
260 PRINT LEFT$(A$,I)
270 FOR T=1 TO 200+200*I:NEXT T
280 PRINT "t:l"
290 PRINT "TYPE WHAT YOU JUST SAW AND PR
ESS RETURN"
300 INPUT 8$
310 IF BS<>A$ THEN 360
320 PRINT "CORRECT"
330 FOR T=1 TO 1000:NEXT T
340 NEXT I
350 STOP
360 PRINT "WRONG IT IS ";A$
370 PRINT:PRINT "AGAIN? PRESS Y OR N"
380 GET ZS:IF Z$="" THEN 380
390 IF Z$="Y" THEN 100
400 STOP
Note: The symbol C represents SHIFT CLEAR/HOME.
"FLASH" 9

''Flash''
AIM OF THE GAME
This program tests your ability at reading and remembering a
sequence of letters or numbers that are flashed on the screen. You are
asked for your choice of letters or numbers. Then starting with a single
character and increasing by one character each time, a randomly
generated sequence is flashed for a short time on the screen. Each time
you enter a correct sequence another sequence with an additional
character is flashed for you.
It is very difficult to get more than 10 characters correct.
Psychologists have found that the average person can usually recall about
7 characters. This program stops at 24 characters, but it is not difficult
to increase this limit by changing the limit in line 140.
This program is designed to increase your concentration and your
reading speed. You will find it very challenging.

HOW TO PLAY
When you run this program the screen clears and the following
message appears.
NUMBERS OR LETTERS?
PRESS N OR L KEY
Suppose you press the L key. Immediately the screen clears, the word
WORKING ...
appears momentarily (when you are up to sequences that are 24
characters in length it takes a few seconds to construct the sequence).
Then you are prompted with the message
PRESS A KEY WHEN READY
The reason for this is that you can blink your eyes, clear your throat,
scratch the itchy spot on your nose and generally prepare yourself for the
flash. When you are ready you press any key. A single letter is flashed on
the screen and disappears. Let us say it was
w
Then you are prompted with the message
TYPE WHAT YOU JUST SAW AND PRESS RETURN
You type the letter W and then enter it. The message
CORRECT
10 CONCENTRATION GAMES

is displayed for a short time, then back to WORKING ... and the whole
thing is repeated except that the sequence will contain two characters this
time. Each time you enter a correct sequence the next sequence will
contain one more character than the previous one.
If you should happen to enter an incorrect sequence, as we all do
eventually, you will be given the message
WRONG IT IS ABCXYZ
(whatever the sequence was)
AGAIN? PRESS Y OR N
If you feel up to playing another game, press the Y key. Otherwise,
press any other key to quit for a well deserved rest.
You may feel that a little more time is needed in order to view the
sequence and memorize it. All you need to do is alter the statement in line
270 to 200 + 300*1 or something like that. Do not make the pause too
long or you will defeat the purpose of the game.
Happy flashing!
"KNIGHT" 11

''Knight''
10 PRINT "O":PRINT:PRINT:PRINT " INSTRU
CTIONS? PRESS Y OR N"
15 GET A$:IF A$="" THEN 15
20 IF A$<>"Y" THEN 110
25 PRINT "~":PRINT:PRINT
30 A$="• • • • ":B$=" • • • •":E$="-----
___ , .· R$=" -------- "·. T$="1" .· Y$-"l"
-
35 PRINT TAB(9) ;"ABCDEFGH":PRINT TAB(9);
R$
40 FOR 1=1 TO 8:PRINT TAB(5);I;Y$;
45 LET C$=A$:IF I-2*INT((I/2))=0 THEN C$
=B$
50 PRINT C$;T$:NEXT !:PRINT TAB(9);E$
55 PRINT:PRINT "THIS GAME USES A CHESS B
OARD"
60 PRINT "A LETTER AND A NUMBER ARE USED
TO MARK EVERY POSITION ON THE BOARD"
65 PRINT "THE GAME IS TO MOVE A KNIGHT F
ROM ONE"
70 PRINT "POSITION TO ANOTHER IN AS FEW
MOVES AS"
75 PRINT "POSSIBLE (MOVES ARE IN 1-2 COM
BINATIONS)"
90 INPUT "PRESS RETURN TO START";
110 LET C=0
120 PRINT "0"
125 PRINT:PRINT"ENTER MOVE IN THE FORM L
ETTER NUMBER":PRINT "ENTER -1 TO STOP"
130 LET A=INT(8*RND(0))+ASC("A")
140 LET B=INT(8*RND(0))+ASC("A")
150 LET M=INT(8*RND(0))+ASC("1")
160 LET N=INT(8*RND(0))+ASC("1")
165 IF A=B AND M=N THEN 130
170 PRINT: PRINT "MOVE FROM ";CHR$(A) ;CHR
$(M);" TO ";CHR$(B) ;CHR$(N)
180 LET C=C+1
190 PRINT C;" ";CHR$(A);CHR$(M) ;"TO";
200 INPUT A$
201 IF VAL(A$)=-1 THEN 275
205 LET D$=LEFT$(A$,1)
12 CONCENTRATION GAMES

206 LET E$=RIGHT$(A$,1)


210 IF A=ASC(D$) OR M=ASC(E$) OR ABS(A-A
SC(D$))+ABS(M-ASC(E$))<>3 THEN 300
230 LET A=ASC(D$)
240 LET M=ASC(E$)
250 IF A=B AND M=N THEN 270
260 GOTO 180
270 PRINT "NUMBER OF MOVES =";C
275 PRINT "AGAIN? PRESS Y OR N"
276 GET Z$:IF Z$="" THEN 276
280 IF Z$="Y" THEN 110
290 STOP
300 PRINT " INVALID MOVE"
310 GOTO 180
Note: The symbol t:l represents SHIFT CLEAR/HOME. In line 30 use
the graphics symbols on the +, E, R, T and Y keys.
"KNIGHT" 13

''Knig ht''
AIM OF THE GAME
This is a game of concentration and teaches you to visualize a
situation in your mind. This ability is essential in the study of
mathematics. To start with, however, you might prefer to use a chess
board with the letters A to H marked across the board from left to right,
and the numbers 1 to 8 marked on the edge of the board starting with 1
at the bottom and ending with 8 at the top.
The game is to move a chess piece, called a knight, from one given
location on a chess board to another given location. The knight moves in a
peculiar way crossing three squares each move. It can move in any
combination of 1 up or down and 2 across, or 2 up or down and 1 across.
Your moves are checked and counted. The idea is to try to get to your
final destination in the least number of moves. However, it is a bit like
running a marathon because there is no penalty for being slow. The idea
is to stick with it until you finish.

HOW TO PLAY
When you run this program, the screen clears and you are given a
message like
ENTER MOVE IN THE FORM LETTER NUMBER
ENTER -1 TO STOP
MOVE FROM C2 TO C4
1 C2 TO ?
Your entry must be in a string, first a letter and then a number. Your
move will be checked. If it is not a valid move, you will be asked to repeat
the move. Suppose you were to enter 04. On the screen you would see
2 04 TO ?
Suppose you now entered C4 (wishful thinking!). You would get the
message
INVALID MOVE
3 04 TO ?
Now let us go back to C2. We will not fool around any more. The message
is
4 C2 TO ?
We enter E3. The message is
5 E3 TO ?
14 CONCENTRATION GAMES

Now we can enter C4. The message we get is


NUMBER OF MOVES = 5
AGAIN? PRESS Y OR N
If you wish to play again press the Y key. To quit, you just press the
key N. To quit during the game, enter the move -1 in response to the
prompt to enter a move.

THE CODE
Lines 10 to 90 may be omitted. They only give some instructions on
how to play the game. The INPUT in line 90 halts the execution so that the
screen can be read.
Could you design a subroutine that determines the solution in the
minimum number of moves? You could then have the computer print out
that solution.
''KNIGHTMARE'' 15

''Knightmare''
10 PRINT "a":PRINT:PRINT "THIS IS A 3-D
VERSION OF KNIGHT"
20 PRINT "YOU SHOULD PLAY KNIGHT BEFORE
TACKLING"
30 PRINT "THIS HORROR!"
40 PRINT:PRINT "MOVES MUST BE ENTERED AS
LETTER NUMBER"
50 PRINT "LETTER LIKE":PRINT TAB(4) ;"E5G
":PRINT
60 PRINT "EVERY COORDINATE MUST CHANGE"
70 PRINT "THE TOTAL CHANGE MUST BE 6":PR
INT
90 INPUT "PRESS RETURN TO START";
110 LET C=0
120 PRINT "a"
125 PRINT:PRINT "ENTER MOVE IN THE FORM
'E5G' LETTER":PRINT "NUMBER LETTER"
126 PRINT "ENTER -1 TO STOP":PRINT
130 LET A=INT(8•RND(0))+ASC("A")
140 LET B=INT(8·RND(0))+ASC("A")
145 LET O=INT(8•RND(0))+ASC("A")
150 LET M=INT(8·RND(0))+ASC("1")
160 LET N=INT(8·RND(0))+ASC("1")
165 LET P=INT(8·RND(0))+ASC("A")
166 LET Z=ABS(A-B)+ABS(M-N)+ABS(O-P)
167 IF Z-2·INT(Z/2)<>0 THEN 130
170 PRINT "MOVE FROM ";CHR$(A) ;CHR$(M) ;C
HR$(0) ;"TO ";CHR$(B) ;CHR$(N) ;CHR$(P)
180 LET C=C+1
190 PRINT C;" ";CHR$(A) ;CHR$(M) ;CHR$(0);
"TO";
200 INPUT A$
201 IF VAL(A$)=-1 THEN 275
205 LET D$=LEFT$(A$,1)
206 LET E$=MID$(A$,2,1)
207 LET F$=RIGHT$(A$,1)
210 IF A=ASC(D$) OR M=ASC(E$) OR O=ASC(F
$) THEN 300
215 IF ABS(A-ASC(D$))+ABS(M-ASC(E$))+ABS
(0-ASC(F$))<>6 THEN 300
16 CONCENTRATION GAMES

230 LET A=ASC(D$)


240 LET M=ASC(E$)
245 LET O=ASC(F$)
250 IF A=B AND M=N AND O=P THEN 270
260 GOTO 180
270 PRINT "NUMBER OF MOVES =";C
275 PRINT "AGAIN? PRESS Y OR N"
276 GET Z$:IF Z$="" THEN 276
280 IF Z$="Y" THEN 110
290 STOP
300 PRINT " INVALID MOVE"
310 GOTO 180
Note: The symbol t:J represents SHIFT CLEAR/HOME.
"KNIGHTMARE'' 17

''Knightm are''
AIM OF THE GAME
This is a three-dimensional version of the chess game called
"KNIGHT". You should try the standard two-dimensional game "KNIGHT"
before tackling this game. As the name implies, you might have trouble
going to sleep after playing this horror.

HOW TO PLAY
You are given a starting point and a finishing point on a three-
dimensional chess board. The locations are given by a letter A to H,
followed by a number 1 to 8, and then another letter A to H. Your knight
can move a total of six squares in any combination of (2,2,2), (4, 1, 1) or
(1 ,2,3). By any combination, I mean for instance, in the (1 ,2,3) move, any
one of the coordinates can be changed by 1 unit, another by 2 units and
the remaining coordinate by three units. A typical run would go like this.
You are given the prompt
ENTER MOVE IN THE FORM 'E5G' LETTER
NUMBER LETTER
ENTER -1 TO STOP
MOVE FROM G5A TO E4B
1 G5A TO ?
Every coordinate must alter. So suppose you enter the move F7D. You
would get the message
1 G5A TO ? F7D
2 F7D TO ?
Now you can enter E4B and you are home in 2 moves!
You must enter your moves in a string containing all the three new
coordinates in the format, letter, number and then letter.
Your moves are checked each time for validity. Every coordinate
must change and the total change must be precisely 6. When you get
home you are prompted to play another game or to quit.

THE MATHEMATICS AND CODE


You write down the difference in the starting and ending coordinates.
In the case given above it was-2,-1, 1. This means we have to back up
two positions on the first coordinate, subtract one from the second and
add one to the third coordinate. This is a total change of 4 so we cannot
do it in one move. If we make the changes 1, -2, -3 we are left with -1,
18 CONCENTRATION GAMES

-3, -2 by adding. This means we can get home in just two moves, and the
moves made above in HOW TO PLAY accomplish this.
You will notice that I only used the (1, 2, 3) move, which I think
should be the only one allowed. However, you might find the game
difficult enough without adding this restriction! If you would like a more
difficult challenge, you could limit yourself to the (1, 2, 3) combinations,
or even add lines to the program to prohibit any other move. For instance,
you might add the lines
216 LET X=ABS(A-ASC(D$))
217 LET Y=ABS(M-ASC(E$))
218 LET Z=ABS(O-ASC(F$))
219 IF X=Y OR X=Z OR Y=Z THEN 300
You will notice that in lines 166 and 167 I had to add (as an
afterthought) the limitation that the difference in start and finish
positions could not be odd. No such restriction was needed in the two-
dimensional game. Can you work out why this restriction is necessary?
"SIMON" 19

''Simon''
100 LET B$=""
110 PRINT "Q":PRINT:PRINT" THIS GAME 0
F SIMON TESTS YOUR MEMORY"
120 PRINT "WOULD YOU LIKE NUMBERS OR LET
TERS? PRESS THE KEY N OR L"
130 GET A$:IF A$="" THEN 130
135 IF A$<>"N" AND A$<>"L" THEN 110
140 LET I=1
150 PRINT "l:J"
160 IF A$="N" THEN LET B$=LEFT$(B$,I-1)+
RIGHT$(STR$(10*RND(0)) ,1)
170 IF A$="L" THEN LET B$=LEFT$(B$,I-1)+
CHR$(26*RND(0)+ASC("A"))
180 FOR J=1 TO I
190 LET S$=MID$(B$,J,1):PRINT S$;
195 IF A$="N" THEN N=VAL(S$)
196 IF A$="L" THEN N=ASC(S$)-ASC("A")
200 FOR T=1 TO 500:NEXT T
210 NEXT J
220 PRINT "l:J":PRINT:FOR G=0 TO 10*RND(0)
+4:GET S$:NEXT:REM PREVENTS CHEATING
230 PRINT "ENTER THE SAME ";
235 IF A$="N" THEN PRINT "NUMBERS"
240 IF A$="L" THEN PRINT "LETTERS"
250 GOTO 1000
260 PRINT:PRINT "WRONG. IT IS"
265 FOR G=0 TO 10:GET S$:NEXT:REM: ERASE
S ANY ACCIDENTAL KEY PRESSES
270 PRINT LEFT$(B$,I)
275 IF I>1 THEN PRINT "YOUR SCORE WAS ";
I-1
280 PRINT "ANOTHER TURN? PRESS Y OR N"
290 GET A$:IF A$="" THEN 290
300 IF A$="Y" THEN 100
305 STOP
310 PRINT:PRINT "RIGHT ON"
320 FOR T=1 TO 2000:NEXT T
330 LET I=I+1
340 GOTO 150
1000 REM:CHECKS EACH ENTRY
20 CONCENTRATION GAMES

1010 LET J=0


1020 GET SS:IF S$="" THEN 1020
1030 PRINT S$; :J=J+1
1040 IF AS="N" THEN N=VAL(S$)
1050 IF AS="L" THEN N=ASC(S$)-ASC("A")
1070 IF J<I AND SS=MIDS(BS,J,1) THEN 102
0
1080 IF S$=MID$(8$,J,1) THEN 310
1090 GOTO 260
Note: The symbol t:J represents SHIFT CLEAR/HOME.
"SIMON" 21

''Simon''
(Long Version)
10 REM: THE HI/LO FREQUENCIES FOR THE NO
TES ARE READ FROM DATA
20 REM:SET UP ORGAN
25 FOR I=54272 TO 54296:POKE I,0;NEXT I:
REM:THIS SETS ALL VOICE QUANTITIES TO 0
30 POKE 54296,15:REM SETS VOLUME TO LOUD
EST
35 FOR I=54277 TO 54291 STEP 7:POKE I,19
0:POKE I+1,255:NEXT:REM:SET SYNTHESIZER
40 PRINT "~":PRINT:PRINT" NEED INSTRU
CTIONS? PRESS Y OR N"
45 GET A$:IF A$="" THEN 45
50 IF A$<>"Y" THEN 100
55 PRINT:PRINT "WE GIVE SOUNDS TO THE NU
MBERS 0 TO 9"
60 PRINT:PRINT "THEY ARE:":PRINT
70 FOR N=0 TO 9:PRINT N:"GOSUB 8000
75 NEXT N
80 PRINT:PRINT:PRINT "LETTERS SOUND LIKE
THE FOLLOWING":PRINT
85 FOR N=0 TO 25:PRINT CHR$(ASC("A")+N);
:GOSUB 8000
90 NEXT N
100 PRINT "~":PRINT:PRINT" THIS GAME 0
F SIMON TESTS YOUR MEMORY"
120 PRINT "WOULD YOU LIKE NUMBERS OR LET
TERS? PRESS THE KEY N OR L"
130 GET A$:IF A$="" THEN 130
135 IF A$<>"N" AND AS<>"L" THEN 110
140 LET I=1
150 PRINT "~"
160 IF A$="N" THEN LET B$=LEFT$(B$,I-1)+
RIGHT$(STR$(10*RND(0)),1)
170 IF A$="L" THEN LET B$=LEFT$(B$,I-1)+
CHR$(26*RND(0)+ASC("A"))
180 FOR J=1 TO I
190 LET S$=MID$(B$,J,1) ;PRINTS$;
195 IF A$="N" THEN N=VAL(S$)
22 CONCENTRATION GAMES

196 IF A$="L" THEN N=ASC(S$)-ASC("A")


200 GOSUB 8000
210 NEXT J
220 PRINT "~":PRINT:FOR G=0 TO 10*RND(0)
+4:GET S$:NEXT:REM PREVENTS CHEATING
230 PRINT "ENTER THE SAME ";
235 IF A$="N" THEN PRINT "NUMBERS"
240 IF A$="L" THEN PRINT "LETTERS"
250 GOTO 1000
260 PRINT: PRINT "WRONG. IT IS"
265 FOR G=0 TO 10:GET SS:NEXT:REM: ERASE
S ANY ACCIDENTAL KEY PRESSES
270 PRINT LEFT$(B$,I)
275 IF I>1 THEN PRINT "YOUR SCORE WAS ";
I-1
280 PRINT "ANOTHER TURN? PRESS Y OR N"
290 GET A$;IF A$="" THEN 290
300 IF A$="Y" THEN 100
305 STOP
310 PRINT:PRINT "RIGHT ON"
320 FOR T=1 TO 2000:NEXT T
330 LET I=I+1
340 GOTO 150
1000 REM:CHECKS EACH ENTRY
1010 LET J=0
1020 GET S$:IF S$="" THEN 1020
1030 PRINTS$; :J=J+1
1040 IF A$="N" THEN N=VAL(S$)
1050 IF A$="L" THEN N=ASC(S$)-ASC("A")
1060 GOSUB 8000
1070 IF J<I AND S$=MID$(B$,J,1) THEN 102
0
1080 IF S$=MID$(B$,J,L) THEN 310
1090 GOTO 260
8000 REM: THIS SUBROUTINE PLAYS NOTES
8005 REM: THE HI/LO FREQUENCIES FOR THE
NOTES ARE READ FROM DATA
8006 RESTORE:FOR HL=0 TO N:READ HI,LO:NE
XT HL
8010 FOR V=54272 TO 54286 STEP 7:POKE V+
1,HI:POKE V,LO:NEXT V
8020 FOR V=54276 TO 54290 STEP 7:POKE V,
33:NEXT V:REM THIS SETS THE WAVEFORM
8030 FOR D=1 TO 500:NEXT D:REM THIS IS T
"SIMON" 23

HE DURATION OF THE NOTE


8040 FOR V=54276 TO 54290 STEP 7:POKE V,
0:NEXT V:REM THIS TURNS OFF THE NOTE
8050 RETURN
9000 REM: DATA LISTED HERE
9010 DATA 7,53,8,23,8,147,9 ,159,10,205,1
1 '114' 12' 216' 14' 107' 16' 4 7' 17' 37' 19' 63
9020 DATA 21,154,22,227,25, 177,28,214,32
,94,34,75,38,126, 43,52,45,198,51,9 7
9040 DATA 57,172,64,188,68, 149,76,252,86
' 105
24 CONCENTRATION GAMES

''Sinto n''
AIM OF THE GAME
We have all played "Simon Says" or "Follow the Leader" at some
time or other in our lives. This game provides two versions of Follow the
Leader. You have your choice between numbers or letters. A sequence is
displayed one character after another. At first, the sequence consists of a
single character. After a short period of time, the sequence vanishes from
sight, but hopefully, not from your memory, and you are asked to enter
the same sequence. If you are correct, the same sequence is repeated
with an extra character tacked on to the end.

HOW TO PLAY
When you run this program you are given the prompt
THE GAME OF SIMON TESTS YOUR MEMORY
WOULD YOU LIKE NUMBERS OR LETTERS?
PRESS THE KEY N OR L
You press the N key if you want numbers or the L key if you want a
sequence of letters. You should find numbers easier since the choice for
each member of the sequence is only ten possibilities. Suppose you press
the L key. Immediately a single letter appears in the top left hand corner
of the screen, say
B
This vanishes in about Y2 a second. You are then prompted with the
message
ENTER THE SAME LETTERS
Suppose you enter the letter B. The message
RIGHT ON
is displayed for a few seconds and then the sequence is repeated with an
additional letter. In this case it turned out to be BA followed by BAG,
followed by BAGM and so on.
This is a very demanding game and requires tremendous
concentration. In some respects it is much easier than "FLASH", since the
same sequence is repeated over and over. But this is the very aspect that
makes this game so mentally exhausting.
When you finally make a mistake, unintentional or not, you get the
message
"SIMON" 25

WRONG. IT IS
8AGMP
(or whatever the actual sequence was)
YOUR SCORE WAS 4
ANOTHER TURN? PRESS Y OR N
If you want to quit press Nor any key other than Y. If you wish to quit
during the game, press the key RUN/STOP in response to a prompt.

THE CODE
The string variable 8$ which stores the sequence is initialized to an
empty string in line 100. In lines 160 and 170, additional characters are
added to the string 8$ on the right hand end. Unfortunately, strings of
numbers like STR$(2) would be stored as" 2" and not "2". The blank is
stripped away in line 160, by using the RIGHT$( ... , 1) function.
Since the GET command only reads in one key press at a time and
your keyboard has a memory buffer which stores key presses, lines 220
and 265 are included to prevent cheating and an accidental response to
the prompt in line 280 about quitting.
In the long version, the lines before 100 set up an organ-like sound
and demonstrate the sounds given to each letter and number. The
subroutine at line 8000 plays the notes by setting the high and low
frequencies and turning on the voices with the sawtooth waveform (33).
After the duration in line 8030, the voices are turned off by changing the
waveform to 0.
The data in the lines after line 9000 contain the high and low
frequencies of the scale of notes used in the program.
If desired, you could store the frequencies of the twenty six notes in a
matrix A(25,1). This removes the slight delay in playing the notes. You
would need to make the following changes.
5 DIM A(25,1)
10 FOR I=0 TO 25:FOR J=0 TO 1 :READ A(
I , J ) : NEXT J , I
You would delete line 8006 and change 8010 to
8010 for V=54272 TO 54286 STEP 7:POKE
V+1,A(N,0) :POKE V,A(N,1) :NEXT V
The reason I did not use this was because if you use the DISK LOAD
and LOADER programs in the Appendix, an error is caused by the
dimension statement in line 5 and automatic execution is aborted.
28 WORD GAMES

''Anagrams I''
10 PRINT"C":PRINT:PRINT "INSTRUCTIONS? P
RESS Y OR N":PRINT
20 GET A$:IF A$="" THEN 20
30 IF AS<>"Y" THEN 85
40 PRINT "THIS GAME GIVES YOU A JUMBLED
STRING OF "
50 PRINT "LETTERS AND IT IS YOUR JOB TO
FIND THE
60 PRINT "HIDDEN WORD.THE STRING ROMST,F
OR EXAMPLE"
70 PRINT "IS A SCRAMBLED VERSION OF STOR
M."
80 PRINT:INPUT "PRESS RETURN TO START";
85 FOR L=54272 TO 54296:POKE L,0:NEXT:PO
KE 54296,15:REM SETS UP VOICES
90 FOR I=1 TO 3:V=54269+7*I:POKE V+1,190
:POKE V+2,250:NEXT
95 PRINT "t:l"
100 LET G=INT(6*RND(0)) :REM CHOOSES WORD
LIST
105 ON G GOTO 120,130,140,150,160
110 LET A$="ALLYBAILCHITDIMEDIGITFLIRTGR
AINHOUSEFIDGETHOURLYLININGNOZZLE"
115 LET A$=A$+"MOVABLEOPINIONPARTNERRECO
VER"
119 GOTO 200
120 LET A$="RILETORNCLIPPOURFIFTHSURLYSL
OTSARGUESLOWLYFIRMLYGROWTHMENACE"
125 LET A$=A$+"FIGURESTRUMPETSMARTENBAST
ION"
129 GOTO 200
130 LET A$="FROGHALFMASTRENTWHEELTRUSTST
UFFSCRUBSCRIPTREBORNPUBLICMUTTER"
135 LET A$=A$+"INTRUDEHAUGHTYGIRAFFEENCH
ANT"
139 GOTO 200
140 LET A$="GUSHHAILLOVERENDWHARFTRULYST
UDYSCREWSCREENREALTYPUCKERMUTINY"
145 LET A$=A$+"INSTANTHATRACKGHOSTLYDEVE
LOP"
"ANAGRAMS I" 29

149 GOTO 200


150 LET A$="AGOGBACKCHATDIALCHASMFLINGGE
NUSTHIGHHECTICJIGGERLIGATEMOTHER"
155 LET A$=A$+"LIGHTENNOXIOUSPAPRIKAREBO
UNO"
159 GOTO 200
160 LET A$="AIRYBAB ECHEFDIETCH ESTFIGHTGE
RMSHEDGEHEIGHTJESTERLIMPETNOTICE"
165 LET A$=A$+"MOUTHEDOILSKINPARABLEREAL
IZE"
200 LET W=INT(4*RND (0)) :REM CHOOSES ONE
OF FOUR WORDS
210 LET X=INT(4*RND (0))
220 LET L=X+4:REM CHOOSES WORD LENGTH
230 LET P=X*(2*X+14) +W*L+1
240 LET B$=MID$(A$, P,L) :REM PLUCKS OUT A
WORD
250 LET E$=B$
260 LET FS=B$
270 FOR I=1 TO L:REM SCRAMBLES THE WORD
280 LET J=INT(L*RND (0))+1
290 LET C$=MID$(F$ ,J,1)
300 IF C$=" " THEN 280
310 LET B$=LEFT$(B$ ,I-1)+C$+RIG HT$(B$,L-
I )
320 LET F$=LEFT$(F $,J-1)+" "+RIGHT$(F$ ,L
-J)
330 NEXT I
340 IF BS=E$ THEN 260:REM CHECKS THAT TH
E WORD HAS BEEN SCRAMBLED
350 FOR T=1 TO 1000: NEXT T
360 PRINT "r.l"
370 PRINT TAB(5) ;B$
380 PRINT:PRINT "ENTER THE UNSCRAMBLED W
ORO"
390 PRINT:PRINT "ENTER TO QUIT"
400 PRINT:INPUT 0$
410 IF 0$="." THEN PRINT "THE WORD IS ";
ES:GOTO 470
420 IF DS<>E$ THEN 450
430 PRINT:PRINT "RIGHT ON! NOW TRY THIS"
:Z=1:GOSUB 9000
440 GOTO 100
450 PRINT:PRINT "WRONG! TRY AGAIN":Z=0:G
30 WORD GAMES

OSUB 9000
460 GOTO 350
470 PRINT:PRINT "AGAIN? PRESS Y OR N"
480 GET AS:IF A$="" THEN 480
490 IF AS="Y" THEN 95
500 STOP
9000 REM PLAYS MUSIC
9010 RESTORE:IF Z=0 THEN 9020
9015 FOR I=1 TO 7*5:READ D:NEXT
9020 FOR I=1 TO 3:READ H(I),L(I):NEXT I:
READ D
9025 FOR I=1 TO 3:POKE 54269+7*I,33:NEXT
9030 IF D=-1 THEN 9080
9040 FOR I=1 TO 3:V=54266+7*I:POKE V,H(I
) :POKE V-1, L (I) :NEXT I
9050 FOR T=1 TO D:NEXT T
9060 GOTO 9020
9080 FOR I=1 TO 3:POKE 54269+7*I,0:NEXT:
REM TURNS OFF NOTE
9090 RETURN
9900 DATA 17,37,12,216,10,205,400
9910 DATA 14,107,10,205,8,147,400
9920 DATA 12,216,10,205,7,53,400
9930 DATA 10,205,8,147,7,53,400
9940 DATA -1,-1,-1,-1,-1,-1,-1
9950 DATA 38,126,28,214,9,159,400
9960 DATA 28,214,28,214,12,32,100
9970 DATA 32,94,25,177,12,216,100
9980 DATA 28,214,24,63,9,159,100
9990 DATA -1,-1,-1,-1,-1,-1,-1
Note: The symbol t.:l represents SHIFT CLEAR/HOME.
"ANAGRAMS I" 31

''An agra ms I''


AIM OF THE GAME
Some people are good at anagrams, making words out of a jumble of
letters. This program is designed to improve your skill with anagrams.
Mathematics can sometimes be like sorting out an anagram. You
have all the facts, all the data, but they are jumbled up. With training, you
learn how to make sense out of the chaos. By trial and error you patiently
work through the different possibilities, until a flash of inspiration or
insight guides you to the correct solution.
Of course, the list of words is short and fixed, so to make the game
more interesting, you should have the word list altered by a friend, and
then test your skill. To save memory space and to disguise the words, I
have placed the "dictionary" of words in A$. There are 4 words of 4
letters followed by 4 of 5 letters, then 4 of 6 letters and finally 4 of 7
letters. You must replace words in precisely this fashion because the
program is designed to pick out a sequence of letters from A$ that forms
a word. I used a mathematical technique called interpolation to do this.

BOW TO PLAY
When you run this program a scrambled word containing from 4 to 7
letters is displayed in this fashion
FHAWR
ENTER THE UNSCRAMB LED WORD
ENTER . TO QUIT
After you enter your solution to the anagram, it is compared with the
correct word and the message
RIGHT ON! NOW TRY THIS
or
WRONG! TRY AGAIN
appears. If you are right a new anagram is posed. If you were wrong, the
same anagram is repeated.
If you get stuck on a word, enter a period (.) when prompted to enter
the answer. The program will respond with the message
THE WORD IS WHARF (or whatever it is)
AGAIN? PRESS Y OR N
If you press the Y key another scrambled word is given to you. If you
press the key N, the program will stop.
32 WORD GAMES

THE MATH EMAT ICS AND CODE


The start of the four letter words is at the first letter of A$. The start
of the five letter words is at letter 17 of A$. Six letter words start at the
37th letter of A$ and seven letter words at the 61 st letter of A$. The
formula
P = X*(2*X+1 4) + 1
is used in line 230 to find the starting point of the words, where the
length of the words is X+4.
Let us try out the formula. For X=0 (words of length 4), P=l. For
X= 1 (words of length 5), P= 17 and so on. A method of finding this
formula is called Lagrange Interpolation. What I needed wasP= 1 for X=0,
P=17 for X=1, P=37 for X=2, P=61 for X=3.1 decided to subtract 1
from all the values of P to have easier numbers. So now I wanted P=0 for
X=0, P=16 for X=1, P=36 for X=2 and P=60 for X=3.
We construct the required function P of X by using polynomials. A
polynomial P , for example, is a function like P = 2x 2 - 3x + 4 . We build
the polynomial P from basic polynomials that have the value 1 at only one
point and are zero for all of the other points. In this case, we want a
polynomial that is zero at X = 0, 2 and 3. We start with R = X(X-2)(X- 3)
which has the required values of zero. We want R = 1 at X = 1. In this
case, R = 2, so we divide by 2 to get
R = X(X-2) (X-3)/2
In a similar fashion, we define
S = -X(X-1)(X -3)/2
which has the value 1 at X = 2 and is zero at X = 0, 1 and 3. Finally, we
define
T = X(X-1)(X- 2)/6
which has the value 1 at X = 3 and is zero at X = 0, 1 and 2.
The interpolating polynomial P that we require is a combination of
these basic polynomials.
P = R*16 + S*36 + T*60
= X*(2*X + 14)
"ANAGRA MS II" 33

''A nag ram s II''


(W ith Hin ts)
10 PRINT" O":PRI NT:PRI NT "INSTRUCTIONS?P
RESS Y OR N":PRI NT
20 GET A$:IF A$="" THEN 20
30 IF AS<>"Y" THEN 85
40 PRINT "THIS GAME GIVES YOU A JUMBLED
STRING OF "
50 PRINT "LETTERS AND IT IS YOUR JOB TO
FIND THE
60 PRINT "HIDDEN WORD.THE STRING ROMST,F
OR EXAMPLE"
70 PRINT "IS A SCRAMBLED VERSION OF STOR
M."
80 PRINT:INPUT "PRESS RETURN TO START";
85 FOR L=5427 2 TO 54296:P OKE L,0:NE XT:PO
KE 54296,1 5:REM SETS UP VOICES
90 FOR I=1 TO 3:V=54 269+7* I:POKE V+1,19 0
:POKE V+2,25 0:NEXT
95 PRINT "0"
100 LET G=INT (6*RND (0)) :REM CHOOSES WORD
LIST
105 ON G GOTO 120,13 0,140, 150,16 0
110 LET A$="ALLYBAILCHITDIMEDIGITFLIRTGR
AINHOUSEFIDGETHOURLYLININGNOZZLE"
115 LET A$=A$+"MOVABLEOPINIONPARTNERRECO
VER"
119 GOTO 200
120 LET A$="RILETORNCLIPPOURFIFTHSURLYSL
OTSARGUESLOWLYFIRMLYGROWTHMENACE"
125 LET A$=A$+"FIGURESTRUMPETSMARTENBAST
ION"
129 GOTO 200
130 LET A$="FROGHALFMASTRENTWHEELTRUSTST
UFFSCRUBSCRIPTREBORNPUBLICMUTTER"
135 LET A$=A$+"INTRUDEHAUGHTYGIRAFFEENCH
ANT"
139 GOTO 200
140 LET A$="GUSHHAILLOVERENDWHARFTRULYST
UDYSCREWSCREENREALTYPUCKERMUTINY"
34 WORD GAMES

145 LET A$=A$+"INSTANTHATRACKGHOSTLYDEVE


LOP"
149 GOTO 200
150 LET A$="AGOGBACKCHATDIALCHASMFLINGGE
NUSTHIGHHECTICJIGGERLIGATEMOTHER"
155 LET A$=A$+"LIGHTENNOXIOUSPAPRIKAREBO
UNO"
159 GOTO 200
160 LET A$="AIRYBABECHEFDIETCHESTFIGHTGE
RMSHEDGEHEIGHTJESTERLIMPETNOTICE"
165 LET A$=A$+"MOUTHEDOILSKINPARABLEREAL
IZE"
200 LET W=INT(4*RND(0)) :REM CHOOSES ONE
OF FOUR WORDS
205 LET C=0
210 LET X=INT(4*RND(0))
220 LET L=X+4:REM CHOOSES WORD LENGTH
230 LET P=X*(2*X+14)+W*L+1
240 LET B$=MID$(A$,P,L) :REM PLUCKS OUT A
WORD
250 LET E$=B$
260 LET F$=B$
270 FOR 1=1 TO L:REM SCRAMBLES THE WORD
280 LET J=INT(L*RND(0))+1
290 LET C$=MID$(F$,J,1)
300 IF C$=" " THEN 280
310 LET B$=LEFT$(B$,1-1)+C$+RIGHT$(B$,L-
I )
320 LET F$=LEFT$(F$,J-1)+" "+RIGHT$(F$,L
-J)
330 NEXT I
340 IF B$=E$ THEN 260:REM CHECKS THAT TH
E WORD HAS BEEN SCRAMBLED
350 FOR T=1 TO 1000: NEXT T
360 PRINT "C"
370 PRINT TAB(5) ;B$
380 PRINT:PRINT "ENTER THE UNSCRAMBLED W
ORO"
390 PRINT:PRINT "ENTER TO QUIT"
400 PRINT:INPUT 0$
410 IF 0$="." THEN PRINT "THE WORD IS ";
E$:GOTO 470
420 IF D$<>E$ THEN 450
430 PRINT:PRINT "RIGHT ON! NOW TRY THIS"
"ANAGRAMS II" 35

: Z= 1 : GOSUB 9000
440 GOTO 100
450 PRINT:PRI NT "WRONG! TRY AGAIN":Z =0:G
OSUB 9000
460 GOTO 510
470 PRINT:PRI NT "AGAIN? PRESS Y OR N"
480 GET A$:IF A$="" THEN 480
490 IF A$="Y" THEN 95
500 STOP
510 PRINT:PRI NT "WANT SOME HELP? PRESS Y
OR N"
520 GET Z$:IF Z$="" THEN 520
530 IF Z$<>"Y" THEN 360
540 LET C=C+1
550 PRINT:PRI NT "THE WORD STARTS WITH":P
RINT
560 PRINT TAB(5) ;LEFT$(E$ ,C)
570 GOTO 350
9000 REM PLAYS MUSIC
9010 RESTORE: IF Z=0 THEN 9020
9015 FOR I=1 TO 7*5:READ D:NEXT
9020 FOR I=1 TO 3:READ H(I) ,L(I) :NEXT I:
READ D
9025 FOR I=1 TO 3:POKE 54269+7*I ,33:NEXT
9030 IF D=-1 THEN 9080
9040 FOR I=1 TO 3:V=54266 +7*I:POKE V,H(I
):POKE V-1,L(I) :NEXT I
9050 FOR T=1 TO D:NEXT T
9060 GOTO 9020
9080 FOR I=1 TO 3:POKE 54269+7*I ,0:NEXT:
REM TURNS OFF NOTE
9090 RETURN
9900 DATA 17,37,12, 216,10,20 5,400
9910 DATA 14,107,10 ,205,8,14 7,400
9920 DATA 12,216,10 ,205,7,53 ,400
9930 DATA 10,205,8, 147,7,53, 400
9940 DATA -1,-1,-1, -1,-1,-1, -1
9950 DATA 38,126,28 ,214,9,15 9,400
9960 DATA 28,214,28 ,214,12,3 2,100
9970 DATA 32,94,25, 177,12,21 6,100
36 WORD GAMES

9980 DATA 28,214,24,63,9,159,100


9990 DATA -1,-1,-1,-1,-1,-1,-1
Note: The symbol C represents SHIFT CLEAR/HOME.
"ANAGRAMS II" (WITH HINTS) 37

''Anagrams II''
(With Dints)
If you were having trouble finding the correct word for the anagrams
in the previous program, then you might prefer this version. It is the same
game, except that if you are wrong, you are then prompted with
WANT SOME HELP? PRESS Y OR N
If you press the key Y, then you are told that
THE WORD STARTS WITH
w
for the case of WHARF scrambled up as FHAWR.
After a short pause, the hint disappears and the anagram is again
presented. If you are still stuck and enter a wrong answer, and if you want
more help, you are told that the word starts with
WH
and so on. Eventually the entire word will be presented to you, but I am
sure that with a little help you will be able to solve the anagram.
38 WORD GAMES

''Han gman ''


10 PRINT "C":PRINT :PRINT "INSTRUCTIONS?
PRESS Y OR N":PRINT
20 GET A$:IF A$="" THEN 20
30 IF A$<>"Y" THEN 100
40 PRINT "YOU MUST DISCOVER WHAT THE WOR
D IS BY"
50 PRINT "GUESSING LETTERS. YOU ONLY HAV
E 13 MOVES"
60 PRINT:INPUT "PRESS RETURN TO START";
100 PRINT "C":LET G=INT(6*R ND(0)) :REM RA
NDOMLY CHOOSES WORD LIST
101 LET Z$="***** **":REM FORMS A SCREEN
102 LET L$=""
105 ON G GOTO 120,130,1 40,150,16 0
110 LET A$="ALLYBAILCHITDIMEDIGITFLIRTGR
AINHOUSEFIDGETHOURLYLININGNOZZLE"
115 LET A$=A$+"MOVABLEOPINIONPARTNERRECO
VER"
119 GOTO 200
120 LET A$="RILETORNCLIPPOURFIFTHSURLYSL
OTSARGUESLOWLYFIRMLYGROWTHMENACE"
125 LET A$=A$+"FIGURESTRUMPETSMARTENBAST
ION"
129 GOTO 200
130 LET A$="FROGHALFMASTRENTWHEELTRUSTST
UFFSCRUBSCRIPTREBORNPUBLICMUTTER"
135 LET A$=A$+"INTRUDEHAUGHTYGIRAFFEENCH
ANT"
139 GOTO 200
140 LET A$="GUSHHAILLOVERENDWHARFTRULYST
UDYSCREWSCREENREALTYPUCKERMUTINY"
145 LET A$=A$+"INSTANTHATRACKGHOSTLYDEVE
LOP"
149 GOTO 200
150 LET A$="AGOGBACKCHATDIALCHASMFLINGGE
NUSTHIGHHECTICJIGGERLIGATEMOTHER"
155 LET A$=A$+"LIGHTENNOXIOUSPAPRIKAREBO
UNO"
159 GOTO 200
160 LET A$="AIRYBABECHEFDIETCHESTFIGHTGE
''HANGMAN'' 39

RMSHEDGEHEIGHTJESTERLIMPETNOTICEff
165 LET A$=A$+ffMOUTHEDOILSKINPARABLEREAL
I ZEn
200 LET W=INT(4*RND(0)) :REM PICKS ONE OF
FOUR WORDS
210 LET X=INT(4*RND(0))
220 LET L=X+4:REM PICKS WORD LENGTH
230 LET P=X*(2*X+14)+W*L+1 :REM INTERPOLA
TES
240 LET B$=MID$(A$,P,L) :REM PLUCKS WORD
OUT OF A$
250 LET M=13:REM SETS LIMIT ON NUMBER OF
MOVES
255 LET L=LEN(B$)
260 LET F$=MID$(Z$,1,L)
270 GOTO 360
280 FOR I=1 TO L:REM CHECKS IF ENTERED L
ETTER IS IN THE WORD
290 IF MID$(B$,I,1)=D$ THEN LET F$=LEFT$
(F$,I-1)+D$+RIGHT$(F$,L-1)
300 NEXT I
360 PRINT ff~ff:PRINT:PRINT
370 PRINT TAB(5) ;FS:PRINT
375 IF F$=B$ THEN 430:REM CHECK IF WORD
HAS BEEN FOUND
376 IF M<1 THEN PRINT ffTHE WORD IS ff;B$:
GOTO 440:REM NO MOVES LEFT
380 PRINT M;ffMOVES TO GOff
390 PRINT:PRINT ffTYPE A LETTER USE A . T
0 QUITff:PRINT
395 LET M=M-1
396 PRINT ffLETTERS USED SO FARff:PRINT:PR
INT L$:PRINT
400 GET ZS:IF Z$=ffff THEN 400
405 LET D$=Z$
410 IF D$=ff.ff THEN PRINT ffTHE WORD IS ff;
BS:GOTO 440
415 FOR I=1 TO LEN(L$):IF D$=MID$(L$,I,1
) THEN 420
416 NEXT I
417 LET L$=L$+D$
420 GOTO 280
430 PRINT ffRIGHT ONff
440 PRINT: PRINT ffAGAIN? PRESS Y OR Nff
~----------------------------

40 WORD GAMES

450 GET Z$:IF ZS="" THEN 450


460 IF ZS="Y" THEN 100
470 STOP
Note: The symbol t:l represents SHIFT CLEAR/HOME.
''HANGMAN'' 41

''Hangman''
AIM OF THE GAME
This game is a version of the children's game, only there are no
gallows and stick-figures. It is enough to know that you only have 13
attempts to find the correct word. This is more difficult, as a result, than
"ANAGRAMS" where you not only know the length of the word, but also
which letters appear in it.

DOW TO PLAY
When you run this program you are presented with a message like
* * * * * * *
13 MOVES TO GO
TYPE A LETTER USE A . TO QUIT
LETTERS USED SO FAR
Suppose you enter the letter A, then the message is repeated with 12 in
place of 13. Now suppose you enter the letter E, then the message
becomes

11 MOVES TO GO
TYPE A LETTER USE A . TO QUIT
LETTERS USED SO FAR
AE
You continue in this fashion until you run out of moves, or you find all the
letters in the word. If you run out of moves, the word is given to you. If you
get frustrated, and want to quit, just enter a period (.) instead of a letter
and the word is given to you. In any case, you will be invited to play again,
or to quit with the prompt
AGAIN? PRESS Y OR N
If you would like to alter the number of guesses allowed, change
M= 13 in line 250 to something that suits your fancy.
This program is a very simple program with no audio-visual effects.
You might like to add your own. The usual graphics for this game is a
gallows and a stick figure which is gradually drawn with each guess that is
wrong. The program does not check repeated entries so that if you fail to
notice a letter in the used letter list and use it a second time, it is a wasted
move. It would be a simple exercise for the beginning programmer to add
lines to run a check on the entry and print a message if the letter has
already been used. Take a look at lines 280 to 300 and use that for a
42 WORD GAMES

guide. Replace 8$ with L$ in the checking part, and change the


conclusion after the THEN.
"FIND A WORD" 43

''Find a Word''
10 PRINT "a":PRINT:PRINT "FIND A WORD IS
A GAME WHERE YOU HAVE TO "
20 PRINT "FIND WORDS HIDDEN IN A SQUARE
OF LETTERS"
30 PRINT "THERE ARE FROM 7 TO 11 WORDS,U
P AND DOWN"
40 PRINT "ACROSS AND DIAGONAL"
50 PRINT:PRINT "ENTER . TO QUIT"
60 PRINT:INPUT "PRESS RETURN TO START";
100 PRINT "~":PRINT:PRINT "GET A SNACK,
THIS WILL TAKE A MOMENT"
101 FOR I=0 TO 9:FOR J=0 TO 9:M$(I,J)="*
":NEXT J,I:REM FILLS M WITH"*"
102 LET KK=6+INT(5*RND(0)) :N=KK:REM CHOO
SES NUMBER OF WORDS
103 WL=0:REM CHECKS NUMBER OF LETTERS IN
ALL WORDS
104 FOR K=0 TO KK:REM SELECTS WORDS
105 LET G=INT(6*RND(0))
108 ON G GOTO 120,130,140,150,160
110 LET A$="ALLYBAILCHITDIMEDIGITFLIRTGR
AINHOUSEFIDGETHOURLYLININGNOZZLE"
115 LET A$=A$+"MOVEABLEOPINIONPARTNERRECO
VER"
119 GOTO 200
120 LET A$="RILETORNCLIPPOURFIFTHSURLYSL
OTSARGUESLOWLYFIRMLYGROWTHMENACE"
125 LET A$=A$+"FIGURESTRUMPETSMARTENBAST
ION"
129 GOTO 200
130 LET A$="FROGHALFMASTRENTWHEELTRUSTST
UFFSCRUBSCRIPTREBORNPUBLICMUTTER"
135 LET A$=A$+"INTRUDEHAUGHTYGIRAFFEENCH
ANT"
139 GOTO 200
140 LET A$="GUSHHAILLOVERENDWHARFTRULYST
UDYSCREWSCREENREALTYPUCKERMUTINY"
145 LET A$=A$+"INSTANTHATRACKGHOSTLYDEVE
LOP"
149 GOTO 200
44 WORD GAMES

150 LET A$="AGOGBACKCHATDIALCHASMFLINGGE


NUSTHIGHHECTICJIGGERLIGATEMOTHER"
155 LET A$=A$+"LIGHTENNOXIOUSPAPRIKAREBO
UNO"
159 GOTO 200
160 LET A$="AIRYBABECHEFDIETCHESTFIGHTGE
RMSHEDGEHEIGHTJESTERLIMPETNOTICE"
165 LET A$=A$+"MOUTHEDOILSKINPARABLEREAL
IZE"
200 LET W=INT(4*RND(0)) :REM CHOOSES ONE
OF FOUR WORDS
210 LET X=INT(4*RND(0))
220 LET L=X+4:REM CHOOSES WORD LENGTH
230 LET P=X(2*X+14)+W*L+1
240 LET B$(K)=MID$(A$,P,L) :REM PLUCKS OU
T A WORD
241 IF K=0 THEN 246
242 FOR I=0 TO K-1:IF B$(K)=B$(I) THEN 1
05:REM CHECKS FOR DUPLICATES
245 NEXT I
246 WL=WL+LEN(B$(K))
250 NEXT K
255 IF WL>55 THEN 102:REM IF TOTAL LETTE
RS >55 CHOOSE A NEW SET OF WORDS
260 FOR K=0 TO KK
265 L=LEN(B$(K))
270 I=INT(10*RND(0)) :J=INT(10*RND(0)) :RE
M CHOOSES STARTING POINT OF WORD
272 FOR M=0 TO 7:C(M)=1:NEXT M
275 FOR S=0 TO L-1 :REM CHECKS FOR WAY TH
E WORD CAN RUN
280 IF J+S>9 THEN C(0)=0
281 IF J+S>9 THEN 285
282 IF M$(I,J+S)<>"*" THEN C(0)=0
285 IF J-S<0 THEN C(1)=0
286 IF J-S<0 THEN 290
287 IF M$(I,J-S)<>"*" THEN C(1)=0
290 IF(I+S>9 OR J+S>9) THEN C(2)=0
291 IF(I+S>9 OR J+S>9) THEN 295
292 IF M$(I+S,J+S)<>"*" THEN C(2)=0
295 IF(I-S<0 OR J-S<0) THEN C(3)=0
296 IF(I-S<0 OR J-S<0) THEN 300
297 IF M$(I-S,J-S)<>"*" THEN C(3)=0
300 IF(I-S<0 OR J+S>9) THEN C(4)=0
"FIND A WORD" 45

301 IF(I-S<0 OR J+S>9) THEN 305


302 IF M$(I-S,J+S)<>"*" THEN C(4)=0
305 IF(I+S>9 OR J-S<0) THEN C(5)=0
306 IF(I+S>9 OR J-S<0) THEN
307 IF M$(1+S,J-S)<>"*" THEN C(5)=0
310 IF I+S>9 THEN C(6)=0
311 IF I+S>9 THEN 315
312 IF M$(I+S,J)<>"*" THEN C(6)=0
315 IF I-S<0 THEN C(7)=0
316 IF I-S<0 THEN 320
317 IF M$(I-S,J)<>"*" THEN C(7)=0
320 NEXT S
325 FOR M=0 TO 7:IF C(M)=1 THEN 338
330 NEXT M
335 GOTO 270
338 M=INT(8*RND(0)) :IF C(M)=0 THEN 338:R
EM CHOOSES DIRECTION OF WORD
340 ON M+1 GOTO 341,342,343,344,345,346,
347,348
341 U=0:8=1:GOTO 350:REM SETS UP AND ACR
OSS PARAMETERS
342 U=0:B=-1 :GOTO 350
343 U=1:8=1 :GOTO 350
344 U=-1:B=-1:GOTO 350
345 U=-1:8=1:GOTO 350
346 U=1:B=-1 :GOTO 350
347 U=1:B=0:GOTO 350
348 U=-1 :8=0
350 FOR S=0 TO L-1:M$(I+S*U,J+S*B)=MID$(
8$(K),S+1,1) :NEXT S:REM PLACES WORDS
355 NEXT K
358 IF N=-1 THEN 400
359 D$(N)=""
360 PRINT "~":PRINT "FIND";N+1;"WORDS";
361 FOR I=0 TO 9:FOR J=0 TO 9
362 IF M$(I,J)="*" THEN M$(I,J)=CHR$(ASC
("A")+INT(26*RND(0))) :REM FILLS M$
363 NEXT J,I
365 FOR I=0 TO 9:PRINT:PRINT:FOR J=0 TO
9:PRINT " ";M$(I,K); :NEXT J,I
366 PRINT "00":PRINT:PRINT:PRINT:PRINT TA
8(25);"WORDS FOUND"
367 FO~ I=KK TON STEP -1:PRINT TAB(25);
D$(I) :NEXT I
46 WORD GAMES

368 PRINT "~":FOR I=1 TO 21 :PRINT:NEXT I


:REM PRINTS AT SCREEN BOTTOM
370 PRINT:INPUT "ENTER A WORD YOU SEE";D
$(N)
371 IF N=KK THEN 375
372 FOR K=KK TO N+1 STEP -1:IF D$(N)=D$(
K) THEN 358:REM CHECKS REPEAT ENTRY
374 NEXT K
375 IF D$(N)="." THEN 500
380 FOR K=0 TO KK:IF D$(N)<>B$(K) THEN 3
90:REM CHECKS ENTRY AGAINST WORD LIST
385 PRINT "CORRECT":N=N-1 :GOTO 399
390 NEXT K
395 PRINT "SORRY THAT IS NOT ONE THAT CO
UNTS"
399 FOR T=1 TO 1000:NEXT T:GOTO 358
400 PRINT "~":PRINT:PRINT "WELL DONE! Y
OU FOUND ALL THE WORDS"
410 PRINT:PRINT "AGAIN? PRESS Y OR N"
420 GET A$:IF A$="" THEN 420
430 IF A$="Y" THEN 100
440 STOP
500 PRINT "~":PRINT "THE WORDS WERE":PRI
NT
510 FOR K=0 TO KK:PRINT 8$(K) :NEXT K:GOT
0 410
Note: The symbol ~ represents SHIFT CLEAR/HOME, and
~ represents HOME.
"FIND A WORD" 47

''Find a Word' '


AIM OF THE GAME
Using the same dictionary of words as in the previous two games, we
can make a very challenging game of find a word. Words are hidden in a
10 by 10 square of letters. The words can go up or down, across or back,
or even diagonally. A random number of words are placed randomly in the
square and the remaining positions in the square are filled with letters.
You have to locate the hidden words.

HOW TO PLAY
There will be at least 7 words and at most 11 words hidden in the
square. You will be told how many there are. When you RUN this game it
takes about one minute to set up the puzzle. While this is going on, you
get the message

GET A SNACK THIS TAKES A MOMENT


Then you will be presented with a puzzle like
FIND 8 WORDS

X K E s u 0 H J u H
T R u M p E T E L A
L J X B y 0 F s N u WORDS FOUND
p F L L F E p T c G
y B u L H L D E A H
0 R v s F B w R X T
T J u N T A X w B y
s u N E G R T v N A
s G z s A A B A I L
c M p F u p H K R E

ENTER A WORD YOU SEE?


If you enter the word BAIL, the message is repeated, only there will be
7 words to find, and the list of words found will contain the word BAIL.
You may find a word that accidently appears in the square. If your
entry does not match one of the chosen words, you are told

SORRY THAT IS NOT ONE THAT COUNTS


and then the previous prompt is repeated.
48 WORD GAMES

It will not accept the same word twice. If you want to quit, just enter .
(a period) when asked to enter a word. The list of words is displayed and
you are prompted with
AGAIN? PRESS Y OR N
You press the key N to quit or Y to play another game.

THE CODE
This is a very long program for this book. The main difficulty was
fitting the words into the square. The words are not allowed to intersect
one another. Otherwise we would have to make sure that they intersected
in a common letter and this would be too time consuming. I made the
choice of not letting words intersect. But then it might become impossible
to locate all the words in the 10 by 10 array. By trial and error, I found
that if the chosen words had more than 65 letters in total, it might be
impossible to locate all the words in the 100 places available. So in line
255, if the total number of letters is greater than 55 (a considerable
margin for error), then a new list of words is generated. You may
experiment with the number of letters allowed by changing the limit in line
255.
Fitting the words into the square is done in lines 260 to 355. This is
the part that takes all the time in setting up the puzzle. The square M$ is
filled with "*" to start with in line 101. A random row I and column J is
selected in line 170. Lines 272 to 320 check to see which way the word
could run, forward, backward, diagonally down to the right, diagonally up
to the left, diagonally up to the right, diagonally down to the left, vertically
down or up. The vector C contains a 1 or a 0 in each of eight positions
corresponding to the above possibilities. A 0 means that that particular
choice is blocked. Line 338 chooses one of the possible ways of locating
the word. Lines 340 to 348 select the appropriate parameters and line
350 locates the word. For instance, if the word is being placed diagonally
down to the left, starting from (I,J), U would be 1 and B would be 1.
Then:
M$(1,J) = the first letter in the word B$(K)
M$(1+1,J+1) = the second letter in the word B$(K)
M$(1+2,J+2) = the third letter in the word B$(K)
"FIND A WORD" 49

If the word was BAIL and (I,J) was (3,5), then we would have

Column 5
Row 3 8
A
I
L

Finally, lines 361 to 363 fill up the spaces in M$ that still contain *'s
with randomly chosen letters. I use this loop to create a pause during the
playing of the game so that the message about how many words to go is
displayed on its own.
I did not add any audio-visual effects as I thought the listing was too
long as it was. You might like to try the following. When a word is correctly
located, it is displayed in reverse letters, or in a different color, or simply
flashes for a moment while the congratulatory music plays!
50 WORD GAMES

''Enigma I''
110 PRINT "r.J"
120 LET C=INT(25*RND(0))+1
130 LET Z=ASC("Z")
135 PRINT "ENTER YOUR MESSAGE AND I WILL
CODE IT."
136 PRINT
137 PRINT "USE A ";CHR$(34) ;".";CHR$(34)
;"TO END CODE."
138 PRINT
140 GET A$:IF A$="" THEN 140
150 LET B=ASC(A$)
160 IF A$=" "THEN PRINT" "; :GOTO 140
170 IF A$="." THEN 220
180 LET D=B+C
185 IF A$<"A" OR AS>"Z" THEN PRINT A$; :G
OTO 140
190 IF D>Z THEN LET D=D-26
200 PRINT CHR$(D);
210 GOTO 140
220 PRINT
230 PRINT "THE CODE IS PLUS?"
240 INPUT E
250 IF E=C THEN PRINT "RIGHT ON"
260 IF E<>C THEN PRINT "WRONG IT IS";C
270 PRINT:PRINT "AGAIN? PRESS Y OR N"
275 GET A$:IF A$="" THEN 275
280 IF A$="Y" THEN 110
290 STOP
Note: In lines 140 and 275, the double quotes""define an empty
string. The symbol rJ represents SHIFT CLEAR/HOME.
"ENIGMA I" 51

''Enigma I''
AIM OF THE GAME
This program converts your computer into a coding typewriter,
something like the German coder of the second world war, which was
called ENIGMA. As you type in a message, it is displayed in coded form.
You terminate your message by typing a period. A space is accepted so
you may use the space key to type a space in your message.
After your message has been typed, you are asked to solve the code.
The letters in the alphabet have simply been shifted to the right by a
constant number of letters. This is the number you are prompted to enter.
Of course it will not be challenging for you because you know what
the original message was! But challenge a friend to solve the code. One
person types in a message and then the other attempts to decode it.
After a little experience you will be a master at decoding secret
messages.
This is a very simple form of coding and would be too easy to crack.
Mathematicians and computer scientists are working on developing codes
that are almost impossible to crack. So much important personal and
financial information is stored in data banks, you can imagine how
important it is to protect that information from unauthorized use.

DOW TO PLAY
The program commences with the prompt
ENTER YOUR MESSAGE AND I WILL CODE IT
USE A . TO END CODE.
Your keyboard will now act like a typewriter. Every key you press will give
an instant response. If you press a letter key, a different letter will appear
on the screen. Other characters will be typed as themselves except for a
period, which will end the input. You will then be asked the question
THE CODE IS PLUS?
You enter your answer, it is checked and a message
RIGHT ON
or
WRONG IT IS 15 (or some such number).
Then you will be invited to play again or to quit.
You might like to try a much more challenging version of "ENIGMA".
In this version, the letters of the alphabet are randomly mixed up! You
52 WORD GAMES

could not find a more difficult code to break. You have to look at patterns
of repeating letters and three letter words and, by trial and error, try to
break the code.
"ENIGMA II" 53

''Enigma II''
100 LET Y=ASC("A")-1
110 PRINT "I:J"
115 PRINT "GENERATING THE CODE"
116 C$="":D$="":REM INITIALIZE
119 FOR I=0 TO 25
121 LET C$=C$+STR$(ASC("A")+1)
123 NEXT I
125 FOR I=1 TO 26
127 LET J=INT(26*RND(0))+1
129 IF MID$(C$1JI1)="*" THEN 127:REM CHE
CKS IF LETTER HAS BEEN ASSIGNED
131 LET C$=LEFT$(C$~J-1)+"*"+RIGHT$(C$ 1 2
7-J) :REM REPLACES LETTER BY *
133 LET B$=B$+CHR$(J+Y) :REM Y DEFINED IN
LINE 1001 THIS CHOOSES A RANDOM LETTER
134 NEXT I
135 PRINT "I:J":PRINT:PRINT "ENTER YOUR ME
SSAGE AND I WILL CODE IT"
136 PRINT:PRINT "USE A ";CHR$(34);".";CH
R$(34);" TO END CODE."
140 GET A$: IF A$="" THEN 140
150 LET B=ASC(A$)-Y:REM B IS A NUMBER FR
OM 1 TO 26
170 IF A$="." THEN 220
175 IF A$<" A" OR A$>"Z" THEN PRINT A$;: G
OTO 9010:REM USE GOTO 140 TO OMIT SOUND
180 LET D$=MID$(B$1BI1) :REM CHOOSES THE
CODED FORM OF INPUT LETTER
190 PRINT D$; :GOTO 9010:REM MAKES SOUND
OF OLD TYPEWRITER (MAY OMIT GOTO 9010)
200 GOTO 140
220 PRINT:PRINT
230 PRINT "PRESS 0 FOR THE CODE 1 FOR A
I

NEW CODE"
235 GET A$:IF A$="" THEN 235
240 E=VAL(A$)
250 IF E<>0 THEN 110
260 PRINT "I:J"
270 FOR I=1 TO 26
280 PRINT CHR$(Y+1) ;"=";MID$(B$1II1)I
54 WORD GAMES

290 NEXT I
295 PRINT:PRINT:PRINT "AGAIN? PRESS Y OR
N"
296 GET A$:IF A$="" THEN 296
297 IF A$="Y" THEN 110
298 STOP
300 PRINT" ";
310 GOTO 140
9000 REM:SOUND OF ANCIENT TYPEWRITER
9010 V=54296:W=54276:A=54277:H=54273:L=5
4272
9020 FOR X=13 TO 10 STEP-1 :POKEV,X:POKEW
,129:POKEA,99:POKE H,40:POKE L,200:NEXT
9030 POKE W,0:POKE A,0:GOTO 140
Note: The symbol C represents SHIFT CLEAR/HOME.
"ENIGMA II" 55

''Enigma II''
HOW TO PLAY
When you run this program it commences with the message
GENERATING THE CODE
After a short period of time, a new message appears
ENTER YOUR MESSAGE AND I WILL CODE IT
You then type in your message. As you touch the keys the message
appears in coded form. You cannot erase mistakes, so type carefully!
Punctuation and other characters appear in their own form, except a
period (.) which terminates your message. You are then prompted with
PRESS 0 FOR THE CODE, 1 FOR A NEW CODE
If you press the key 0, a listing of the code appears, giving the code
letter for each letter in the alphabet, for example
A=T B=R C=Y D=F
E=G F=O G=L H=V
I=J J=C K=N L=B
M=D N=H O=P P=A
O=S R=U S=Q T=M
U=Z V=K W=W X=E
Y=I Z=X
AGAIN? PRESS Y OR N
and you are prompted to play again or to quit.

THE CODE
You may find it inconvenient that the period is used to end the
message. If you have a long message, with many sentences, you may like
to use the period for punctuation. In that case, you should alter line 170
and replace the"." by"#" or something that you prefer.
There are also some small sound effects in lines 9000 to 9030. You
may omit these. Then in lines 175 and 190 you should replace
GOTO 9010
with
GOTO 140
------------------------------------------------------
58 LOGIC AND STRATEGY GAMES

''Invader''
10 PRINT "~":PRINT:PRINT"INSTRUCTIONS? P
RESS Y OR N":PRINT
20 GET AS:IF AS="" THEN 20
30 IF AS<>"Y" THEN 100
40 PRINT "SUBMARINES ARE HIDDEN IN A 9 B
Y 9 SQUARE"
50 PRINT "THERE IS ONE SUBMARINE IN EVER
Y ROW":PRINT
60 PRINT "YOU MUST FIND THE SUBS IN 40 T
RIES"
70 PRINT:INPUT "PRESS RETURN TO START";
100 PRINT "~"
105 LET H=0
106 LET T=0
110 FOR 1=1 TO 9: LET AS(I)="": LET A(I)
=INT(9*RND(0))+1
120 FOR J=1 TO 9: LET AS(I)=AS(I)+"
130 NEXT J,I
140 PRINT "~";TAB(3)
150 FOR 1=1 TO 9: PRINT STRS(I); :NEXT I
160 PRINT
170 FOR 1=1 TO 9:PRINT: PRINT I;A$(1) :NE
XT I
180 IF H=9 THEN 270
185 IF T=40 THEN 300
186 PRINT "ENTER -1 TO QUIT MOVE=";T+1
190 INPUT "ENTER ROW NUMBER";!
200 IF I=-1 THEN 400
210 INPUT "ENTER COLUMN NUMBER";J
215 IF J=-1 THEN 400
220 IF 1<1 OR J<1 OR 1>9 OR J>9 THEN 140
225 LET T=T+1
230 IF MIDS(A$(1) ,2*J,1)="1" THEN 140
235 IF A(I)=J THEN LET H=H+1
240 IF A(I)=J THEN LETS$=" I"
245 IF A(I)<>J THEN LETS$=" *"
250 LET AS(I)=LEFTS(A$(1),2*J-2)+SS+RIGH
TS(A$(1), 18-2*J)
260 GOTO 140
270 PRINT "YOU WIN IN";T;"TRIES"
"INVADER" 59

280 GOTO 310


300 PRINT "HA, I WIN"
305 FOR T=1 TO 2000:NEXT:GOTO 400
310 PRINT "AGAIN? PRESS Y OR N"
320 GET ZS:IF Z$="" THEN 320
330 IF Z$="Y" THEN 100
340 STOP
400 REM DISPLAYS ALL THE SUBMARINES
405 PRINT "O":PRINT TAB(3) ;"THE SOLUTION
IS"
410 PRINT TAB(3)
420 FOR 1=1 TO 9:PRINT STR$(1); :NEXT I
430 PRINT
440 FOR 1=1 TO 9:PRINT:PRINT I;
450 FOR S=1 TO 9:IF A(I)=S THEN PRINT"
I"; : GOTO 4 70
460 PRINT".";
470 NEXT S:PRINT:NEXT I
480 PRINT:INPUT "PRESS RETURN TO GO ON";
490 GOTO 310
Note: I is the character obtained by pressing the SHIFT and Q keys
together. The symbol t:J represents SHIFT CLEAR/HOME.
60 LOGIC AND STRATEGY GAMES

''Invader''
AIM OF THE GAME
If you think that the Swedish Navy is inept at catching those small
submarines that creep into its territorial waters and seem to escape at
will, then here is your chance to see if you could do any better!
This game sets up a 9 by 9 array of points. The points are displayed
on the screen in 9 rows and 9 columns. Each row is numbered, and each
column is also numbered. There are 9 invading submarines located one to
each row. It is your task to try to locate all the submarines with just 40
moves.
You can try to develop your own search patterns, or you can search
randomly. In any case, you will learn something about the behavior of the
random number function, RND.

HOW TO PLAY
When you run this program, you are confronted with an array of dots
on the screen arranged in nine numbered rows and columns and a
prompt.
2 3 4 5 6 7 8 9
1
2
3
4
5
6
7
8
9
ENTER -1 TO QUIT MOVE
ENTER ROW NUMBER?
ENTER COLUMN NUMBER?
You enter a row number, I, which should be an integer from 1 to 9.
Then you enter a column number, J, also from 1 to 9. Your entries are
checked in line 220. If they are not in the range from 1 to 9 then the
array and prompt are displayed again.
Suppose you enter 5 and then 8. Instantly, the point at the location
row 5 and column 8 on the screen is replaced by * or I. The * indicates
that a submarine was not located at that point, while I indicates that you
have located a submarine.
"INVADER" 61

A count of your attempts is kept by the variable T (for tries). You only
have 40 tries in a game. A count of your successes is kept by the variable
H (for hits). If H < 9 when T = 40, then you have not been able to locate
all the submarines in 40 tries and you have lost the game. The
submarines do not move, so if you repeat the entry of a point, it is
counted as a try, but it is a wasted move.
There are very sophisticated search methods programmed into the
computers onboard the subchasers. These are based upon statistical
theory. However, when all is said and done, if you are lucky, or have a
sixth sense for this sort of thing, you will beat the computer every time.
To quit at any time, enter -1 when prompted to enter the row number
or the column number. The position of the submarines will then be
displayed.

THE MATHEMATICS
This game is meant to give you some experience with probability and
the random number function. You start by setting up a simple search
pattern, say test 1,5 then 2,5 and so on down the fifth column to 9,5.
Then quarter the set of points by testing the 5th row, or another row if the
point 5,5 has already located a submarine. When a submarine is found on
a row, there is no need to test any more points on that row. Odd and even
columns should have the same probability of containing a submarine.
Similarly, columns less than the 5th should be chosen with the same
frequency as columns greater than the 5th. Finally, as you get down to the
last few submarines, you should check those columns that have not
revealed a submarine.
However, after saying all this, in one game anything is possible! You
might try a simple pattern of testing each row sequentially. That is, start
at 1,1 then 1,2 and so on until you find the submarine in that row. Then
you start on the second row. Since there are nine positions on each row,
there is a better than 50% chance of locating the submarine in the first 5
positions of each row. In fact, the probability is %, in other words,
55%%. Overall, this would amount to 45 tries for the 9 rows. What is the
probability of successfully locating all the submarines in 45 tries by this
method?
You could use this game as a research tool to investigate the answer
to this question, but you would need a lot of patience! You would need to
play many times, a hundred would do for a rough estimate. You would
record the number of times this search pattern was successful. You would
need to change line 185 to

185 IF T = 45 THEN 300


62 LOGIC AND STRATEGY GAMES

For those of you who are like me (a bit lazy), you could change the
program so that the computer does the search for you. The following is a
simple program that does this.
"PROBABILITY'' 63

''Prob ability ''


110 PRINT "f:l"
120 PRINT "ENTER NUMBER OF TESTS"
130 INPUT N
140 LET S=0
150 FOR K=1 TO N
160 LET H=0
170 FOR I=1 TO 9
180 LET H=H+INT(9*R ND(0))+1
190 NEXT I
200 IF H<46 THEN LET S=S+1
210 NEXT K
215 PRINT
220 PRINT "NUMBER OF WINS IN";N
225 PRINT "GAMES IS";S
230 PRINT "PROBABILITY IS";100*S/N; "%"
235 PRINT
240 PRINT "ANOTHER EXPERIMENT?"
245 PRINT "PRESS Y OR N"
250 GET AS:IF A$="" THEN 250
260 IF A$="Y" THEN GOTO 110
270 STOP
Note: The symbol t.'J represents SHIFT CLEAR/HOME.
64 LOGIC AND STRATEGY GAMES

''Pro babi lity''


When you run this program, you are prompted by the message
ENTER NUMBER OF TESTS
It takes about 30 seconds to run 100 tests, so be prepared for a long
wait if you enter a large number. Usually 100 tests will give you a fairly
accurate estimate of the probabilities.
I hope the answer you obtain agrees with the answer you worked out
for yourself. If it does, pat yourself on the back and go raid the freezer for
some of your favorite ice cream. If it does not agree with your answer,
what the heck, go have some ice cream anyway!
Now I have a more difficult question for you. Suppose you only tried
the first 5 positions on each row. That is, whether you were successful or
not, you check out the 45 positions which are the first five positions in
each row. What is the probability of finding all 9 submarines by this
method?
Make the following alterations to "PROBABILITY" to conduct this
experiment.
180 IF 6>INT(9*R ND)+1 THEN LET H=H+1
200 IF H=9 THEN LET S=S+1
You will find it necessary to increase the number of tests to more
than 100. By changing H=9 in line 50, I would suggest that you check the
probability of finding 8 submarines by this method, and then 7, 6 and so
on down to 0.
You should plot the results on a graph. If you change the grid
dimension from 9 by 9 to 8 by 8, and search the first four positions in
each row, these results would be symmetrical. That is, the probability of
finding all 8 would be the same as finding none. These probabilities are
called Binomial. You would study these and other probability distributions
in an elementary course on probability and statistics. Your results should
reproduce approximations to the following:
(% + %)9
= (t9)9 + 9(%)8(%) + 36(%f(%)2 + 84(%)6(%)3

+ 126(%) 5 (%) 4 + 126(t9) 4 (%) 5 + 84(%) 3(%) 6

+ 36(t9) 2(%) 7 + 9(%)(%) 8 + (%)9


This is a binomial expansion. If the probability of finding a submarine
in the first five positions on a row is% then the probability of not finding a
''PROBABILITY'' 65

submarine must be % (they always add up to 1). Since there are nine
rows, we multiply (% + %) by itself 9 times. The terms in this expansion
all add up to 1 and they represent the probabilities of finding 0, 1, 2, ... , 9
submarines respectively. Consequently, the probability of finding all nine
submarines is (%) 9 = .005 (approximately) or Y2 %. So it is a very poor
search pattern!
The numbers that multiply the powers in the expression are called
the binomial coefficients. An easy way to find these numbers is to use
Pascal's triangle.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Start and end each row with 1. Add the two numbers above for each entry
in a row. Entries in the nth row are binomial coefficients for (a+b)n-1.
I hope my explanation has not caused you to do anything rash, like
run away to join the Swedish Navy!
66 LOGIC AND STRATEGY GAMES

''Sea Bunt ''


10 PRINT "O":PRINT :PRINT "INSTRUCTIONS?
PRESS Y OR N":PRINT
20 GET A$:IF A$="" THEN 20
30 IF A$<>"Y" THEN 100
40 PRINT "SHIPS OF SIZES 2,3,4 & 5 ARE H
IDDEN BY A"
50 PRINT "9 BY 9 SQUARE OF DOTS (.) YOU
HAVE UP TO"
60 PRINT "40 MOVES TO LOCATE THE SHIPS B
Y LOOKING "
70 PRINT "AT WHAT IS BEHIND EACH DOT"
80 PRINT:INPUT "PRESS RETURN TO START";
100 GOSUB 510
105 LET H=0
106 LET T=0
110 FOR !=1 TO 9: LET A$(!)=""
120 FOR J=1 TO 9: LET A$(I)=A$ (I)+"
130 NEXT J,I
140 PRINT "O";TAB(3 )
150 FOR I=1 TO 9: PRINT STR$(I); :NEXT I
160 PRINT
170 FOR !=1 TO 9:PRINT: PRINT I;A$(!) :NE
XT I
180 IF H=14 THEN 270
185 IF T=40 THEN 300
186 PRINT "ENTER -1 TO QUIT MOVE=";T+1
190 INPUT "ENTER ROW NUMBER";!
200 IF I=-1 THEN 400
210 INPUT "ENTER COLUMN NUMBER ";J
215 IF J=-1 THEN 400
220 IF !<1 OR J<1 OR !>9 OR J>9 THEN 140
225 LET T=T+1
230 IF MID$(A$( I) ,2*J,1)=" 1" THEN 140
231 LET S$=""
232 FOR K=1 TO 5
240 IF A(I,K)=J THEN LETS$=" I":LET H=H
+1
245 NEXT K
246 IF S$<>" I" THEN LET S$=" *
250 LET A$(I)=LE FT$(A$(I) ,2*J-2)+S$ +RIGH
"SEA HUNT" 67

T$(A$(1),18-2*J)
260 GOTO 140
270 PRINT "YOU WIN IN";T;"TRIES"
280 GOTO 310
300 PRINT "HA, I WIN":FOR T=1 TO 2000:NE
XT:GOTO 400
310 PRINT:PRINT "AGAIN? PRESS Y OR N"
320 GET Z$:1F Z$="" THEN 320
330 IF Z$="Y" THEN 100
340 STOP
400 REM DISPLAYS ALL THE SHIPS
405 PRINT "~":PRINT TAB(3) ;"THE SOLUTION
IS"
410 PRINT TAB(3)
420 FOR 1=1 TO 9:PRINT STR$(1);"NEXT I
430 PRINT
440 FOR 1=1 TO 9:PRINT:PRINT I;
450 FOR J=1 TO 9:P=0:FOR S=1 TO 5:IF A(l
,S)=J THEN PRINT " I";
455 IF A(I,S)=J THEN P=1
460 NEXT S:IF P=0 THEN PRINT" ";
470 NEXT J:PRINT:NEXT I
480 PRINT:INPUT "PRESS RETURN TO GO ON";
490 GOTO 310
500 REM THIS SUBROUTINE SETS UP THE SHIP
S UP-DOWN OR ACROSS
510 PRINT "~":PRINT:PRINT
520 PRINT "SETTING UP THE GAME GO HAVE A
SNACK THIS"
525 PRINT "WILL TAKE A MOMENT"
530 FOR 1=1 TO 9
540 FOR J=1 TO 5
550 LET A(I,J)=0:NEXT J,I:REM INITIALIZE
560 FOR S=2 TO 5
565 LET I=INT((10-S)*RND (0))+1 :REM START
lNG POSITION OF SHIPS
566 LET J=INT(9*RND(0))+1
567 LET D=INT(2*RND(0)) :REM SHIPS GO ACR
OSS IF D=0 AND DOWN IF D=1
570 IF D=0 THEN LET A(1,1)=J
575 IF D=1 THEN LET A(J,1)=1
576 FOR K=2 TO S
578 IF D=0 THEN LET A(I+K-1,1)=J
579 IF D=1 THEN LET A(J,K)=I+K-1
68 LOGIC AND STRATEGY GAMES

580 NEXT K,S


585 LET S=0
590 FOR I=1 TO 9
595 FOR J=1 TO 5
600 IF A(I,J)>0 THEN LET S=S+1 :REM COUNT
S THE SQUARES OCCUPIED BY SHIPS
610 NEXT J,I
620 IF S<14 THEN 530:REM 2+3+4+5=14 IF S
IS NOT 14 THEN SHIPS CROSS EACH OTHER
630 RETURN
Note: I is the graphics character obtained by pressing the SHIFT and
Q keys together. The symbol l:l represents SHIFT CLEAR/HOME.
"SEA HUNT" 69

''Sea Bunt''
AIM OF THE GAME
This game is similar to "INVADER", but more difficult because there
is less information to guide you. Ships of various lengths from 2 to 5
consecutive positions in the grid are hidden in the 9 by 9 grid of points.
They can be vertical or horizontal. There is no longer any guarantee that
every row will be occupied by a piece of a ship. There are a total of 14 hits
to be made in order to win the game, but you are limited to 40 tries.

BOW TO PLAY
When you run this game a message is displayed
SETTING UP THE GAME GO HAVE A SNACK THIS
WILL TAKE A MOMENT
while the ships are being located randomly in the grid. A grid of points is
then displayed and you are prompted to enter the row number and
column number (I,J) of a grid point. The numbers I,J should be in the
range from 1 to 9. An entry of -1 will quit the game and the location of
the ships will be displayed. A prompt to play again is then given.

THE CODE
This program is much longer than "INVADER" because setting up the
ships is more complicated. The subroutine from lines 500 to 630 does
this. The edge of the ships are located at a safe distance from the
boundaries so they will lie inside the 9 by 9 grid. Then, after the ships
have been randomly located, a count of the occupied points is made. If
this turns out to be less than 14, then two or more ships must have a grid
point in common. This set-up is discarded and another arrangement is
made. For this reason the setting up can take some time.
As a programming exercise, see if you can add to the setting up
routine so that ships can be located diagonally. You would choose four
values for D in line 567. If D=3 then the ship would be located on a
diagonal from left upper to right lower, and for D=4 the ship would be
located in the opposite diagonal direction.
70 LOGIC AND STRATEGY GAMES

''Verif y''
110 LET C=1
120 LET F=INT(10*RN D(0))
130 PRINT "t:'J"
140 PRINT "YOU HAVE 10 GOLF BALLS ONE HA
S A":PRINT
150 PRINT "DIFFERENT WEIGHT FROM THE OTH
ERS. FIND": PRINT
160 PRINT "THE DEFECTIVE BALL BY COMPARI
NG ANY":PRINT
170 PRINT "NUMBER OF BALLS ON A BALANCE
180 PRINT
190 PRINT "ENTER BALL NUMBERS 012 ... 9 IN
A STRING"
195 PRINT:PRINT "FOR EXAMPLE,TO COMPARE
BALLS 0,4,5,7 AND"
196 PRINT "9,ENTER THE STRING '04579'"
200 PRINT:INPUT A$
210 FOR I=1 TO LEN(A$)
215 B$=MID$(A$, I ,1): IF B$<"0" OR B$>"9"
THEN 130:REM CHECKS FOR NUMBERS
220 IF VAL(MID$(A $,I,1))=F THEN 260
230 NEXT I
240 PRINT "EQUAL"
250 GOTO 280
260 PRINT "UNEQUAL"
270 PRINT
280 PRINT "GIVE UP? PRESS Y OR N"
290 GET B$:IF B$="" THEN 290
300 PRINT
310 IF B$="Y" THEN PRINT "YOU HAD";C;"MO
VES. THE BALL WAS?":GOTO 340
320 LET C=C+1
330 IF B$="N" THEN 130
340 INPUT Z
350 IF Z=F THEN PRINT "CORRECT"
360 IF Z<>F THEN PRINT "WRONG. IT WAS";F
370 PRINT "AGAIN? PRESS Y OR N"
380 GET Z$:IF Z$="" THEN 380
"VERIFY" 71

390 IF Z$="Y" THEN 110


400 STOP
Note: The symbol t:l represents SHIFT CLEAR/HOME.
72 LOGIC AND STRATEGY GAMES

''Verify''
AIM OF THE GAME
This game is similar to "LOOSE CHANGE", but it is much easier. You
should master this game before playing "LOOSE CHANGE".
You have 10 golf balls and a scale. One of the balls is defective as it
does not weigh the same as the others. The balls are numbered 0, 1, 2, ... ,
9. You may select any number of balls to compare their weight. You can
even select an odd number because the computer has a spare good ball
to make up the same number on each side of the scales.
Each time you compare a set of balls you are told whether they are
equal or unequal. The number of moves you take are counted. If you can
find the defective ball in 3 moves or less that is an excellent score. A good
score is 4 or 5 moves. More than 5 moves and you need to practice a few
times. Maybe you would prefer to play with real golf balls on a golf course!

HOW TO PLAY
When we run this program it commences with the message
YOU HAVE 10 GOLF BALLS ONE HAS A
DIFFERENT WEIGHT FROM THE OTHERS.FIND
THE DEFECTIVE BALL BY COMPARING ANY
NUMBER OF BALLS ON A BALANCE.
ENTER BALL NUMBERS 012 ... 9 IN A STRING
FOR EXAMPLE,TO COMPARE BALLS 0,4,5,7 AND
9,ENTER THE STRING '04579'
Suppose we enter the string 12345. In this case we receive the
message
EQUAL
GIVE UP? PRESS Y OR N
We are not ready to quit so we press the key N. The first message is
repeated. This time we might enter the string 6 7 8. Again we received the
message that they are equal. We press N and enter 0. This turned out to
be OK so now we know that 9 is the defective ball. This time we press the
key Y in response to
GIVE UP? PRESS Y OR N
We obtain the message
YOU HAD 3 MOVES. THE BALL WAS?
We enter 9 and obtain the response
"VERIFY" 73

CORRECT
AGAIN? PRESS Y OR N
If you wish to quit, press the key N. Otherwise, press the key Y and a
new game will be generated.
74 LOGIC AND STRATEGY GAMES

''Loose Change''
110 LET C=1
115 LET N=INT(5*RND(0))+1
120 LET F=INT(12*RND(0))+ASC("A")
130 PRINT "l:l"
135 LET B$=""
140 PRINT:PRINT "ONE OF 12 DIMES HAS AD
IFFERENT WEIGHT"
145 PRINT:PRINT "THAN THE REST.FIND THE
ODD DIME BY "
150 PRINT:PRINT "WEIGHING";N;"AGAINST";N
·" ,
155 PRINT:PRINT "WE REPRESENT THE COINS
BY LETTERS A TO L"
160 PRINT "TYPE THE COIN LETTERS A,B,C, .
. . ,L THAT"
165 PRINT:PRINT "YOU WISH TO COMPARE":PR
INT
170 LET 1=0
180 LET W=0
190 GET A$:1F A$="" THEN 190
195 IF A$<"A" OR A$>"L" THEN 190
200 IF A$=CHR$(F) THEN LET W=1
205 FOR J=1 TO I:IF A$=MID$(B$,J,1) THEN
190
206 NEXT J
207 LET B$=B$+A$
210 PRINT A$;
220 LET 1=1+1
230 IF 1<2*N THEN 190
240 PRINT "l:l"
250 IF W=1 THEN PRINT "UNEQUAL"
260 IF W=0 THEN PRINT "EQUAL"
270 PRINT "GIVE UP? PRESS Y OR N"
280 GET A$:1F A$="" THEN 280
290 IF A$="N" THEN 350
300 PRINT:PRINT "YOU HAD";C;"MOVES. THE
COIN WAS?"
310 INPUT B$
320 IF ASC(B$)=F THEN PRINT "CORRECT"
330 IF ASC(BS)<>F THEN PRINT "WRONG! THE
"LOOSE CHANGE" 75

COIN IS ";CHR$(F)
340 GOTO 370
350 LET C=C+1
360 GOTO 130
370 PRINT:PRINT "AGAIN? PRESS Y OR N"
380 GET A$:IF A$="" THEN 380
390 IF A$="Y" THEN 110
400 STOP
Note: In lines 135, 190, 280 and 380, "" are two quotes that
define an empty string. The symbol rJ represents SHIFT CLEAR/HOME.
76 LOGIC AND STRATEGY GAMES

''Loose Change''
AIM OF THE GAME
This is a game of strategy and skill. Someone has been making
counterfeit dimes. "Why bother?", you might ask. That is not the question
we are concerned with. You have 12 dimes, one of which is counterfeit.
An illegal dime does not weigh the same as the genuine article. By
comparing the weight of a chosen number of coins, you must determine
which coin is the counterfeit in the fewest number of moves.
The coins are represented by the letters A,B, ... ,L. You are prompted
to enter letters that represent coins by pressing the appropriate keys. The
number of coins used in the comparison is chosen randomly. This means
that you will have to develop a different strategy each time.

HOW TO PLAY
When you run this program, you are given a message like this
ONE OF 12 DIMES HAS A DIFFERENT WEIGHT
THAN THE REST. FIND THE ODD DIME BY
WEIGHING 3 AGAINST 3
WE REPRESENT THE COINS BY LETTERS A TO L
TYPE THE COIN LETTER A,B,C ... ,L THAT
YOU WISH TO COMPARE
The program makes your keyboard act like a typewriter. The first three
letters you press would be the coins on one side of the scale. The last
three letters represent the coins on the other side. Line 195 ignores any
keys that are not one of the letters A to L and line 205 ignores any
repetitions of the same letters. After you have entered a sufficient
number of letters, 6 in this case, you will be given a message
UNEQUAL (or EQUAL as the case may be)
GIVE UP? PRESS Y OR N
If the fake coin was in your selection, then you would get the
UNEQUAL message. You now press N to continue or Y to stop. Suppose
we entered the letters A,B,C,D,E, and F and were told they were unequal.
We would press N and when the prompt for letters appeared, we might
enter the letters A,B,C,G,H and I. Suppose we now get the message
EQUAL
which means that one of D,E or F is the false coin. We would press N and
enter the letters A,B,C,D,E and G. Suppose these turned out to be
unequal, so then we would try A,B,C,D,G and H. If these turned out to be
"LOOSE CHANGE" 77

equal, we would know that E is counterfeit. This time we would press Y


and be given the message
YOU HAD 4 MOVES.THE COIN WAS?
We would enter E and the program would respond with
CORRECT
AGAIN? PRESS Y OR N
If you enter an incorrect guess, you would be told that you were
wrong and the correct answer would be given.

THE MATHE MATICS AND CODE


If you are given the additional information of which way the scale tips,
it is possible to design a scheme that guarantees locating the counterfeit
coin in a minimum number of weighings, and in addition, you will know if it
is heavy or light.
You might like to alter this program so that the information about
which way the scale tips is given to you. The following are the steps you
should take to alter the program, however, I will leave the actual
alterations to you.
1) Choose H=0 for light or 1 for heavy in line 125.
2) When checking each letter in line 200 you will need to keep track
of which side of the scale the entered letter is on. For instance, if
I<N, then the letter being entered is on the left-hand side of the
scale. You will need two lines in place of line 200. The first one,
for example, could be
200 IF A$=CHR$(F ) AND I<N THEN LET W=1
Use the values of W to record the presence of the counterfeit
coin (W>0), and whether the counterfeit coin is on the left
{W=1) or the right {W=2).
3) Now the message should be altered according to which side of
the scale the counterfeit coin is on, and whether it is heavy or
light. You should replace lines 250 and 260. The information
you give will be one of the following:
(a) EQUAL
(b) UNEQUAL:LEFT SIDE DOWN
(c) UNEQUAL:LEFT SIDE UP
You do not say on which side the counterfeit is located, or
whether it is heavy or light. Everything must be deduced from
the meager information given in (a), (b) or (c).
4) In entering the answer you should first check that the coin has
been correctly identified, and then ask if it is heavy or light. You
78 LOGIC AND STRATEGY GAMES

will need to include some extra lines between lines 310 and
350. For instance
320 IF ASC(B$)=F THEN PRINT "CORRECT":GO
TO 335
325 PRINT "COIN IS ";CHR$(F) ;" IT IS ";
327 IF H=0 THEN PRINT "LIGHT"
330 IF H=1 THEN PRINT "HEAVY"
333 GOTO 370
335 PRINT "IS IT LIGHT OR HEAVY? ENTER 0
OR 1"
337 INPUT Z
340 IF Z=H THEN PRINT "CORRECT":GOTO 370
345 PRINT "WRONG IT IS "; :GOTO 327
As a boy of 15 I can remember solving the problem in the case of
comparing four coins against four. The teachers at my school were unable
to solve it, so I spent the weekend working on it and was so excited when I
found the solution. My family had to patiently sit through several sessions
while I rehearsed the presentation I intended to give to the teachers in
their coffee room before school started on Monday morning. You can
imagine how I felt when I was told by the faculty that my mathematics
teacher had found the solution and had already bored the entire staff to
tears with his explanations before I had arrived at school! The solution, by
the way, required only three weighings in this case. Can you design such a
scheme?
"NIM I" 79

''Nim I''
100 PRINT "t:l"
110 LET C=INT (10*RND(0))
120 LET 8=C+INT (10*RND(0))
130 LET A=8+INT (10*RND(0))
150 PRINT "t:l"
160 PRINT A;TA8(3);8;TA8(6) ;C
165 PRINT
170 PRINT "YOUR MOVE"
180 PRINT " ENTER THE STACKS IN DECREAS
lNG ORDER"
190 INPUT A,8,C
200 IF A+8+C=1 THEN 400
210 IF 8=0 THEN 420
220 IF C=0 THEN 280
230 IF A=8 THEN LET C=0
240 IF 8=C AND C=1 THEN 340
250 IF 8=C AND C<>1 THEN LET A=0
260 IF A>0 AND C>0 THEN 330
270 GOTO 150
280 IF A>8 THEN 310
290 LET 8=8-1
300 GOTO 150
310 IF 8=1 OR A=1 THEN GOTO 420
315 LET A=8
320 GOTO 150
330 IF A+8+C-6>0 THEN 360
340 LET A=A-1
350 GOTO 150
360 LET S=8+C
370 IF A-6+S>0 AND S>2 AND S<6 THEN LET
A=6-S
380 IF A+8+C=6 THEN 150
390 GOTO 340
400 PRINT "YOU WIN"
410 GOTO 430
420 PRINT "I WIN"
430 PRINT: PRINT "AGAIN? PRESS Y OR N"
440 GET A$:1F A$="" THEN 440
80 LOGIC AND STRATEGY GAMES

450 IF A$="Y" THEN 110


460 STOP
Note: The symbol t:l represents SHIFT CLEAR/HOME.
"NIM II" 81

''Nim II''
10 PRINT "a":PRINT:PRINT "INSTRUCTIONS?
PRESS Y OR N":PRINT
20 GET A$:1F A$="" THEN 20
30 IF AS<>"Y" THEN 100
40 PRINT "YOU HAVE THREE STACKS OF COUNT
ERS LISTED"
50 PRINT "IN DECREASING ORDER OF MAGNITU
DE.ON YOUR"
60 PRINT "MOVE YOU MAY TAKE COUNTERS FRO
M ONE PILE"
70 PRINT "YOU MAY TAKE ANYTHING FROM ONE
TO ALL OF"
80 PRINT "THE COUNTERS FROM THAT ONE PIL
E"
90 PRINT:INPUT "PRESS RETURN TO START";
100 PRINT "a"
110 LET C=INT(10*RND(0))
120 LET B=C+INT(10*RND(0 ))
130 LET A=B+INT(10*RND(0 ))
150 PRINT "l:l"
160 PRINT A;TAB(3) ;B;TAB(6);C
165 PRINT
170 PRINT "YOUR MOVE":PRINT
180 PRINT " ENTER THE STACKS IN DECREAS
lNG ORDER"
182 INPUT X,Y,Z
183 IF(X+Y+Z)>=(A+B+C ) THEN 150
184 IF (X<>A AND X<>B AND X<>C) AND (Y<>
A AND Y<>B AND Y<>C) THEN 150
186 IF (X<>A AND X<>B AND X<>C) AND (Z<>
A AND Z<>B AND Z<>C) THEN 150
188 IF (Y<>A AND Y<>B AND Y<>C) AND (Z<>
A AND Z<>B AND Z<>C) THEN 150
189 IF X<Y OR X<Z OR Y<Z THEN 150
190 LET A=X
192 LET B=Y
194 LET C=Z
200 IF A+B+C=1 THEN 400
210 IF B=0 THEN 420
220 IF C=0 THEN 280
82 LOGIC AND STRATEGY GAMES

230 IF A=8 THEN LET C=0


240 IF 8=C AND C=1 THEN 340
250 IF 8=C AND C<>1 THEN LET A=0
260 IF A>0 AND C>0 THEN 330
270 GOTO 150
280 IF A>8 THEN 310
290 LET 8=8-1
300 GOTO 150
310 IF 8=1 OR A=1 THEN GOTO 420
315 LET A=8
320 GOTO 150
330 IF A+8+C-6>0 THEN 360
340 LET A=A-1
350 GOTO 150
360 LET S=8+C
370 IF A-6+S>0 AND S>2 AND S<6 THEN LET
A=6-S
380 IF A+8+C=6 THEN 150
390 GOTO 340
400 PRINT "YOU WIN"
410 GOTO 430
420 PRINT "I WIN"
430 PRINT: PRINT "AGAIN? PRESS Y OR N"
440 GET A$:IF A$="" THEN 440
450 IF A$="Y" THEN 110
460 STOP
Note: The symbol l:l represents SHIFT CLEAR/HOME.
"NIM I" AND "NIM II" 83

''Nim I'' and ''Nim II''


AIM OF THE GAME
NIM is an ancient Chinese game for two players. Counters are
arranged in stacks between the two players. There can be any number of
stacks and any number of counters in each stack. When it is your turn you
may take any number of counters from any one stack. The aim of the
game is to force your opponent to take the last counter. That is, all the
stacks are empty, except one that has only one counter in it.
There is a winning strategy for the player who goes first. In the simple
versions of NIM presented here in "NIM I" and "NIM II", the program
always lets you start.

HOW TO PLAY
There are only three stacks of counters. The number of counters in
each stack is chosen randomly and will be from 0 to a maximum of 27.
These numbers are displayed in decreasing order of magnitude.
In this simple version of NIM, you are prompted to enter your move.
You will enter the number of counters left in the stacks after your move,
one at a time, in decreasing order. The program will make dumb moves if
you do not enter the numbers in decreasing order.
Let us suppose that you get the message
12 4 4
YOUR MOVE
ENTER THE STACKS IN DECREASING ORDER
Suppose you enter 4, 4 and then 4, then the response would be
4 4 0
and you would be prompted to enter your move again. Suppose you now
enter 4, 2 and then 0. The response would be
2 2 0
Finally, you would enter 2, 1 and then 0 and the computer would win
because it would remove all the counters from the first stack leaving you
the one counter in the second stack. The response would be:
I WIN
AGAIN? PRESS Y OR N
If you wish to quit you would press the key Nor any key other than Y.
By pressing the key Y you would commence a new game.
84 LOGIC AND STRATEGY GAMES

These short programs are meant to provide you with a challenging


game, but not a perfect game, so you should be able to beat your
computer! Version I is very short and easy to input. It is easy for you to
cheat because your move is not checked and you can study the
computer's response to any set up. In this way you will be able to learn
some strategies. There are two winning strategies that the program does
not check. It may not take you long to find them. On the other hand ....
Version II does not play a superior game, but it will not allow you to
cheat. In this version your entries are checked to make sure that you
adhere to the rules of the game.

THE CODE
Since I have not come across a program that plays a perfect game of
"NIM", I decided to try to write one. The listing "NIM Ill" does, I believe,
play a perfect game. However, a law of computing is that there is no such
thing as a perfect program. All I can say is that this program seems to
play a perfect game in the tests conducted at our home.
If you enter this program you will appreciate why I have tried to keep
all the programs in this book as short as possible. When you type in the
listing, I suggest you save your listing from time to time, in order to
protect your work in case of a power failure.
"NIM III" 85

''Nim Ill''
100 PRINT "~":PRINT:PRINT
110 REM: SET UP THE GAME
120 GOSUB 300
130 IF MS="N" THEN 160
140 REM: YOUR MOVE
150 GOSUB 400
160 REM: COMPUTER'S MOVE
170 PRINT "I AM THINKING"
180 GOSUB 500
190 REM: CHECK SPECIAL CASES
200 IF VAL(S$)=1 THEN PRINT:PRINT "YOU W
IN!":GOTO 270
210 GOSUB 600
220 REM: DISPLAY STACKS
230 GOSUB 950
240 IF Z=1 THEN PRINT:PRINT "I WIN, HURR
AH!"
250 IF Z<=1 THEN 270
260 GOTO 140
270 PRINT "AGAIN? PRESS Y OR N"
280 GET ZS:IF Z$="" THEN 280
290 IF ZS="Y" THEN 110
295 STOP
300 PRINT "DO YOU WANT THE FIRST MOVE?P
RESS Y OR N"
305 GET MS:IF M$="" THEN 305
310 PRINT:PRINT "ENTER NUMBER OF STACKS
FROM 3 TO 9"
315 GET SS:IF S$="" THEN 315
320 LET S=VAL(S$)
325 IF S<3 OR S>9 THEN 310
330 PRINT:PRINT "DO YOU WANT TO CHOOSE T
HE NUMBER OF"
335 PRINT "COUNTERS IN THE STACKS?"
340 PRINT:PRINT "PRESS Y OR N"
345 GET SS:IF S$="" THEN 345
350 PRINT "~"
355 FOR 1=1 TO S
360 LET A(I)=INT(15*RND(0 ))+1
365 PRINT "STACK";!;"=";
86 LOGIC AND STRATEGY GAMES

370 IF S$="Y" THEN INPUT A(I)


375 IF A(I)<0 THEN 365
380 IF SS<>"Y" THEN PRINT A(I)
385 NEXT I
390 RETURN
400 PRINT:PRINT "WHICH OF THE STACKS DO
YOU WISH TO"
405 PRINT "ALTER? ENTER 1 TO";S
410 INPUT MS
415 IF MS<1 OR MS>S THEN 400
420 IF A(MS)=0 THEN 400
425 PRINT:PRINT "HOW MANY COUNTERS ARE Y
OU REMOVING?"
430 PRINT "ENTER 1 TO";A(MS)
435 INPUT MM
440 IF A(MS)<MM OR MM<1 THEN 425
445 LET A(MS)=A(MS)-MM
450 RETURN
500 REM: CONVERT STACKS TO BINARY
505 LET S$="0"
510 FOR J=1 TO S
515 LET BS(J)=""
520 LET T=A(J)
525 LET 1=1
530 IF LEN(S$)<1 THEN LET S$="0"+S$
535 LET LS=LEN(S$)
540 LET C=A(J)-2*INT(A(J)/2)
545 LET A(J)=INT(A(J)/2)
550 LET BS(J)=RIGHTS(STRS(C),1)+BS(J)
555 LET MS=STRS(VAL(MID$(SS,LS+1-1,1))+C
)
560 LET SS=LEFT$(SS,LS-I)+RIGHTS(M$,1)+R
IGHTS(S$, I-1)
565 LET I=I+1
570 IF A(J)>0 THEN 530
575 LET A(J)=T
580 NEXT J
581 REM: FILL ALL BS(J) TO THE LENGTH LS
582 LET F$="0000000000000000000000000000
000000"
583 FOR J=1 TO S:LET B$(J)=RIGHT$(F$,LS-
LEN(B$(J)))+BS(J) :NEXT J
585 RETURN
600 LET 0=0
"NIM III" 87

605 LET R=0


610 LET P=0
615 LET R=0
617 LET W=0
620 FOR J=1 TO S
625 IF A(J)=1 THEN LET O=J
630 IF A(J)=1 THEN LET W=W+1
635 IF A(J)>0 THEN LET P=P+1
640 IF A(J)>1 THEN LET R=J
645 NEXT J
650 IF P=1 THEN LET A(R)=1
655 IF P=2 AND W<>1 THEN GOSUB 700
660 IF P=2 AND W=1 THEN LET A(R)=0
665 IF P>2 AND P=(W+1) THEN GOSUB 680
670 IF P>2 AND P<>(W+1) THEN GOSUB 700
675 RETURN
680 IF P-2*INT(P/2)=0 THEN LET A(R)=0
685 IF P-2*INT(P/2)<>0 THEN LET A(R)=1
690 RETURN
700 REM: POSSIBLE SUBTRACTION
705 LET D=0
710 FOR I=1 TO LS
715 LET Y=VAL(MID$(S$,I,1)) :IF Y-2*INT(Y
/2)>0 THEN LET D=D+2t(LS-I)
720 NEXT I
725 REM: IF D>0 THEN COMPUTER CAN MAKE A
PERFECT MOVE, OTHERWISE IT CAN NOT.
730 IF D>0 THEN 750
735 IF 0>0 THEN LET A(O)=A(0)-1
740 IF 0=0 AND R>0 THEN LET A(R)=A(R)-1
745 RETURN
750 REM: IF ONE STACK HAS THE MAXIMUM, R
EMOVE ALL THE COUNTERS.
755 FOR I=1 TO S
760 IF A(I)=D THEN LET A(I)=0:RETURN
765 NEXT I
770 REM: ADJUST A SUITABLE STACK.
775 FOR I=1 TO S
780 LET SUB=0
785 FOR J=1 TO LS
790 LET Y=VAL(MID$(S$,J,1)) :IF Y-2*INT(Y
/2)=0 THEN 830
800 LET Y=VAL(MID$(B$(I),J,1)) :IF Y-2*IN
T(Y/2)=0 THEN 825
88 LOGIC AND STRATEGY GAMES

810 LET SUB=SUB+2t(LS-J)


820 GOTO 830
825 LET SUB=SUB-2t(LS-J)
830 NEXT J
835 IF SUB>0 THEN 845
840 NEXT I
845 LET A(I)=A(I)-SUB
850 RETURN
950 PRINT "(;.)"
955 LET Z=0
960 FOR I=1 TO S
965 PRINT "STACK";I;"=";A(I)
970 LET Z=Z+A(I)
975 NEXT I
980 RETURN
Note: The symbol (;J represents SHIFT CLEAR/HOME.
"NIM Ill" 89

''Nim Ill''
HOW TO PLAY
In this version there are up to 9 stacks of counters. There are many
prompts given so that you can design the game to meet your
requirements for difficulty. A typical run would proceed as follows.
DO YOU WANT THE FIRST MOVE? PRESS Y OR N
If you want the computer to have the first move you would press N. If you
press any other key, you will have the first move. You have to make this
choice before you see the stacks, so don't give the computer the first
move if you want to win! The computer is absolutely ruthless in this game
and plays to win. Only press N if you wish to study how the computer
reacts to a certain given situation.
The next prompt is
ENTER NUMBER OF STACKS FROM 3 TO 9
All you need to do is press a key from 3 to 9. Any other key will not be
accepted and the prompt will be repeated. Suppose you press the key 3.
The next prompt appears.
DO YOU WANT TO CHOOSE THE NUMBER OF
COUNTERS IN THE STACKS?
PRESS Y OR N
If you press the key Y you get a series of prompts like
STACK 1 = ?
You enter a number that can be as large as you like. I would suggest that
you keep it to less than 100 for the sake of keeping the game short. If you
press the key N, the number of counters are chosen for you and are kept
to 15 or less. You could change this by altering line 360.
Suppose you had pressed the key N. Then the situation might be
something like this
STACK 1 8
STACK 2 = 15
STACK 3 = 9
WHICH OF THE STACKS DO YOU WISH TO
ALTER? ENTER 1 TO 3
Suppose you enter 2. Then the next prompt would be
HOW MANY COUNTERS ARE YOU REMOVING?
ENTER 1 TO 15
90 LOGIC AND STRATEGY GAMES

Suppose you enter 10, then the computer would respond with
I AM THINKING
After a short period of time the stacks would be listed again and you
would be prompted to make your move.
Finally, you get a message of victory or defeat and are asked if you
wish to play another game.

THE MATHEMATICS AND CODE


The key to a winning strategy in this game is to convert the number
of counters in each stack to binary. Then you add up all these numbers,
using the usual decimal addition. The idea is to make a move that leaves
an even number in every position in the sum. If that sounds confusing, let
me show you with an example.
Suppose you had three stacks with 8, 15 and 9 counters. Converting
to binary notation we would have
8 = 1000
15 = 1111
9 = lillli
sum= 3112
In order to make each of these digits in the sum even, we would need
to subtract 1 from the first, 1 from the second and 1 from the third. That
is the number 1110 = 14 should be subtacted. Only 15 is greater than
this, so we would subtract 14 from 15 leaving 8, 1 and 9 in the
respective stacks.
Sometimes it is not possible to do this because there is no number
large enough. For instance
5 101
4 100
6 l_lil
sum 311
Here we need to subtract 111 = 7, but there is no number this big.
The strategy then is to subtract when we can, and add if we cannot
subtract. For example, let us adjust the number 5. We subtract 100 = 4
to make the first column add up to an even number. We cannot subtract
anything from the second column, so we add 10 = 2. Finally, we subtract
1 from the last column. So the adjustment to 5 is -4 + 2 - 1 = -3. This
leaves us with
2 = 10
4 = 100
6 = l_lil
sum = ll.Q
"NIM III" 91

This does the trick since 0 is considered even. Similarly, we could have
adjusted 4 by subtracting 4 and adding 3 to give
5 = 101
3 = 11
6 _l_il!
sum 222
As you can see, there is more than one way to make your move.
There are some special situations that occur, such as each stack
having 1 counter, which would have to be handled differently.
The program is fairly complicated to a beginner, so I have used some
block structure and remarks to make it easier for you to follow. All the
action takes place in subroutines. The main program, which calls the
subroutines, is in lines 100 to 295. The subroutine in lines 300 to 390
sets up the game. The subroutine in lines 400 to 450 handles your move.
Converting the stacks to binary and adding up the totals in each column is
done in the subroutine from line 500 to 585. Special cases are handled in
lines 600 to 690, and the computer's move is done in lines 700 to 850.
The display of the stacks is handled in the subroutine from 950 to 980.
You might like to think about this question. How would you modify
the program so that the object of the game is to take the last counter
instead of forcing your opponent to take the last one?
92 LOGIC AND STRATEGY GAMES

''To"Wers of Hanoi''
110 PRINT "~":PRINT "THE NUMBER OF COUNT
ERS IS? ENTER 1 TO 9"
120 LET K=0
130 INPUT N:PRINT "~"
140 IF N<1 OR N>9 THEN 110
150 LET M=0
160 LET A$(1)="ABCDEFGJHI"
170 LET A$ ( 2) ="I I I I I I I I I I"
180 LET A$(3)=A$(2)
190 PRINT "~"
200 LET M=M+1
210 FOR I=1 TO N
220 PRINT MIDS(A$(1),I,1) ;TAB(9) ;MIDS(A$
(2) ,I,1) ;TAB(18) ;MIDS(A$(3) ,I,1)
230 NEXT I
240 IF K=1 THEN 450
250 PRINT:PRINT "ENTER A STRING OF TWO D
IGITS FOR MOVE";M
255 PRINT "FOR EXAMPLE '13' MEANS MOVE T
HE TOPMOST "
260 INPUT "COUNTER FROM STACK ONE TO STA
CK THREE";BS
270 LET C=VAL(MIDS(B$,1,1))
280 LET D=VAL(MIDS(B$,2,1))
290 IF C<1 OR D<1 OR C>3 OR D>3 OR C=D T
HEN 430
300 FOR I=1 TO N
310 IF MIDS(A$(C),I,1)="i'' THEN 420
320 FOR J=N TO 1 STEP -1
330 IF MIDS(AS(D),J,1)<>"1" THEN 380
340 LET AS(D)=LEFTS(AS(D) ,J-1)+MID$(A$(C
) , I, 1 )+RIGHTS(AS(D) ,9-J)
350 IF J=N THEN 390
360 IF MIDS(A$(D),J,1)>MIDS(A$(D),J+1,1)
THEN 430
370 GOTO 390
380 NEXT J
390 LET AS(C)=LEFTS(AS(C), I-1 )+''i"+RIGHT
S(AS(C) ,9-I)
400 IF I=N AND J=1 AND D=3 THEN LET K=1
"TOWERS OF HANOI" 93

410 GOTO 190


420 NEXT I
430 PRINT:PRINT ·THE UNIVERSE IS DOOMED
- INVALID MOVEN
440 GOTO 480
450 PRINT:PRINT "MOVES TO COMPLETE TASK
IS";M-1
460 IF M-1=INT(2tN-1) THEN PRINT "THE UN
!VERSE IS SAFE, THANKS TO YOU"
470 IF M-1>INT(2tN-1) THEN PRINT ·uNFORT
UNATELY YOU HAD TOO MANY MOVES"
480 PRINT:PRINT "AGAIN? PRESS Y OR N"
490 GET Z$:IF Z$="" THEN 490
500 IF Z$="Y" THEN 110
510 STOP
Note: The symbol t:l represents SHIFT CLEAR/HOME and "I" is the
SHIFT G symbol.
94 LOGIC AND STRATEGY GAMES

''To"Wers of Hanoi''
AIM OF THE GAME
In a monastery, high in the mountains of Tibet, a group of monks
struggle valiantly to save the universe from destruction. Their task,
imposed by a superior being from another universe, is to shift 60
counters of different magnitudes from one stack to another. They are
allowed to form three stacks while doing this. At the beginning, all the
counters are on the first stack, arranged in increasing order. Only one
counter at a time can be moved. The problem is to move all the counters
to the third stack in the least number of moves. There is one vital
restriction. At no time should a greater counter be on top of a lesser one.
One mistake and the universe, as we know it, ends. So the monks toil on,
carefully contemplating each move.

HOW TO PLAY
In this game you get to choose the number of counters (from 1 to 9).
The game commences with a prompt requesting you to enter the number
of counters. You then get to move the counters, which are the letters A, B,
C, ... , initially stacked in increasing alphabetical order from the top down
in the first stack. For example, if you choose 3 counters, the computer
would respond with

A
B
c
ENTER A STRING OF TWO DIGITS FOR MOVE 1
FOR EXAMPLE '13' MEANS MOVE THE TOPMOST
COUNTER FROM STACK ONE TO STACK THREE?
You are invited to enter your move as a two digit number. If you wish to
move the top counter from stack 1 to stack 3, you would input 13. The
computer would respond with the message

B
c A
You might now want to move the counter B from stack 1 to stack 2, so
you would enter 12. The situation would then be displayed as
"TOWERS OF HANOI" 95

c B A
You would then enter 32 followed by 13, 21, 23 and finally 13. The
completion of your task is then signalled by the message
MOVES TO COMPLETE TASK IS 7
THE UNIVERSE IS SAFE, THANKS TO YOU
AGAIN? PRESS Y OR N
If you wish to quit, press the key N. To play another game, press the
key Y. Your moves are checked, if you make a mistake, the universe is
doomed! If you take too many moves, you do not save the universe, but
you do get another chance!

THE MATHEMATICS
I leave you to solve the following mathematical problems. In order to
solve the problem posed by the game in the fewest number of moves, you
will have to move the first counter into the correct stack. For an odd
number of counters, you should move the first counter into which stack?
Once you have this figured out, you will also know which stack to move
the first counter into if you have an even number of counters. Now, what
is the minimum number of moves for N counters? Solve the game for N =
1, 2, 3, ... and see if you can arrive at the general rule.
--------------------------------------------------------------------------------------------------
98 ARITHMETIC GAMES

''Barter''
100 LET COW=INT(151*RND(0) )+20
110 LET Z=INT(COW/19)
120 PRINT "~":PRINT
130 PRINT " CATS ARE WORTH $1"
135 PRINT " HENS ARE WORTH $3"
140 PRINT " DOGS ARE WORTH $5"
145 PRINT "AND PIGS ARE WORTH $10":PRINT
150 PRINT "THERE ARE NO MORE THAN";Z+1 ;"
ANIMALS OF":PRINT
155 PRINT "EACH KIND. HOW MANY OF EACH K
IND WOULD":PRINT
160 PRINT "YOU BARTER FOR A $";COW;" COW
?":PRINT
165 PRINT "ENTER YOUR ANSWER IN A STRING
LIKE 3233":PRINT:INPUT A$
170 PRINT "~"
180 FOR P=-1 TO
190 FOR 0=-1 TO
200 FOR R=-1 TO
210 FOR S=-1 TO 1
220 IF P+3*0+5*R+10*S=COW-Z*19 THEN 240
230 NEXT S,R,Q,P
240 FOR 1=1 TO 4
250 IF VAL(MID$(A$,1,1))>Z+1 THEN 290
260 NEXT I
270 LET A=VAL(MID$(A$,1,1))+3*VAL(MID$(A
$,2, 1) )+5*VAL(MID$(A$,3, 1))
280 IF A+10*VAL(MID$(A$,4,1))=COW THEN P
RINT "YOU ARE CORRECT":GOTO 290
285 PRINT "YOU ARE WRONG!":PRINT
290 PRINT "MY ANSWER IS";P+Z;O+Z;R+Z;S+Z
300 PRINT
310 PRINT "AGAIN? PRESS Y OR N"
330 GET A$:IF A$="" THEN 330
340 IF A$="Y" THEN 100
350 STOP
Note: The symbol ~ represents SHIFT CLEAR/HOME.
"BARTER" 99

''Barter''
AIM OF THE GAME
This is a game that encourages you to sharpen your mental
arithmetic skills. You have a cow worth a given amount. The amount is
chosen randomly. You are seeking a fair exchange for a number of cats,
hens, dogs, and pigs, which are each worth a certain fixed amount. There
is only a limited quantity of each of the cats, hens, dogs, and pigs. You are
asked to determine how many of each you are willing to exchange for your
cow.

HOW TO PLAY
When you run the program a problem is posed like:
CATS ARE WORTH $1
HENS ARE WORTH $3
DOGS ARE WORTH $5
AND PIGS ARE WORTH $10
THERE ARE NO MORE THAN 9 ANIMALS OF
EACH KIND. HOW MANY OF EACH KIND WOULD
YOU BARTER FOR A $54 COW?
ENTER YOUR ANSWER IN A STRING LIKE 3233
Note that your entry should be in a string, so if you decide that you
want 3 cats, 2 hens, 3 dogs and 3 pigs, you would enter 3233. No
individual number will ever exceed 9, so your entry will always contain
only four digits.
The computer checks your answer. If you are right, the computer will
print the message
YOU ARE CORRECT
Then right or wrong, you will get the answer computed by the computer,
MY ANSWER IS 3233
AGAIN? PRESS Y OR N
If you wish to quit, press the key N or any key other than Y. If you
press Y, another problem is given to you.

THE MATHEMATICS
The mathematical solution to this problem is based on the fact that
combinations of the numbers 1 ,3,5 and 10 give you all the numbers from
1 to 19. For instance, 7=10-3, 17=10+5+3-1. Each number is used at
100 ARITHMETIC GAMES

most once, either by adding or subtracting. So if a cow is worth $60, we


divide 19 into 60 to get 3 with a remainder of 3. This means we take 3 of
each of 1 ,3,5 and 10 and one more 3. So 3433 is the solution in the
format required by this program. Another solution is 4243, so do not be
surprised if your answer is different from the answer given by your
computer. The solution is computed in lines 180 to 230. If you wanted
the game to have unique answers, you would have to specify that at least
Z and no more that Z+ 1 animals of each kind should be bartered, and
then you would change the limits in the FOR statements from -1 TO 1 to 0
TO 1. I leave you to experiment with that.
Here is a connected problem for you to think about.
The face value of the individual pieces in a currency are chosen so
that transactions can be made with relatively few individual pieces of
money. Design a system of coins so that the values from 1¢ to 99¢ can
be made with the fewest number of coins.
"BASE" 101

''Base''
110 LET Z=ASC("A")-10
115 PRINT"~"
120 PRINT "ENTER BASE 2 TO 36"
130 INPUT D:IF D< 2 OR 0>36 THEN 115
135 PRINT "t:l"
136 LET S$=""
140 PRINT "BASE IS";D
150 PRINT:PRINT "ENTER ANY NUMBER"
160 PRINT "1 TO 4294967295"
170 INPUT A$
180 LET A=VAL(A$)
182 IF A<1 OR A>4294967295 THEN 135
185 PRINT:PRINT A$;" IN BASE";D;"IS"
190 GOSUB 300
200 PRINT:PRINT
210 PRINT "PRESS 0 FOR NEW BASE"
215 PRINT "1 FOR A NEW NUMBER"
216 PRINT "ANYTHING ELSE TO STOP"
220 GET G$:IF G$="" THEN 220
230 IF G$="0" THEN 115
240 IF G$="1" THEN 135
250 STOP
300 LET I=1
320 LET B=A-D*INT(A/D)
330 LET A=INT(A/D)
335 IF B>9 THEN LET S$=S$+CHR$(Z+B)
336 IF B>9 THEN 350
340 LET S$=S$+STR$(B)
350 LET I=I+1
360 IF A>0 THEN 320
370 FOR J=2*I TO 1 STEP -1
380 LET C$=MID$(S$,J,1)
385 IF C$<>" " THEN PRINT C$;
390 NEXT J
400 RETURN
Note: The symbol ~ represents SHIFT CLEAR/HOME.
102 ARITHMETIC GAMES

''Base''
AIM OF THE GAME
This is a simple program that teaches you how to convert any
decimal number of your choice to its representation in another base of
your choice. Limitations are given in the prompts because:
(a) You will notice that numbers represented in base 2 contain only
two digits, 0 and 1. Generally, a number represented in base N
contains N digits, 0, 1, 2, ... , N-1. We do not have single
characters for representing numbers greater than 9, unlike the
Babalonians of antiquity, who used a base of 60! We use the
letters A, 8, C, ... , Z to represent the numbers 10, 11, 12, ... , 35
respectively. The program could easily be altered so that bases
up to 60 could be used, but then we would have to use the
graphics characters, and it is more difficult to recognize an
increasing order. We would have to establish some order and
commit it to memory. You will find that this limitation will not
prevent you from obtaining the knowledge necessary to convert
a number to its representation in any desired base.
(b) The limitation that the number must be less than 4294967295
is needed because this is the largest integer that your computer
can store accurately. It is 2r32-1, which in base 2 is
represented by thirty two ones, as in 1111111111111111111
1111111111111. The numbers 0 and 1 are represented by
these respective digits in any base. This program does not
convert negative numbers, but it could be done easily. I leave you
to make the necessary changes so that your number lies
between the limits -4294967295 and 4294967295.
If you remove the restriction that numbers must be less than
4294967295 by removing line 182 in the code, then you can
experiment to find out what happens when you enter numbers that
exceed this limit.
The development of computer technology necessitated the study of
representations of numbers in bases other than 10. The number base
usually used by a computer is binary, or base 2. This is the easiest
because there are only two digits needed in the representation of
numbers, 0 to 1. However, base 8 and base 16 are used in many
computers.
The reason that strings were used in the code to store the
representations was that some representations have up to 32 figures in
them, and could not be displayed in number format.
"BASE" 103

HOW TO PLAY
When you run this program the first prompt you are given is
ENTER BASE 2 TO 36
Suppose you enter the number 2. The screen clears and you are
given a new message and prompt
BASE IS 2
ENTER ANY NUMBER
1 TO 4294967295
Suppose you enter the number 15, then below the above message
appears
15 IN BASE 2 IS
1111
This is the representation of 15 in base 2. Then you are given the
prompt
PRESS 0 FOR NEW BASE
1 FOR A NEW NUMBER
ANYTHING ELSE TO STOP
If you enter the number 0, then the screen will clear and the program
returns to the prompt requesting you to enter the base. If you enter the
number 1, then the same base is used and you are prompted to enter any
number. To quit, you just enter any other character, Q for example.

THE MATHEMATICS
The mathematics in this game is repetitive division which is
performed in the subroutine starting at line 300. For instance, in the
example cited above, to express the number 15 in the base 2, use the
following procedure.
2 15 You divide 2 into 15 to get 7 with a remainder of
1. You keep dividing by 2 until you reach 0, the
remainders give you the required representation, in
this case 1111. This binary representation means that:
15 = 1 *(2 t 3) + 1 *(2 t 2) + 1 *(2) + 1

From the binary representation of 15, it is clear why this process


works. Let us go through the process step by step.
Divide 15 by 2 to get 1 *(2 t 2)+ 1 *(2 t 1)+ 1 with a remainder of 1.
Divide again by 2 to get 1 *(2 t 1)+ 1 with a remainder of 1.
Divide again by 2 to get 1 with a remainder of 1.
Divide again by 2 to get 0 with a remainder of 1.
104 ARITHMETIC GAMES

The representation of 15 in the base 3 is calculated in the following


manner.
3 The representation is given by 120. Notice that the
last remainder is the first digit in the required
representation. This means that:
15 = 1*(3t2) + 2*(3) + 0

The representation of 15 in the base 4 is given by 33.

411~13
03
You will have noticed that for a base N, the only digits occurring in
the remainders are N-1, N-2, ... , 1 ,0.
For bases greater than 10, the arithmetic becomes more difficult for
us, but not for the computer. We have to use the letters A, 8, ... to
represent remainders which are greater than 9. For example, in finding
the representation of 119 in the base 12, we proceed in the following
manner.

12~19 The representation is 98, where 8 stands for the


number 11.
9 11
09
Using A to represent a remainder of 10, 8 for 11 and so on, up to Z
for 35, we are able to represent any number in a base from 2 to 36.
When you have mastered this game, you might like to try your hand
at doing arithmetic with numbers in various bases. For instance, try
adding and subtracting numbers represented in base 2. Having mastered
that, you might like to try multiplication and division.
The program "YOUR NUMBER IS UP" tests your ability at converting
numbers in any base, back into numbers represented in the standard
base 10.
"YOUR NUMBER IS UP" 105

''Your Number Is Up''


110 LETS$=""
115 LET B=INT(35*RND(0))+ 2
120 PRINT "t'J"
130 PRINT "BASE IS";B
140 LET N=INT(999*RND(0)) +36
150 LET N$=STR$(N)
160 GOSUB 300
170 PRINT:PRINT "NUMBER IS ";RIGHT$(S$,I
-1 )
190 PRINT:PRINT "WHAT IS THE NUMBER IN B
ASE 10?"
200 INPUT G:PRINT
210 IF G=N THEN PRINT "YOU ARE CORRECT"
220 IF G<>N THEN PRINT "WRONG, IT IS";N
230 PRINT:PRINT "AGAIN? PRESS Y OR N"
240 GET A$:IF A$="" THEN 240
250 IF A$="Y" THEN 110
260 STOP
290 REM THIS CONVERTS N TO BASE B
300 LET I=1
310 LET R=N-B*INT(N/B)
320 LET N=INT(N/B)
330 IF R>9 THEN LET S$=LEFT$(S$,32-I)+ CH
R$(ASC("A")-10+R )+RIGHT$(S$,I-1)
340 IF R>9 THEN 360
350 LET S$=LEFT$(S$,32-I)+ RIGHT$(STR$(R)
,1)+RIGHT$(S$,I-1 )
360 LET I=I+1
370 IF N>0 THEN 310
380 LET N=VAL(N$)
390 RETURN
Note: The symbol t'J represents SHIFT CLEAR/HOME.
106 ARITHMETIC GAMES

''Your Nuntber Is Up''


AIM OF THE GAME
This is the opposite of the game "BASE". You should first play
"BASE" in order to learn how to convert any given decimal number to its
representation in another base other than 10. "YOUR NUMBER IS UP"
tests your ability to convert numbers represented in any base, back into
the base 10.
You will find this a very challenging game since it tests your skill in
converting numbers written in bases from 2 to 36. If you find this too
difficult you could easily change the code so that bases from 2 to 16 are
chosen. Simply change the 35 in line 115 to 15.

BOW TO PLAY
When you run this program, the screen clears and a message similar
to this is displayed
BASE IS 11
NUMBER IS 5A2
WHAT IS THE NUMBER IN BASE 10?
The representation 5A2 stands for
5*(11t2) + 10*(11) + 2, which is 5*(121) + 112 = 717. You enter
717 and you will get the message
YOU ARE CORRECT
AGAIN? PRESS Y OR N
If you press the key Y, the screen clears and a new problem is posed.
If you press the key N, or any key other than Y, the program stops.
If you enter an incorrect answer, say you enter 617 in the above
example, then you will receive the message
WRONG, IT IS 717
AGAIN? PRESS Y OR N

THE MATHEMATICS
The mathematics is the reverse of the mathematics in "BASE", so
this time it is repetitive multiplication. Suppose the base is B, and the
representation is 123. The number can be written as a polynomial
1*(Bt2) + 2*(B) + 3 = 1B2 + 2B + 3
"YOUR NUMBER IS UP" 107

For example, 123 in base 10 is 1*100+2*10+3 . The simplest way to


evaluate an expression with 8 not equal to 10, is by nested multiplication,
where we write the numbers as
((1 *8)+2)*8+3
You can set this up in the following way:
1 2 3
ADD .....-8 ~(8+2)*8
8* 17 8+2/(8+2)*8+ 3 = required decimal
representation
This process is called SYNTHETIC DIVISION and is the most
economical way of evaluating polynomials like
P(X) = AX 2 + 8X + C
Of course, nothing much is gained with a polynomial of low degree,
but when you have a polynomial like
P(X) = AX 9 + 8X 8 + CX 7 + ... + IX + J
then the gains are substantial in both accuracy and time.
There is more to be gained by this process than meets the eye. This
process of synthetic division is used to divide polynomials by linear
factors. For instance, if you attempt to divide a linear factor like X-3 into
the polynomial P(X), synthetic division gives you the remainder R and the
quotient polynomial Q(X), where P(X) = Q(X)*(X-3) + R. R is the value of
the polynomial at X= 3 since P(3) = 0 + R. What is more, in Calculus, the
use of Newton's method for finding the roots of a polynomial requires the
evaluation of the polynomial P(X) and its derivative P' (X). This is
achieved by using synthetic division twice, once with the original
polynomial, and a second time on the quotient polynomial arrived at in the
process of synthetic division.
If you are familiar with Calculus, then you will observe that what we
achieve from the factoring of P(X) into
P(X)=Q(X)*(X-3) +R
is that P' (X)=Q' (X)(X-3)+Q(X) when we differentiate. So that
P' (3)=Q(3) which is the value of the quotient polynomial at X=3.
One final word. The numbers chosen in line 140 are mostly limited to
2 and 3 digits. If you would like to increase this, simply add a few more
9's to the number 999.
108 ARITHMETIC GAMES

''Cryptic''
110 PRINT "~":PRINT:PRINT
120 FOR P=2 TO 9
127 LET S=0
130 PRINT "PRESS THE KEY + OR
140 GET OP$:IF OP$="" THEN 140
145 IF OP$<>"+" AND OP$<>"-" THEN 110
150 LET I=INT(15*RND(0))
155 LET M=10t(9-P)
157 LET L=10t(P-1)
160 LET C=INT(555555555/M*RND(0))
170 LET B=INT(444444444/M*RND(0))
180 IF OP$="+" THEN LET A=C-B
190 IF OP$="-" THEN LET A=C+B
200 IF A<L OR B<L OR C<L THEN 160
205 LET Z=4294967200
206 IF A>Z OR B>Z OR C>Z THEN 160
210 LET B$(1)=STR$(A)
220 LET B$(2)=STR$(B)
230 LET B$(3)=STR$(C)
240 FOR J=1 TO 3
245 LET A$(J)=""
250 FOR K=2 TO P+1
255 LET B=VAL(MID$(B$(J) ,K,1))
260 LET A$(J)=A$(J)+CHR$(B+I+ASC("A"))
270 NEXT K
280 NEXT J
290 PRINT "~":PRINT:PRINT "THE FOLLOWING
EQUATION IS IN CODE":PRINT
300 PRINT A$(1) ;OP$;A$(2) ;"=";A$(3)
320 PRINT:PRINT "ENTER Q TO QUIT"
330 PRINT:PRINT "THE DECODED ANSWER IS?"
340 PRINT:INPUT Z$
345 LET S=S+1
350 IF VAL(Z$)<>C THEN 380
355 PRINT:PRINT "CORRECT"
360 PRINT "NUMBER OF TRIES =";S
361 IF P<9 THEN PRINT "LET ME TRY YOU WI
TH A LONGER ONE"
363 FOR T=1 TO 4000:NEXT T
364 PRINT "1:1"
"CRYPTIC" 109

365 NEXT P
370 PRINT "AGAIN? PRESS Y OR N"
375 GET A$:IF A$="" THEN 375
376 IF A$="Y" THEN 110
377 STOP
380 IF Z$="0" THEN PRINT "THE ANSWER IS"
;C:GOTO 370
385 PRINT "WRONG. ENTER A DIGIT."
390 INPUT W
395 PRINT "t:l"
400 FOR J=1 TO 3
410 FOR K=2 TO P+1
415 LET B=VAL( MID$(8$ (J) ,K,1))
416 LET J$=CHR$ (W+ASC ("0"))
420 IF B=W THEN LET A$(J)=L EFT$(A $(J) ,K-
2)+J$+R IGHT$(A $(J),P+1 -K)
430 NEXT K
440 NEXT J
460 GOTO 290
Note: The symbol t:l represents SHIFT CLEAR/HOME.
110 ARITHMETIC GAMES

''Cryp tic''
AIM OF THE GAME
In this program you have the choice of problems in addition or
subtraction. You are presented with an equation in code, where the digits
in the numbers are represented by letters. For instance, the equation
123+234=357
might be displayed as
ABC+BCD=CEG
The game is to work out the answer 35 7 in the fewest number of
moves. You are given clues as you proceed, and based upon these and
your deductive reasoning powers, you work out the answer.
You are given two letter numbers to start with, and each time you
solve a problem, the next problem involves numbers with one extra digit,
up to a maximum of 9 digits.

HOW TO PLAY
When you run this program, you are prompted by the message
PRESS THE KEY + OR -
Say you press +, the screen clears and you are presented with an
equation involving two digit numbers, for instance
THE FOLLOWING EQUATION IS IN CODE
LS+NP=PN
THE DECODED ANSWER IS?
Suppose you enter 53, since L and S appear in the problem, L can
not be 0, so it might be 1 and S would then be 8. Of course L could be 2
and then S would be 9, in which case, the correct answer would be 64. If
you are wrong, the message
WRONG.ENTER A DIGIT
appears on the screen. You might enter the digit 6. The screen would
clear and you would be presented with the equation
LS+N6=6N
THE DECODED ANSWER IS?
Now you know that the answer is 64, so you would enter 64. Then
you would be given the message
CORRECT
NUMBER OF TRIES=2
"CRYPT IC" 111

LET ME TRY YOU WITH A LONGER ONE


After a pause, the screen clears and you are prompte d to press + or
- again.

THE COD E
You should be able to deduce the answer in 2 moves at most. If you
would like to increase the difficulty , then you would need to replace the
digits by letters in an arbitrar y fashion, rather than in a sequence as this
program does. I leave you to make the necessary changes.
Since the arithme tic with 9 digit numbers is not always exact, lines
205 and 206 guard against any of the numbers exceeding the largest
integer 429496 7295 which can be stored exactly inside the computer.
You could attempt to change the code, so that every number is treated as
a string. You would have to introduce subroutines to accomplish the
arithme tic on the strings, but then the limitation of 9 digit numbers could
be removed.
112 ARITHMETIC GAMES

''Bunt an Arithn tetic''


110 PRINT "l:l"
120 LET J=INT(4*RND (0))
130 LET A=INT(19*RN D(0))-9
140 LET B=INT(19*RN D(0))-9
150 LET C=INT(19*RN D(0))-9
160 LET D=INT(19*RN D(0))-9
170 IF C=0 AND D=0 THEN 150
180 LET K=SQR(C*C+D *D)
190 IF J=3 AND K<>INT(K) THEN 150
200 LET P=42+(J-2)* (13*J*J-28* J-3)/6
210 PRINT "(";STR$(A) ;",";STR$(B );")";CH
RS(P) ;"(";STR$(C) ;",";STR$(D) ;")=(E,F)"
220 PRINT:PRINT "ENTER E AND F AS AN INT
EGER OR AS":PRINT
225 PRINT "A FRACTION LIKE 2/7"
230 PRINT:INPUT "ENTER E";E$
235 LET E=VAL(E$)
240 PRINT:INPUT "ENTER F";F$
245 LET F=VAL(F$)
250 ON J+1 GOTO 260,260,300 ,330
260 LET M=1-2*J
270 LET G=A+M*C
280 LET H=B+M*D
290 GOTO 370
300 LET G=A*C-B*D
310 LET H=A*D+B*C
320 GOTO 370
330 LET K=K*K
340 LET C=C/K
350 LET D=-D/K
360 GOTO 300
370 FOR 1=1 TO LEN(E$)
375 IF MIDS(E$,1,1 )="/" THEN LET E=VAL(L
EFT$(E$,1-1) )/VAL(RIGH T$(E$,LEN(E $)-I))
380 NEXT I
385 FOR 1=1 TO LEN(F$)
390 IF MIDS(F$,1,1 )="/" THEN LET F=VAL(L
EFT$(F$,1-1) )/VAL(RIGH T$(F$,LEN(F $)-I))
395 NEXT I
400 PRINT
"HUMAN ARITHMET IC" 113

405 IF G=E AND H=F THEN 430


410 PRINT "WRONG IT IS ";G;"," ;H
420 GOTO 440
430 PRINT "RIGHT ON"
440 PRINT:P RINT "AGAIN? PRESS Y OR N"
450 GET A$:IF A$="" THEN 450
460 IF A$="Y" THEN 110
470 STOP
Note: The symbol rJ represents SHIFT CLEAR/HOME.
114 ARITHMETIC GAMES

''Hu man Arith meti c''


AIM OF THE GAME
Leopold Kronecker, a 19th century mathematician who developed
the theory of algebraic numbers, once said "God gave us the natural
numbers (integers greater than 0); everything else was created by man".
Hence the name for this program.
One of the most useful number systems invented is the one
composed of COMPLEX NUMBERS. Numbers belonging to the real
number system are used to represent the solutions of algebraic equations
like X2 = 2. We say X=+ SQR(2) or- SQR(2), where the quantity SQR(2)
is a real number whose square is precisely 2. We are unable to write
SQR(2) as a fraction, so we can only approximately represent it as a
decimal expansion 1.41421356 2 ... to any required accuracy.
If you want to solve an equation like X2 = -1, what numbers do you
use? The square of a non-zero real number is positive, so complex
numbers were invented for this purpose. The key step is to introduce the
quantity SQR(-1) called i. Then i 2 = -1 so we have X2 = i 2 and the solution
is X = +i and X = -i. Such numbers were referred to as "imaginary"
numbers when they were first introduced, but now the term "complex"
number is generally used.
In general, a complex number a + ib has two parts, a real part, a, and
an imaginary part, b. We abbreviate the notation from a + b SQR(-1) to
a + ib and finally to (a, b), which is an ordered pair. It is always
understood that the second member of a pair is multiplied by i.
The program, "HUMAN ARITHMETIC", gives you practice in the
arithmetic of complex numbers. Problems of addition, subtraction,
multiplication and division are chosen at random. You are prompted to
enter the solution as an ordered pair (E, F). First enter your guess for the
number E and then enter your guess for the number F. If you are wrong,
the correct answer is displayed. In either case, a new problem is posed
after a pause.
The rules for addition and subtraction are simple.
(a, b) + (c, d) = (a + c, b + d)
(a, b) - (c, d) = (a - c, b - d)
However, multiplication and division are more complex! Keep in mind
that i 2 = -1 , then
(a, b) *(c, d) = (a + ib) *(c + id)
= a*c + i(a*d) + i(b*c) + i 2 (b*d)
(a*c - b*d, a*d + b*c)
"HUMAN ARITHMETIC" 115

Note that (a, 0) acts like a non-complex number, as indeed it is. So


(a,0) *(c, 0) = a*c and (a, 0) *(c, d) = (ac, ad). In fact the role of 1 in the
arithmetic is played by the pair (1, 0) .
Division (a, b)/(c, d) is a little tricky. We can divide by all complex
numbers except (0, 0). The trick is to define the multiplicative inverses
for each complex number. That is, what is the number 1/(c, d)? A better
way of saying this is, what is the complex number (x, y) such that (c, d)
*(X, y) = (1, 0) ?
You can verify by multiplication that the answer is

So the rule is

HOW TO PLAY
When you run this program the screen clears and you are given a
message like this (although the + symbol might be -, *Or /)

( -9, 7) + ( -3, -3) = ( E, F)


ENTER E AND F AS AN INTEGER OR AS
A FRACTION LIKE 2/7
ENTER E?
ENTER F?
You work out what you think E and F should be, in this case E=-12
and F=4, then you enter them one after the other. You will be given the
message:

RIGHT ON
AGAIN? PRESS Y OR N
If you press the key Y you get another problem. Pressing any other
key stops the game. If you enter an incorrect result, say -12,10 in the
above example, you are given the message

WRONG IT IS -12,4
AGAIN? PRESS Y OR N
116 ARITHMETIC GAMES

THE CODE
You should note that when an example is given for division, roundoff
in the machine may cause a perfectly good answer to be rejected as
wrong. For instance, a fraction like h and .14285 7143 would not be
considered to be the same. For this reason, the answers E and F are
entered as strings. The lines 370 and 395 scan the entries for a I. If it is
there, the fraction is correctly evaluated. This allows you to enter your
answer like 7'7 in precisely that fashion. If your entry is a number, it is
treated as such.
"JUGS" 117

''.Jugs''
10 PRINT "~":PRINT:PRINT "INSTRUCTIONS?
PRESS Y OR N":PRINT
20 GET A$:IF A$="" THEN 20
30 IF A<>"Y" THEN 100
40 PRINT "YOU HAVE TO MEASURE A PRECISE
AMOUNT OF "
50 PRINT "MILK USING THREE JUGS OF DIFFE
RING SIZES"
60 PRINT "BY ADDING AND REMOVING AMOUNTS
OF MILK
70 PRINT "WITH THE THREE JUGS"
80 PRINT:INPUT "PRESS RETURN TO START";
100 PRINT "~"
110 LET A=INT(10*RND(0))
120 IF A-2*INT(A/2)=0 THEN 110
130 LET B=A+2*(INT(10*RN D(0))+1)
140 LET C=A+2*(INT(10*RN D(0))+1)
150 IF C=B THEN 140
160 LET D=INT(20*RND(0))
170 IF D=A OR D=B OR D=C OR D=0 THEN 110
180 PRINT:PRINT "YOUR THREE JUGS HOLD"
190 PRINT:PRINT STR$(A) ;",";STRS(B);" AN
D";STR$(C);" PINTS."
200 PRINT:PRINT "HOW WOULD YOU GET";D;"P
INTS?"
210 PRINT:PRINT "ENTER YOUR SOLUTION IN
THE FORM -2,1,2"
215 PRINT "WHICH MEANS YOU WOULD ADD THE
SECOND JUG"
216 PRINT "PLUS TWICE THE THIRD, AND SUB
TRACT TWICE"
218 PRINT "THE FIRST JUG"
220 PRINT:INPUT "ENTER R,S,T";R,S,T
230 FOR L=1 TO 9
240 FOR I=-L TO L
250 FOR J=-L TO L
260 FOR K=-L TO L
270 IF I*A+J*B+K*C=D THEN 290
280 NEXT K,J,I,L
290 IF A*R+B*S+C*T=D THEN PRINT:PRINT "R
118 ARITHMETIC GAMES

IGHT ON":GOTO 300


295 PRINT:PRINT "YOU ARE WRONG!"
300 PRINT: PRINT "MY ANSWER IS ";STRS(I);
",";STRS(J) ;"AND ";STRS(K)
310 PRINT:PRINT "AGAIN? PRESS Y OR N"
320 GET A$:IF A$="" THEN 320
330 IF A$="Y" THEN 100
340 STOP
Note: The symbol t:l represents SHIFT CLEAR/HOME.
''JUGS'' 119

''Jags''
AIM OF THE GAME
This is a painless way of practicing your arithmetic. You have three
measuring jugs which hold different amounts of milk. By adding and
subtracting the contents of the jugs, you have to get a precise quantity
which is requested by a customer, who has a large empty container. There
is usually more than one correct answer, so do not be surprised if your
answer is different from the one given by the computer.

HOW TO PLAY
When you run the program, the screen clears and you are given a
message like
YOUR THREE JUGS HOLD
5,11, AND 25 PINTS.
HOW WOULD YOU GET 2 PINTS?
ENTER YOUR SOLUTION IN THE FORM -2,1,2
WHICH MEANS YOU WOULD ADD THE SECOND JUG
PLUS TWICE THE THIRD, AND SUBTRACT TWICE
THE FIRST JUG
ENTER R,S,T?
One solution is -4 of the 5 pint jug, +2 of the 11 pint jug. So you
would enter the numbers -4, then 2, and then 0. The computer checks
your answer, and also works out an answer for itself. Then you would get
the message
RIGHT ON
MY ANSWER IS 1,2 AND -1
AGAIN? PRESS Y OR N
If your answer was not correct, the computer gives you the answer it
computed. To obtain another problem, just press the key Y. If you wish to
quit, just press the key N.

THE CODE
The lines 230 to 280 find a solution. The surprising thing in these
problems is that at least one solution is usually found in the range -3 to 3
for the measuring jugs. You might like to increase the possible size of the
measuring jugs by changing lines 110 to 140, in which 10*RND(0) could
be altered to 20*RND(0) or something else. You will notice that in these
lines I have made the quantities A,B and C, which are the volumes of the
120 ARITHMETIC GAMES

three jugs, to be always odd. The final quantity D can be either odd or
even. Can you tell why I had to impose this restriction? It is an odd
thought!
122 GEOMETRY GAMES

''Detonation''
100 REM:SET UP BOMB COORDINATES
1 10 FOR I= 1 TO 3
120 LET X(I)=INT(100*RND(0))
130 IF X(I)=0 THEN 120
140 LET A(I)=0
150 NEXT I
160 LET C=0
170 PRINT "l:l"
180 LET D=0
190 FOR I=1 TO 3
200 LET D=D+ABS(X(I)-A(I))t2
210 NEXT I
220 LET D=INT(SQR(D))
230 IF D=0 THEN 400
240 IF C=20 THEN 420
250 PRINT "YOU ARE AT";STR$(A(1));",";ST
R$ (A( 2));","; STR$ (A( 3)): PRINT
255 PRINT "IN A LARGE HOTEL WHICH HAS 10
00000 ROOMS"
260 PRINT "A TIME BOMB IS SET AT A DISTA
NCE";D:PRINT
261 PRINT "YOU HAVE";20-C;"MOVES TILL IT
GOES OFF":PRINT
265 PRINT "PRESS S TO STOP RETURN TO CON
TINUE"
270 GET A$:IF A$="" THEN 270
280 PRINT:PRINT "ENTER 0 TO 99"
290 INPUT "ENTER COORDINATE A";A(1)
300 INPUT "ENTER COORDINATE B";A(2)
310 INPUT "ENTER COORDINATE C";A(3)
320 LET C=C+1
330 GOTO 170
400 PRINT "WELL DONE! YOU LOCATED THE BO
MB IN ONLY";C;"MOVES."
405 IF C<4 THEN PRINT "YOU MUST BE A GEN
IUS."
406 IF C>3 AND C<9 THEN PRINT"YOU ARE VE
RY CLEVER."
407 IF C>8 THEN PRINT "YOU NEED PRACTICE
"DETONATION" 123

410 GOTO 430


420 PRINT"~":FORI=0T07:FORJ=IT08:FORT=1T
06:POKE 53281,J:NEXT T,J,I:POKE 53281,6
422 PRINT "~":PRINT:PRINT "BOOM! TOO LAT
E."
425 GOSUB 9000:REM PLAYS FUNERAL MUSIC
426 PRINT" BOMB WAS LOCATED AT ";X(1);
",";X(2);",";X(3)
430 PRINT:PRINT "ANOTHER GAME? PRESS Y 0
R N"
440 GET A$:IF A$="" THEN 440
450 IF A$="Y" THEN 110
460 STOP
9000 FOR L=54272 TO 54296:POKE L,0:NEXT:
POKE 54296,15
9010 FOR I=1 TO 3:V=54269+7*I:POKE V+1,1
90:POKE V+2,250:NEXT I
9020 RESTORE
9030 FOR I= 1 TO 3: READ H (I) , L (I) :NEXT I :
READ D
9040 IF D=-1 THEN 9080
9050 FOR I=1 TO 3:V=54266+7*I:POKE V,H(I
) :POKE V-1,L(I) :POKE 54269+7*I,33:NEXT
9060 FOR T=1 TO 4*D:NEXT
9065 FOR I=1 TO 3:POKE 54269+7*I,0:NEXT
9070 GOTO 9030
9080 FOR 1=1 TO 3:POKE 54269+7*1,0:NEXT
9090 RETURN
9900 DATA 17,37,12,216,8,147,100
9910 DATA 17,37,12,216,8,147,200
9920 DATA 17,37,12,216,8,147,50
9930 DATA 17,37,12,216,8,147,100
9935 DATA 20,100,15,70,10,60,200
9940 DATA 19,63,14,107,9,159,50
9945 DATA 19,63,14,107,9,159,200
9950 DATA 17,37,12,216,8,147,50
9955 DATA 17,37,12,216,8,147,200
9960 DATA 16,47,13,156,11,114,50
9965 DATA 17,37,12,216,8,147,200
9999 DATA -1,-1,-1,-1,-1,-1,-1
Note: The symbol ~ represents SHIFT CLEAR/HOME.
124 GEOMETRY GAMES

''Detonation''
AIM OF THE GAME
A bomb has been planted somewhere in a large building that has one
million rooms in it. Your job is to locate the bomb before it goes off! To
give you some assistance, your computer tells you how far away you are
from the bomb. You move to another room and the new distance from the
bomb lets you know if you are getting any warmer or colder (which in our
house around Easter egg-time means getting nearer or further away).
Each move is counted and you have a limit of only 20 moves until the
bomb explodes. This is a challenging game and the suspense builds as
you approach the moment of DETONATION. Fortunately no one is hurt if
you fail to find the bomb in time, so you can have another go if you fail,
and another, and ...

HOW TO PLAY
You start at the room with coordinates 0,0,0. Each room is
designated by a set of three coordinates, 0 to 99, 0 to 99, 0 to 99. With
each turn you are given the distance, D, from your present position and
the bomb and are prompted to enter new coordinates. The distance is
measured by the Euclidean rule, or 2-norm, which is
D=SQR((A-X) 2 + (B-Y) 2 + (C-Z) 2 )
Actually, you are given an integer, INT(D). The coordinates X, Y and Z are
the bomb coordinates, while A, B and C are the coordinates for your
present position. When you enter new coordinates, the values of A, B and
C are altered to the new values that you enter.
A typical game goes as follows. The first message is something like
this
YOU ARE AT 0,0,0
IN A LARGE HOTEL WHICH HAS 1000000 ROOMS
A TIME BOMB IS SET AT A DISTANCE 44
YOU HAVE 20 MOVES TILL IT GOES OFF
PRESS S TO STOP RETURN TO CONTINUE
When you press a key other than S, you are prompted to enter your
coordinates A, B and Cone after the other. These are your guesses for the
bomb coordinates X, Y and Z respectively. Suppose you enter 44, then 0
and then 0. The screen clears and a new message appears
YOU ARE AT 44,0,0
IN A LARGE HOTEL WHICH HAS 1000000 ROOMS
''DETONATION'' 125

A TIME BOMB IS SET AT A DISTANCE 22


YOU HAVE 19 MOVES TILL IT GOES OFF
and so on. In this case we overshot on the first coordinate. The first
coordinate is likely to be around 33, so we could try 33,20,0 as our next
guess. By trial and error you should be able to find the bomb. But if at
first you don't succeed, maybe you should read the next section.
If you press the key S when prompted to STOP, you will be given the
location of the bomb, for instance,
BOMB WAS LOCATED AT 39,11,19
ANOTHER GAME? PRESS Y OR N
If you want to quit, press the key N or any key other than Y.

THE MATHEMATICS
Mathematically, the solution is given by the point of intersection of
three spheres inside the cubic building centered, as an example, at
(0,0,0), (99,0,0) and (0,99,0).
That is, you must solve
x2 + y2 + z2 = Di

(X - 99) 2 + Y2 + Z2 = D~

X2 + (Y - 99) 2 + Z2 = D~
If you do this you are guaranteed to find the bomb in 3 moves! This is an
example of the use of a branch of mathematics called analytic geometry
which is a very useful mathematical technique. If you study calculus at a
university, you will also study analytic geometry.
To solve this system of second degree equations, we subtract the
first equation from the second to obtain
(X - 99) 2 - X2 = D~ - Di
Expanding the first part we get
X2 - 198X + (99) 2 - X2 = D~ - Di
Cancelling the X2 , we get by subtracting (99) 2 from both sides
-198X = - (99) 2 + D~ - Di
Since the distances D1 and D2 are given to you, all of the quantities
on the right hand side are known, so we can find X by dividing by -198.
We do the same with the third equation. We subtract the first
equation from the third equation to get
(Y - 99) 2 - Y2 = D~ - Di
From which we obtain Y.
126 GEOMETRY GAMES

Finally, Z is found from the first equation, now that we know what X
and Y are. Thus
z2 = Of - x2 - y2
We take the positive square root for Z.
Note: All of this is perfectly correct in theory, provided the distances
0 1 , 0 2 and 0 3 are known exactly. However, in this game the distances are
rounded off to integers. It usually makes no difference. However, I want to
warn you that if, after all this analytic geometry and algebra, you are still
distance 1 from the bomb, you will have to finish the problem "by guess
and by golly".
If you fail to find the bomb in 20 moves, the bomb explodes with a
SLAM and the coordinates where the bomb was located are displayed.
There is nothing sacred about the points (99,0,0) and so on that I
used in my solution. You will find that the arithmetic is easier to do if you
use the points (50,0,0), (0,50,0) and (0,50,0). I leave you to work out
the details.

THE CODE
Line 420 creates flashing lights by changing the screen colors. While
lines 9000 to 9999 make the funeral music. You could delete these lines
and line 425 to shorten the listing. You would need to have a line 420
because it is referred to in lines 240 and 280. You could use
420 GOTO 422
Note the difference in the output in lines 250 and 426. When you
use STR$(A(l)) a blank appears only on the left of the number A(l), to
allow the possibility of a negative sign.
"QUEST" 127

''Quest''
10 PRINT "~":PRINT:PRINT:PRINT" NEED
INSTRUCTIONS? PRESS Y OR N"
20 GET AS:IF AS="" THEN 20
30 IF AS<>"Y" THEN 100
40 PRINT:PRINT "YOU ARE AT THE CENTER OF
AN ISLAND WHICH"
50 PRINT "IS A SQUARE WITH SIDES FROM -9
9 TO 99
60 PRINT "TREASURE IS BURIED ON THE ISLA
NO AND YOU"
70 PRINT "WANT TO FIND IT IN 11 MOVES OR
LESS
80 INPUT "PRESS RETURN TO GO ON";
100 PRINT "~"
110 LET C=0
120 LET A=0
130 LET B=0
140 LET T=INT{199*RND{0))-99
150 LET R=INT{199*RND{0))-99
160 IF T=A AND R=B THEN 120
170 LET D=ABS{A-T)+ABS{B-R)
180 IF 0=0 THEN 250
190 PRINT "~":PRINT:PRINT
200 PRINT "YOUR PRESENT POSITION IS";STR
$ {A) ; " , " ; S TR$ { B ) : PRINT
205 PRINT "THE TREASURE IS BURIED";D;"PA
CES AWAY":PRINT
206 PRINT "ENTER -111 TO OUIT":PRINT
210 INPUT "ENTER COORDINATE A";A
215 IF A=-111 THEN 285
220 INPUT "ENTER COORDINATE B";B
225 IF B=-111 THEN 285
230 LET C=C+1
240 GOTO 170
250 PRINT:PRINT "YOU FOUND IT IN ONLY ";
C; "MOVES"
260 IF C<4 THEN PRINT "YOU'RE A GENIUS":
GOTO 290
270 IF C>=4 AND C<12 THEN PRINT "YOU ARE
VERY SMART":GOTO 290
128 GEOMETRY GAMES

280 IF C>11 THEN PRINT "TOO SLOW,THE PIR


ATES GOT IT"
285 PRINT:PRI NT "THE TREASURE WAS AT";T;
",";R
290 PRINT:PRI NT "AGAIN? PRESS Y OR N"
300 GET A$:IF A$="" THEN 300
310 IF A$="Y" THEN 110
320 STOP
Note: The symbol 0 represents SHIFT CLEAR/HOME.
"QUEST" 129

''Quest''
AIM OF THE GAME
You start at the center (0,0) of a square island with sides of length
199. Somewhere in the square is buried the pirates' treasure. The
coordinates of the treasure are T and R which will be integers from -99 to
99. The difference between your coordinates (A,B) and the location of the
treasure (T,R) is given to you at each turn and you are asked to supply
new coordinates. You enter your new coordinates A and B one at a time.
They can be any integers. The distance is different from the Euclidean
distance used in "DETONATION". The distance from (A,B) to (T,R) is
defined as
D = ABS(A-T) + ABS(B-R)
This measure of distance is referred to as the 1-norm.
A count of your moves is kept and if you are too slow, the pirates beat
you to the treasure. This program is fun to run and it is not difficult to
beat the pi rates.

HOW TO PLAY
A typical run would proceed in this fashion. You are given the
message
YOUR PRESENT POSITION IS 0,0
THE TREASURE IS BURIED 131 PACES AWAY
ENTER -111 TO QUIT
ENTER COORDINATE A?
Suppose you enter 99 and then 0 to test the extreme of one
coordinate. In this trial run we received the following message
YOUR PRESENT POSITION IS 99,0
THE TREASURE IS BURIED 185 PACES AWAY
So we were on the right track, but we overshot the mark. This means
that we improved for a distance X, then we worsened by that same
amount, and worsened another 27 units after that. So 99 = 2X + 27
which gives X = 36. So the first coordinate is 36. Let us try 36, followed
by 95 (which adds up to 131). The message we got was
YOU FOUND IT IN ONLY 2 MOVES
YOU'RE A GENIUS
AGAIN? PRESS Y OR N
130 GEOMETRY GAMES

If you wish to play again, press the key Y. By pressing any other key,
you will quit the game.
If the explanation for my move was not very helpful, you should read
the next section where a different approach is suggested. Even if you have
no trouble locating the treasure, you might find something of value in the
mathematical description.

THE MATHEMA TICS


The mathematical problem behind this game is to find the points of
intersection of squares. This is because when distance is measured in the
fashion described above, for a given distance D, the possible location of
the point (T,R) lies on a square surrounding your location (A,B).

E (T,R) CF = ABS (A-T)


FE= ABS (8-R)
so D = CF +FE

When you move to a new location, then a new distance is computed


and this defines a second square.

The two squares usually intersect at two points. (Why not no points?)
However, in rare instances they may touch at only one point, or they may
coincide along a portion of a common boundary so that there are an
infinite number of points in common. In the latter case, you have to shift
to a new location and waste a turn.
"QUEST" 131

You are lucky if there is only one point in common because that is the
solution. For the usual case of two points in common you only have to
check one point. You have a 50% chance of being right. In any case, you
are guaranteed to find the solution in three attempts (unless you were
unlucky and had to waste a turn).
So far I have only given you a geometrical interpretation of the
solution to this problem. How do you go about producing numbers?
I suggest that instead of using equations as we have done in other
cases, this time you should use graph paper. Locate an origin in the
middle of your paper, draw a horizontal line and a vertical line through
your origin. These are your coordinate axes. Now locate the point (A,B) on
your map (it is the origin to start with) and draw the square which is a
distance 0 from the point (A,B). You do this by locating the corners at
(A+O,B), (A-0,8), (A,B+O) and (A,B-0). Now join the corners by straight
lines using a ruler and you will have drawn the square. The treasure lies
somewhere on this square. Now shift your coordinates to another point
and draw the second square with the value of 0 given to you by the
computer. From your graph, estimate the coordinates of the points of
intersection of the squares and try entering them. After some trial and
error, you will become more proficient at drawing graphs and you will
easily be able to find the treasure before the pirates get to it because they
do not have any graph paper!

THE CODE
Lines before 100 may be left out since they only give instructions.
Note the use of INPUT messages. Line 80 contains only a device for
stopping the execution of the program while the screen message is being
read.
132 GEOMETRY GAMES

''Tre k''
10 PRINT "a":PRINT :PRINT"IN STRUCTIO NS? P
RESS Y OR N":PRINT
20 GET A$:IF A$="" THEN 20
30 IF AS<>"Y" THEN 100
40 PRINT "YOUR SPACESHIP IS LOST IN A CU
BIC BLOCK"
50 PRINT "OF SPACE. HOME BASE IS SOMEWHE
RE IN THE"
60 PRINT "CUBE. YOU HAVE TO FIND THE COO
RDINATES"
70 PRINT "(H,O,M) OF HOME BASE, THEY ARE
NUMBERS"
80 PRINT "BETWEEN 0 AND 99.":PRIN T
90 INPUT "PRESS RETURN TO START"; :REM WA
ITS WHILE SCREEN MESSAGE IS READ
100 PRINT "1:.1"
110 LET E=0
120 LET A=0
130 LET B=0
140 LET C=0
150 LET H=INT(100 *RND(0))
160 LET O=INT(100 *RND(0))
170 LET M=INT(10 0*RND(0))
180 LET D=ABS(A-H)+ABS(B-O)+ABS(C-M) :REM
COMPUTES DISTANCE FROM HOME BASE
190 IF D=0 THEN 300
200 PRINT "a"
210 PRINT "YOUR SPACESHIP IS AT"
220 PRINT A;",";B;" ,";C:PRIN T
230 PRINT "THE DIFFERENCE BETWEEN YOUR C
OORDINATES":PRINT
240 PRINT "AND HOME BASE IS";D:PRI NT
250 PRINT "ENTER COORDINATES FOR A JUMP
TO HOME":PRINT
255 PRINT "BASE.ENTER -1 TO OUIT.":PR INT
260 INPUT "ENTER YOUR ESTIMATE OF H";A
262 IF A=-1 THEN 380
264 INPUT "ENTER YOUR ESTIMATE OF O";B
265 IF B=-1 THEN 380
266 INPUT "ENTER YOUR ESTIMATE OF M";C
"TREK" 133

267 IF C=-1 THEN 380


270 LET E=E+1
275 PRINT "~":GOSUB 9000:REM FLASHES COL
ORS FOR SPACE JUMP AND PLAYS MUSIC
280 GOTO 180
300 PRINT "YOU ARE HOME IN";E;"MOVES"
310 IF E<4 THEN PRINT "YOU MUST BE A GEN
IUS"
320 IF E>3 AND E<12 THEN PRINT "YOU ARE
VERY CLEVER"
330 IF E>11 THEN PRINT "GOOD TRY"
340 PRINT:PRINT "AGAIN? PRESS Y OR N"
350 GET A$:IF A$="" THEN 350
360 IF A$="Y" THEN 100
370 STOP
380 PRINT:PRINT"HOME BASE WAS";STR$(H) ;"
, "; STR$ ( 0) ; " , "; STR$ ( M)
390 GOTO 340
9000 REM:SOUND AND COLORS
9010 FOR L=54272 TO 54296:POKE L,0:NEXT:
POKE 54296,15
9020 FOR I=1 TO 3:V=54269+7*I:POKE V+1,1
90:POKE V+2,250:NEXT I
9030 RESTORE
9035 FOR I=1 TO 3:POKE 54269+7*I,33:NEXT
I
9040 FOR I=1 TO 3:READ H(I) ,L(I) :NEXT I:
READ D
9050 IF D=-1 THEN 9090
9060 FOR I=1 TO 3:V=54266+7*I:POKE V,H(I
) :POKE V-1,L(I) :NEXT I
9070 FOR T=1 TO D/20:POKE 53281,9*RND(0)
:NEXT T:REM HOLDS NOTE, COLORS SCREEN
9080 GOTO 9040
9090 FOR I=1 TO 3:POKE 54269+7*I,0:NEXT:
POKE 53281,6:REM TURNS OFF MUSIC, COLORS
9095 RETURN
9900 DATA 11,114,0,0,5,185,1600
9910 DATA 17,37,11,114,8,147,1600
9920 DATA 22,227,17,37,11,114,1600
9930 DATA 28,214,17,37,14,107,100
9940 DATA 27,56,22,227,11,114,2800
9950 DATA 11,114,0,0,5,185,1600
9960 DATA 17,37,11,114,8,147,1600
134 GEOMETRY GAMES

9970 DATA 22,227,17,37 ,11,114,1600


9980 DATA 28,214,17,3 7,14,107,10 0
9990 DATA 30,141,19,6 3,15,70,280 0
9999 DATA -1,-1,-1,-1 ,-1,-1,-1
Note: The symbol l:l represents SHIFT CLEAR/HOME.
"TREK" 135

''Trek''
AIM OF THE GAME
This game is similar to the earlier game of "QUEST". You are the
captain of a spaceship lost in space. You only know the absolute
difference between your coordinates, (A,B,C) and HOME BASE, (H,O,M).
The values of H,O and M can be anything from 0 to 99. You are asked to
supply new coordinates each time you jump through space. The aim is to
get to HOME BASE in the fewest number of turns. If you can do it in three
moves or less you are a GENIUS. Good luck!

HOW TO PLAY
A typical run would proceed in this fashion. We are given the
message
YOUR SPACESHIP IS AT
0,0,0
THE DIFFERENCE BETWEEN YOUR COORDINATES
AND HOME BASE IS 52
ENTER COORDINATES FOR A JUMP TO HOME
BASE, ENTER -1 TO QUIT.
ENTER YOUR ESTIMATE OF H?
Suppose we enter 52, then enter 0 and 0 in response to the prompts
to enter 0 and M. The message is repeated, only this time the distance
turned out to be 76! Evidently we overshot by 24. If we reduce the jump
by 24 and enter 28,0,0 then the difference would remain at 52. This
means that the correct jump is to 14,0,0 and the difference from home
base would be 38. We could now try the warp coordinates 14, then 38,
and then 0. After this move, the distance turned out to be 20. We
continue to try different values in the second coordinate until we are
satisfied that we can not get any closer to home by changes in the second
coordinate. Then we change the last coordinate.
There is a very neat mathematical way of solving this problem in 3
moves. If you are interested, you should read the next section.

THE MATHEMATICS
The mathematical problem behind this game is to find the point of
intersection of three planes. A plane in three-dimensional space is a linear
equation in three variables, such as
2X + 3Y- 4Z = 5
--------------------

136 GEOMETRY GAMES

You always start at the point (0,0,0), and the ship is located at an
unknown point (H,O,M). All the coordinates are non-negative, so the first
distance 01, supplied to you tells you that the sum of the coordinates H,O
and M is 0 1• We write this as
H + 0 + M = 01
If you shift to a point say (99,0,0), then the new distance 0 2 supplied
to you by the computer tells you that the sum of the difference 99-H,O
and M is 0 2 . We write this as
(99-H) + 0 + M = 02
Similarly a shift to (0,99,0) gives you a third distance 03 and a third
linear equation
H + (99-0) + M = 0 3
The solution of these three equations guarantees that you will get
home in three moves, but don't tell you friends how you did it! If you are
having trouble solving these equations, read on.
You should subtract the first equation from the second one to get
(99-H) - H = 0 2 - 0 1
so that 99 - 2H = 0 2 - 01 . This means that
2H = 99 + 0 1 - 0 2 or H = (99+0 1 - 0 2)/2
and H should be an integer, otherwise you have made a mistake in your
arithmetic.
In exactly the same way, when you subtact the first equation from the
third, you will get
0 = (99 + 0 1 - 0 3 )/2
Then, from the first equation, you can find M = D1 - H - 0.
If you would like to practice Gauss Elimination on the matrix
representing the three equations, then you start with the matrix
1 1 1 01
-1 1 1 02-99
1 -1 1 03-99
and add the first row to the second row to get
1 1 1 01
0 2 2 0 1+0 2-99
1 -1 1 Or99
then subtract the first row from the third row to get
1 1 1 01
0 2 2 01+02-99
0 -2 0 Or0 1-99
"TREK" 137

Now you add the second row to the third row to get
1 1 1 01
0 2 2 0 1 +0 2 -99
0 0 2 0 2 +0r198
This is the equivalent system of equations that you can solve by back
substitution. The last equation states that
2M = 02 + 03 - 198 or M = (0 2 + 0 3 )/2 - 99
The second to the last equation gives you
0 = (0 1 + 0 2-99)/2 - M
The first equation gives
H = 01 - 0- M
In this case, the formal process of Gauss Elimination takes longer
than the elimination process described earlier. The point is, Gauss
Elimination can be programmed easily. It does not pay attention to any
particular short cuts that a system of equations may have, but plods a
well defined path to the ultimate goal.

THE CODE
As usual in these listings, lines 10 to 90 give information, while lines
9000 to 9999 create the sound and screen effects. You could ignore
these lines and also delete line 2 7 5 for a much shorter program.
138 GEOMETRY GAMES

''U-Boat''
10 PRINT "~":PRINT:PRINT "INSTRUCTIONS?
PRESS Y OR N":PRINT
20 GET A$:IF A$="" THEN 20
30 IF AS<>"Y" THEN 100
40 PRINT "YOU ARE IN CONTROL OF A SUBCHA
SER. YOUR"
50 PRINT "SONAR GIVES THE RANGE AND BEAR
lNG OF A"
60 PRINT "SUBMARINE. THE SUB IS LOCATED
SOMEWHERE"
70 PRINT "IN A SQUARE -99 TO 99, AND IS
AT A DEPTH"
80 PRINT "OF 0 TO 99. YOUR TASK IS TO FI
ND THE SUB"
90 INPUT "PRESS RETURN TO START";
100 PRINT "~"
110 LET S=INT(199*RND(0))-99:REM SET UP
SUBMARINE COORDINATES
120 LET U=INT(199*RND(0))-99
130 LET B=INT(99*RND(0))
140 LET C=0
150 LET X=0
160 LET Y=0
170 LET Z=0
180 GOSUB 400
190 IF S-X=0 THEN LET A=SGN(U-Y)*90:REM
SUB IS DIRECTLY NORTH OR SOUTH
200 IF S-X<>0 THEN LET A=INT(ATN((U-Y)/(
S-X))*180/rr) :REM SUB BEARING
210 IF S-X<0 THEN LET A=A+180
220 PRINT "SUBMARINE BEARING:";A;"DEGREE
S"
230 PRINT:PRINT "RANGE:";R;"METERS"
235 PRINT:PRINT "YOUR PRESENT POSITION I
S ";X;",";Y
240 PRINT:PRINT "ENTER COORDINATES X,Y A
ND Z FOR THE SUB. USE -111 TO QUIT."
250 PRINT:INPUT "ENTER COORDINATE X";X
252 IF X=-111 THEN 330
254 INPUT "ENTER COORDINATE Y";Y
"U-BOAT" 139

256 IF Y=-111 THEN 330


258 INPUT "ENTER COORDINATE Z";Z
259 IF Z=-111 THEN 330
260 LET C=C+1
270 PRINT "0"
280 IF C=20 THEN 330
290 GOSUB 400
300 IF R>2 THEN 170
310 PRINT "GOT IT IN";C;"MOVES"
320 GOTO 340
330 FOR I=1 TO 9:FOR T=1 TO 9:POKE 53281
,9*RND(0) :NEXT T,I:REM EXPLOSIVE COLOR
332 PRINT "O":POKE 53281,6:REM RETURNS S
GREEN COLOR TO BLUE
335 PRINT:PRINT "SUBMARINE GOT YOU!":PRI
NT "LOCATION WAS:";S;",";U;"," ;B
340 PRINT:PRINT "WANT TO TRY AGAIN?"
350 PRINT "PRESS Y OR N"
360 GET A$:IF A$="" THEN 360
370 IF A$="Y" THEN 100
380 STOP
400 LET R=SQR( (S-X)t2+(U-Y)t2+(B -Z)t2) :R
EM COMPUTES RANGE
410 RETURN
Note: The symbol t:l represents SHIFT CLEAR/HOME_
140 GEOMETRY GAMES

''U-Boat''
AIM OF THE GAME
You are in control of a subchaser. The detection devices, called
SONAR, on your ship provide you with the range and bearing of the U-
boat. Your job is to track down the U-boat in less than 20 moves. This is
similar to the game "DETONATION", however, you have to use both the
Cartesian coordinate system, (X,Y,Z) and the cylindrical polar coordinate
system (R,A,Z).
The U-BOAT is located at the Cartesian coordinates (S,U,B). The S
and U values are the coordinates in the X (or East) direction and theY (or
North) direction and are integers randomly chosen from -99 to 99. The
value of B is the depth, and is an integer from 0 to 98. Evidently, this U-
BOAT cannot fly!
Your coordinates initially are set to be X=0, Y=0 and Z=0. Based
upon the range and bearing of the submarine, you enter your guess at the
coordinates S, U and B. Your subchaser moves to the new position X=S,
Y=U and Z=0. The coordinate Z remains 0 since your subchaser remains
on the surface, at least until you are hit by a torpedo, which does happen
if you are too slow in finding the submarine!
A new range and bearing are given to you, and again you are
prompted to enter your guess at the coordinates S,U and B. The chase
continues until you locate the submarine, or run out of moves.

DOW TO PLAY
A typical run of this program commences with a message like this.
SUBMARINE BEARING: 82 DEGREES
RANGE: 105.569882 METERS
YOUR PRESENT POSITION IS 0,0
ENTER COORDINATES X,Y AND Z FOR THE SUB.
USE -111 TO QUIT.
ENTER COORDINATE X?
You enter your guess of X, then Y and finally the depth Z. If you are not
successful in locating the submarine, your subchaser will now be located
at (X,Y,0). The coordinate Z is set to zero, since your subchaser remains
on the surface, at least until you are hit by a torpedo (which does happen
if you are too slow in finding the submarine!)
The distance is measured in the Euclidean or 2-norm, so the
mathematical way of solving this game is similar to the method described
"U-BOAT" 141

in "DETONATION". If you are interested in exploring this, you should read


the next section.

THE MATHEMA TICS


The mathematical way to solve this is to follow the method outlined in
"DETONATION". You look for the intersection of three spheres, centered,
say, at (0,0,0), (±50,0,0), (0,±50,0). The choice of the sign +50 or -50
is up to you, and depends on the initial bearing of the submarine which
will tell you in which quadrant the submarine is located.
Suppose you try (50,0,0) and (0,50,0), then you will have to solve
the equations

S2 + U2 + B 2 Rf

where R1 , R2 and R3 are the initial range and the ranges at (50,0,0) and
(0,50,0) respectively. Solve these equations and you are guaranteed to
locate the submarine in 3 moves!

THE CODE
The range or distance in the program is the Euclidean distance
worked out in the subroutine at line 400,

R = SQR((S-X) 2 + (U-Y) 2 + (B-Z) 2 )

This is not the distance a crow flies, unless the submarine happens to
be on the surface. Rather, it is the distance a fish swims, to coin a phrase!
The value of R is not an integer, and this makes the arithmetic more
difficult. If you would prefer an integer, put INT in front of the SQR in line
400, but that could be misleading as you found in "DETONATION".
The tricky piece of mathematics in this program, is in the calculation
of the BEARING or direction of the submarine. Here, I use the usual polar
coordinates, based on the East and North coordinates of your ship and
the submarine.
142 GEOMETRY GAMES

N (S, U)

U-Y

(X,Y) S-X E

The submarine is located at (S,U) and you are at (X,Y). The difference
in the East direction is S-X while the difference in the North direction is U-
Y. The BEARING, or angle measured in degrees in a counter clockwise
direction from the East direction is A=ATN((U-Y)/(S-X)) .
Since the submarine could be in any of the four quadrants relative to
your position, this angle can be positive or negative. In lines 190 to 210, I
take care of this as well as the case when S=X, where the submarine is
either directly north or south of you.
"WARP" 143

''Warp ''
10 PRINT "O":PRINT:PR INT"INSTRUC TION? P
RESS Y OR N":PRINT
20 GET AS:IF A$="" THEN 20
30 IF AS<>"Y" THEN 100
40 PRINT " YOUR SHUTTLE IS LOST IN A TIM
E WARP."
50 PRINT "YOUR INSTRUMENTS ONLY GIVE YOU
THE NET"
60 PRINT "ABSOLUTE DIFFERENCE IN YOUR CO
ORDINATES"
70 PRINT "AND THE COORDINATES OF YOUR MO
THER SHIP"
80 PRINT "YOU HAVE FUEL FOR ONLY 20 MOVE
S TO FIND"
90 PRINT "THE COORDINATES X,Y,Z AND T 0
F YOUR"
95 PRINT "MOTHER SHIP.":PRINT :INPUT "PRE
SS RETURN TO START"; :REM MAKES A PAUSE
100 PRINT "0"
110 LET E=20
120 LET A=0
130 LET B=0
140 LET C=0
145 LET F=0
150 LET H=INT(100*R ND(0)) :REM SETS UP SH
IPS COORDINATES
160 LET O=INT(100*R ND(0))
170 LET M=INT(100*R ND(0))
175 LET G=INT(100*R ND(0))
180 LET D=ABS(A-H)+ABS(B-O)+ABS(C-M)+ABS
(F-G) :REM COMPUTES DISTANCE
190 IF D=0 THEN 300
200 PRINT "0"
210 PRINT "YOUR SHUTTLE IS LOST IN A TI
ME WARP":PRINT
220 PRINT "DISTANCE FROM SHIP IS";D;"GAL
ACTIC UNITS":PRINT
230 PRINT "YOUR COORDINATES ARE";STRS(A )
·" "·STRS(B)·" "·STRS(C)·" "·F·PRINT
I I I I I I I I I 0

240 PRINT "YOU HAVE FUEL FOR ONLY";E;"MO


144 GEOMETRY GAMES

VES"
250 PRINT:PRINT "ENTER COORDINATES 0 TO
99 OF YOUR SHIP"
255 PRINT:PRINT "ENTER -1 TO OUIT":PRINT
260 INPUT "ENTER COORDINATE X";A
261 IF A=-1 THEN 400
262 INPUT "ENTER COORDINATE Y";B
263 IF B=-1 THEN 400
264 INPUT "ENTER COORDINATE Z";C
265 IF C=-1 THEN 400
266 INPUT "ENTER COORDINATE T";F
267 IF F=-1 THEN 400
270 LET E=E-1
275 IF E=0 THEN 400
276 PRINT "~":GOSUB 9000:REM FLASHES COL
ORS FOR SPACE JUMP AND PLAYS MUSIC
280 GOTO 180
300 PRINT "Q":PRINT "SUCCESS! YOU ARE HO
ME IN";20-E;"MOVES":PRINT
310 IF E>16 THEN PRINT "YOU SHOULD BE A
STOCK BROKER!"
320 IF E<17 AND E>8 THEN PRINT "YOU ARE
VERY CLEVER"
330 IF E<9 THEN PRINT "GOOD TRY"
340 PRINT:PRINT "AGAIN? PRESS Y OR N"
350 GET AS:IF A$="" THEN 350
360 IF AS="Y" THEN 100
370 STOP
400 PRINT "Q"
405 PRINT "TOO BAD YOU RAN OUT OF FUEL"
410 PRINT "THE SHIP WAS AT";H;O;M;G
420 GOTO 330
9000 REM:SOUND AND COLORS
9010 FOR L=54272 TO 54296:POKE L,0:NEXT:
POKE 54296,15
9020 FOR I=1 TO 3:V=54269+7*I:POKE V+1,1
90:POKE V+2,250:NEXT I
9030 RESTORE:REM RESETS DATA
9035 FOR I=1 TO 3:POKE 54269+7*I,33:NEXT
I
9040 FOR I=1 TO 3:READ H(I) ,L(I) :NEXT I:
READ D
9050 IF D=-1 THEN 9090
9060 FOR I=1 TO 3:V=54266+7*I:POKE V,H(I
"WARP" 145

) :POKE V-1,L(I) :NEXT I


9070 FOR T=1 TO D/20:POKE 53281,9*RND (0)
:NEXT T:REM HOLDS NOTE COLORS SCREEN
9080 GOTO 9040
9090 FOR I=1 TO 3:POKE 54269+7*I,0: NEXT:
POKE 53281,6:REM TURNS OFF MUSIC, COLORS
9095 RETURN
9900 DATA 11,114,0,0,5 ,185,1600
9910 DATA 17,37,11,114 ,8,147,1600
9920 DATA 22,227,17,37 ,11,114,1600
9930 DATA 28,214,17,3 7,14,107,10 0
9940 DATA 27,56,22,227 ,11,114,2800
9950 DATA 11,114,0,0,5 ,185,1600
9960 DATA 17,37,11,11 4,8,147,160 0
9970 DATA 22,227,17,37 ,11,114,1600
9980 DATA 28,214,17,3 7,14,107,10 0
9990 DATA 30,141,19,6 3,15,70,280 0
9999 DATA -1,-1,-1,-1 ,-1,-1,-1
Note: The symbol I:.'J represents SHIFT CLEAR/HOME.
146 GEOMETRY GAMES

''Warp''
AIM OF THE GAME
This is the most difficult of all the hide and seek type of games in this
book. You are separated from your spaceship. You are in a shuttle lost in
a time warp! Your instruments can only give you the total difference in
your coordinates and those of the ship. There are four such coordinates,
three spatial coordinates and one for time. You are prompted to enter
new coordinates each move and you are given your new distance from the
spaceship. You enter the coordinates one after the other. You only have
sufficient fuel for 20 moves, after that you are doomed to float about in
space forever!

HOW TO PLAY
When you run this program you are given a message like this
YOUR SHUTTLE IS LOST IN A TIME WARP
DISTANCE FROM SHIP IS 73 GALACTIC UNITS
YOUR COORDINATES ARE 0,0,0,0
YOU HAVE FUEL FOR ONLY 20 MOVES
ENTER COORDINATES 0 TO 99 OF YOUR SHIP
ENTER -1 TO QUIT
ENTER COORDINATE X?
You enter your guess at the coordinates, one coordinate after the
other. The new message will give you a new distance from your ship. If you
try to solve this game by "guess and by golly", you will have a very
difficult time solving it in the 20 moves allowed. But then again, you might
be lucky.

THE MATHEMATICS
Using analytic geometry, the solution is found in four moves by
finding the point of intersection of four hyperplanes. A hyperplane in the
space with coordinates X, Y, Z, T is represented by a linear equation such
as
2X + 3Y + 5Z + T = 24
I suggest that you go back to the game called "TREK" and master
that game first. The mathematical explanation there will help you. This
game is very similar and you should be able to find four linear equations
in the four unknowns. Thus you are guaranteed a solution in four moves.
If you do better than that, you should become a stock broker!
CHAP TER VI
148 ALGEBRA GAMES

''Monkey Puzzle''
100 FOR A=3 TO 8
105 PRINT"t:l"
110 LET B=INT((A-1)*RND(0))+1
120 PRINT A;"MEN COLLECT A PILE OF COCON
UTS THEY"
130 PRINT "INTEND TO SHARE.WHILE THE OTH
ERS SLEEP,"
140 PRINT "EACH MAN IN TURN WAKES UP AND
DIVIDES"
150 PRINT "THE PILE EVENLY AND HE HAS";B
;"LEFT OVER"
155 PRINT "WHICH HE HANDS OVER TO A MONK
EY,THEN HE"
160 PRINT "HIDES HIS SHARE. NEXT MORNING
THEY"
170 PRINT "SHARE THE REST OF THE COCONUT
S,GIVING";STRS(B)
175 PRINT "TO THE MONKEY."
176 PRINT
180 PRINT "HOW MANY DID THEY HAVE AT THE
START?"
190 INPUT C:IF C=0 THEN 190
200 LET B=INT(At(A+1)-B*(A-1))
210 LET Z=INT(C/B)-C/B
220 IF Z<>0 THEN 270
230 PRINT "RIGHT.NOW TRY ONE MORE"
240 FOR T=1 TO 2000:NEXT T
250 NEXT A
270 FOR I=1 TO A+1
280 LET S=INT(C/A)
290 PRINT" DIVISION ";I;" IS ";S;" MON
KEY GETS ";C-A*S
300 LET C=(A-1)*S
310 NEXT I
320 PRINT "YOUR ANSWER IS WRONG"
330 PRINT "IT SHOULD BE";B
340 PRINT:INPUT "PRESS RETURN TO GO ON";
350 PRINT:PRINT AGAIN? PRESS Y OR N"
360 GET AS:IF A$="" THEN 360
"MONKEY PUZZLE" 149

370 IF A$="Y" THEN A=A-1:GOTO 250


380 STOP
Note: The symbol 1::1 represents SHIFT CLEAR/HOME.
150 ALGEBRA GAMES

''Monkey Puzzle''
AIM OF THE GAME
This is a very tricky puzzle. Your ability to work out the answers will
not necessarily depend upon the amount of mathematics you have
studied. I ran into this problem at a party of university mathematics
professors and their wives. None of the wives had any mathematics
training beyond high school, and yet the most elegant solution to this
puzzle was given by one of the wives, in a matter of a few minutes.
A pile of coconuts is divided up among a group of men in a strange
fashion. First of all, one man divides the pile into equal piles and finds out
that he has some left over, which he gives to a nearby monkey. (That is
where the monkey comes into the story.) The man takes his share away,
and heaps the remaining piles back into one pile. Each of the men, in turn,
do the same thing. There is always the same number of extra coconuts,
which are given to the monkey. Finally, they divide the pile once more, and
again there is the same remainder given to the monkey.
The question you have to answer is: How many coconuts were there
at the start?
The program provides you with all the information necessary to solve
the problem. You start with three men. When you get a correct answer,
you progress to more difficult problems.

HOW TO PLAY
When you run the program, a problem is posed. All the information
needed to solve the problem is given to you. For instance, the first
problem given to you might look like this
3 MEN COLLECT A PILE OF COCONUTS THEY
INTEND TO SHARE.WHILE THE OTHERS SLEEP,
EACH MAN IN TURN WAKES UP AND DIVIDES
THE PILE EVENLY AND HE HAS 1 LEFT OVER
WHICH HE HANDS OVER TO A MONKEY,THEN HE
HIDES HIS SHARE. NEXT MORNING THEY
SHARE THE REST OF THE COCONUTS,GIVING
TO THE MONKEY.
HOW MANY DID THEY HAVE AT THE START?
When you have worked out your answer, you enter it. Suppose you
enter the number 121. The computer will work out the division at each
stage, and display the remainder to be given to the monkey. This way you
"MONKEY PUZZLE" 151

can check the accuracy of your arithmetic. In this case, the computer will
display the following:
DIVISION 1 IS 40 MONKEY GETS 1
DIVISION 2 IS 26 MONKEY GETS 2
DIVISION 3 IS 17 MONKEY GETS 1
DIVISION 4 IS 11 MONKEY GETS 1
YOUR ANSWER IS WRONG.
IT SHOULD BE 79
PRESS RETURN TO GO ON
AGAIN? PRESS Y OR N
You can see that the result of the second division is wrong. You also
learn what the correct answer should be. You press Y and the program
continues by asking you another problem. There are only two problems
involving 3 men. In one, the monkey gets 1 coconut, in the other he gets
2. If you get the same problem, you will enter 79 and the computer will
respond with:
RIGHT.NOW TRY ONE MORE
After a short pause, a new problem will be posed involving 4 men. Each
time you get a correct answer, the new problem involves one more man.
The maximum number of men is 8 because the numbers become too
large for your computer with any more men!

THE MATHEMATICS
The solution is provided by the formula in line 200. Since any
multiple of this solution is also a solution, line 210 checks whether your
answer is a multiple of the solution. You may wonder how I arrived at the
solution. Well I used an old mathematical trick of "borrowing" some
hypothetical coconuts at the start to make the arithmetic work out
exactly, without any remainder, and then the "borrowed" coconuts are
returned after the final division is made.
To understand this process, we use a little algebra. We let X be the
number of coconuts to start with. In the illustrative example given in HOW
TO PLAY, after X has been divided by 3, there is one left over, then X+2
will be exactly divisible by 3 with none left over. So we borrow 2 coconuts
to make the stack X+2. Now, after X+2 has been divided into 3 equal
piles, each pile contains one more coconut than it should. So the first
man, let's call him A, gives his extra one to the monkey. Then he hides his
correct amount. The remaining two stacks will still contain 2 more
coconuts than they are supposed to. This means that the next division by
3 will also work out with none remaining. This time the second man, call
him B, gives his extra one to the monkey. And so on. Every time the
division by 3 works out with no remainder, each man hides his correct
152 ALGEBRA GAMES

amount by giving one to the monkey, so the monkey gets the correct
amount. In the last division, each man has an extra coconut. One man
gives his to the monkey, the other two return one each of the borrowed
coconuts. All the conditions have been satisfied. So X+2 is divisible by 3 a
total of 4 times. So X+2 =constant *81. The constant can be 1,2, .... Say
it is 1, then X+2=81 or X=79.
This solution is fairly neat, but becomes more difficult to generalize
to the case of any number of men M and a remainder of R= 1 or 2 or ...
M-1. The following is the solution given by the wife of a mathematical
colleague of mine.
Consider the number -2. If you divide -2 by 3, you get three equal
shares of -1, and + 1 left over! Give the + 1 to the monkey, take one share
of -1 away, and you are left with two shares of -1 for a total of -2. Thus,
the number -2 is never changed by the process. Any number that is
divisible by 3 a total of 4 times may be added to this -2. So we add
k*3 4 =k*81 where k=1,2, .... For k=1, we have
X = -2 + 81 = 79
This process is more easily generalized. The monkey gets the positive
remainder, while the men each get a share which is the negative of the
monkey's share.
Suppose we have M men, and the monkey gets a remainder of R.
Then the men each get an equal share of -R. This means the "magic"
number is
M*(- R) + R = - R*(M- 1)
In the case worked out above, M=3, R= 1 so the "magic" number is
-1 *(3-1) = -2.
Now we add a multiple of MM+l to the "magic" number to get the
required formula. The case when the multiple is 1 gives
X = Mt(M+1) - R*(M-1)
"OBSCURE AGES" 153

''Obscure Ages''
100 PRINT "1:1"
110 LET P=INT(10*RND(0) )+5
120 LET J=P+INT(20*RND(0))
130 LET R=J+INT(20*RND(0))
140 PRINT "RON IS";R-J;"YEARS OLDER THA
N JIM":PRINT
150 PRINT "RON IS";R-P;"YEARS OLDER THA
N PAT":PRINT
160 PRINT "THEIR COMBINED AGE IS";P+J+R:
PRINT
170 PRINT "WHAT ARE THEIR AGES?":PRINT
175 INPUT "ENTER PAT'S AGE";A
180 INPUT "ENTER JIM'S AGE";B
185 INPUT "ENTER RON'S AGE";C:PRINT
190 IF P=A AND J=B AND R=C THEN 220
200 PRINT "WRONG IT IS ";P;",";J;",";R
210 GOTO 230
220 PRINT "RIGHT ON"
230 FOR T=1 TO 2000:NEXT T
240 PRINT:PRINT "AGAIN? PRESS Y OR N"
250 GET AS:IF A$="" THEN 250
260 IF AS="Y" THEN 100
270 STOP
Note: The symbol 1:1 represents SHIFT CLEAR/HOME.
154 ALGEBRA GAMES

''Obscure Ages''
AIM OF THE GAME
You are given information about the ages of RON, JIM and PAT. From
this information you are asked to deduce their ages. This game is meant
to give you practice at solving a linear system of algebraic equations.
When you have worked out their ages, you enter their ages one after the
other in the order requested by the prompting message on the screen.

HOW TO PLAY
A typical run starts with the following message
RON IS 6 YEARS OLDER THAN JIM
RON IS 24 YEARS OLDER THAN PAT
THEIR COMBINED AGE IS 72
WHAT ARE THEIR AGES?
ENTER PAT'S AGE?
ENTER JIM'S AGE?
ENTER RON'S AGE?
When you have worked out their ages, you enter them in the order
requested, one after the other. Suppose you enter 10, 28 and 34
respectively. You are given the message
RIGHT ON
AGAIN? PRESS Y OR N
If you enter an incorrect answer, then you receive the message
WRONG IT IS 10,28,34
AGAIN? PRESS Y OR N
If you wish to have another go, press the key Y, otherwise press N.

THE MATHEMATICS
The mathematics behind this game is called linear algebra. In this
case, you have to solve three linear equations simultaneously. Let us
suppose that the ages of PAT, JIM and RON are represented by the letters
P, J and R respectively. Then in the game presented in HOW TO PLAY, we
conclude from the given data that
R- J = 6
R- P = 24
P + J + R = 72
"OBSCURE AGES" 155

These equations are called linear equations, because of the variables


P, J and R are only multiplied by constants and added together.
In order to solve these equations, you may manipulate them in three
ways. You may interchange the order of the equations, add a multiple of
one equation to another, or multiply any equation by a nonzero number.
In this case if we add the first two equations to the third equation we
get.
3R = 102 so R = 34
We substitute R = 34 in R- J = 6 to get 34 - J = 6, so that J = 28
and similarly in R - P = 24 we get 34 - P = 24 so P = 10.
When you have mastered this process, you might like to try "MORE
OBSCURE AGES". First, try to solve the game without reading the section
on the mathematics.
----------------

156 ALGEBRA GAMES

''More Obscure Ages''


100 PRINT "1:1"
110 LET R=INT(25·RND(0))+10
120 LET Y=INT(20·RND(0))+2
125 IF Y>=R THEN 120
130 LET O=INT(5·RND(0))+2
135 LET P=O·(R-Y)+Y
140 LET Z=INT(5·RND(0))+2
145 IF Q=Z THEN 140
146 LET J=Z·(R-Y)+Y
147 IF P>100 OR J>100 THEN 110
150 PRINT Y;"YEARS AGO PAT WAS";O;"TIMES
RON'S AGE":PRINT
155 PRINT "AT THAT TIME.JIM WAS";Z;"TIME
S RON'S AGE.":PRINT
160 PRINT "THEIR COMBINED AGE NOW IS";R+
2·Y+O•(R-Y)+Z·(R-Y) ;".":PRINT
170 PRINT "WHAT ARE THEIR AGES?":PRINT
175 INPUT "ENTER PAT'S AGE";A
180 INPUT "ENTER JIM'S AGE";B
185 INPUT "ENTER RON'S AGE";C:PRINT
190 IF A=P AND B=J AND C=R THEN 220
200 PRINT "WRONG IT IS ";P;",";J;",";R
210 GOTO 230
220 PRINT "RIGHT ON MATE!"
230 FOR T=1 TO 2000:NEXT T
240 PRINT:PRINT "AGAIN? PRESS Y OR N"
250 GET A$:IF A$="" THEN 250
260 IF A$="Y" THEN 100
270 STOP
Note: The symbol t:J represents SHIFT CLEAR/HOME.
"MORE OBSCURE AGES" 157

''More Obscure Ages''


HOW TO PLAY
This game is similar to "OBSCURE AGES" but is more difficult. A
typical run will present you with a problem like this
8 YEARS AGO PAT WAS 6 TIMES RON'S AGE
AT THAT TIME. JIM WAS 5 TIMES RON'S AGE.
THEIR COMBINED AGE NOW IS 84.
WHAT ARE THEIR AGES?
ENTER PAT'S AGE?
ENTER JIM'S AGE?
ENTER RON'S AGE?
You enter the ages in the order requested, if you are wrong, the
correct answer is given to you. In either case, you are given a prompt to
quit or go on to another problem.

THE MATHEMATICS
Once again you must solve linear equations. We let P, J and R be the
respective ages of PAT, JIM and RON. Then from the information given
we have
P - 8 = 6(R - 8) or P = 6R- 40
J - 8 = 5(R - 8) or J = 5R - 32
and finally,
P + J + R = 84
If we substitute for P and J in the final equation, which is like
subtracting the first two equations from the third equation, we get
12R - 72 = 84 or 12R = 156 or R = 13
Substituting in the first two equations gives P = 6(13) - 40 or P =
38, and J = 5(13) - 32 = 33.
When you have mastered this game, you should try yourself on
"MUCH MORE OBSCURE AGES". Try to solve the game without referring
to the section on the mathematics.
158 ALGEBRA GAMES

''Much More
Obscure Ages''
100 PRINT "t."J"
110 LET P=INT(25*RND(0))+10
120 LET O=INT(5*RND(0))+2
130 LET Z=INT(5*RND(0))+2
135 IF O=Z THEN 120
140 LET J=O*P
143 LET R=(Z+0)/2
145 IF INT(R)-R<0 THEN 120
147 LET R=R*P
148 IF P>99 OR J>99 OR R>99 THEN 110
150 PRINT "WHEN RON WAS AS OLD AS JIM IS
NOW, HE":PRINT
155 PRINT "WAS";Q;"TIMES AS OLD AS PAT I
S NOW.":PRINT
156 PRINT "WHEN JIM IS AS OLD AS RON IS
NOW, RON":PRINT
157 PRINT "WILL BE";Z;"TIMES AS OLD ASP
AT IS NOW.":PRINT
160 PRINT "THEIR COMBINED AGE IS";P+J+R;
".":PRINT
170 PRINT "WHAT ARE THEIR AGES?":PRINT
175 INPUT "ENTER PAT'S AGE";A
180 INPUT "ENTER JIM'S AGE";B
185 INPUT "ENTER RON'S AGE";C:PRINT
190 IF A=P AND B=J AND C=R THEN 220
200 PRINT "WRONG IT IS ";P;",";J;",";R
210 GOTO 230
220 PRINT "VERY CLEVER OF YOU"
230 FOR T=1 TO 2000:NEXT T
240 PRINT:PRINT "AGAIN? PRESS Y OR N"
250 GET AS:IF AS="" THEN 250
260 IF AS="Y" THEN 100
270 STOP
Note: The symbol t."J represents SHIFT CLEAR/HOME.
"MUCH MORE OBSCURE AGES" 159

''Much More
Obscure Ages''
HOW TO PLAY
This game is similar to the previous two games, but more difficult as
the name implies. A typical problem looks like this
WHEN RON WAS AS OLD AS JIM IS NOW, HE
WAS 2 TIMES AS OLD AS PAT IS NOW.
WHEN JIM IS AS OLD AS RON IS NOW, RON
WILL BE 6 TIMES AS OLD AS PAT IS NOW.
THEIR COMBINED AGE IS 154.
WHAT ARE THEIR AGES?
ENTER PAT'S AGE?
ENTER JIM'S AGE?
ENTER RON'S AGE?
As before, you enter the ages in the requested order. If you are
wrong, the correct answer is given to you. In any case, a prompt is given
to continue with another problem or to quit.

THE MATHEMATICS
As before, we let the ages be represented by P,J and R respectively.
From the information given in the problem, we have to introduce another
unknown, say D, which is the difference in the ages of RON and JIM. Then
we have
J = R- D = 2P
R + D = 6P
P + J + R = 154
If we add the first two equations, the D is eliminated to give 2R=8P
or R=4P. We also have J=2P from the first equation, so substituting
these into the third equation gives
7P= 154 or P=22
So J=44 and R=88.

THE CODE
The essential difficulty in preparing the code was to define the
correct quantities. P and the multipliers are defined. J was automatically
defined from these quantities. Now came the tricky part. I tried defining a
160 ALGEBRA GAMES

difference D, but the arithmetic would not work out. The solution was to
define R, but only if the sum of the multipliers are even!
Now I leave you with the question, why was this restriction imposed?
The multipliers are Q and Z, and R is defined to be

(Q + Z)
R *P , (see lines 143 to 147)
2
GENERAL MATHEMATICS
BACKGROUND
The three games involving OBSCURE AGES all involved systems of
linear equations, which we solved by manipulating the equations to obtain
a simpler equation involving just one unknown. We then substituted the
value of this unknown back into other equations.
One of the major uses of computers is to solve very large systems of
linear equations that may contain thousands of equations. The equations
are stored in matrix form and many methods have been designed to solve
these large systems economically and accurately. Numerous books have
been written about matrix algebra and linear systems and a lot of
research is being done right now on these problems. By the term matrix,
we mean an array. For example the linear equations X + Y + Z = 4 , 2X-
Y = 3, 2Y - Z = 5 can be represented by the array
1 1 1 4
2 -1 0 3
0 2 -1 5
Here we drop the X, Y, Z and = . The columns keep track of which symbol
belongs to which number. Now in order to solve this system, we first
reduce it to an equivalent system that has the same solution, but is
simpler to solve. We subtract twice the numbers in the first row from the
corresponding numbers in the second row so that the number 2 in the
first column becomes 0. Thus the equivalent system is
1 1 1 4
0 -3 -2 -5
0 2 -1 5
We now add the numbers in the third row to the corresponding numbers
in the second row to get the equivalent system
1 1 1 4
0 -1 -3 0
0 2 -1 5
Now we add twice the numbers in the second row to the corresponding
numbers in the third row which reduces the number 2 in the third row,
second column to 0. The equivalent system is
"MUCH MORE OBSCURE AGES" 161

1 1 1 4
0 -1 -3 0
0 0 -7 5
Now we solve this system which is called a row reduced system, where the
matrix of coefficients is said to be triangular. The method of solution is
called back-substitution. We start with the third row, that stands for
- 7Z = 5
The solution is easy. It is Z = -o/1. The second to the last row stands for
- Y - 3Z = 0 or Y = - 3Z = 1o/7
Finally, the first row stands for
X+Y+Z=4
but now we know what Y and Z are, so
X= 4 - Y - Z = 4 - 1 o/1 + o/1 = 1 o/1
This process is called Gauss Elimination.
162 ALGEBRA GAMES

''Prod uct and Sums' '


100 PRINT "t:J"
110 LET A=INT(19*RN D(0))-9
120 LET B=INT(19*RN D(0) )-9
130 LET C=INT(19*RN D(0))-9
140 IF A>=B OR A>=C OR B>=C THEN 110
150 IF A*B*C=0 THEN 110
160 PRINT "l:l"
170 PRINT "PRODUCT A*B*C =";A*B*C
180 PRINT:PRINT "SUM A*B+A*C+B*C =";A*B+
A*C+B*C
190 PRINT:PRINT "AND SUM A+B+C =";A+B+C
200 PRINT:PRINT "THE VALUES OF A,B AND C
ARE FROM -9 TO 9"
205 PRINT "AND A<=B<=C. WHAT ARE A,B AND
C?":PRINT
210 INPUT "ENTER THE VALUE OF A";P
220 IF A<>P THEN 290
230 PRINT "A IS RIGHT"
240 INPUT "ENTER THE VALUE OF B";Q
250 IF 8<>0 THEN 290
260 PRINT "B IS RIGHT"
270 INPUT "ENTER THE VALUE OF C";R
280 IF C=R THEN 320
290 PRINT:PRINT "WRONG! PRESS Q TO QUIT
ANY OTHER KEY TO TRY AGAIN.":PRIN T
300 GET A$:IF A$="" THEN 300
305 IF A$="0" THEN PRINT"THE VALUES ARE"
:PRINT A,B,C:PRINT: GOTO 330
310 GOTO 160
320 PRINT:PRINT "RIGHT ON"
330 PRINT "AGAIN? PRESS Y OR N"
340 GET AS:IF A$="" THEN 340
350 IF A$="Y" THEN 100
360 STOP
Note: The symbol l:l represents SHIFT CLEAR/HOME.
"PRODUCT AND SUMS" 163

''Product and Sums''


AIM OF THE GAME
This is another game in which you have to determine three unknowns
from certain given facts. However, it is different from the "OBSCURE
AGES" type of problems because in this case you have to solve a system
of nonlinear equations. You may use algebra or arithmetic to solve these
problems so this game could be included in the chapter on arithmetic
games.

HOW TO PLAY
Prompts provide you with all the instructions necessary. A typical run
would look like this
PRODUCT A*B*C=336
SUM A*B+A*C+B*C=-50
AND SUM A+B+C=-7
THE VALUES OF A,B AND C ARE FROM -9 TO 9
AND A<=B<=C. WHAT ARE A,B AND C?
ENTER THE VALUE OF A?
Suppose you work out that A = -8, B = -6 and C = 7 then you would
enter these numbers one by one. After each entry you are given a
message. After you enter A you are told
A IS RIGHT
After you enter B, the message
B IS RIGHT
appears, and after you enter C you get the message
RIGHT ON
AGAIN? PRESS Y OR N
If you want another problem, press the Y key. If you wish to quit, just
press any other key.
If you enter an incorrect entry then the message WRONG appears
and you are prompted to quit or to try the same problem again.

THE MATHEMATICS
The arithmetic way of solving this problem is to find all the factors of
the product. In the example given, this would be:
336 = 2*2*2*2*3*7
164 ALGEBRA GAMES

Since the product is positive, there has to be either two negative


numbers or none. As the sum is negative, there cannot be no negatives,
so there must be two. After a little thought, the numbers -8, -6 and 7 are
found to fit all the facts given. Here we are guided by the factors, 8 =
2*2*2, 6 = 2*3 and 7 include all the factors in the product. Essentially
the solution is obtained by mental trial and error.
The algebraic method is to eliminate some of the unknowns. We
multiply the second equation

A*B + A*C + B*C = -50


by A, to obtain

A*A*(B+C) + A*B*C = -50*A


Now we substitute for the product A*B*C = 336, and from the sum, A+
B + C = -7, we have that B + C = -7 -A. Thus the equation becomes

A*A*(-7-A) + 336 = -50*A

or

A3 + 7 A 2 - 50A - 336 = 0

which is a single cubic (degree three) equation in A. You will notice that
the coefficients in this equation are the numbers (except for signs) that
appeared in the statement of the problem. The reason for this is that the
cubic equation for A contains all the information about the three
unknowns. The three roots of this equation will represent the three values
of A, B and C.
If we consider the three factors (X-A), (X-B) and (X-C) and multiply
them together we get

(X-A) (X-B) (X-C)

Now if we expand the product we get

X3 - (A+B+C)X 2 + (AB+AC+BC)X - ABC

If we substitute the values given for the sums and products we get

X3 + 7X 2 - 50X - 336

The roots of this polynomial are the values A, B, and C that we seek.
By trial and error we first substitute X = 1, -1, 2, -2 and so on until
we get X= -6. We find that the expression is zero. This means that one of
the roots is -6, and one of the factors is X+6. We now divide the factor
(X+6) into X3 + 7X 2 - 50X- 336 using the process of synthetic division
described in "YOUR NUMBER IS UP".
"PRODUCT AND SUMS" 165

1 7 -50 -336

X =-6 1:;::;""' 1:;:::-""'" -5 6 /" 0

This means X3 + 7X 2 - 50X - 336 = (X+6)(X 2 +X-56)


The factors of X2 + X - 56 = (X+8)(X-7)
Thus the required factors are found, and the roots are -8, 7, -6. The
order required by the program makes A=-8, 8=-6 and C=7.

THE CODE
For a more challenging game of product and sums, you might like to
alter the definitions of A, 8 and C in lines 110 to 130 to allow larger
numbers.
Note: To find out more about roots and root finding, check into the
programs "FIXED-POINT" and "BRACKET" in Chapter VII of this book.
166 ALGEBRA GAMES

''Rally''
100 PRINT "t:l"
110 LET R=INT(9*RND(0))+5
120 LET C=R+INT(15*RND(0))+5
125 IF C<R THEN 110
130 LET I=INT(80*RND(0))+20
140 LET J=INT(80*RND(0))+20
150 LET T=I+J
160 LET D=R*I+C*J
165 IF D>1000 THEN 110:REM LIMIT TOTAL D
!STANCE TO 1000 MILES
170 PRINT "YOU CAN RUN AT A SPEED OF";R;
"M.P.H.":PRINT
180 PRINT "AND YOU CAN CYCLE AT";C;"M.P.
H. HOW LONG":PRINT
190 PRINT "WOULD YOU RUN AND HOW LONG WO
ULD YOU":PRINT
195 PRINT "CYCLE SO AS TO COVER ";D;"MIL
ES IN";T:PRINT
196 PRINT "HOURS EXACTLY?":PRINT
200 INPUT "ENTER RUN TIME";A:INPUT "ENTE
R CYCLE TIME";B;PRINT
210 IF A=I AND B=J THEN PRINT "RIGHT"
220 IF A<>l OR B<>J THEN PRINT "WRONG. I
T IS";I;",";J
230 PRINT "AGAIN? PRESS Y OR N"
240 GET AS:IF A$="" THEN 240
250 IF A$="Y" THEN 100
260 STOP
Note: The symbol t:1 represents SHIFT CLEAR/HOME.
"RALLY" 167

''Rally''
AIM OF THE GAME
This is a simple game where you are asked to cover a given distance
in an exact time. You may run or ride a bicycle. The speeds for both
modes of travel are given to you.
This is a quick and easy game to play and is designed to shape up
your ability to do mental arithmetic. It involves the solution of linear
equations, only this time you have two equations in two unknowns.

DOW TO PLAY
A hypothetical run could go like this. You are presented with a
problem
YOU CAN RUN AT A SPEED OF 1 M.P.H.
AND YOU CAN CYCLE AT 3 M.P.H. HOW LONG
WOULD YOU RUN AND HOW LONG WOULD YOU
CYCLE SO AS TO COVER 264 MILES IN 118
HOURS EXACTLY?
ENTER RUN TIME?
ENTER CYCLE TIME?
Suppose you work out the run time to be 45 hours and the cycle time
to be 73 hours. You would enter 45 and then 73. You are correct, and the
computer responds with the message
RIGHT
AGAIN? PRESS Y OR N
If you want to try another problem, press the Y key. Press the N key
to quit.
If you enter a wrong answer, you will be given the correct answer and
prompted to continue or to quit.

THE MATHEMATICS
If we let the run time be R and the cycle time be C, then from the
information given in the sample problem, we can write down two
equations. They are
(l)R+C 118
(2) R + 3C = 264
168 ALGEBRA GAMES

This system is particularly simple. We subtract equation (1) from


equation (2) to obtain 2C = 146 or C = 7 3. Substituting this into (1)
gives us R + 73 = 118 and so R = 45.
You will always find that the run speed is at least 5 M.P.H. In that
case you might end up with equations like
R + C = 88
5R + 8C = 644
This time we subtact 5 times the first equation from the second to
get 3C = 204 or C = 68 and so R = 20.
A*B*C 4
A*B+A *B+B*C
--4
A+B+C 1
A ?B ?C?
170 ROOT FINDERS

''Fixed-Point''
100 DEF FNG(X)=-(P*X*X+O*X+R)/(X*X)
110 PRINT"~": LET I=0
120 PRINT "DEFINE FUNCTION X=G(X) IN LIN
E 100"
130 PRINT:PRINT "NUMBER OF SIGNIFICANT D
IGITS? ENTER 1-8"
140 INPUT S
150 LET S=.5*10t(1-S)
160 PRINT:PRINT "ENTER P,O,R"
170 INPUT P,O,R
180 PRINT:PRINT "ENTER INITIAL VALUE OF
THE ROOT"
190 INPUT X1
200 PRINT "~":PRINT "ITERATION ROOT DIG
ITS"
210 LET I=I+1
220 LET X2=FNG(X1)
230 LET A=ABS((X2-X1)/X2)
240 PRINT I;TAB(5) ;X2;TAB(14) ;INT(1-LOG(
2 *A) I LOG ( 10) )
250 LET T=ABS((FNG(X2)-FNG(X1))/(X2-X1))
260 IF T>=1 THEN PRINT "NOT CONVERGING"
270 IF A<=S THEN PRINT "CONVERGED.ANOTHE
R ROOT? PRESS Y OR N":GOTO 300
280 LET X1=X2
290 GOTO 210
300 GET Z$:IF Z$="" THEN 300
310 IF Z$="Y" THEN LET I=0:PRINT "~":GOT
0 180
320 STOP
Note: The symbol ~ represents SHIFT CLEAR/HOME.
''FIXED-POINT '' 171

''Fix ed-P oint' '


This program and the next, "BRACKET", are not games. They are
strictly mathematical problems, although experimenting with different
numerical methods can be a sort of game. The idea is to find out which
method works best on a given problem, and what pitfalls lurk in a method
to trap the unwary.
Solving the equation F(X) = 0 is a very important problem. These
values of X are referred to as zeros or roots of the function F. You came
across this problem in "PRODUCTS AND SUMS".
There are many different numerical methods for finding zeros, but
they do not always work equally well. In fact some very good, fast
methods, like Newton's method, can fail miserably at times.
The method used in this program is not fast. It often fails to
converge, but it is widely used. It is extremely simple to program, and has
important generalizations to the problem of solving large systems of
linear equations by iteration.

THE MATHE MATICS


The idea is to change the equation F(X) = 0 to an equation X = G(X).
A fixed-point of the function G is a value of X such that X = G(X), then
X - G(X) = F(X) = 0 and the fixed-point of G is a zero of F.
In "PRODUCTS AND SUMS", we needed to find a zero of the
polynomial
F = X3 + PX 2 + QX + R
In this case, P, Q and R are my names for the numbers that are the
coefficients. There are many ways of constructing fixed-point expres-
sions. Here are just two.
(1) We write F(X) = 0 as X3 = -(PX 2 + QX + R), then divide by X2 to
get
X = -(PX 2 + QX + R)/X 2
Thus, we define
G(X) = - (P*X*X + Q*X + R)/(X*X)
This is done in the program listing.
(2) We take a cube root in the expression above to get
X =- (PX 2 + QX + R) t(%)
In this case we define
G(X) =- (ABS(P*X*X + Q*X + R)) t(Y3)
172 ROOT FINDERS

Having constructed suitable fixed-point expressions, we now


generate a sequence of numbers that we hope, will converge to a root.
We start with any approximate value of the root, call it X0 , in the case
of the expression in (1), we should avoid the value of 0. Any other number
will do. Then, the program defines the sequence
X1 G(X 0 )

and so on. Provided the rate of change in G is less than 1, in absolute


value, this simple sequence will converge. The code estimates the rate of
change using line 250, which checks the value
ABS((G(X 2 ) - G(X 1))/(X 2 - X1))
and gives a message
NOT CONVERGING
if this expression is not less than 1.
We must provide a stopping rule. The one in this program is based
upon an estimate of the number of correct significant figures in the last
iteration. The rule is given in the following.
If X is the exact value, and X an approximate value of X, then there
are said to be S significant figures in X provided
X-X
ASS ( - - ) < = 0.5*10i(1-S)
X

Of course we do not know what X is, so in place of xxx we use the


difference in the last two iterates, divided by the last iterate. This usually
causes the program to stop too soon, so you should ask for more
significant figures than you really need.

HOW TO PLAY
Messages and prompts give you all the instructions that you need.
However, you will need to have some values of P, Q and R. I suggest you
use the values given in the program "PRODUCTS AND SUMS". In which
case P = 7, Q =-50 and R = -336. In every case the R in this program is
-(PRODUCT), Q = (SUM OF PRODUCTS), and P = -(SUM). You can
make up your own values, like 3,4 and 5, or 6, 7 and 8 etc. Then
experiment with different starting values.
''FIXED-P OINT'' 173

I encourage you to experiment with different choices of the function


G(X). Simply change the definition in line 100. You might like to try the
simple quadratic, PX 2 + QX + R = 0 and find a zero by constructing a
fixed-point function G.
Note: If you are still reading this far, I want to encourage you to
investigate the most importan t application of this method to iterative
methods for large systems of linear equations.
We write a system of linear equations in matrix form as
AX = 8
where A is a matrix with DIM A(N,M) say. X and 8 are vectors, DIM X(M)
and DIM 8(N). In this case we have N linear equations in M unknowns.
Instead of solving AX- 8 = 0 for a zero, we split the matrix A, and
form a fixed-point iteration scheme
AX =
8 becomes X = D - GX
We then form the iterates X0 , X1 , ... starting from any initial X0 , and
using
xl+l =D - GX1
This scheme will converge provided the rate of change, measured by
the norm of G, is less than 1.
174 ROOT FINDERS

''Bracket''
100 DEF FNF(X)=X*X*X+P*X*X+O*X+R
110 PRINT "a":LET I=0
120 PRINT "DEFINE FUNCTION F(X)=0 IN LIN
E 100"
130 PRINT:PRINT "NUMBER OF SIGNIFICANT D
IGITS? ENTER 1-8"
140 INPUT S
150 LET S=.5*10t(1-S)
160 PRINT:PRINT "ENTER P,O,R"
170 INPUT P,Q,R
180 PRINT:PRINT "ENTER X1,X2 THAT BRACKE
T THE ROOT"
190 INPUT X1,X2
200 LET X3=(X1-X2)/2
205 PRINT:PRINT "ENTER 1 FOR BISECTION M
ETHOD":PRINT
206 PRINT "OR 2 FOR REGULAR FALSI":INPUT
M
210 PRINT "ITERATION ROOT DIGITS"
220 LET F1=FNF(X1)
230 LET F2=FNF(X2)
235 IF SGN(F1)=SGN(F2) THEN PRINT "ROOT
NOT BRACKETED":PRINT
236 IF SGN(F1)=SGN(F2) THEN PRINT "CONT
INUE? PRESS Y OR N":GOTO 340
240 LET I=I+1
250 LET X4=(X1+X2)/2:IF M=2 THEN LET X4=
(F1*X2-F2*X1)/(F1-F2)
260 LET F3=FNF(X4)
270 IF SGN(F3)=SGN(F1) THEN LET X1=X4
280 IF SGN(F3)=SGN(F2) THEN LET X2=X4
285 IF X4=0 THEN LET X4=. 1E-10
290 LET A=ABS((X3-X4)/X4)
300 PRINT I;TAB(5) ;X4;TAB(14) ;INT(1-LOG(
2 *A) I LOG ( 10) )
310 LET X3=X4
320 IF A<=S THEN PRINT "CONVERGED.ANOTHE
R ROOT? PRESS Y OR N":GOTO 340
330 GOTO 220
340 GET ZS:IF Z$="" THEN 340
"BRAC KET" 175

350 IF Z$="Y " THEN LET 1=0:P RINT "O":G OT


0 180
360 STOP
Note: The symbol 0 represents SHIFT CLEAR/HOME.
176 ROOT FINDERS

''Bracket ''
If you have not tried "FIXED-POINT", please do so before running this
program.

HOW TO PLAY
Messages and prompts give you all the instructions that you need. As
in the program "FIXED-POINT", you will need to enter values of P, Q and
R that define a cubic X3 + PX 2 + QX + R for which you wish to find a zero.
An essential difference in this program is that you must enter two initial
values. These two values must bracket the zero. This means one must be
less than the zero, while the other must be more. If you have no idea
where the zero is, you might try -100,000 and 100,000. This is crude,
but it works.
This program provides you with a menu of two different methods.
They are very similar in nature. The difference will be explained in the
mathematics section. Simply experiment by choosing either of the two
methods.
If you fail to choose initial values that bracket a root, then a message
will be given to you prompting for new initial values.

THE MATHEMATICS
There are two methods used in this program. The first method is
called the method of BISECTION. It is a very simple idea, and it always
works, provided the function is continuous. Polynomials are always
continuous, so it will always find the real zeros of polynomials.
We wish to find a zero of the function F(X) that lies between two
values X1 and X2 . We simply evaluate the function at both X 1 and X2 , say
F1 = F(X 1 ) and F2 = F(X 2 ). Now we choose the mid-point of the interval
[X 1 , X2 ]. That is, let
X3 = (X 1 + X2 )/2 . We find F3 = F(X 3 ).
If the sign of F3 is the same as the sign of F1 , we replace X 1 by X3 ,
otherwise we replace x2 by x3.
x3
_ _ _ ._X_ __
By doing this we have halved the width of the interval that contains
the required zero. After 10 such halvings, we will have reduced the
interval by a factor of 1024!
"BRACKET" 177

This method is guaranteed to work. What is more, you will be able to


locate all the zeros of the function F by this method. With "FIXED-POINT"
you could only locate one zero, if you could find any at all!
The second method in this program is called REGULAR FALSI or the
method of false position. It is similar to the method of BISECTION, but it
tries to do better than just select the mid-point of the interval [X 1 , X2 ]. If
the magnitude of F1 is much greater than F2 , it stands to reason that the
zero is more likely to be closer to F2 . So the new point X3 is chosen as a
weighted average of the points X1 and X2 . In line 250, we define the new
point as
(F 1 *X 2 - F2 *X 1 )/(F 1
- F2 )

The reason for the subtractions is that F1 and F2 have opposite signs,
so in effect, we will now be adding quantities with the same sign, (unless
the values of X have different signs, in which case X3 should be nearer 0).
Note: This is a very powerful program for finding the zeros of any
kind of function, not just polynomials. It has only one fault. That is, it can
only find real roots. If roots are complex, then there are other methods
designed for just that purpose.

PROBL EMS
Here are some problems for you to experiment with.
1. Find a root of X3-X-1=0 in the interval [1,2] to an accuracy of 5
significant figures.
2. Find a root of X-2-x=0 in the interval [0, 1] to an accuracy of 5
significant figures.
3. Solve the equations
(a) 3X = 2-3x+X 2
(b) X = cosX
(c) 2sinX = X near X = 1. 7
(d) X = tan X near X = 100 (difficult to bracket the root of X-
tan X near X = 100, and gets considerably worse at 1000,
10,000 and so on).
APPENDIX 179

App end ix
These utility programs have been written in order to save you time.
The first two programs take all the work out of loading and running the
programs in this book from disk, not that this task is particularly arduous!
The music maker program helps you create your own tunes which you can
add to the listings of the games in this book.
The first two programs, "DISK LOAD", and "LOADER", are meant to
be used only if you have a disk drive. The idea is that once you have
loaded the program "DISK LOAD", and typed in RUN, then without
interruptin g the execution, you can load and run programs continuously.
These programs were written for the floppy disk that can be purchased
with this book. If you have not purchased the prerecorded disk, and want
to store some or all of the games in this book on your own floppy disk, you
will find these programs very useful.
The first program, called "DISK LOAD", is not essential. This simply
turns on different colors for the border and the screen and gives the
credits. If you do not like my choice of colors, you should experiment with
the settings in lines 230 to 300. "DISK LOAD" is used to load the
program that does all the work. This program is called "LOADER".
When you run "DISK LOAD" it stops with the message
LOAD"LO ADER" ,8
HIT RETURN AND THEN TYPE RUN
The curser is poised on the LOAD command so that when you press
RETURN, the program, "LOADER" is loaded into the computer. You then
type in RUN to execute "LOADER".
The program "LOADER" displays the chapters of the book, and the
games in each chapter. When you make a selection, say the game
"SIMON", the program stops with the message
LOAD"SIM ON" ,8
PRESS RETURN
The curser is positioned on the LOAD command so that when you press
the RETURN key, "SIMON" will be loaded.
In order to make an endless chain of loadings, you must alter every
STOP statement in each game program. For example, in "SIMON" you
must change line 305 from
305 STOP
to
305 LOAD"DI SK LOAD",8
180 APPENDIX

If you do not like a particular choice of color combinations that is


generated in "DISK LOAD", you can always return to the standard colors
by pressing the RESTORE key while you hold down the RUN/STOP key.
"DISK LOAD" 181

''D isk Loa d''


100 PRINT "rJ"
110 FOR I=1 TO 10:GOSUB 200
120 POKE 53280,B :REM: SETS BORDER COLOR
130 POKE 53281,S :REM: SETS SCREEN COLOR
140 FOR T=1 TO 99:NEXT T:REM PAUSES
150 NEXT I
160 PRINT: PRINT: PRINT CHRS(W) ;TAB(1 0) ;"R
OBERT J. BRADY CO."
165 PRINT: PRINT TAB(5 );"A PRENTICE-HALL
PUBLISHING AND"
170 PRINT TAB(8) ;"COMMUNICATIONS COMPANY
175 PRINT: PRINT: PRINT TAB(15 );"PRES ENTS"
180 PRINT: PRINT: PRINT TAB(5) ;"BRAIN GAME
S FOR KIDS AND ADULTS"
185 PRINT: PRINT: PRINT TAB(18 );"BY"
190 PRINT: PRINT TAB(11 );"JOHN STEPHENSON
191 PRINT: PRINT TAB(1 5);"(C) 1984"
195 PRINT: PRINT: PRINT: PRINT "LOAD ";CHRS
(34) ;"LOAD ER";CH R$(34) ;",8"
196 PRINT: PRINT " HIT RETURN AND THEN T
YPE RUNOO":FOR I=1 TO 13:PRIN T:NEXT :STOP
200 REM: THIS CHOOSES B=BORDER COLOR,S=S
CREEN COLOR AND W=WRITE COLOR
210 P=INT( 8*RND (0))+1
220 ON P GOTO 230,24 0,250, 260,27 0,280, 29
0,300
230 B=3:S= 10:W=2 8:RETU RN
240 B=10:S =3:W=3 1:RETU RN
250 B=7:S= 3:W=31 :RETUR N
260 B=13:S =10:W= 28:RET URN
270 B=6:S= 3:W=31 :RETUR N
280 B=3:S= 14:W=3 1:RETU RN
290 B=10:S =0:W=1 50:RET URN
300 B=14:S =7:W=1 49:RET URN
Note: 00 in line 196 is the key CLEAR/HOME and sends the curser
scurrying back to the top left hand corner of the screen. Do not use SHIFT
and CLEAR/HOME as this would clear the screen as well.
182 APPENDIX

''Loa der''
100 PRINT "C"
110 Z=1:REM Z CONTROLS THE REPEAT DISPLA
Y IN CASE AN ENTRY IS NOT VALID
120 PRINT "THE CHAPTERS ARE:"
130 PRINT
140 PRINT II 1.CONCENTRATION GAMES"
150 PRINT II 2.WORD GAMES"
160 PRINT" 3.LOGIC AND STRATEGY GAMES"
170 PRINT II 4.ARITHME TIC GAMES"
180 PRINT II 5.GEOMETRY GAMES"
190 PRINT II 6.ALGEBRA GAMES"
195 PRINT II 7.ROOT FINDERS"
196 PRINT " 8.APPEND IX"
200 PRINT:PRI NT "TO LIST GAMES PRESS AN
UMBER 1 TO 8"
210 PRINT:PRI NT "PRESS S TO STOP"
220 GET A$:IF A$="" THEN 220
230 IF A$="S" THEN STOP
240 P=INT(VA L(A$)) :REM PIS THE VALUE OF
THE INPUT AND CONTROLS THE GOTO IN 200
250 IF P>0 AND P<9 THEN ON P GOTO 1000,2
000,3000 ,4000,500 0,6000,70 00,8000
255 REM:CONTROL COMES HERE WHEN ENTRY IS
NOT VALID
260 PRINT "WRONG ENTRY TRY AGAIN"
270 FOR T=1 TO 1000:NEXT T:ON Z GOTO 100
,300:REM WAITS WHILE 260 IS VIEWED
300 PRINT "C"
310 Z=2
320 IF P<8 THEN PRINT "THE GAMES IN CHAP
TER ";A$;" ARE:":PRI NT
325 IF P=8 THEN PRINT "THE APPENDIX PROG
RAMS ARE:":PRI NT
330 FOR I=1 TO N
340 PRINT STR$(I); ".";N$(I) ;REM THIS LIS
TS THE GAMES IN A CHAPTER
350 NEXT I
360 PRINT:PRI NT "TO LOAD PRESS A NUMBER
1 TO ";N
370 PRINT:PRI NT "PRESS S TO STOP, OR C T
"LOADER" 183

0 VIEW CHAPTERS"
380 GET C$:IF C$="" THEN 380
390 IF C$="S" THEN STOP
400 IF C$="C" THEN 100
410 Q=INT(VA L(C$))
420 IF 0>0 AND O<N+1 THEN 440
430 GOTO 260
440 PRINT "C":REM NORMALIZE SCREEN
445 POKE 53280,14:P OKE 53281,6:P RINT CHR
$(154)
450 REM:LOADING INSTRUCTIONS ARE HERE
460 PRINT "C":PRINT :PRINT:PR INT
470 PRINT "LOAD ";CHR$(34 );N$(Q);C HR$(34
);",8"
480 PRINT:PRI NT"PRESS RETURN";"OO"
490 STOP
999 REM:GAMES DATA IS LOADED INTO N$ BY
THESE SUBROUTINES
1000 N=5
1010 N$(1)="CONCENTRATION"
1020 N$(2)="FL ASH"
1030 N$(3)="KN IGHT"
1040 N$(4)="KNIGHTMARE"
1050 N$(5)="SIM ON"
1060 GOTO 300
2000 N=6
2010 N$(1)="ANAGRAMS I"
2020 N$(2)="ANAGRAMS II"
2030 N$(3)="HANGMAN"
2035 N$(4)="FI ND A WORD"
2040 N$(5)="EN IGMA I"
2050 N$(6)="EN IGMA II"
2060 GOTO 300
3000 N=8
3010 N$(1)="INV ADER"
3020 N$(2)="PR OBABILIT Y"
3030 N$(3)="SE A HUNT"
3040 N$(4)="VE RIFY"
3050 N$(5)="LO OSE CHANGE"
3060 N$(6)="NI M II"
3070 N$(7)="NI M III"
3080 N$(8)="TOW ERS OF HANOI"
3090 GOTO 300
4000 N=6
184 APPENDIX

4010 N$(1)="BARTE R"


4020 N$(2)="BASE "
4030 N$(3)="YOUR NUMBER IS UP"
4040 N$(4)="CRYP TIC"
4050 N$(5)="HUMAN ARITHMETIC"
4060 N$(6)="JUGS "
4070 GOTO 300
5000 N=5
5010 N$(1)="DETONATION"
5020 N$(2)="0UEST "
5030 N$(3)="TREK "
5040 N$(4)="U-BOA T"
5050 N$(5)="WARP "
5060 GOTO 300
6000 N=6
6010 N$(1)="MONKEY PUZZLE"
6020 N$(2)="0BSCU RE AGES"
6030 N$(3)="MORE OBSCURE AGES"
6040 N$(4)="MUCH MORE OBSCURE AGES"
6050 N$(5)="PRODUCT AND SUMS"
6060 N$(6)="RALLY "
6070 GOTO 300
7000 N=2
7010 N$(1)="FIXED -POINT"
7020 N$(2)="BRACK ET"
7030 GOTO 300
8000 N=3
8010 N$(1)="DISK LOAD"
8020 N$(2)="LOADE R"
8030 N$(3)="MUSIC MAKER"
8040 GOTO 300
If you would like to add a little music to the program "LOADER", I
would suggest that you add the lines before 100 and after 9000 in the
following long version of "LOADER". You will also need to use lines 220
and 380 from this version.
The rest of this long version is included so that you never have to
press RETURN and type RUN for each program that is loaded. When you
RUN this long version, each program is loaded and run automatically
without stopping.
You will need to change "DISK LOAD". Replace lines 195 and 196
with
195 LOAD "LOADER",8
''LOADER '' 185

In order to run continuously, there is an essential trick which you


should know about. The program "DISK LOAD" is much shorter than the
program "LOADER". This means that when you run "DISK LOAD", it will
not be able to execute line 170 safely. The program "LOADER" will not be
completely loaded.
The trick is to fool the computer into believing that "DISK LOAD" is a
lot bigger than it really is! You do this by saving "DISK LOAD" in the
following way. You temporarily insert the line
1 LOAD "DISK LOAD", 8
in the program "LOADER" and then you run "LOADER". You should press
RUN/STOP and RETURN to halt the execution of "DISK LOAD", otherwise
you will lose it because it loads "LOADER" again. (You could prevent this
by changing line 170 in "DISK LOAD" into a REM statement temporarily.)
When you have the program "DISK LOAD" in the computer memory
by this means, it uses up the same storage as the much bigger program
"LOADER". If you now save this version on a disk, it will take up as much
disk memory as "LOADER" does. Delete the temporary changes and save
the final correct versions on the disk. Now you are all set for continuous
loading and running. However, all the game programs in this book must
be saved in the same fashion. First you load the programs using
"LOADER", then stop the execution by pressing the key RUN/STOP in
answer to a program prompt (when a GET statement is being executed).
Then you save this version of the game program. It will take as much
memory as "LOADER".
186 APPENDIX

''Loader' ' (With Music)


5 REM:SET UP ORGAN LIKE VOICES
10 FOR L=54272 TO 54296:POKE L,0:NEXT
15 POKE 54296,15
20 FOR 1=1 TO 3:V=54269+7*1:POK E V+1,190
:POKE V+2,250:NEXT I
100 PRINT "t"J"
110 Z=1:REM Z CONTROLS THE REPEAT DISPLA
Y IN CASE AN ENTRY IS NOT VALID
115 0=0:REM USED FOR STOP IN LINE 460
120 PRINT" THE CHAPTERS ARE:"
130 PRINT
140 PRINT" 1.CONCENTRATION GAMES"
150 PRINT " 2.WORD GAMES"
160 PRINT " 3.LOGIC AND STRATEGY GAMES"
170 PRINT " 4.ARITHMETIC GAMES"
180 PRINT " 5.GEOMETRY GAMES"
190 PRINT " 6.ALGEBRA GAMES"
195 PRINT " 7.ROOT FINDERS"
196 PRINT " 8.APPENDIX"
200 PRINT:PRINT "TO LIST GAMES PRESS AN
UMBER 1 TO 8"
210 PRINT:PRINT "PRESS S TO STOP"
220 GET A$:IF A$="" THEN 9000
230 IF A$="S" THEN 440
240 P=INT(VAL(A$)) :REM PIS THE VALUE OF
THE INPUT AND CONTROLS THE GOTO IN 200
250 IF P>0 AND P<9 THEN ON P GOTO 1000,2
000,3000,4000,500 0,6000,7000,8000
255 REM:CONTROL COMES HERE WHEN ENTRY IS
NOT VALID
260 PRINT "WRONG ENTRY TRY AGAIN"
270 FOR T=1 TO 1000:NEXT T:ON Z GOTO 100
,300:REM WAITS WHILE 260 IS VIEWED
300 PRINT "t"J"
310 Z=2
320 IF P<8 THEN PRINT "THE GAMES IN CHAP
TER ";A$;" ARE:":PRINT
325 IF P=8 THEN PRINT "THE APPENDIX PROG
RAMS ARE:":PRINT
330 FOR 1=1 TO N
"LOADER" (WITH MUSIC) 187

340 PRINT STR$(I);" .";N$(I);R EM THIS LIS


TS THE GAMES IN A CHAPTER
350 NEXT I
360 PRINT:PRI NT "TO LOAD PRESS A NUMBER
1 TO ";N
370 PRINT:PRI NT "PRESS S TO STOP, OR C T
0 VIEW CHAPTERS"
380 GET C$:IF C$="" THEN 9000
390 IF C$="S" THEN 440
400 IF C$="C" THEN 100
410 O=INT(VA L(C$))
420 IF 0>0 AND O<N+1 THEN 440
430 GOTO 260
440 PRINT "C":REM NORMALIZE SCREEN
445 POKE 53280,14:P OKE 53281,6:P RINT CHR
$ ( 154)
450 FOR I=1 TO 3:POKE 54269+7*I ,0NEXT:R
EM TURNS OFF MUSIC
460 IF 0=0 THEN STOP
480 PRINT "LOADING ";N$(0)
490 REM:LOADING INSTRUCTIONS ARE HERE
500 ON P GOTO 550,650,6 00,700,75 0,800,85
0,900
550 ON 0 GOTO 555, 560,565,5 70,575
555 LOAD "CONCENTRATION",8
560 LOAD "FLASH",8
565 LOAD "KNIGHT" ,8
570 LOAD "KNIGHTMARE",8
575 LOAD "SIMON",8
600 ON 0 GOTO 605,610,6 15,620,62 5,630,63
5,640
605 LOAD "INVADER ",8
610 LOAD "PROBABI LITY",8
615 LOAD "SEA HUNT",8
620 LOAD "VERIFY" ,8
625 LOAD "LOOSE CHANGE",8
630 LOAD "NIM II",8
635 LOAD "NIM III",8
640 LOAD "TOWERS OF HANOI",8
650 ON 0 GOTO 655,660,6 65,670,67 5,680
655 LOAD "ANAGRAMS I",8
660 LOAD "ANAGRAMS II",8
665 LOAD "HANGMAN",8
670 LOAD "FIND A WORD",8
188 APPENDIX

675 LOAD "ENIGMA 1",8


680 LOAD "ENIGMA 11",8
700 ON 0 GOTO 705,710,7 15,720,72 5,730
705 LOAD "BARTER" ,8
710 LOAD "BASE",8
715 LOAD "YOUR NUMBER IS UP",8
720 LOAD "CRYPTIC ",8
725 LOAD "HUMAN ARITHMET IC",8
730 LOAD "JUGS",8
750 ON 0 GOTO 755,760,7 65,770,77 5
755 LOAD "DETONAT ION",8
760 LOAD "QUEST",8
765 LOAD "TREK",8
770 LOAD "U-BOAT" ,8
775 LOAD "WARP",8
800 ON 0 GOTO 805,810,8 15,820,82 5,830
805 LOAD "MONKEY PUZZLE",8
810 LOAD "OBSCURE AGES",8
815 LOAD "MORE OBSCURE AGES",8
820 LOAD "MUCH MORE OBSCURE AGES",8
825 LOAD "PRODUCT AND SUMS",8
830 LOAD "RALLY",8
850 ON 0 GOTO 855,860
855 LOAD "FIXED-PO INT",8
860 LOAD "BRACKET ",8
900 ON 0 GOTO 905,910,9 15
905 LOAD "DISK LOAD",8
910 LOAD "LOADER" ,8
915 LOAD "MUSIC MAKER",8
999 REM:GAMES DATA IS LOADED INTO N$ BY
THESE SUBROUTINES
1000 N=6
1010 N$(1)="CONCENTRATION"
1020 N$(2)="FL ASH"
1030 N$(3)="KN IGHT"
1040 N$(4)="KNIGHTMARE"
1050 N$(5)="SIM ON"
1060 GOTO 300
2000 N=5
2010 N$(1)="ANAGRAMS I"
2020 N$(2)="ANAGRAMS II"
2030 N$(3)="HANGMAN"
2035 N$(4)="FI ND A WORD"
2040 N$(5)="EN IGMA I"
"LOADER" (WITH MUSIC) 189

2050 N$(6)="ENIGM A II"


2060 GOTO 300
3000 N=8
3010 N$(1)="INVAD ER"
3020 N$(2)="PROBA BILITY"
3030 N$(3)="SEA HUNT"
3040 N$(4)="VERIF Y"
3050 N$(5)="LOOSE CHANGE"
3060 N$(6)="NIM II"
3070 N$(7)="NIM III"
3080 N$(8)="TOWER S OF HANOI"
3090 GOTO 300
4000 N=6
4010 N$(1)="BARTE R"
4020 N$(2)="BASE "
4030 N$(3)="YOUR NUMBER IS UP"
4040 N$(4)="CRYP TIC"
4050 N$(5)="HUMAN ARITHMETIC"
4060 N$(6)="JUGS "
4070 GOTO 300
5000 N=5
5010 N$(1)="DETON ATION"
5020 N$(2)="0UEST "
5030 N$(3)="TREK "
5040 N$(4)="U-BOA T"
5050 N$(5)="WARP "
5060 GOTO 300
6000 N=6
6010 N$(1)="MONKEY PUZZLE"
6020 N$(2)="0BSCU RE AGES"
6030 NS(3)="MORE OBSCURE AGES"
6040 N$(4)="MUCH MORE OBSCURE AGES"
6050 N$(5)="PRODU CT AND SUMS"
6060 N$(6)="RALLY "
6070 GOTO 300
7000 N=2
7010 N$(1)="FIXED -POINT"
7020 NS(2)="BRACK ET"
7030 GOTO 300
8000 N=3
8010 N$(1)="DISK LOAD"
8020 NS(2)="LOADE R"
8030 N$(3)="MUSIC MAKER"
8040 GOTO 300
190 APPENDIX

9000 REM:THIS PLAYS THE MUSIC FROM DATA


9005 FOR I=1 TO 3:READ H(I) ,L(I) :NEXT I:
READ D
9010 IF D=-1 THEN RESTORE:GOTO 9000
9020 FOR I=1 TO 3:V=54266+7*I:POK E V,H(I
) :POKE V-1, L (I) :NEXT I
9030 FOR I=1 TO 3:POKE 54269+7*I,33:NEXT
9040 FOR T=1 TO D:NEXT
9050 ON Z GOTO 220,380
9900 DATA 17,37,0,0,0,0,75
9905 DATA 16,47,0,0,0,0,75
9910 DATA 17,37,0,0,0,0,200
9912 DATA 12,216,0,0,0,0,20 0
9915 DATA 13,156,0,0,0,0,20 0
9917 DATA 17,37,0,0,0,0,75
9920 DATA 16,47,0,0,0,0,75
9922 DATA 17,37,0,0,0,0,200
9925 DATA 19,63,0,0,0,0,200
9927 DATA 12,216,0,0,0,0,20 0
9930 DATA 17,37,0,0,0,0,75
9932 DATA 16,47,0,0,0,0,75
9935 DATA 17,37,0,0,0,0,200
9937 DATA 19,63,0,0,0,0,200
9940 DATA 11,114,0,0,0,0,75
9942 DATA 12,216,0,0,0,0,75
9945 DATA 13,156,0,0,0,0,70 0
9947 DATA 12,216,0,0,0,0,75
9950 DATA 11,114,0,0,0,0,75
9952 DATA 10,60,0,0,0,0,75
9955 DATA 17,37,0,0,0,0,75
9957 DATA 16,47,25,177,0,0, 75
9960 DATA 14,107,24,63,0,0, 75
9962 DATA 12,216,25,177,0,0 ,75
9965 DATA 11,114,25,117,0,0 ,75
9967 DATA 10,60,17,37,0,0,7 5
9970 DATA 9,159,17,37,0,0,7 5
9972 DATA 8,147,20,100,0,0, 200
9975 DATA 20,100,25,177,0,0 ,75
9977 DATA 20,100,24,63,0,0, 75
9980 DATA 19,63,25,177,0,0, 200
9981 DATA 17,37,28,214,0,0, 200
9982 DATA 15,70,19,63,0,0,2 00
9983 DATA 14,107,25,177,0,0 ,75
9984 DATA 14,107,24,63,0,0, 75
"LOADER" (WITH MUSIC) 191

9985 DATA 15,70,25,177,0,0,200


9986 DATA 17,37,28,214,0,0,200
9987 DATA 12,32,17,37,0,0,75
9988 DATA 12,32,19,63,0,0,75
9989 DATA 12,216,20,100,0,0,200
9990 DATA 14,107,20,100,0,0,200
9991 DATA 12,32,19,63,0,0,75
9992 DATA 12,32,17,37,0,0,75
9993 DATA 12,216,15,70,0,0,1000
9999 DATA -1,-1,-1,-1,-1,-1 ,-1
NOTE: There are two disadvantages with the automatic loading. The
first is that the programs take longer to load from the disk. The second is
the DIM statements have to be avoided. Otherwise, when you attempt to
reload a program with a DIM statement, automatic execution will cease,
and an error message will be given about attempting to redefine an array.
If you type RUN, the program will execute without any problems, but it is
annoying to have the automatic execution interrupted with an error
message.
The final program in this appendix is "MUSIC MAKER". When you
RUN this program, prompts are given for
WAVEFORM? (you enter 17 for pipes, 33 for or-
gan and 65 for piano)
ATTACK/DECAY? (you enter anthing from 0 to 255,
try 190)
SUSTAIN/RELEASE? (you enter anything from 0 to 255,
try 250)
The music will then play, with repeats until you press a key.
You may like to experiment with different sound characteristics. A
swell and diminution can be created with the respective inputs, 33 and
then 255 followed by 50.
192 APPENDIX

''Music Maker''
10 PRINT "O":RESTORE
9000 REM:INITIALIZE ALL SETTINGS TO ZERO
9010 FOR L=54272 TO 54296:POKE L,0:NEXT
9020 REM:SET VOLUME TO MAXIMUM
9030 POKE 54296,15
9035 PRINT:PRINT
9040 PRINT "THE WAVEFORM, ATTACK/DECAY A
NO SUSTAIN/RELEASE SETTINGS ARE USED "
9050 PRINT "TO SYNTHESIZE INSTRUMENTS"
9055 PRINT:PRINT:PRINT"INPUT -1 TO STOP"
9056 PRINT:PRINT
9060 INPUT "WAVEFORM";W:IF W=-1 THEN STO
p
9061 IF W=65 THEN FOR I=54274T054288 STE
P 7:POKEI,255:POKEI+1,0:NEXT
9065 INPUT "ATTACK/DECAY";A:IF A=-1 THEN
STOP
9066 INPUT "SUSTAIN/RELEASE";S:IF S=-1 T
HEN STOP
9070 REM:SET THESE NUMBERS INTO EACH VOl
CE LOCATION
9080 FOR I=1 TO 3:V=54269+7*I:POKE V+1,A
:POKE V+2,S:NEXT I
9100 REM:GET CREATES A LOOP TERMINATED B
Y PRESSING A KEY
9101 PRINT "l'J"
9102 PRINT "PRESS ANY KEY TO STOP NOTE A
NO RESTART, PRESS RUN/STOP TO STOP"
9103 RESTORE
9110 GET AS:IF A$="" THEN 9130
9120 GOTO 10
9130 FOR I=1 TO 3:READ H(I) ,L(I) :NEXT I
9140 READ D
9150 IF D=-1 THEN RESTORE:GOTO 9130
9160 REM:SET VOICES
9170 FOR I=1 TO 3:V=54266+7*I:POKE V,H(I
) :POKE V-1 , L (I) :NEXT I
9175 FOR I=1 TO 3:POKE 54269+7*I,W:NEXT
9180 REM:DURATION
9190 FOR T=1 TO D:NEXT
"MUSIC MAKER" 193

9195 IF W=65 THEN FOR I=1 TO 3:POKE 5426


9+7*I ,W-1 :NEXT
9200 GOTO 9110
9890 REM:
9895 REM:THE DATA FOR THE NOTES IS IN TH
E FORM H,L,H,L,H,L AND DURATION D
9900 DATA 17,37,0,0,0,0,75
9905 DATA 16,47,0,0,0,0,75
9910 DATA 17,37,0,0,0,0,200
9912 DATA 12,216,0,0,0,0,200
9915 DATA 13,156,0,0,0,0,200
9917 DATA 17,37,0,0,0,0,75
9920 DATA 16,47,0,0,0,0,75
9922 DATA 17,37,0,0,0,0,200
9925 DATA 19,63,0,0,0,0,200
9927 DATA 12,216,0,0,0,0,200
9930 DATA 17,37,0,0,0,0,75
9932 DATA 16,47,0,0,0,0,75
9935 DATA 17,37,0,0,0,0,200
9937 DATA 19,63,0,0,0,0,200
9940 DATA 11,114,0,0,0,0,75
9942 DATA 12,216,0,0,0,0,75
9945 DATA 13,156,0,0,0,0,700
9947 DATA 12,216,0,0,0,0,75
9950 DATA 11,114,0,0,0,0,75
9952 DATA 10,60,0,0,0,0,75
9955 DATA 17,37,0,0,0,0,75
9999 DATA -1,-1,-1,-1,-1,-1,-1
By changing the data statements in "MUSIC MAKER" you can create
your own music. The following are a few samples.

MESSIAH - HALLELU JAB CHORUS


9905 DATA 38,126,28,214,9,159,400
9910 DATA 28,214,28,214,12,32,100
9915 DATA 32,94,25,177,12,216,100
9920 DATA 28,214,24,63,9,159,100
9925 DATA 0,0,0,0,0,0,200
9930 DATA 38,126,28,214,9,159,400
9935 DATA 28,214,28,214,12,32,100
9940 DATA 32,94,25,177,12,216,100
9945 DATA 28,214,24,63,9,159,100
9950 DATA 0,0,0,0,0,0,200
9955 DATA 38,126,28,214,12,32,25
194 APPENDIX

9956 DATA 0,0,0,0,0,0,1


9960 DATA 38,126,28,214,12,32,25
9965 DATA 38,126,32,94,12,216,100
9970 DATA 38,126,28,214,9,159,100
9975 DATA 0,0,0,0,0,0,200
9980 DATA 38,126,28,214,12,32,25
9981 DATA 0,0,0,0,0,0,1
9982 DATA 38,126,28,214,12,32,25
9983 DATA 38,126,32,94,12,216,100
9984 DATA 38,126,28,214,9,159,100
9985 DATA 0,0,0,0,0,0,200
9986 DATA 38,126,28,214,12,32,100
9987 DATA 36,85,25,177,10,205,100
9988 DATA 38,126,24,63,9,159,100
9989 DATA 38,126,21 '154, 14,107' 100
9990 DATA 36,85,21,154,14,107,100
9991 DATA 38,126,24,63,159,400
9995 DATA 0,0,0,0,0,0,400
9999 DATA -1,-1,-1,-1 ,-1 ,-1 ,-1

THE PLANETS (YEAR 2tttt I)


9900 DATA 1 1 ' 11 4 '0' 0' 5 ' 1 8 5 ' 1 600
9910 DATA 1 7 '3 7 ' 1 1 ' 11 4 ' 8 ' 1 4 7 ' 1 600
9920 DATA 2 2 ' 2 2 7 ' 1 7 '3 7 ' 11 ' 1 1 4 ' 1 600
9930 DATA 28,214,17,37,14,107,100
9940 DATA 27,56,22,227,11,114,2800
9950 DATA 11' 114,0,0,5, 185,1600
9960 DATA 17' 37' 11 ' 114 '8' 14 7' 1600
9970 DATA 2 2' 2 2 7 ' 1 7 '3 7 ' 11 ' 1 1 4 ' 1 600
9980 DATA 28,214,17,37' 14,107' 100
9990 DATA 30,141,19,63,15,70,2800
9999 DATA -1,-1,-1,-1 ,-1 ,-1 ,-1

BATTLE HYMN OF THE REPUBLIC


9900 DATA 19,63,19,63,19,63,100
9901 DATA 19,63,12,216,6,108,300
9902 DATA 19,63,19,63,19,63,100
9903 DATA 19,63,12,216,6,108,300
9904 DATA 17,37,17,37,17,37,100
9905 DATA 16,47,12,216,6,108,300
9906 DATA 19,63,19,63,19,63,100
9907 DATA 25,177,12,216,6,108,300
9908 DATA 28,214,28,214,28,214,100
"MUSIC MAKER" 195

9909 DATA 32,94,16,47,6,108,300


9910 DATA 32,94,32,94,32,94,100
9911 DATA 32,94,16,47,6,108,300
9912 DATA 28,214,28,214,28,214,100
9913 DATA 25,177,16,47,6,108,600
9914 DATA 25,177,16,47,6,108,300
9915 DATA 24,63,24,63,24,63,100
9916 DATA 21,154,12,216,4,73,300
9917 DATA 21,154,21,154,21,154,100
9918 DATA 21,154,12,216,4,73,300
9919 DATA 24,63,24,63,24,63,100
9920 DATA 25,177,12,216,4,73,300
9921 DATA 24,63,24,63,24,63,100
9922 DATA 25,177,12,216,4,73,300
9923 DATA 21,154,21,154,21,154,100
9924 DATA 19,63,12,216,6,108,300
9925 DATA 21,154,21,154,21,154,100
9926 DATA 19,63,12,216,6,108,300
9927 DATA 16,47,16,47,16,47,100
9928 DATA 19,63,12,216,6,108,400
9929 DATA 19,63,12,216,6,108,300
9930 DATA 19,63,19,63,19,63,100
9931 DATA 19,63,12,216,6,108,300
9932 DATA 19,63,19,63,19,63,100
9933 DATA 19,63,12,216,6,108,300
9934 DATA 17,37,17,37,17,37,100
9935 DATA 16,47,12,216,6,108,300
9936 DATA 19,63,19,63,19,63,100
9937 DATA 25,177,12,216,4,73,300
9938 DATA 28,214,28,214,28,214,100
9939 DATA 32,94,16,47,6,108,300
9940 DATA 32,94,32,94,32,94,100
9941 DATA 32,94,16,47,6,108,300
9942 DATA 28,214,28,214,28,214,100
9943 DATA 25,177,16,47,6,108,600
9944 DATA 25,177,19,63,16,47,800
9945 DATA 28,214,21,154,17,37,800
9946 DATA 28,214,21,154,17,37,800
9947 DATA 25,177,19,63,16,47,800
9948 DATA 24,63,19,63,17,37,800
9949 DATA 25,177,19,63,16,47,3200
9960 DATA 19,63,16,47,6,108,700
9961 DATA 17,37,14,107,6,108,100
9962 DATA 16,47,12,216,6,108,300
196 APPENDIX

9963 DATA 19,63,16,47,6,108,100


9964 DATA 25,177,16,47,6,108,300
9965 DATA 28,214,17,37,6,108,100
9966 DATA 32,94,19,63,6,206,400
9967 DATA 32,94,19,63,9,159,400
9968 DATA 25,177,16,47,12,216,300
9969 DATA 25,177,16,47,12,32,100
9970 DATA 10,205,0,0,0,0,300
9971 DATA 9,159,0,0,0,0,100
9972 DATA 21,154,17,37,12,216,700
9973 DATA 24,63,19,63,12,216,100
9974 DATA 25,177,21,154,12,216,300
9975 DATA 24,64,19,63,12,216,100
9976 DATA 25,177,21,154,12,216,300
9977 DATA 21,154,17,37,12,216,100
9978 DATA 19,63,16,47,12,216,400
9979 DATA 19,63,16,47,9,159,400
9980 DATA 16,47,12,216,9,159,300
9981 DATA 16,47,12,216,8,147,100
9982 DATA 8,23,0,0,0,0,300
9983 DATA 7,53,0,0,0,0,100
9984 DATA 19,63,16,47,9,159,700
9985 DATA 17,37,14,107,9,159,100
9986 DATA 16,47,12,216,9,159,300
9987 DATA 19,63,16,47,9,159,100
9988 DATA 25,177,16,47,9,159,300
9989 DATA 28,214,17,37,9,159,100
9990 DATA 32,94,19,63,12,216,400
9991 DATA 32,94,20,100,12,32,400
9992 DATA 25,177,21,154,10,205,400
9993 DATA 25,177,21,154,16,47,400
9994 DATA 28,214,21,154,17,37,400
9995 DATA 28,214,21,154,17,37,400
9996 DATA 25,177,19,63,16,47,400
9997 DATA 24,63,19,63,17,37,400
9998 DATA 25,177,19,63,16,47,3200
9999 DATA -1,-1,-1,-1,-1,-1,-1
Index
1-norm, xii, 129, 135 Bisection, 1 7 4, 1 7 6, 1 7 7
2-norm, 124, 140 block structure, 91
BRACKET, viii, xiii, 165, 1 71,
A 174, 176, 184, 188, 189

ABS, 12, 15, 18, 122, 127, 129, c


132, 143, 170, 171, 172,
174 Calculus, 106, 125
absolute value, 135, 172 Cartesian coordinate, xii, 140
addition, 90, 110 CHR$, 7, 11, 12, 15, 19,21,45,
Algebra, viii, xii, 126, 151, 154, 50, 53, 54, 74, 75, 77, 78,
163 101, 108, 109, 181, 183
algebraic, 114, 164 COMPLEX NUMBERS, xii, 114,
ANAGRAMS, vii, xi, xii, xiii, 28, 115
31,33,37,41, 183,187,188 complex, 114, 1 7 7
Analytic Geometry, xi, xii, 125, CONCENTRATION, vii, ix, xii, 2, 5,
126, 146 13, 24, 183, 187, 188
arithmetic, vii, xii, 97, 99, 104, coordinate, xii, 5, 15, 17, 122,
119, 126, 151, 160, 163, 124, 125, 127, 129, 131,
167 132, 135, 136, 138, 139,
ASC, 7, 11, 12, 15, 16, 19, 20, 140, 141, 143, 144, 146
21, 22, 45, 50, 53, 74, 75, CRYPTIC, viii, xii, 108, 110, 184,
78, 101, 105, 108, 109 188, 189
ATN, 138, 142 cubic equation, 164, 176
cylindrical polar coordinate, xii,
B 140

Babalonians, 102 D
back-substitution, 13 7, 161
BARTER, vii, xii, 98, 99, 184, DATA,23,30,35,36, 123,133,
188, 189 134, 145, 190, 191, 193,
BASE, vii, xii, xiii, 101, 102, 103, 194, 195, 196
104, 105, 106, 107, 184, DEF FNF, 173
188, 189 DEF FNG, 170
basic polynomials, 32 DETONATION, viii, xii, xiii, 122,
binary, 90, 102, 103, 104 124, 129, 140, 141, 184,
binomial coefficients, 65 188, 189
binomial expansion, xii, 64 differentiate, 106
Binomial, 64 DIM, 25, 173, 191
197
198 INDEX

DISK LOAD, 25, 179, 180, 181, GOSUB,2,3,21,22,29,30,35,


184, 185, 188, 190, 197 66, 85, 87, 101, 105, 123,
133, 138, 139, 144, 181
E
H
ENIGMA, vii, xi, xiii, 50, 51, 53,
55, 183, 188, 189 HANGMAN, vii, xiii, 38, 41, 183,
equation, 110, 125, 126, 131, 184, 188, 189
136, 137, 157, 159, 164, HUMAN ARITHMETIC, viii, xii,
168, 171 112, 114, 184, 188, 189
Euclidean distance, xii, 124, 129,
140, 141 I
even, 88, 91, 95, 160
imaginary numbers, 114
F INT, 2, 3, 7, 11, 15, 28, 29, 33,
34, 38, 39, 43, 44, 45, 50,
factor, 107, 163, 164, 165 53, 58, 63, 64, 67, 70, 74,
factoring, 10 7 79, 81, 85, 86, 87, 98, 101,
FIND A WORD, vii, xiii, 43, 47, 105, 108, 112, 117, 122,
183, 188, 189 124, 127, 132, 138, 141,
FIXED-POINT, viii, xiii, 165, 170, 143, 148, 153, 156, 158,
171, 172, 173, 176, 177, 162, 166, 170, 181, 182,
184, 188, 189 183, 186, 187
FLASH, vii, xii, 7, 9, 24, 183, interpolating polynomial, 32
187, 188 interpolation, 31
FNF, 174 INVADER, vii, xiii, 58, 60, 69,
FNG, 170 183, 187, 189

G J

Gauss Elimination, xii, 136, 13 7, JUGS, viii, xii, 11 7, 119, 184,


161 188, 189
GET, 2, 4, 7, 8, 11, 12, 16, 19,
20, 21' 22, 28, 30, 33, 35, K
38, 39, 40, 46, 50, 53, 54,
58, 59, 63, 66, 67, 70, 74, KNIGHT, vii, xiii, 11, 13, 15, 1 7,
75, 79, 81, 82, 85, 93, 98, 183, 187, 188
101, 105, 108, 109, 113, KNIGHTMARE, vii, xiii, 15, 1 7,
117, 118, 122, 123, 127, 183, 187, 188
128, 132, 133, 138, 139, Kronecker, 114
143, 144, 148, 153, 156,
158, 162, 166, 170, 175, L
182, 183, 185, 186, 187,
192 Lagrange Interpolation, xi, xii, 32
INDEX 199

LEFT$, 3, 8, 12, 15, 19, 21, 22, 0


29, 34, 35, 39, 53, 58, 67,
86, 92, 105, 109, 112 OBSCURE AGES, viii, xii, 153,
154, 157, 160, 163, 184,
LEN, 40, 45, 70, 86, 112
188, 189
linear equation, xi, xii, 135, 136,
odd, 18, 95, 120
146, 155, 157, 160, 167, ON, 28, 33, 38, 43, 45, 112,
171, 173 181, 182, 186, 187, 188,
linear systems, 160 190
LOADER, 25, 179, 181, 182, ordered pair, 114
184, 185, 188, 190
LOG, 170, 174 p
LOOSE CHANGE, vii, xii, 72, 74,
Pascal's triangle, xii, 65
76, 183, 187, 189
polar coordinates, xii, 141
POKE, 21, 22, 23, 25, 28, 30,
M 33, 35, 54, 123, 133, 139,
144, 145, 181, 183, 186,
matrix, 25, 136, 160, 173 187, 190, 192, 193
matrix algebra, 160 polynomial, 31, 106, 164, 171,
MID$,3, 15, 19,20,21, 22,29, 176
probabilities, xi, xiii, 64, 65
34, 39, 44, 45, 53, 54, 58,
PROBABILITY, vii, xiii, 61, 63, 64,
66, 70, 74, 86, 87, 92, 98,
65, 183, 187, 189
101, 108, 112 PRODUCT AND SUMS, viii, xii, xiii,
MONKEY PUZZLE, viii, xii, xiii, 162, 163, 165, 171, 172,
148, 150, 184, 188, 189 184, 188, 189
MORE OBSCURE AGES, viii, xii,
155, 156, 157, 184, 188, Q
189
MUCH MORE OBSCURE AGES, quadratic, xi, xiii, 173
viii, xii, 157, 158, 159, 184, QUEST, viii, xii, 127, 129, 135,
184, 188, 189
188, 189
quotient, 107
multiplicative inverses, 115
R
N
RALLY, viii, xii, 166, 16 7, 184,
188, 189
nested multiplication, 107 READ,22,25,30,35, 123,133,
NIM, vii, xi, xiii, 79, 81, 83, 84, 144, 190, 192
85, 89, 183, 187, 189 real roots, 176, 177
nonlinear equations, xi, xiii, 163 Regular FALSI, 174, 177
norm, xii, 124, 129, 135, 173 remainder, 103, 104, 106, 107,
152
200 INDEX

repetitive multiplication, 106 STOP, 4, 8, 12, 16, 19, 22, 25,


repetitive division, 103 30, 35, 40, 46, 50, 54, 59,
RETURN,3, 7,8, 11, 15, 23, 29, 63, 67, 71, 75, 80, 82, 85,
33, 35, 38, 43, 58, 59, 66, 93, 98, 101, 105, 109, 113,
67, 68, 81, 86, 87, 88, 101, 118, 122, 123, 125, 128,
105, 117, 122, 123, 127, 133, 139, 144, 148, 153,
132, 133, 138, 139, 143, 156, 158, 162, 166, 170,
145, 148, 181, 183, 184, 175, 179, 181, 182, 183,
197 186, 187, 192
RESTORE,22,30,35, 123,133, stopping rule, 172
144, 180, 190, 192 STR$, 3, 7, 19, 21, 25, 53, 58,
59, 66, 67, 86, 101, 105,
RIGHT$, 3, 7, 12, 15, 19, 21,
108, 112, 117, 118, 122,
25, 29, 34, 39, 53, 58, 67,
126, 127, 133, 143, 148,
86, 92, 105, 109, 112
182, 187
RND, 2, 7, 11, 15, 19, 21, 28,
string, 7, 25, 28, 33, 70, 72, 92,
29, 33, 34, 38, 39, 43, 44,
94, 98, 102, 115
45, 50, 53, 58, 63, 64, 66, subroutine, 6, 91, 103
70, 74, 79, 81, 85, 87, 98, subtraction, 90, 119, 125
105, 108, 112, 117, 119, synthetic division, 106, 164
122, 127, 132, 133, 138,
143, 148, 153, 156, 158, T
162, 166, 181
root, xiii, 126, 164, 165, 170, TAB, 11, 15,29,34,35,39,45,
171, 172, 174 46, 58, 59, 66, 67, 79, 81,
92, 170, 174, 181
s three-dimensional, 17, 135
TOWERS OF HANOI, vii, xiii, 92,
94, 183, 187, 189
SEA HUNT, vii, xiii, 66, 69, 183,
TREK, viii, xii, 132, 135, 146,
187, 189
184, 188, 189
second degree, 12 5 two-dimensional, 17, 18
sequence, 9, 10, 24, 25, 172
SGN, 138, 174 u
significant figures, 170, 172,
174, 177 U-BOAT, viii, xii, 138, 140, 184,
SIMON, vii, xi, xii, 19, 21, 24, 188, 189
179, 183, 187, 188
SQR, 112, 114, 122, 124, 139, v
141
SQR(-1), 114 VAL, 2, 11, 15, 19, 20, 22, 53,
statistics, xii, xiii, 64 70, 85, 86, 87, 92, 98, 101,
STEP, 21, 22, 23, 46, 54, 92, 105, 108, 109, 112, 182,
192 183, 186, 187
INDEX 201

vectors, 1 7 3 y
VERIFY, vii, xii, 70, 72, 183, 187,
189 YOUR NUMBER IS UP, viii, xii, xiii,
104, 105, 106, 164, 184,
188, 189
w
z
WARP, viii, xii, 143, 146, 184,
zero, xiii, 32, 140, 164, 171,
188, 189
173, 176, 177
------------------------------
203

Instructions For
Making A Back-Up
Copy Of Your Brain
Games Diskette
1. Insert a blank disk into the disk drive. Be sure to use a disk without
anything on it that you wish to keep.
2. Type:
OPEN15,8,15
then press RETURN. Type:
PRINT#15,"N:BRAIN-BACKUP,64"
then press RETURN. This operation will blank and reformat your disk
and will take a few minutes.
3. Type:
CLOSE15
Press RETURN. Type:
LOAD"$" ,8
Press RETURN. Type:
LIST
and press RETURN. At this point you should have on your screen:
0 "BRAIN-BACKUP "64 2A
664 BLOCKS FREE
4. Now you are ready to copy the programs from the BRAIN GAMES
disk to your back-up disk. This will involve LOADing each of 41
programs from the BRAIN GAMES disk into the computer and then
SAVEing each on your disk.
Follow these steps:
a. Remove your back-up disk.
b. Insert the BRAIN GAMES disk.
c. Type: LOAD"CONCENTRATION",8 (Press RETURN)
d. Remove the BRAIN GAMES disk.
e. Insert your back-up disk.
f. Type: SAVE"CONCENTRATION",8 (Press RETURN)
204

Follow the above steps for each of the programs listed on the next
page. Be sure to type in the names exactly, including periods and
spaces. For your convenience, a checklist is provided.

If you find you have misspelled a program name on your back-up disk,
you can correct the program spelling by the following procedure:
1. Insert your back-up disk.
2. Type:
OPEN15,8,15
Press RETURN.
3. Type:
PRINT#15,"R:N EWNAME=OLD NAME"
Press RETURN. NOTE: In place of NEWNAME you should type the
correctly spelled program name, and in place of OLDNAME you type
the misspelled program name.
4. Type:
CLOSE15
It is recommended that you use your back-up disk and put the original
BRAIN GAMES disk in a safe place.
205

STEPS
PROGRAM a b c d e

"CONCENTRATION"
"FLASH"
"KNIGHT"
"KNIGHTMARE"
"SIMON"
"ANAGRAMS I"
"ANAGRAMS II"
"HANGMAN"
"FIND A WORD"
"ENIGMA I"
"ENIGMA II"
"INVADER"
"PROBABILITY"
"SEA HUNT"
"VERIFY"
"LOOSE CHANGE"
"NIM II"
"NIM Ill"
"TOWERS OF HANOI"
"BARTER"
"BASE"
"YOUR NUMBER IS UP"
"CRYPTIC"
"HUMAN ARITHMETIC"
"JUGS"
"DETONATION"
"QUEST"
"TREK"
"U-BOAT"
"WARP"
"MONKEY PUZZLE"
"OBSCURE AGES"
"MORE OBSCURE AGES"
"MUCH MORE OBSCURE AGES"
"PRODUCT AND SUMS"
"RALLY"
"FIXED-POINT"
"BRACKET"
"DISK LOAD"
"LOADER"
"MUSIC MAKER"
207

Instr uctio ns for


Oper ating the
Prer ecor ded Flop py
Disk
To load and execute the programs on the disk, type in
LOAD "DISK LOAD",8
then press RETURN. The program loads in less than 10 seconds. To
execute the program, type RUN and press RETURN.
Sit back and relax, there is nothing more to do but to make your
choices from the menu of chapters and programs that are displayed on
your screen.
--------------------------------------------------------~

You might also like