Programming by Example

A BB4W Compendium

Start Here

This page is devoted to programs covering fundamental concepts within the UK National Curriculum for Computing KS1-4 (2014). The first 7 programs are applications, available compiled and accompanied by a descriptive pdf.

For an introduction to BASIC code, see the Simple Solar System Sim and Wordsearch programs below.

A GUI based application presenting 'low level' programming challenges. Students must use a low level instruction set to create programs to produce simple graphical patterns (pre-set exercises).

Useful for introducing 'low level' programming concepts and for developing problem solving/decomposing skills. (Note: This application was devised, in part, because a low level instruction set lent itself to controlled exercises.)

How to Build a Simple Solar System Simulator... a programming demonstration

The text below can be copied and pasted into the BB4W editor (use this on copying to remove blank lines.)

This first section of code demonstrates all of the BASIC commands used in the Simple Solar System Simulation. Read the REM comments in combination with running the code to gain an understanding of BASIC fundamentals.

REM This program demonstrates all of the components used in

REM the Simple Solar System Simulation

REM 'REM' statements are used for the programmers' comments

REM 'FOR NEXT' loops allow portions of code to be repeated

PRINT "Read the program comments to learn about loops"

FOR a=0 TO 4

PRINT a

NEXT

REM Colons ':' allow multiple statements on a single line

FOR a=0 TO 4:PRINT a:NEXT

REM 'REPEAT UNTIL' is another type of loop

REM The type of looping command depends on the problem

REM This loop ends when the variable a is greater than 4

a=0

REPEAT

PRINT;a

a=a+1

UNTIL a>4

REM a+=1 is the same as a=a+1

PRINT:REM Printing nothing moves the text cursor down one line

B=1

PRINT "B is a numeric variable, currently equal to ";B

B$="Strings are very useful"

PRINT "B$ is a string variable. ";B$

PRINT:PRINT "Press any key to continue, but note the flashing text cursor first"

a$=GET$:REM Waits for the user to press a key

REM CLS clears the 'screen'

CLS

REM *FONT allows us to choose a font for printing text

*FONT Arial,16

PRINTTAB(10,10);"PRINTTAB(x,y) positions text based on a fixed character width and height"

COLOUR 1:REM Text colour = red

PRINTTAB(0,0);"The text 'origin' is at the top left of the window"

REM With a little effort we can position text anywhere

REM The graphics origin is usually at the bottom left

VDU 5:REM Ancient BBC BASIC command to draw text at graphics cursor

FOR a=0 TO 16 STEP 4

MOVE 100+a,100+a:REM Move the graphics cursor

PRINT "HELLO"

NEXT

VDU 4:REM Print text at the text cursor

COLOUR 0

PRINT "We're back here again, after printing HELLO below"

OFF:REM Turn the text cursor off

PRINTTAB(10) "and the flashing text cursor has gone"

PRINT '"Press any key to continue"

a=GET:REM Similar to a$=get$ only a equals the ASCII value of the character

CLS

PRINT "Time for some maths..."'

PRINT "Multiplication: 2 times 7 = ";2*7

PRINT "Division: 7 divided by 2 = ";7/2

PRINT "Whole number division: 7 divided by 2 = ";7 DIV 2

PRINT " with a remainder of ";7 MOD 2

PRINT ''"SIN & COS are used in triganometry and for drawing circles"

ORIGIN 0,200:REM Move the graphics origin

FOR angle=0 TO 360

r=RAD(angle):REM Convert degrees into radians to keep the computer happy

GCOL 0,1:PLOT angle*2,SIN(r)*100

GCOL 0,2:PLOT angle*2,COS(r)*100

WAIT 1:REM Pause for one hundreth of a second

NEXT

PRINT '"Press any key to continue":REPEATUNTILGET

CLS

PRINT "An array can be a list or a table (like a spreadsheet)"

REM To reserve space for an array we must 'Dimension' it

REM This one has 3+1=4 columns and 2+1=3 rows

DIM table(3,2)

