An Alternative Display Method – Tank Battle

See all articles from SYNC v2 n3
Author(s): Drew Nisbet
Publication: SYNC
Pub Details: SYNC v2 n3 (Volume 2, Issue 3)
Date: May/June 1982
Pages: 38-41

Perhaps the greatest inconvenience presented by the ZX80 to the person programming in Basic is the loss of video which occurs whenever the CPU is otherwise occupied. While you are sitting, perhaps not so patiently, waiting for a display to appear, the amount of time that the screen is blank seems much longer than it actually is. This may be only mildly irritating when the program does not depend on user interaction. However, if the program involved is a board game which requires that an ever-changing playing arena be displayed repeatedly and that the program interact with the player to some degree, then periods of time spent before an unimpressive, grey screen can be fatal to the attention span of the player.

The majority of board games written in Basic for the ZX80 display the playing surface by storing print codes in an integer array and printing each member of this array by means of a FOR/NEXT loop. Any change on the board, such as the movement of a token or the destruction of a coordinate, requires that the screen be cleared and that the display be re-created. Generally speaking, the larger the playing surface of a game, the better, but displaying a board which uses a large portion of the screen area by the method noted above can take up to 30 seconds. Add to this the time required by the computer to perform any calculations which are required between displays and you may wind up with the plug being pulled on your program! In addition, any prompts or messages displayed for the player’s information must be kept to a minimum, or the playing surface must again be cleared in order to accommodate them.

Figure 1: Token Use Directions.

Symbol        | Meaning
--------------+-------------------------------
+             | Available coordinate
■             | Destroyed coordinate
H             | Location of player's tank
C             | Location of computer's tank °
Inverse *     | Player's tank destroyed °
Inverse C     | Computer's tank destroyed °

° Displayed at end of game

Although the most valuable use of the PEEK and POKE commands may be to program and debug ZX80 machine code programs, they are extremely valuable when employed in any Basic program as tools for both examining and altering memory location contents. After an initial display has been created on the screen, it may be examined or altered by PEEKing or POKEing memory locations relative to the address stored in the D-FILE pointer (located in the two bytes starting at address 16396). The game program which follows makes extensive use of this function.

The screen display for Tank Battle is divided into two sections: the playing surface, or board, which occupies two thirds of the display, and a “message center” which makes use of the remaining third of the screen and is located to the right of the game board. This arrangement affords the largest square playing area available. The two sections are prominently separated by a black line. The PRINT command is not employed after the initial playing board has been displayed as all alterations to the playing surface are made by POKEing character codes into memory. All prompts and messages are displayed and erased in the same manner. The longest period of time during which the screen is blank is approximately five seconds, the amount of time depending on the number of lines which must be deleted from the message portion of the screen.

Figure 2: Screen Display after Several Moves.

     TANK BATTLE      | PLAY:  S
 # 1 2 3 4 5 6 7 8 9  |
 1 + + + + + + + + +  | COORD:23
 2 + + ■ + + + + + +  |
 3 + ■ ■ + + + + + +  | I FIRED
 4 + + ■ + + + ■ + ■  | FROM 42
 5 + + ■ + + + + H +  | TO 68
 6 + + + + + + + ■ +  |
 7 + + + + + + + + +  | PLAY:
 8 + + + + + + + + +  |
 9 + + + + + + + + +  |
----------------------+---------
'L'

Figure 2 shows the screen layout after both the player and the computer have taken several turns. The drawing is not to scale as the playing board is really double spaced both vertically snd horizontally. The . which does not print on the screen, is the point from which all PEEKing and POKEing is done in order to examine or alter the board contents. The address stored in the D-FILE pointer and 67 (32 print codes plus 1 newline character times 2 plus 1 print code on the third print line equals 67). The address for any coordinate on the board is determined by multiplying the row coordinate by 6 (2 times 32 print codes plus 1 newline character), adding 2 times the column coordinate (for the horizontal double spacing) and adding 67 (for the reference point) to the contents of the D-FILE pointer. Alterations to the message portion are accomplished by POKEing the appropriate character codes into memory locations relative to the contents of the D-FILE pointer.

