GOBANG
by A.N. Morton
from ZX Computing Dec'84/Jan'85

A brilliant version of Chinese Checkers from A N Morton
in W Germany - can YOU beat the computer?


If you enjoy playing board games and are fascinated by their
hidden logic, then this program is for you!

Gobang must be the Chinese forerunner of "noughts and
crosses", although somewhat more elaborate, as one would
expect from an oriental game.

The aim of the game is to make a line of five "stones"
diagonally, vertically, or horizontally. The black or white
stones are placed on the intersections of the board. The
rules of the game are fully explained in the BASIC program.
This program combines machine-code and BASIC, so that
computer responses are very rapid, considering the lengthy
calculations involved in generating a single move. The game
can be played at three levels of varying subtlety. Level
three should present a challenge to even the most
experienced of players.


Program structure

The REM statement houses the machine-code routines which are
explained in a following section.

Lines 10 to 31 print out the rules of the game, which are
displayed for as long as it takes to set up the variables
between lines 94 and 112, corresponding to the board
boundaries. The game level is established between lines 33
and 38, then lines 60 to 93 print a 9x9 board with
coordinates.

Lines 113 to 118 play the first move, (computer always
starts), by printing an "X" at board centre and noting the
move in the two "internal boards", A(z,k), and the machine
code matrix contained above RAMtop. The A(z,k) matrix is for
generating and checking "allowed moves" and the machine code
matrix is for identifying lines of stones.

Lines 120 to 134 then ask for the player's move, checking
whether it is valid, then noting the move on A(z,k) and in
the machine code matrix, where the value poked for a
computer's stone is 3, for a player's stone 7. Line 134
finally prints an "O" at the position chosen by the player.

From here on the work starts! Lines 137 to 143 route the
program to various subroutines depending on the number of
moves "M" played. Lines 200 to 308 produce the first three
opening moves. After the sixth move, the program is sent on
a long and tortuous journey through countless IF ... THEN
statements to finally end up at line 145 where the move is
then printed on screen.

After the ninth move and each move thereafter, lines 119 and
140 route the program to subroutines 600 and 641 to check
for a possible win, in which case the result of the game is
printed, together with the number of moves played. Lines 630
and 658 clear the machine code matrix, then an option for
replay is given. The rest of the program generates
appropriate moves after identification of 4, 3 or 2-stone
lines in the machine code matrix. Thereafter, the program
operates as described in table 1.


Table 1
=======

Lines 1000 to 1074  search for 4-stone lines (computer)
Lines 899 to 909    search for 4-stone lines (player)
Lines 910 to 937    generate winning or blocking move
Lines 800 to 867    search for 3-stone lines (level 3 computer)
Lines 700 to 731    search for 3-stone lines (level 3 player)
Lines 732 to 777    generate appropriate move for a "3-line"
Lines 480 to 508    search for 3-stone lines (level 2,3, computer)
Lines 400 to 407    search for 3-stone lines (level 2,3, player)
Lines 418 to 446    generate appropriate move
Lines 309 to 379    deal with the 2-stone lines


Variables used in program

(Z,K), (R,C), (J,I), (W,L), (F,E) and (S,T) represent board
coordinates (line,column) at different points of the program.

A(Z,K) represents the value assigned to a particular point
(Z,K) on the board.

  A =  3 for board boundaries
  A = -1 for computer stone "X"
  A =  2 for player stone "O"
  A =  0 for unoccupied positions

M represents number of moves played; H the game level; all
other variables are for calculations.


Machine code routines

(USR 16514, USR 16650, USR 16637)

The machine code routines are the "heart" of the program,
without which the game would be much too slow. Essentially,
these routines search through a block of memory, reserved
above RAMtop, for values corresponding to stone patterns,
which are POKEd into the memory matrix after each move. When
a "line" is identified, the routine returns to BASIC, where
the contents of reg-pair BC are decoded to enable the
appropriate move to be generated. The value of B gives the
position of the first stone in the line. The C value
corresponds to the type of line, vertical, horizontal or
diagonal. If no line is found, then BC = 0, so USR 16514 or
USR 16650 = 0. If the move generated by USR 16514 is
invalid, as can happen at board boundaries, then the program
is sent back to search for another line via USR 16650, after
the BC values have been re-poked into addresses 31985 to
31988. Addresses 32000 to 32002 are reserved for stone value
(3 or 7), line length and line sum. USR 16637 clears
addresses 32017 to 32145 corresponding to the machine code
matrix.


Getting it all together

Before doing anything, RAMtop must be lowered to address
31985, by typing in:

  POKE 16388,241
  POKE 16389,124
  NEW

To check that RAMtop is at 31985, enter:

  PRINT PEEK 16388 + 256 * PEEK 16389

The next step is to type in the hex loader program, making
sure that the REM statement contains at least 145 characters
and that the first address to be poked (if using another hex
loader), is 16514. Now RUN the loader program and type in
the hex code in the same order as listed in "Machine code
routines", pressing NEWLINE after each line of code entered.
After entering the last line of code, type in ST to stop
program. Now delete each line of the loader program leaving
the REM line, then type in POKE 16510,0 + NEWLINE to give an
unerasable REM line. As a precaution, SAVE the REM line on
tape. You are now ready to GO-BASIC!

With the REM line still in memory, type in the BASIC program
as listed. Type RUN 1076 + NEWLINE to SAVE the completed
program on tape. The program will then start, asking you for
a game! You might need a break after all the typing you will
have done up to this point! By entering S after the board
has been printed, you can opt out (apart from pulling out
the jack!). Remember to lower RAMtop to 31985 before loading
the program from tape.


--
Another Fine Product transcribed by:
Jim Grimwood (jimg@globalnet.co.uk), Weardale, England
--
