Adapting to Tiny BASIC for BASIC Programmers
You may be old enough to remember the 1980s home computer boom. Your computer booted directly into BASIC, and that's what you learned to program with. That BASIC would have features like string handling and arrays. It would have a FOR loop structure for counting. And if you were very lucky, it might even have a DO or WHILE loop structure.
Tiny BASIC has none of these things. But this article shows how you might simulate some of these things in Tiny BASIC and get around those limitations.
This is just about impossible in Tiny BASIC, so let's get it out of the way first. You can only input numbers, so there's no asking the user for their name. If you want to ask a simple Yes/No question, then you'll have to ask the user to enter something like 1 for yes or 0 for no. Instead of asking a player if they would like to "Play first?" for example, you could phrase a question as "Play first or second (1/2)?"
Strings can be output in a PRINT statement. If you want to print one of a set number of strings, you could use a variable to store which one in numeric form, and use a series of IF statements to print the appropriate one, like in this example:
100 IF C=1 THEN PRINT "Red" 110 IF C=2 THEN PRINT "Green" 120 IF C=3 THEN PRINT "Blue"
You can implement arrays in a limited fashion with the computed GOTO or GOSUB. If the values of the array never change, then you can have a series of LET statements which are accessed through a computed GOTO, like this:
100 GOTO 100+10*B 110 LET G=15 115 GOTO 140 120 LET G=4 125 GOTO 140 130 LET G=7 140 ...
This is similar to the "what's in the box" example from the Skipping Ahead section of the tutorial. Here, boxes 1, 2 and 3 contain 15, 4 and 7 pieces of gold respectively. In a full BASIC language we'd have stored these in an array; here we use a computed GOTO instead. Note that there is no way to change these values unlike in a proper array.
Another way to simulate arrays is by procedural generation. Again this doesn't allow values in the "array" to be changed during the course of the program, but it could be used to generate a world full of locations. Consider a small world of, say, 10x10 squares for a strategy game. Normally you'd generate the terrain and store it in an array. In Tiny BASIC, you'd set a seed, and generate squares 1 to N where N is the number of square you want to look at, 1 to 100. This routine does something similar:
100 LET R=1234 110 LET S=1 120 GOSUB 160 130 LET T=R-R/4*4 140 IF S=N THEN RETURN
145 LET S=S+1 150 GOTO 120 160 LET R=5*R+35 170 LET R=R-R/6547*6547 180 RETURN
The code above forms a subroutine that we'd call with a GOSUB 100. N is the number of the square we're interested in, which should be set before calling the subroutine. The terrain of that square, T, will be a random number from 0 to 3. In a full BASIC language we'd probably store all these terrain values in an array when the program begins, and access square N just by referring to T(N). In Tiny BASIC we can't do that, so we regenerate the series of pseudorandom numbers from 1 to N each time we want to retrieve a terrain value. This method is used with advantage even in full BASIC languages when the game world is bigger than the memory has room for.
FOR, DO and WHILE Loops
This has to be constructed by IF and GOTO, along with a counter that we change ourselves:
10 LET N=1 20 PRINT "Counting: ",N 30 LET N=N+1 40 IF N<=10 THEN GOTO 20
This simulates a "FOR N=1 TO 10" loop that you'd encounter in standard BASIC. A DO loop, where the condition is tested after the loop body has run, would be broadly similar:
10 INPUT N 20 PRINT "You entered ",N 30 IF N<>0 THEN GOTO 10
This finishes the loop when the user enters zero. A traditional WHILE loop tests the condition before executing the loop body. It would be useful in the above loop if the zero is taken as an exit code and not printed, and the equivalent to a WHILE loop would look like this:
10 INPUT N 20 IF N=0 THEN GOTO 50 30 PRINT "You entered ",N 40 GOTO 10 50 ...
AND Operators for IF
In most BASICs, an IF statement can link conditions with the operators OR and AND. The OR statement is very cumbersome:
10 LET F=0 20 IF A=1 THEN PRINT "Do something..." 30 IF A=2 THEN PRINT "Do something..."
This simulates the traditional BASIC construct of "IF A=1 OR A=2 THEN ...". If there's a possibility of both conditions being true, further care needs to be taken to avoid printing the message twice; that will be left as an exercise for the reader. Simulating AND is a bit more elegant:
100 IF N>=1 THEN IF N<=10 THEN PRINT "N is in range."
Hopefully this article has shown that, even though Tiny BASIC lacks some of the features you might be used to, they can be simulated using the more elementary building blocks that it provides.