In the first Commodore 64 BASIC Programming lesson, we looked at BASIC and some core Commodore BASIC commands, but we didn’t really create a useful program.
Today’s lesson is inspired by Dungeons and Dragons. In fact, we’re going to create a character generator!
If you’re not already familiar with Dungeons and Dragons and how characters are created, each character has various statistics. Strength, dexterity, constitution, intelligence, wisdom, and charisma. These numbers are usually generated using dice rolls, and that’s something we can also do in Commodore 64 BASIC …
See the D&D Character Generator in action now right in your web browser
D&D Character Sheets
This is my character, Algar Algarson. He is a Level 4 Dwarf Warlock.
He’s got a strength of 11, a dexterity of 13 constitution of 16 and so on. These are numbers between 1 and 20, determined randomly by rolling dice.
In our basic program we can do the same. We can generate each statistic randomly, and print them out on screen.
To do this, we’re going to use three of the four attributes of a structured program.
Structured BASIC with GOSUB and FOR NEXT
We are going to use subroutines with
GOSUB, sequences using things called Arrays (numbered lists), and we’re going to iterate using
What we’re not going to do this time around is
IF logic, that will come up next time.
Commodore BASIC Random Number Generation
Random numbers are generated in Commodore basic using the
Now computers aren’t known for being able to generate truly random numbers (unless you use Lava Lamps), but it’s near enough for our purposes and to make it better, to make it more random, we use a built-in timer called TI as the seed for our random number generator.
TI counts “Jiffys”, and they are based on your video update frequency (PAL or NTSC).
R=RND(-TI), and that initializes our random generator. Afterwards, every time we call
RND(1), we’ll get a random number between zero and one, (never zero and never one).
Integer and Float Variables in Commodore BASIC
Just as a reminder, we’ve got numbers and we’ve got strings. We actually have two types of numbers, Floats and Integers.
In Commodore 64 basic, unlike most programming languages, integers are smaller but not faster. They’re actually slower than using the regular floating point numbers. Floating point means that you’ve got a decimal whereas an integer is a whole number between -32767 and +32767. It’s a 16 bit integer.
Another interesting thing about Commodore 64 basic is the variables are only unique by the first two characters of the variable names. So you can have longer names, but they’re only unique because of the first two characters, unless they’ve got a different symbol.
Another restriction is you cannot use the name of any built-in Commodore BASIC command.
Below I have a cheat sheet list of Commodore BASIC commands available for download to subscribers.
GET THE COMMODORE BASIC COMMAND GUIDE + MORE
Input your email address to never miss an update PLUS get access to these downloads:
- Commodore BASIC Commands List Cheat Sheet
- The Dungeons and Dragons Character Generator BASIC listing
- Slides PDF from the video presentation
We can also convert between variable types. So when we’re doing a random number, we actually want only whole numbers so we’ll use the
INT Commodore BASIC command to convert it to an integer.
Commodore BASIC Subroutines with GOSUB
Subroutines are very, very important for splitting your program into smaller, more manageable and more reusable parts.
In Commodore BASIC we use
GOSUB, which takes us to a line number and then when we’ve done with our subroutine, we’ve done all the commands we need to, we use
RETURN to go back to the next line after where we left.
Iteration and Loops with FOR NEXT
For iteration, we’re going to use the
FOR NEXT. This increments a counter up to a value that you set.
FOR repetitions = 0 to 9
When it hits nine, it will stop executing and go to the next part of our program.
While we’re inside the loop, we can reference which iteration we are on by checking the index variable and output it.
At the end of the loop we use
NEXT to go to the next iteration.
We necessarily don’t have to use a variable name if we are in the innermost loop (
FOR loops can be nested), but it’s good practice for readability to make sure we use the variable name if we have the space.
Commodore BASIC Arrays
As well as individual variables, we can also have numbered lists. They’re called arrays.
By default, you can have 11 slots. Arrays in Commodore basic start at zero. So if you have 10 slots, it would be zero to 9. If you need more than 11 slots, then you need to “dimension” the array. That tells BASIC how much memory to set aside.
You can also have two-dimensional arrays, which makes it like a grid or a matrix. In this case, I’m pointing to square
(1,1). So one across and one down.
Putting it all Together
Finally, here is our character generator in action.
It prints each attribute and what our score is.
First we set the screen up, we clear the screen. We set the text to white.
Then we initialize our random number generator as we mentioned before.
For the individual attributes we create an array, an array with six slots. So we dimension it as five (0 to 5).
Then we loop through each attribute using a
FOR loop, and we
GOSUB to a subroutine that generates the random number for it. As you can see, these Commodore BASIC commands are essential for iterating without repeating yourself!
For the random number generation we’re using the average of 4x D20 dice rolls. That’s not ideal because that can mean that we get quite low scores, but next time we can fix that by adding some logic and choosing the 3 highest out of 4x D6 rolls.
Remember to grab your downloads!
Remember the slides, .D64 disk and bonus Commodore BASIC Commands List Cheat Sheet PDF are all in the bonus content to download as a .ZIP file. Just enter your email address for immediate access. Head back up and get them right now!