Complete programs
These programs put loops, strings, arrays and routines to work.
Use Run to open one in the playground, or download its source and run it
with jbcc using a command such as uvx jbcc --run fizzbuzz.b.
The sources come from the jbcc program fixtures
and are distributed under the MIT License.
FizzBuzz
MOD tests divisibility while the loop counts from 1 to 100.
The := operator appends each matching word, so 15 prints
FizzBuzz and 16 prints 16.
Download fizzbuzz.b.
EQUATE limit TO 100 FOR number = 1 TO limit text = '' IF MOD(number, 3) EQ 0 THEN text := 'Fizz' IF MOD(number, 5) EQ 0 THEN text := 'Buzz' IF text EQ '' THEN text = number CRT text NEXT number
Factorial
Start with a product of 1 and use FOR to multiply it by each
integer from 2 through 20. The program prints 2432902008176640000.
Try setting number to 5 to get 120.
Download factorial.b.
number = 20 factorial = 1 FOR factor = 2 TO number factorial *= factor NEXT factor CRT factorial
Quine
A quine prints its own source. This one stores its printing expression
in Q and uses CHAR to print the punctuation around
that string. The output is the complete line below, including the final
newline. Download quine.b.
Q="CRT CHAR(81):CHAR(61):CHAR(34):Q:CHAR(34):CHAR(59):Q";CRT CHAR(81):CHAR(61):CHAR(34):Q:CHAR(34):CHAR(59):Q
Maze
The program carves a passage to an unvisited neighbour and backtracks
when it reaches a dead end. A dynamic array holds the
path, and GOSUB calls routines to choose a neighbour and open a
wall. The default maze is 12 by 8 cells. To set the width, height and
seed, download maze.b and run
uvx jbcc --run maze.b -- 30 15 7. The same seed and dimensions
give the same maze.
EQUATE modulus TO 2147483647 EQUATE multiplier TO 16807 width = SENTENCE(1) height = SENTENCE(2) seed = SENTENCE(3) IF width EQ '' THEN width = 12 IF height EQ '' THEN height = 8 IF seed EQ '' THEN seed = 1 IF NOT(NUM(width) AND NUM(height) AND NUM(seed)) OR width LT 1 OR height LT 1 THEN CRT 'usage: maze [width [height [seed]]]' EXIT(1) END seed = MOD(seed, modulus) IF seed LE 0 THEN seed += modulus - 1 lines = 2 * height + 1 DIM grid(lines) FOR y = 1 TO lines STEP 2 grid(y) = STR('+--', width) : '+' NEXT FOR y = 2 TO lines STEP 2 grid(y) = STR('| ', width) : '|' NEXT DIM visited(width, height) MAT visited = 0 path = '1*1' depth = 1 visited(1, 1) = 1 LOOP WHILE depth GT 0 x = FIELD(path<depth>, '*', 1) y = FIELD(path<depth>, '*', 2) GOSUB unvisited_neighbours IF neighbours EQ '' THEN DEL path<depth> depth-- END ELSE choices = DCOUNT(neighbours, @FM) GOSUB next_random pick = neighbours<MOD(seed, choices) + 1> next_x = FIELD(pick, '*', 1) next_y = FIELD(pick, '*', 2) GOSUB knock_down_wall visited(next_x, next_y) = 1 depth++ path<depth> = next_x : '*' : next_y END REPEAT grid(1)[2, 2] = ' ' grid(lines)[3 * width - 1, 2] = ' ' FOR y = 1 TO lines CRT grid(y) NEXT STOP unvisited_neighbours: neighbours = '' IF y GT 1 THEN IF NOT(visited(x, y - 1)) THEN neighbours<-1> = x : '*' : y - 1 END IF x LT width THEN IF NOT(visited(x + 1, y)) THEN neighbours<-1> = x + 1 : '*' : y END IF y LT height THEN IF NOT(visited(x, y + 1)) THEN neighbours<-1> = x : '*' : y + 1 END IF x GT 1 THEN IF NOT(visited(x - 1, y)) THEN neighbours<-1> = x - 1 : '*' : y END RETURN knock_down_wall: BEGIN CASE CASE next_x GT x grid(2 * y)[3 * x + 1, 1] = ' ' CASE next_x LT x grid(2 * y)[3 * next_x + 1, 1] = ' ' CASE next_y GT y grid(2 * y + 1)[3 * x - 1, 2] = ' ' CASE next_y LT y grid(2 * next_y + 1)[3 * x - 1, 2] = ' ' END CASE RETURN next_random: seed = MOD(seed * multiplier, modulus) RETURN
Sieve of Eratosthenes
A dimensioned array records which numbers are composite. For each prime, an inner loop marks its multiples from its square onward. FMT aligns the primes in columns, and the last line reports 46 primes up to 200. Download sieve.b.
EQUATE limit TO 200 EQUATE per_row TO 10 DIM composite(limit) MAT composite = 0 * Cross out the multiples of each prime, starting from its square. FOR candidate = 2 TO limit IF NOT(composite(candidate)) THEN FOR multiple = candidate * candidate TO limit STEP candidate composite(multiple) = 1 NEXT multiple END NEXT candidate primes = 0 row = '' FOR candidate = 2 TO limit IF NOT(composite(candidate)) THEN primes += 1 row := FMT(candidate, '5R') IF MOD(primes, per_row) EQ 0 THEN CRT row row = '' END END NEXT candidate IF row NE '' THEN CRT row CRT primes:' primes up to ':limit
Rule 110
This cellular automaton starts with one live cell in a row of 63. Each cell's next state depends on its current state and its two neighbours. The three states form a number from 0 to 7, which selects a character from the rule string. A second array holds the new states until MAT copies them back together. The output shows 32 generations, with # for live cells and . for dead cells. Download rule110.b.
EQUATE width TO 63 EQUATE generations TO 32 * The extra cell at each end stays dead. DIM cells(width + 1), next_cells(width + 1) MAT cells = 0 MAT next_cells = 0 cells((width + 1) / 2) = 1 * Rule 110 in binary, ordered from neighborhood 111 down to 000. rule = '01101110' FOR generation = 1 TO generations row = '' FOR x = 1 TO width IF cells(x) THEN row := '#' END ELSE row := '.' END neighborhood = 4 * cells(x - 1) + 2 * cells(x) + cells(x + 1) next_cells(x) = rule[8 - neighborhood, 1] NEXT x CRT row MAT cells = MAT next_cells NEXT generation