Friday, 4 August 2023

"Fox and Geese" by Input Magazine (1984)

I came across a very interesting article in "Input Magazine" with a simple AI BASIC program. "Input" was an early 1980s computer publication by Marshall Cavendish.  It was dedicated to teaching the basics of computer programming. Every issue included both BASIC and machine language programs with detailed explanations of the listings. The magazine also included very engaging artwork in the form of playful cartoon characters and scenes illustrating aspects of the programs.

In order to illustrate simple AI techniques like the Alpha-Beta algorithm the authors of the magazine chose a simple board game to automate (rather than a complex game like chess) called "Fox and Geese."  In his comment on my Youtube video about the game @me_fault provided a nice little synopsis of the game:

Only the green squares are used. The four Geese on the green squares at one edge; the fox on a green on the opposite edge. The objective of the Fox is to cross from one side of the board to the other; the Geese's objective is to prevent it from doing so. The fox is trapped when it can no longer move to a vacant square. 

The Geese move diagonally forward one square (only one goose per turn); the fox diagonally forward or backward any number of greens. There is no jumping, occupying the same green, taxes or removal of animals.

Here is a graphic of the game to illustrate the elements of @me_fault's description:


The geese are the yellow objects at the bottom, the fox is the red object in the top half of the board. 

As was normal for "Input" magazine, listings were provided for Commodore 64, Sinclair Spectrum, Coco/Dragon 32 and Acorn Electron. The Coco version illustrated the game board (standard 8X8 chess board) using hires graphics. The scan of the magazine was very good, so the text capture required (relatively speaking) much less editing to get it into an operating condition. The latest version of the VCC Coco emulator includes a feature to paste text directly into the emulator and BASIC.  Unfortunately, it randomly drops lines during the pasting process, so I had to go back to my utilities that transfer Windows .txt files to virtual disks to get a clean transfer.  Once that initial task was done, I could use the paste function to input lines fixed in the Windows file into the emulator.

I had a hard time getting the program running because there were some errors in the magazine listing.  The most vexing was a missing minus sign in the following line:

2026 BX=B(31)*2-B(24):E=1E30:H=1E30

Which should have read

2026 BX=B(31)*2-B(24):E=1E30:H=-1E30

I only spotted this one because I gave up on getting the program running and had started working on the Commodore 64 source code to try to convert it to an MC-10 version using lowres graphics. Since I had combed through the Coco code so many times looking for transcription errors, when I saw the minus sign on the H variable I remembered that the Coco code lacked that.  I think I noticed this because it seemed odd to assign two variables (probably constants) with the exact same value. So when I saw the  C64 code with two values that were different, it triggered a recognition.  When I fixed this, the Coco version finally worked.

Another problem I ran across involved some DEFN mathematical functions (there are a bunch). The program kept throwing errors when two of these functions were called.  It turned out that this was because there were some extra brackets in one of the formulas and a misplaced asterisk in another:

2140 DEFFNX(B)=(B=G(1) OR (B=G(2)OR (B=G(3) OR B=G(4))

This should have read:

2140 DEFFNX(B)=(B=G(1)ORB=G(2)ORB=G(3)ORB=G(4))

And:

2150 DEFFNXX(B)=-((7ANDB) <4 * )(28 + 40*(3ANDB))-((7ANDB) > 3)*(128-40*(3ANDB))

which should have read:

2150 DEFFNW(B)=-((7ANDB)<4)*(28+40*(3ANDB))-((7ANDB)>3)*(128-40*(3ANDB))

It's difficult to understand how these errors could have crept into the listing. They are quite catastrophic. I fixed the DEFfunction errors first, because they could be spotted just in terms of bad BASIC syntax. After I had fixed those the program would run, as long as the player only chose level 0 for the computer opponent for the geese or fox.  So the best I can imagine is that the Coco conversion was  based on one of the other versions and that perhaps the programmer only tested human player versus human player function.  Or perhaps the Dragon is somehow more tolerant of bracketing in its implementation of Color BASIC.

Another problem I ran into was that there are places where Color BASIC (on the Coco, not the MC-10) absolutely needs spaces in front of  certain commands for the parser to recognize those commands, which is a problem if they are preceded by an purely alpha variable name.  Otherwise, the parser can't spot the command (i.e. differentiate it from the alpha variable). This is the case for

AND
OR
THEN
TO (in an FOR/NEXT loop construct)

For example the following packed lines will not work

IFA=BTHEN
FORA=BTOC
IFA=BANDC=DTHEN

These need to be re-written as:

IFA=B  THEN
FORA=B  TOC
IFA=B  ANDC=D  THEN

Since the magazine used proportional spacing, some spaces were not always evident (to either a reader or the OCR software). So all such instances had to have spaces added.  At the same time I was able to eliminate unneeded spaces, which should help speed up program execution.

Another problem that I ran into was that the emulator kept running out of memory (OM ERROR).  I've run into this before, most commonly because a program assumes a tape based system.  When a Coco disk cartridge is added some memory is reserved for disk activities.  So programs made for a purely tape-based systems have slightly more space to work with.  This seemed to be the case here.  I was able to try to condense the program in various ways such as shortening messages and removing redundant code, such as consolidating multiple identical win messages into a single subroutine.

Finally, a graphic message is flashed when a player makes wrong moves or provides improper input.  In the original program this involved flashing a simple red box with a buzzer sound. The box was unclear (to me at least) in terms of the message it was meant to convey.  I chose to replace it with a simple flashing X with buzzer, which seemed clearer to me as an indicator of illegal input.  So I replaced:

5000 FORK=1TO14:PUT(200,5)-(210,15),SQ,PRESET:PLAY"T50AC":PUT(200,5)-(210,15),SQ,PSET:PLAY"DA":NEXT:RETURN

with the slightly longer (I felt it was worth the memory):

5000 FORK=1TO14:LINE(200,5)-(210,15),PSET:LINE(210,5)-(200,15),PSET:PLAY"T50AC":LINE(200,5)-(210,15),PRESET:LINE(210,5)-(200,15),PRESET:PLAY"DA":NEXT:RETURN

The source code can be found here:

https://github.com/jggames/trs80mc10/tree/master/quicktype/Artificial%20Intelligence/FoxGeese

The directory is in my MC-10 repository because, as I mentioned, I was going to try to make an MC-10 lowres conversion.  I might still try that.  But in the meantime the Coco version of the game can be played here:

https://archive.org/details/@james_gerrie

Feedback welcome.

No comments:

Post a Comment