This is an article from the February 1983 issue of `Your Computer'. I apologise for the excessive width: I have attempted to recreate the original layout, which is printed on a double page and a single page. If you like you can just read the text of the article. I'm afraid the article was scanned without permission; if the copyright owner objects then I will remove this page.

The listings are shown in image form because my OCR has difficulty in decoding them. You can download the large version of each image if you like and attempt to OCR them yourself. My attempt didn't produce anything reasonable for the ZX Printer output on the first page, but I made a reasonable attempt for the assembler listings on the second page which you can look at. I corrected by hand the short assembler listings on the first page so they should be OK.

The text in blue within the article was not originally present but was pencilled in - I presume from a correction printed in the next issue. Similarly I also had pencilled in a correction to the numeric listing: it had under the caption `Figure 2' the address 16514 and the numbers 118 118 and had corrected 16514 at the top of the listing to 16516. This doesn't make sense to me so I have not shown the correction below. You will see that there are two or three corrections written on image of the assembler listing. One that hasn't been included is that the KYBD routine appears to be referred to as KT in other parts of the listing.

--imc.


FULL ZX-81 CHESS IN 1K

 

Figure 3. Rem display.
4 REM         DISPLAY

17287         LD HL NN   33 67 67    (17219)
              LD DE NN   17 0 72     (18432)
              LD BC NN   1 205 0
              LDIR       237 176
              RET        201
17219  18432  128 8 169 183 173 118
              29 8 55 51 39 48 54 39 51 55 118
17236         30 8 53 53 53 53 53 53 53 53 118
              31 8 0 128 0 128 0 128 0 128 118
17258         32 8 128 0 128 0 128 0 128 0 118
              33 8 0 128 0 128 0 128 0 128 118
17280         34 8 128 0 128 0 128 0 128 0 118
              35 8 181 181 181 181 181 181 181
              181 118
17302         36 8 183 179 167 176 182 167 179
              183 118
              8 8 45 44 43 42 41 40 39 38 118
17324         8 8 8 8 8
17329  18542  LD BC NN   1 0 4
              CALL NN    205 245 8     (2293)
              LD HL NN   33 0 72
              LD B N     6 110
              PUSH BC    197
              PUSH HL    229
              LD A (HL)  126
              RST 16     215
              POP HL     225
              POP BC     193
              INC HL     35
              DJNZ       16 247
17349  18562

              LD HL NN   33 125 64     16509
              LD (NN) HL 34 41 64      16425
              JP NN      195 7 3
© Copyright David Horne 1983
Figure 4. Code map. See also page 102.
ALIST
17837  LD HL NN   33 70 64
(141)  INC (HL)   52
( 66)  LD A (HL)  126
       ADD L      133
       LD L A     111
       LD (HL) C  113
       RET        201
CHGMV
17143  LD HL NN   33 55 67
(247)  LD A (HL)  126
( 66)  ADD N      198 128
       LD(HL) A   119
       RET        201
PMOVE
17151  LD HL (NN) 42 7 64
(255)  LD A (DE)  26
( 66)  LD C A     79
       LD A (HL)  126
       LD(HL) N   54 0
       LD(DE) A   18
       LD B A     71
       RET        201
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
[illustration]
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:
Line
Function
1
A Rem statement. From address 16514 to
17186
2
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.
3
A Rand Usr statement. To call the
machine-code routine.
4
A Rem statement. From address 17207 to
17362 to create the display and move
itself above RAMtop.
10
onwards. Your favourite machine-code
loader or this one.
10 FOR A = 16514 to 18000
11 SCROLL
12 PRINT A; TAB 8;
13 INPUT B
14 POKEA, B
15 PRINT PEEK A
NEXT A.
    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
PRINT USR 1040
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
In the last part of this
series, David Horne
creates a new game by
making enough space for a
playing routine.

Figure 1. Basic program listing.
[LIST]
[lots of numbers]
Figure 2. Machine-code listing
[more numbers]
 
[more numbers]
© Copyright David Horne 1983
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
(continued on next page)


(continued from previous page)
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. []
Figure 1. Code map continued from page 100.
Z80 instructions
Z80 instructions