FOR c=0 TO 3

FOR r=0 TO 2

REM Fill the array with random numbers between 1 and 100

table(c,r)=RND(100)

PRINTTAB(c*4,r*2+2);table(c,r)

NEXT

NEXT

PRINT '"Arrays hold data in a way that is easy to access"

PRINT "'DATA and READ' allow us to fill an array with initial values"'

DIM animal$(4):REM the '$' indicates this is a 'string' array (or variable)

FOR a=0 TO 4:READ animal$(a):NEXT

FOR x=0 TO 4:PRINT animal$(x):NEXT

REM DATA statements are usually found after the END of a program

PRINT '"Press any key to continue"

x=POS:y=VPOS:REM Obtain the current position of the text cursor

REM TIME is a counter updated by BB4W and increments 100 times a second

TIME=0:REPEAT:PRINTTAB(x,y);"Time=";TIME:UNTIL INKEY$(0)<>""

REM BB4W is based on the original BBC BASIC that had specific display modes

MODE 9:OFF

*FONT Arial,16

PRINT "Mode 9 changes the window to 1280 x 1024 graphical units"

PRINT "Note that the background is black and the text is now white"

PRINT '"Right click to finish"

fin=FALSE:REM TRUE and FALSE are boolean values

REPEAT

MOUSE mx,my,mb

PRINTTAB(0,5);"Mouse position : ";mx;",";my;" "

PRINTTAB(0,6);"Button status : ";mb

REM IF statements execute sections of code depending on the conditions

IF mb=1 THEN fin=TRUE

WAIT 1

UNTIL fin

PRINT '"Now you know everything to create a solar system!"

END

DATA Lion,Penguin,Ant,Slug,Aardvark

REM We haven't covered mouse interrupts though!

REM ON MOUSE instructs the program to execute the code that follows whenever

REM a mouse button is clicked (and only then).

REM When 'RETURN' is encountered the program returns to whatever it was

REM doing before it was interrupted.

REM Simple Solar System Sim

MODE 9:OFF:ORIGIN 640,512

*FONT Arial,12,b

DIM slr(8,2),p$(8)

FOR a=0 TO 8:READ p$(a),slr(a,0),slr(a,1),slr(a,2):NEXT

p=3:MAXr=400:day=0

ON MOUSE:p+=1:p=p MOD 9:RETURN

REPEAT

TIME=0:CLS:PRINTTAB(0,0);"Click mouse to move outwards"

day+=slr(p,1) DIV 720

PRINTTAB(0,2);"Day: ";day DIV 10

GCOL 0,3:CIRCLEFILL 0,0,8

FOR a=0 TO p

r=slr(a,0)*MAXr/slr(p,0)

angle=360*(day MOD slr(a,1))/slr(a,1)

x=SIN(RAD(angle))*r:y=COS(RAD(angle))*r

GCOL 0,slr(a,2):PLOT x,y

IF a>p-4 VDU 5:MOVE x+8,y-8:PRINT p$(a):VDU 4

NEXT

WAIT 4-TIME

UNTIL0

END

REM Mean dist from sun in millions of km, Period of revolution in tenths of days

DATA Mercury,57.9,879,1,Venus,108.2,2247,7,Earth,149.6,3653,2

DATA Mars,227.9,6870,1,Jupiter,778.3,43325,7,Saturn,1427,107676,3

DATA Uranus,2871,306850,6,Neptune,4497,602745,6,Pluto,5913,905944,7

The Simulator...

Wordsearch Creator & Solver

The purpose of this demonstration is to provide examples of the following in a single exercise:

File handling: read and write text files one character at a time using BGET and BPUT

FOR, NEXT & REPEAT, UNTIL loops

IF THEN ELSE, including nested IF statements

String functions, including the use of a string array

Calling a PROCedure

Programming needs to be more than theory if students are to exercise problem-solving and design skills. One method of introducing practical programming is to provide examples that contain visual links between the output of the program and the code; so that students and teachers are able to associate the results with the program logic.

