MaN1cPuzzle ported to Tiny BASIC!
Recently, I had some fun putting together TinyBasicBlazor, an interactive Tiny BASIC environment, based on the C# port of Tom Pittman's TinyBasic interpreter and running on (WebAssembly enabled) web browsers.
Besides the sample programs included, I wanted to add some original content, and so decided to convert my MaN1cPuzzle to Tiny BASIC. MaN1cPuzzle is a generalized computer version of the 15-Puzzle (also known as Game of Fifteen or Magic 15) sliding tiles game, originally coded in 20 lines of Sinclair BASIC for the ZX Spectrum home computer. The porting process was fairly straightforward and I only had to face a few challenges due to the differences between the two BASIC dialects.
The first and most obvious difference is that Tiny BASIC only allows one instruction per line. This is not an issue at all, in fact, while on the original MaN1cPuzzle I exploited the multiple instructions per line feature to make the program fit in 20 lines, with the Tiny BASIC port I did not have to fulfill this requirement. So, I took advantage of this constraint and expanded the code, making it more easily readable.
Second, Tom Pittman’s implementation of Tiny BASIC does not support FOR loops, which were introduced with Tiny BASIC Extended. However, they can be easily replaced with GO TOs, even if it is not a good practice in structured programming (and is even considered harmful).
Another substantial difference between the dialects is that Tiny BASIC only manages integer numbers. Nonetheless, even if floating point numbers could be handled with some additional programming, as described in Tiny BASIC Experimenter’s Kit, they are not needed in this kind of game.
Next, Tiny BASIC has no graphics/colors/sound instructions, but for this kind of game, text output with some pseudo graphics made with standard ASCII characters is enough.
The only tricky point in the porting process has been the lack of arrays support in Tiny BASIC. MaN1c puzzle, in fact, uses a bidimensional array for storing the tiles positions in the game grid. I was able to overcome this issue by adopting the approach described in Tom Pittman‘s TIC-TAC-TOE IN TINY BASIC or How to Make a Program TINY, by means of the peek and poke functions, to read and write the content of contiguous memory locations from address 0 to address M*N-1 (where M and N are the game grid dimensions), thus simulating an array. The Tiny BASIC interpreter, in fact, includes some builtin routines that can be invoked with the USR function. In particular, peek function is available at address COLD_START + 20 and poke function is available at address COLD_START + 24, COLD_START being 256 (100h) in the interpreter implementation used by TinyBasicBlazor.
The final result is shown in the picture below and the game is playable in your web browser with TinyBasicBlazor (keyboard required). When the program starts, it asks for COLD_START address; simply enter 256. Then the programs asks for grid dimensions and the number of random moves to use for grid initialization. I have included two shortcuts that allow immediate game start, for playing either with 15 tiles on a 4×4 grid or with 24 tiles on a 5×5 grid. The BASIC listing of the game is also available.
Links and references
Leave a comment
Log in with itch.io to leave a comment.