In this screencast I’ll show you how to write your own INPUT routine in Commodore BASIC. This comes in handy when you want to reject certain keys from being used when asking users for keyboard input. In my example I’m going to allow all alpha characters (A-Z), as well as SPACE, RETURN and the DELETE key.
In this episode I’ll explain the concept of Flow Control in Commodore BASIC. It’s kind of a video update of a post I did a while ago.
In essence, it means that we can tell the programme to take a different route in the code depending on a condition that’s met. We’ll explore the IF/THEN and ON… GOTO/GOSUB statements (available on all versions of Commodore BASIC), as well as the expanded IF/THEN/ELSE version (available on the C128 and Plus/4 only).
In addition, I’ll also show you how to use the BEGIN and BEND clauses that were introduced with the C128.
In this episode I’m adding statistics support to my previous lottery generator on the Commodore 64.
I’ll add an array that is updated if my supplied numbers have been matched, and how many times over how many draws this has happened. I’ll also add an option to pause the programme and display the statistics before random draws can continue.
When this app is run continuously it will collect statistical data on how many lottery draws are necessary to match all supplied numbers.
PS: By the time the video had uploaded, my emulator had drawn over one million sets, and none of them have matched my numbers 🙁
In this episode I’m amending my previous lottery number generator to take six lucky numbers from the user to match against the randomly drawn numbers from the Commodore 64.
This will allow us to compare what the computer has drawn to the user’s input, as well as keep drawing numbers until the user input comes up. It’ll be an interesting experiment to see how many draws that will take…
In this episode I’m demonstrating how to print numbers in evenly spaced columns in Commodore BASIC.
On the C128 and the Plus/4 we can use a nifty little function called PRINT USING for this, with which we can format the output of any printed text or variable.
On the C64 and VIC-20 that function doesn’t exist, so we’ll have to convert a numeric value into a string (using the STR$ function), and then determine how long our string is. Following that we’ll have to manually pad our string value with as many spaces as are required. Continue reading How to print numbers as columns in Commodore BASIC→
In this episode I’ll demonstrate how to sort a numeric array on the Commodore 64. The same principle works for string arrays, and of course on all other Commodore BASIC computers.
The technique I’m using here is called Bubble Sort: in effect we’re comparing the first two items in the array, and if the left one is larger than the right one, the values are swapped around. This loop continues until all items in the array have been compared and sorted (hence the smallest items “bubble” to the front of the array, much like the smallest bubbles in a soda float to the top first).
Here’s the full code I’m building, including the lottery portion. The Bubble Sort code starts in line 200.
In this episode I’ll demonstrate how to draw random lottery numbers on a Commodore 64. The secret sauce here is not only the RND function to generate random numbers, but also two loops inside each other that prevent the same number from coming up more than once.
Here’s the lottery generator code:
To adapt this listing to match your local lottery, change line 20 to the amount of numbers to be drawn from your pool (6 in my example), and change the value in line 30 to match the size of your pool (49 in my example).
Commodore BASIC has some interesting and simple string functions built in. Three of them are self explanatory: LEN, LEFT$ and RIGHT$. But others, like the mysterious MID$ and INSTR functions, are a little tricker, and I can never remember how they works.
So here’s a quick recap on how they all work.
Returns the length of any given string. For example,
a$=”the cake is a lie”
print len (a$)
returns 17, which is the number of characters in our string.
The LEFT$ function takes the x left characters from a given string. Here’s an example:
We get “one”, because those are the 3 leftmost characters in our string a$.
Likewise, RIGHT$ takes the x right characters from any given string:
Here we get “three”, because those are the 5 right characters of a$.
MID$ is a little more complex. It takes x characters from a given string, starting at position y. Let’s look at our earlier example again:
We get “two”, because those are the 3 characters, starting at position 5. The first position in all these string operations counts as one rather than zero.
But did you know that MID$ can also be used to assign and replace different characters in a string? Consider this:
Now we’ve replaced the 3 characters in our string with another string, starting at position 5.
I had no idea it cold do that! All these string operations work in all variations of the Commodore BASIC, except for the MID$ assignment which only works on the Plus/4 and the C128.
INSTR (A$, B$)
On the Plus/4 and C128, we can even check if one string is contained in another and at which position this occurs. Consider this:
a$="the cake is a lie"
In our example, INSTR returns 5 because “cake” has been found at position 5 of “the cake is a lie”.
We can also specify a position from which the search shall be started like this:
Now INSTR returns 0 because “cake” has not been found beyond position 6 of our input string.