The program uses the subroutines at lines 1600 and 1910 to accomplish the above. The subroutine at line 1600 alters any memory location by POKEing a character code M into the location relative to the contents of the D-FILE pointer by an offset of P locations. The subroutine at line 1910 POKEs the message codes into consecutive memory locations by examining the strings (T$) one character at a time. Lines 1430 – 1560 POKE zeros (code for a space) into any line which contains non-blank characters in the message portion of the screen. The routines located at lines 1620, 1690, 1970, and 2010 display the coordinates selected by the player, the shots fired by the computer, the player’s tank when destroyed and the computer’s tank when destroyed respectively.

The game itself was inspired by Lloyd Johnson’s article “Paint Duel” in Creative Computing (July 1981). The player attempts to destroy the computer’s tank by hitting it with a well-placed shot. The two plays available are Shoot or Move. The player’s initial location is row 5 column 8. The computer tries to destroy the player’s tank in turn. The starting location for the computer’s tank is in the first column and a random row. After specifying the play selected the computer requests the coordinates of the target and then displays them in the message portion of the screen. A shot shows up on the screen as a “■” placed at the coordinates specified. A move is legal only if made to a coordinate directly above, below, left of, right of, or diagonally adjacent to the present location. A tank may not move to a coordinate which has previously been shot at.

The computer’s play is determined by the following odds:

1) 10% chance it will do nothing.

2) 10% chance it will shoot.

3) 70% chance it will move if it shot last turn.

4) 70% chance it will fire if player shot last turn.

5) Move.

These odds can be altered by changing the values in lines 890 – 920 of the program. The computer is prevented from firing at a coordinate which has already been shot at by PEEKing into the memory location for the coordinate selected and comparing its contents to 128 (black square). The tanks are prevented from moving into a hit square in the same way. Both the player’s and the computer’s plays are completed simultaneously, which allows the possibility of a tie game if both tanks are destroyed on the same turn. Each player learns of the position of the opponent’s tank only if he is fired upon. If the computer decides to shoot, it will place its shot into a coordinate which it knows the player may have moved to. To end the game press NEWLINE when requested to play.

Ed.—For those who do not wish to enter the listing, it is available from the author on cassette. Write for information.

Figure 3: The Tank Battle Program.

“#” represents a space

“@” stands for the character shifted “Q”.

  10 RANDOMISE
