How many times have you sat down to convert a program for the ZX80, only to find that the first line was 10 DIM A(10,10)? Chances are that you gave up and turned the page. The next time you do not have to turn the page because it is possible to simulate dimensioned arrays on the Sinclair through the use of a simple algorithm.
Many versions of Basic define a two-dimensional matrix by the command DIM A(X,Y); where the X is the row subscript, and Y, the column. Any location on the matrix can be accessed by specifying values for X and Y. For example, LET A(3,4)=9 assigns the value 9 to the element located at row 3, column 4.
On the ZX80, we define a one-dimensional/vector array containing as many elements as we need and then use a simple formula to locate a given element. For example, if we want to initiate a 10 by 10 matrix, the instruction DIM A(99) sets up 100 locations and the formula A(X+Y*10)=9 assigns to the element at (X,Y) the value 9. In order to save space, the first element in the array simulates A(0,0) and the last, A(9,9). If we take the first element to be A(1,1) (as it is in most Basics), then we would use the general formula A((x-1)+(y-1)*X). These formulae result in column-order storage: all of column 1 is stored before 2. To simulate row-order storage, use the formula A((y-1)+(x-1)*Y) as in Figure 1.
ZX80 Simulated Simulated Simulated Actual Column-order Column-order Row-order Location Location Location Location A(L) L=x+y*3 L=(x-1)+(y-1)*3 L=(y-1)+(x-1)*4 A(0) A(0,0) A(1,1) A(1,1) A(1) A(1,0) A(2,1) A(1,2) A(2) A(2,0) A(3,1) A(1,3) A(3) A(0,1) A(1,2) A(1,4) A(4) A(1,1) A(2,2) A(2,1) A(5) A(2,1) A(3,2) A(2,2) A(6) A(0,2) A(1,3) A(2,3) A(7) A(1,2) A(2,3) A(2,4) A(8) A(2,2) A(3,3) A(3,1) A(9) A(0,3) A(1,4) A(3,2) A(10) A(1,3) A(2,4) A(3,3) A(11) A(2,3) A(3,4) A(3,4)
[Figure: Figure 1. Simulated Locations for a 3 by 4 Array]
In similar fashion, arrays of any number of dimensions can be accessed. The element A(X,Y,Z,…) is located at A((x-1)+(y-1)*X+(z-1)*X*Y+(…)*X*Y*Z+…). A simple comparison shows how the ZX80 can simulate three-dimensional Basic:
ZX80 Basic 3-dimensional Basic
10 DIM A(59) 10 DIM A(3,4,5)
20 LET X=3 20 LET X=3
30 LET Y=1 30 LET Y=1
40 LET Z=2 40 LET Z=2
50 LET A((X-1)+(Y-1)*3+(Z-1)*3*4)=9 50 LET A(X,Y,Z)=9
. .
. .
. .
The best way to illustrate the use of dimensioned arrays is by a demonstration program. The one offered below is fun because the movement of the ship is essentially random. You can never know where it is until you blow it up or actually have it captured. Note that in a 10 by 10 matrix the array location is the same as the simulated location: A(37) is equivalent to A(3,7). This allows direct input of the coordinates desired. The display routine illustrates a fairly standard procedure for the printing of a matrix.
Capture
Capture is similar in some respects to many other matrix manipulation games; but, instead of trying to hit the enemy, you must surround and immobilize him. If you do succeed in hitting his location, you lose the game.
You have a total of fifteen mines which you use to block the enemy’s progress. For a capture, his progress must be blocked in every direction. The display will show you where he was on the previous move. You can always place a mine at this previous location, as he has to move one space on each turn.
Lines 10-50 set up the enemy’s initial location. Lines 80-195 output the display which shows: the matrix, the previous enemy location, the number of mines left, and the location of the mines as you place them. Lines 210-60 decide the enemy’s new location, test for capture, and check the remaining number of mines. Lines 370-430 input your mine placement coordinates and test for a hit on the enemy.
To vary the number of mines, change line 60. Line 370 was keyed in using the following space saving technique: 370 INPUT (SHIFT 5) PRINT ” (SHIFT 8) ROW-COLUMN”. If you fail to use this, the program will print error code 4 when run—every byte counts! When entering the coordinates at line 370, enter them both before hitting NEWLINE. Happy hunting!
Capture Program Listing
10 RANDOMIZE
20 DIM A(99)
30 LET X=RND(10)-1
40 LET Y=RND(10)-1
50 LET A(Y+X*10)=148
60 FOR M=-16 TO 0
70 PRINT
80 PRINT "I WAS LAST AT..."
90 PRINT
100 PRINT " 0123456789"
110 FOR I=0 TO 9
120 PRINT I;
130 FOR J=0 TO 9
140 PRINT CHR$(A(J+I*10));
150 NEXT J
160 PRINT
170 NEXT I
180 PRINT
190 PRINT "MINES=";-M
195 PRINT
200 LET A(Y+X*10)=0
210 FOR T=1 TO 64
220 LET I=RND(3)-2
230 LET I=I+X
240 IF I [illegible] 9 OR I [illegible] 0 THEN GO TO 220
250 LET J=RND(3)-2
260 LET J=J+Y
270 IF J [illegible] 9 OR J [illegible] 0 THEN GO TO 250
280 IF I=X AND J=Y THEN GO TO 250
290 IF NOT A(J+I*10)=20 THEN GO TO 330
300 NEXT T
310 PRINT "OOPS-CAPTURED"
320 STOP
330 LET X=I
340 LET Y=J
350 LET A(Y+X*10)=148
360 IF M=0 THEN GO TO 440
370 PRINT " INPUT ROW-COLUMN"
380 INPUT R
390 IF A(R)=148 THEN GO TO 460
410 CLS
420 LET A(R)=20
430 NEXT M
440 PRINT "OUT OF MINES"
450 STOP
460 PRINT "YOU BLEW ME UP"
Capture Sample Run
I WAS LAST AT... 0 1 2 3 4 5 6 7 8 9 0 1 ■ 2 3 4 5 6 7 8 9 MINES=16 INPUT ROW-COLUMN 17 I WAS LAST AT... 0 1 2 3 4 5 6 7 8 9 0 1 * 2 * * 3 * * * * 4 ■ * 5 * * * * 6 7 8 9 MINES=3 INPUT ROW-COLUMN 45 I WAS LAST AT... 0 1 2 3 4 5 6 7 8 9 0 1 * 2 * * 3 * * * * 4 * ■ * 5 * * * * 6 7 8 9 MINES=2 OOPS-CAPTURED
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.