(While non-text based languages excel in this area, there is greater scope for student development using text based languages, and a reduced overhead of required knowledge and effort when using a non-object-oriented approach.)

Program A: Wordsearch Creator

The first program creates a wordsearch puzzle grid, of 16 x 16 letters, using a standard text file containing up to 20 words of 16 characters or less. (Note: The program expects to find the 'words.txt' file in the same folder as the saved program.)

The program loops, reading one word at a time from the file, placing it randomly within the grid in a random direction. Words may overlap if they share a common letter. The code ensures that words do not overlap the edge of the grid. When all of the words have been positioned then random letters fill the remaining gaps. The completed grid is saved as a standard text file named 'wordgrid.txt'.

A word may appear in one of eight directions: horizontally, vertically or diagonally, including in reverse.

The program displays each word as it attempts to locate a valid position. The animation increases speed as more attempts are required.

Program B: Wordsearch Solver

The second program reads the original list of words and the wordgrid.txt file created by the first program. The code searches by row and column for the first letter in the current word. When located, it then attempts to match the entire word to the letters of the grid, one direction at a time.

Although not the most efficient search algorithm, the resulting animation neatly demonstrates the logic applied. Located words are highlighted for a couple of seconds before the next word is read.

Both programs share similar lines of code, including the procedure that displays the entire grid.

REM Wordsearch Creator

MODE 9:OFF

REM Use a standard fixed-width font

*FONT Courier New,12

DIM grid$(15,15):REM Dimension a 16 x 16 element array

grid$()="-":REM Fill the array with "-" characters

wcount=0:REM A count of how many words we've read from the file

PROCshowgrid:REM Show the empty word grid

c=OPENIN("words.txt"):REM Open the text file to read the list of words

IF c=0 PRINT"File not found":END

REM Loop to read the file contents

REPEAT

REM Read the file one character at a time

w$="":REM w$ will hold the next word in the list

REPEAT

b=BGET#c:REM Read a single character (byte) from the file

IF b>=65 THEN w$=w$+CHR$(b)

REM Each line in a text file (except the last) ends with values 13 and 10

UNTIL EOF#c OR b=10

REM Exit the program if we encounter a word longer than 16 characters

IF LEN(w$)>16 THEN PRINT"Word too long: ";w$:CLOSE#c:END

wcount=wcount+1

REM Try to place the current word on the grid

attempt=0

REPEAT

PROCshowgrid:REM Redraw the grid

PRINTTAB(0,wcount);w$:REM Display the current word

placed=TRUE

x=RND(16)-1:y=RND(16)-1:REM A random start position

xstore=x:ystore=y:REM Keep a copy of x & y

d=RND(8):REM A random direction

CASE d OF

WHEN 1 : xd=0:yd=-1

WHEN 2 : xd=1:yd=-1

WHEN 3 : xd=1:yd=0

WHEN 4 : xd=1:yd=1

WHEN 5 : xd=0:yd=1

WHEN 6 : xd=-1:yd=1

WHEN 7 : xd=-1:yd=0

WHEN 8 : xd=-1:yd=-1

ENDCASE

REM Check each letter's proposed position

FOR a=1 TO LEN(w$)

c$=MID$(w$,a,1)

REM Letter off grid?

IF x<0 OR x>15 OR y<0 OR y>15 THEN

placed=FALSE

ELSE

REM Empty slot in grid or matching letter?

IF grid$(x,y)<>"-" AND grid$(x,y)<>c$ THEN

COLOUR 1:PRINTTAB(x*2+24,y+4);"#"

placed=FALSE

ELSE

COLOUR 2:PRINTTAB(x*2+24,y+4);c$

ENDIF

ENDIF

x=x+xd:y=y+yd

pause=10-attempt:REM Speed up as more attempts are required

WAIT pause

NEXT

attempt+=1

UNTIL placed

REM Update grid with word

x=xstore:y=ystore

FOR a=1 TO LEN(w$)

grid$(x,y)=MID$(w$,a,1):x=x+xd:y=y+yd

