IF YOU HAVE followed the series of articles to date, you should have managed to produce a game which consisted of a chessboard and logic which restricts the possible moves to only those that are legal, providing two-player chess.

Examining the memory map of the game you may have noticed that a significant proportion of the code is used in creating the board, which is big by ZX-81 1K display standards. If some way could be found to use the display file saved on tape and a minimal file "to boot", then we would have enough space to write a short computer playing routine.

One further point should be explained. It is not possible to give a good opening or finish to the game because that would require slightly more code than we have available in the 1K game. We actually have only 672 bytes. The game, therefore, starts off with the computer playing white and either the King's pawn or the Queen's pawn moved forward one position. The following describes the machine code and techniques used in the creation of my 1K ZX-81 chess program. In order to produce the code you will require a machine with at least 3K of memory. The code has been created using a 3K machine, there should be no problems using 16K if the procedures given are followed and RAMtop is lowered to 1K before loading from tape.

First the Basic program consists of:

A Rem statement. From address 16514 to 17186
A Slow statement. This has been called from Basic to ensure that all ZX-81s will work irrespective of whether you have an early ROM with error or not.
A Rand Usr statement. To call the machine-code routine.
A Rem statement. From address 17207 to 17362 to create the display and move itself above RAMtop.
onwards. Your favourite machine-code loader or this one.
10 FOR A = 16514 to 18000
12 PRINT A; TAB 8;
To explain the logic of 1-4 above, assume you have entered the machine code in Rems 1 and 4. Delete all Basic lines except 1 to 4 inclusive. Now lower RAMtop by typing:
POKE 16389,72
Now move the King's pawn:
POKE 17241,0
POKE 17252,53
OR move the Queen's pawn:
POKE 17242,128
POKE 17253,53
Run the machine-code program in Rem 4 by typing
RAND USR 17207.
This copies Rem 4 above RAMtop and it is therefore protected when the next commands are typed in. Delete line 4, then type Clear.

This removes all the stored variable making available the maximum possible RAM space for our program. Now add the variable X by typing:

LET X= 16959
This sets up the variable X for the Basic program. Line 3 Rand UsrX actually calls the driver routine. Now type:
RAND USR 18542
At this stage you save the program on tape. The machine-code program loaded above RAMtop will execute. The first section creates a display file on the board and pieces, the second section loads into address 16425 - Sinclair variable Nxtlin, the address of the next program line to be executed - this is the actual start of Rem 1, that is, address 16509.

The program then jumps to the Save routine in ROM; this will Save the display just produced and then auto-run at line 1. The whole of the above most be done in fast mode, or else the Save routine will not be executed.

Now for a brief description of the function of each of the machine-code segments: Kybd is a routine which sets up machine control of the keyboard such that only the eight key codes from code 29 and the eight key codes from code 38 are acceptable entries. Any other key depression is ignored. The subroutine TKP just scans the keyboard waiting for an appropriate key to be depressed. The alpha- numeric entry is then translated to a board address.

STR: this routine takes the board address and determines whether the contents are: different from the current mover colour; empty; the board surround or the same colour as the current mover.

Piece: this sets up pointers to possible move tables and number of steps and directions. Move produces a list of all legal moves available to the piece under consideration.

Pawn produces a list of all possible legal moves including initial double moves. Check locates current mover's Kings and stores the position in the attack register. Square Attack determines whether the opposition can attack the square in the attack register.

Score provides a move score based on the following: first, the "To" position results in taking of a piece. Second, the "From" position is attacked. Third, the "To" position is attacked. Fourth, "To" enables the computer to obtain a check and finally the "From" position is defended.

The current move score is then compared with the previous best and if this is superior, the move is saved as the best so far.

Shift moves the current move list to a safe position whilst Check is being evaluated, and then recovers the move list on completion. It is also used to shift the best move so far up into the move list.

PSC gives a score to a chess piece Q(5), R(4), B(3), N(2), P(1).

MPScan scans the board for computer pieces and, using move and score, determines all legal moves and saves the best. INC determines whether a square is being attacked.

Driver Main control logic, uses all the other subroutines to provide program control.

TestList: tests to see if there are any moves in the move list. AddList: adds to the current legal move list another entry on the end.

The program is copyright. You can produce a copy of the program from the listings for your own use, but you should not copy the listings or parts thereof and offer for sale. []