REM, DIM, READ, AddingStrings, OFF, RUN, INKEY as
delay, colons and semis -Tutorial 005
Type or select/copy/paste the following (blue) program into the BB4W editing
window, then run it.
REM Cyber Poet
REM by Richard Weston
REM 12th Feb 2003
MODE8
OFF
DIM word$(22)
PRINT'"Look...."''
:
FOR w=1 TO 22
IF w=12 THEN PRINT
READ word$(w)
PRINTword$(w) + " ";
delay=INKEY(40)
NEXT w
PRINT''
COLOUR 3
delay=INKEY(150)
PRINT'"Yer......"'
:
FOR p=1 TO 5
COLOUR RND(15)
FOR n=1 TO 12
PRINT" " + word$(RND(21));
rand=RND(5)
IF rand=1 THEN PRINT
",";
delay=INKEY(20+RND(40))
NEXT n
PRINT'
NEXT p
:
delay=INKEY(200)
RUN
:
DATA Mary,had,a,little,lamb,its,fleece,was,white,as,snow
DATA and,everywhere,that,Mary,went,the,lamb,was,sure,to,go
Here is an annotated (/*) version with (unusually for me) line numbers added
to aid reference. Generally line numbers are a distraction and unnecessary.
Don't try to run this - it will crash (Not that it will do any harm; if you
really want to try, do)
10 REM Cyber Poet /*REM
allows you to add remarks or comments to a program
20 REM by Richard Weston
30 REM 12th Feb 2003
40 MODE8
50 OFF /* Turns off the flashing cursor
60 DIM word$(22) /* DIM stands for dimension. Here it creates
23 string variable locations - word$(0), word$(1).. to word$(22) - in which
23 "strings" can be stored. Here we shall not use word$(0).
70 PRINT'"Look...."''
80 : /* a colon does nothing here,
just acts as a spacer to make the program more readable.
90 FOR w=1 TO 22 /* I've used w a s the initial letter
for "word" since that's what it is counting
100 IF w=12 THEN PRINT /* This starts anew line
at the right point in the poem
110 READ word$(w) /* The READ command causes the
computer to go to the DATA (see line number 330) and the words between the
commas are stored in the memory locations word$(w), where w goes from 1 to
22 the number of words in the poem.
120 PRINTword$(w) + " "; /* The + " " adds a space
to the end of the word. The semicolon (;) causes the following to be printed
next, without starting a new line, which would happen if it were omitted.
(You can always edit the program then run it to see these effects - don't
just believe me!)
130 delay=INKEY(40) /* The function INKEY waits
here for 40 centiseconds to give you chance to read each word as it
appears, otherwise everything happens too quickly.
140 NEXT w /* end of the loop starting at line 90
150 PRINT''
160 COLOUR 3
170 delay=INKEY(150) : PRINT'"Yer......"'
180 :
190 FOR p=1 TO 5 /* I used p here to stand for paragraph.
We thus get five pararaphs from the cyber poet
200 COLOUR RND(15)
210 FOR n=1 TO 12 /* twelve words per line
220 PRINT" " + word$(RND(21)); /* the cyber
poet just chooses and prints words from its lexicon randomly, prededed by
a space
230 rand=RND(5)
240 IF rand=1 THEN PRINT ",";
/* throw in a few commas at random (one in five chance seems about right!)
250 delay=INKEY(20+RND(40)) /* a bit
of randomness here makes the poet seemas if it might be fishing for just the
right word
260 NEXT n /* end of nested loop started at line
210
270 PRINT'
280 NEXT p /* end of loop started at line 190
290 :
300 delay=INKEY(200)
310 RUN /* after a short delay the program starts all over
again, but will never(?) repeat itself exactly.
320 :
330 DATA Mary,had,a,little,lamb,its,fleece,was,white,as,snow
340 DATA and,everywhere,that,Mary,went,the,lamb,was,sure,to,go
/* DATA statements are generally put at the end of a program but they do not
have to do so. The commas act as spacers between the items of data.
Exercise: Write a similar jargon generator
perhaps using a smaller set of words in the DATA section
Next Tutorial
Richard Weston's Homepage