NEXT

REM Exit loop when the end of the file has been reached, or 20 words found

UNTIL EOF#c OR wcount=20

CLOSE#c:REM Close the word list file

REM Fill remaining gaps with random letters

FOR y=0 TO 15

FOR x=0 TO 15

IF grid$(x,y)="-" THEN grid$(x,y)=CHR$(RND(26)+64):PROCshowgrid:WAIT 4

NEXT

NEXT

REM Write the contents of the grid to a text file

c=OPENOUT("Wordgrid.txt")

FOR y=0 TO 15

FOR x=0 TO 15

BPUT#c,ASC(grid$(x,y))

NEXT

BPUT#c,13:BPUT#c,10:REM Characters 13 & 10 separate each line

NEXT

CLOSE#c

PRINTTAB(0,wcount+2);"Finished"

END

REM A procedure to display the contents of the word grid

DEF PROCshowgrid

LOCAL x,y

COLOUR 6

FOR y=0 TO 15

FOR x=0 TO 15

PRINTTAB(x*2+24,y+4);grid$(x,y)

NEXT

NEXT

ENDPROC

REM Wordsearch Solver

MODE 9:OFF

REM Use a standard fixed-width font

*FONT Courier New,12

DIM grid$(15,15):REM Dimension a 16 x 16 element array

REM Read the wordgrid file

c=OPENIN("wordgrid.txt")

IF c=0 PRINT"File not found":END

FOR y=0 TO 15

FOR x=0 TO 15

grid$(x,y)=CHR$(BGET#c)

NEXT

b=BGET#c:b=BGET#c

NEXT

CLOSE#c

wcount=0:REM A count of how many words we've read from the file

REM Open the text file to read the list of words

c=OPENIN("words.txt")

IF c=0 PRINT"File not found":END

REM Loop to read the file contents

REPEAT

REM Read the file one character at a time

w$="":REM w$ will hold the next word in the list

REPEAT

b=BGET#c:REM Read a single character (byte) from the file

IF b>=65 THEN w$=w$+CHR$(b)

REM Each line in a text file (except the last) ends with values 13 and 10

UNTIL EOF#c OR b=10

IF LEN(w$)>16 THEN PRINT"Word too long: ";w$:CLOSE#c:END

wcount=wcount+1

COLOUR 7:PRINTTAB(0,wcount);w$

found=FALSE

REM Loop through every row until word found

yp=0

REPEAT

REM Loop through every column until word found

xp=0

REPEAT

PROCshowgrid:REM Redraw the wordgrid

REM Have we found the first letter of the current word?

IF grid$(xp,yp)=LEFT$(w$,1) THEN

REM Loop through every direction looking for the word

d=1

REPEAT

found=TRUE

x=xp:y=yp

CASE d OF

WHEN 1 : xd=0:yd=-1

WHEN 2 : xd=1:yd=-1

WHEN 3 : xd=1:yd=0

WHEN 4 : xd=1:yd=1

WHEN 5 : xd=0:yd=1

WHEN 6 : xd=-1:yd=1

WHEN 7 : xd=-1:yd=0

WHEN 8 : xd=-1:yd=-1

ENDCASE

REM Check each letter's expected position

FOR a=1 TO LEN(w$)

c$=MID$(w$,a,1)

REM Letter off grid?

IF x<0 OR x>15 OR y<0 OR y>15 THEN

found=FALSE

ELSE

REM Matching letter?

IF grid$(x,y)<>c$ THEN

COLOUR 1:PRINTTAB(x*2+24,y+4);"."

WAIT 1

found=FALSE

ELSE

COLOUR 13:PRINTTAB(x*2+24,y+4);c$

ENDIF

ENDIF

x=x+xd:y=y+yd

NEXT

d+=1

WAIT 1

UNTIL found OR d=9

IF found THEN WAIT 200

ENDIF

xp+=1

UNTIL found OR xp=16

yp+=1

UNTIL found OR yp=16

REM Exit loop when the end of the file has been reached, or 20 words found