Creating a Fun Little ZX81 Implementation of Fizzbuzz
My take on FizzBuzz for the ZX81 with some added flair.
Hey, it’s October. Well, it has been for a few weeks now. In any case, it is also time for a ZX81 program. I recently joined an 8-Bit Programming & Engineering Facebook group and its cover is a photo of a FizzBuzz program in Pascal. As a quick exercise, I thought I’d write my own in ZX81 BASIC.
# A little history.
I had to do a little research on FizzBuzz after writing the program. It was vaguely familiar but I didn’t remember from where. After a bit of research, I learned two things. First, it was a children’s game. The program made bit more sense to me after reading that. You see, I had translated it from the code without the context.
For those that don’t know the rules. For a series of numbers, you say “Fizz” if it is divisible by 3, “Buzz” if divisible by 5, and “FizzBuzz” if divisible by both. With the background of the game, the program made a lot more sense.
Second, it is often used as a programming interview question. Obviously the intent wasn’t to solve the problem, which is pretty straight forward. The intent was to understand how you code. There are many sites out there dedicated to solving FizzBuzz in multiple languages. The funniest being the FizzBuzz Enterprise Edition that is so over the top to be unintelligible. Satire at its best.
# Is anyone hiring?
I doubt that someone is going to ask anyone to write a version of FizzBuzz in an archaic language for a computer long since gone. Fortunately, I don’t care about such arguments. I love the minimalism that the ZX81 was. It may be constrained but it is that constraint that makes it challenging. And that challenge is what makes it fun!
Now, FizzBuzz isn’t taxing the ZX81 a whole lot. My biggest challenge was replicating the MOD function. Sadly, there is no similar function that makes this program relatively trivial. Even so, the math to replicate it simple enough. I ended up using I-X*INT(I/X) where X is either 3 or 5.
For the first version of the program, I mostly just transcribed the code. Since the ZX81 doesn’t auto-scroll the screen, I added in a SCROLL
command. Being a literal translation, it wasn’t very interesting. Time to make it my own.
# Adding a bit of spice.
The first thing I did was clean up the program in my style. For that, I changed up the variables and the math quite a bit. I wanted something that you could read and understand, even if you didn’t know ZX81 BASIC. To that end, the variables became FIZZ and BUZZ.
Since the ZX81 used 0 for false, I used a simple NOT
function to make zero remainders true, and any other false. This made the subsequent IF
statements make sense with the new variables and the ultimately more readable.
With that complete, I wanted to add a little title once the sequence was done. To make it a bit more interesting, I printed FiZZBUZZ with the words alternating inverse letters. The first version was a bit too fast, so I looked for options to slow things down.
Usually, I would just add a little delay with a simple LET
command and a lot of slow math functions. This time, I decided to try something different. I decided to use the parameters of the PRINT AT
command to slow things down a bit. But, they also provided the number to print the word at.
The LET T$="PI*PI+1"
resolves to 10. Where LET Z$="NOT PI"
resolves to 0. Using VAL
, the math adds a little delay that is just slow enough to be pleasing.
If you didn’t noticing running the program, I print the title over one of the existing “FIZZBUZZ” words. This was actually a planned accident. I had planned for it to print to the right but I noticed that it lined up nicely with an existing word. Realizing that would be a better way to display the title, I changed the column to line up nicely.
FizzBuzz, ZX81 Screenshot, 2022 by Steven Reid
# Wrapping up.
Once I had the basic structure complete, I focused on cleaning up the code. In all, most of the program was done in less than an hour split over last night and this morning. I probably spent more timing renumber lines then actually playing with the code. I’m sure Dijkstra would be proud that I didn’t use GOTO
in the code.
Once that was done, all I had left was my normal process of saving the program, grabbing a screenshot, and writing this article. It was a nice distraction, but time to get back to working on Gem Quest.