Personal Computer News


Arrayed For Action

 
Published in Personal Computer News #077

If you're having trouble programming arrays in your Spectrum adventures, let Kenn Garroch show you the way.

Arrayed For Action

If you're having trouble programming arrays in your Spectrum adventures, let Kenn Garroch show you the way

Initially arrays can be confusing, but once you grasp the idea it's clear that they're both useful and logical. The easiest way to visualise an array is to think of it as a table, so by saying DIM a(10,12) you're DIMensioning an array of ten characters by twelve. All you're doing, in fact, is drawing up a 10 x 12 table of empty boxes, and reserving the right to fill it with numbers later.

Arrays, both numeric and string, tend to be an important element of adventure programming, and this provides a good illustration of how they operate. But the standard adventure displays only half og the array's potential. Normally, adventures come with the arrays already filled with details. You can, however, design an adventure that allows you to fill in the location descriptions before you actually play it.

Arrays allow you to negotiate a sort of maze, picking up pieces of gold and confronting various monsters. Let's see how to write a program that allows you to define the rooms and passageways and wander around them.

Trade-Off

The best way to start the program design is to work out what arrays will be needed.

Since the program needs a set of descriptions for the rooms etc, you need a string array to hold these. The total number of locations possible is set by the amount of memory you have available. In the program that follows, this is set to 10 using the variable M, and can be increased until the memory runs out. The length of the description string is set with T, which can also be increased, but eats up large quantities of memory. The trade-off is between many locations with short descriptions, or a few with more details.

To use the description array, you need a set of pointers to allow the rooms to be linked together in various ways. You must also set the pointer array to default values. Section 1 of the program does just this.

The next thing to do is to set out some kind of control for the program. This is the main menu and gives the options to edit the descriptions, load and save definitions, like up the rooms in a random way and play the game. This is dealt wwith by Section 2 of the program.

The end option is relatively easy. To make sure you don't come out of the program without saving the data, it is generally a good idea to ask before doing something as drastic as stopping. This is dealt with by Section 3.

The next sections are the major parts of the program. Let's take the editor first. This is again controlled from a menu which provides all the major options for defining a series of locations. Section 4, the menu, is written in exactly the same way as the main menu.

Taking these options in order, Section 5 is a routine to list the locations that have, or haven't, been defined. Obviously, if there are several locations it's a good idea to allow some kind of escape mechanism to get back to the previous menu.

Once of the clever things about the Spectrum's Basic is its ability to perform the print statement in line 366, allowing the exists to be printed only if they have been specified by putting a zero in the 'E' array.

This direction specification is performed in the edit location section that follows (Section 6). This just asks for the location number and then expects a set of exit directions - which are NORTH, SOUTH, EAST and WEST.

Directions such as UP and DOWN etc can be added by changing the DIM in line 25 and 30 to allow space in the E and S arrays.

After entering information, you don't need to use it all. Section 7 checks the entries and puts them in the correct positions on the E array. North being E(T,1), South being E(T,2) and East and West being 3 and 4 respectively. Using this routine allows the exits to be entered in the wrong order without confusing the program.

Once all the locations have been defined and described they need to be attached to each other. Normally an adventure will have these predefined as part of the game. Since this is a simple adventure and contains no movable objects, we have to make it exciting somehow. This is done by allowing locations to be attached randomly to each other, but making sure that if you exist south then you must enter north, as in Section 8.

The juggler routine is split into three main parts. The first is a subroutine that resets the original directions into the E array (subroutine 1060). The juggler then goes through the locations and checks whether they have any valid exists. If they contain 0, an exit, the routine jumps down to line 520 and roots through 100 random locations until one is found that fits the bill of matching North to South, East to West, etc. The location numbers are then swapped over in the E array, making a connection (See Section 9).

Ready To Go

The next major section of the program, Section 10, allows the adventure to be run. First the description of location 1 is displayed and then the exits are given. After entering the direction to be followed, lines 640 and 650 work out whether the direction is valid or whether it has not been assigned. Line 680 assigns the new location number to L, the current position.

The other options are L, which reprints the current description, and F, which allows you to get back to the main menu. Again, to make the program as idiot-proof as possible, it is a good idea to ask if the player wants to end with Section 11.

The final subroutines, Section 12, are generally concerned with data handling, and allow the main game array to be saved and loaded from tape or from Microdrive.

Since the juggler program alters the E array, to rejuggle the program, the directions need to be saved somewhere. Section 13 loads and saves the E array in the S array (S for save).

And that's it. You can probably think of hundreds of improvements to this, and it has been written in such a way as to allow these to be entered with ease.

The art of using the program to define a good adventure is to make the locations interesting enough that they stand up on their own. If you want to provide monsters simply enter a description such as: "You stand in a dank, dark closet. From the corner comes a piercing shriek which dies away into a whimpering moan. Cobwebs brush your face." And the rest is up to you...

Kenn Garroch