20 LET HO = RND(9) * 10 + RND(9) player's last coord=dummy
30 LET HV = RND(9) * 10 + RND(9) player's 2nd last coord=dummy
40 LET HN = 0 player's last move
50 LET CM = 1 computer's move
70 LET SH = 0 computer's target
80 LET A$ = "COORD:"
90 LET B$ = "YOU WIN"
100 LET C$ = "I WIN"
110 LET D$ = "I FIRED"
120 LET E$ = "FROM"
130 LET F$ = "TO"
140 LET G$ = "PLAY:"
150 LET H$ = "TIE GAME"
160 LET I$ = "GAME OVER"
170 LET J$ = "AGAIN?"
180 LET D = 5 player's beginning row
190 LET E = 8 player's beginning column
200 LET HL = 58 player's beginning coord
210 LET CL = RND(9) * 10 + 1
220 PRINT "#####TANK BATTLE####@PLAY:######"
230 PRINT "####################@###########"
240 PRINT "#";
250 FOR I = 1 TO 9
260 PRINT "#";I;
270 NEXT I
280 PRINT "#@###########"
290 PRINT "####################@###########"
300 FOR I = 1 TO 9
310 PRINT I;"#+#+#+#+#+#+#+#+#+#@###########"
320 PRINT "####################@###########"
330 NEXT I
335 FOR I = 1 TO 32
336 PRINT CHR$(131);
337 NEXT I
340 LET P = 67 + 66 * D + E * 2
350 LET M = 45 'H'
360 GO SUB 1600 print player's location
370 INPUT M$ input play
380 IF NOT (M$ = "M" OR M$ = "S" OR if invalid ask again
M$ = "") THEN GO TO 370
390 IF M$ = "" THEN GO TO 800 if end of game branch
400 LET P = 29 print play selected
410 LET M = CODE(M$)
420 GO SUB 1600
430 LET P = 89 print 'COORD:'
440 LET T$ = A$
450 GO SUB 1910
460 INPUT C input coord
470 LET D = C / 10 row
480 LET E = C - D * 10 column
490 IF D < 1 OR D > 9 OR E < 1 OR E > 9 if invalid coord ask again
THEN GO TO 460
500 IF M$ = "S" THEN GO TO 660 if shoot branch
510 LET G = HL / 10 player's row
520 LET H = HL - G * 10 player's column
530 IF ABS(G - D) > 1 OR ABS(H - E) > 1 if illegal distance ask again
THEN GO TO 460
540 LET P = 67 + 66 * D + E * 2
550 IF PEEK(PEEK(16396) + PEEK(16397) if illegal coord ask again
* 256 + P) = 128 THEN GO TO 460
560 GO SUB 1620
570 LET P = 67 + 66 * G + H * 2
580 LET M = 19 '+'
590 GO SUB 1600 blank player's old position
600 LET HL = C coord = new coord
610 LET P = 67 + 66 * D + E * 2
620 LET M = 45 'H'
630 GO SUB 1600 display player's new position
640 LET HM = 1 play = move
650 GO TO 890 branch to computer's turn
660 GO SUB 1620 display coords
670 LET P = 67 = 66 * D + E * 2
680 LET M = 128 '■'
690 GO SUB 1600 display hit coord
700 LET HM = 2 play = shoot
710 LET HO = HL let old coord = present coord
720 GO TO 890 branch to computer's turn
730 LET P = 353
740 LET T$ = J$ print 'GAME OVER'
750 GO SUB 1910
760 INPUT Y$ input yes or no
770 IF Y$ = "N" THEN STOP stop if no replay
780 CLS
790 RUN run if replay
800 LET D = CL / 10 display computer's location
810 LET E = CL - D * 10
820 LET P = 67 + 66 * D + E * 2
830 LET M = 40
840 GO SUB 1600
850 LET P = 287
860 LET T$ = I$ print "GAME OVER"
870 GO SUB 1910
880 GO TO 730 ask for replay
890 LET X = RND(10)
891 IF X > 9 THEN GO TO 1140 10% chance to do nothing
892 IF X < 2 THEN GO TO 1020 10% chance to shoot
900 IF CM = 2 AND RND(10) > 3 THEN 70% chance to move
GO TO 920
910 IF HN = 2 AND RND(10) > 3 THEN 70% chance to shoot
GO TO 1020
920 LET D = CL / 10 computer moves
930 LET E = CL - D * 10
940 LET S = D + RND(3) - 2
950 LET T = E + RND(3) - 2
960 IF S < 1 OR S > 9 OR T < 1 OR if invalid coord
T > 9 THEN GO TO 940
970 LET P = 67 + 66 * S + T * 2
980 IF PEEK(PEEK(16396) + PEEK(16397) if illegal coord
* 256 + P) = 128 THEN GO TO 900
990 LET CL = S * 10 + T computer's new location
1000 LET CM = 1 computer's play = move
1010 GO TO 1140 branch to request next play
1020 LET D = HV / 10 computer shoots
1030 LET E = HV - D * 10
1040 LET S = D + RND(3) - 2
1050 LET T = E + RND(3) - 2
1060 IF S < 1 OR S > 9 OR T < 1 if invalid coord
OR T > 9 THEN GO TO 1040
1070 LET P = 67 + 66 * S + T * 2
1080 IF PEEK(PEEK(16396) + PEEK(16397) if coord already hit
* 256 + P) = 128 THEN GO TO 1040
1090 LET SH = S * 10 + T coord of target
1100 LET M = 128 '■'
1110 GO SUB 1600
1120 GO SUB 1690 display computers shot
1130 LET CM = 2 computers play = shoot
1140 IF HM = 2 AND C = CL THEN GO TO 1170 if computer's tank hit
1150 IF CM = 2 AND SH = HL THEN GO TO 1230 if player's tank hit
1160 GO TO 1390 if no hits
1170 IF CM = 2 AND SH = HL THEN GO TO 1330 if both hit
1180 GO SUB 1970 display computer's hit tank
1190 LET P = 287
1200 LET T$ = B$ print 'YOU WIN'
1210 GO SUB 1910
1220 GO TO 730 branch to ask for replay
1230 GO SUB 2010 display player's hit tank
1240 LET P = 287
1250 LET T$ = C$ print 'I WIN'
1260 GO SUB 1910
1270 LET D = CL 10 display computer's location
1280 LET E = CL - D * 10
1290 LET P = 67 + 66 * D + E * 2
1300 LET M = 40 'C'
1310 GO SUB 1600
1320 GO TO 730 branch to ask for replay
1330 GO SUB 1970 display computer's hit tank
1340 GO SUB 2010 display player's hit tank
1350 LET P = 287
1360 LET T$ = H$ print 'TIE GAME'
1370 GO SUB 1910
1380 GO TO 730 branch to ask for replay
1390 LET P = 287
1400 LET T$ = G$ print 'PLAY:'
1410 GO SUB 1910
1420 INPUT M$ input play
1430 LET M = 0
1440 LET P = 29 blank out old play message
1450 GO SUB 1600
1460 LET P = 89 blank out all messages
1470 IF NOT PEEK(PEEK(16396) + PEEK(16397) lines which are non-blank
* 256 + P) = 0 THEN GO TO 1500
1480 LET P = P + 33
1490 GO TO 1550
1500 FOR I = 1 TO 8
1510 POKE PEEK(16396) + PEEK(16397) * 256 + P,0
1520 LET P = P + 1
1530 NEXT I
1540 LET P = P + 25
1550 IF P > 320 THEN GO TO 1570
1560 GO TO 1470
1570 LET HV = HO 2nd last location = last
1580 LET HN = HM last play = play
1590 GO TO 380 go display next play
1600 POKE PEEK(16396) + PEEK(16397) poke character code in memory
* 256 + P,M
1610 RETURN
1620 LET P = 95 poke coords in message section
1630 LET M = D + 28
1640 GO SUB 1600
1650 LET P = P + 1
1660 LET M = E + 28
1670 GO SUB 1600
1680 RETURN
1690 LET P = 155 display computer's shot
1700 LET T$ = D$ print 'I FIRED'
1710 GO SUB 1910
1720 LET P = 188
1730 LET T$ = E$ print 'FROM'
1740 GO SUB 1910
1750 LET P = 193
1760 LET M = CL / 10 + 28 print coords
1770 GO SUB 1600
1780 LET M = CL - (CL / 10 ) * 10 + 28
1790 LET P = P + 1
1800 GO SUB 1600
1810 LET P = 221
1820 LET T$ = F$ print 'TO'
1830 GO SUB 1910
1840 LET P = 224
1850 LET M = S + 28 print coords
1860 GO SUB 1600
1870 LET M = T + 28
1880 LET P = P + 1
1890 GO SUB 1600
1900 RETURN
1910 LET M = CODE(T$) print first character of
string
1920 GO SUB 1600
1930 LET T$ = TL$(T$) shorten string
1940 IF T$ = "" THEN RETURN if all done return
1950 LET P = P + 1
1960 GO TO 1910 branch to print next character
1970 LET P = 67 + 66 * (CL / 10) + 2 print computer's hit tank
* (CL - (CL / 10) * 10)
1980 LET M = 168 inverse 'C'
1990 GO SUB 1600
2000 RETURN
2010 LET P = 67 + 66 * (HL / 10) + 2 print player's hit tank
* (HL - (HL / 10) * 10)
2020 LET M = 148 inverse '*'
2030 GO SUB 1600
2040 RETURN

SYNCSUM = 123 4K ROM 4K RAM

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.