MATEY.TXT       9-Aug-2004

Matey is a relatively simple chess-playing program. It's intended more as
an XPL0 programming example than as a strong competitor. "Matey" is more
of a "playmate-y", or "stalemate-y", rather than a "checkmate-y". Despite
its limitations, however, it can beat most folks.

It's assumed that you have a pretty good idea of how to play chess. Matey
helps to some extent by preventing illegal moves.


RUNNING THE PROGRAM

Matey begins by asking if you would like to go first, playing the white
pieces. Press Y for yes or N for no, followed by the Enter key. The
default is Y, so merely pressing the Enter key is the same as selecting
"yes". Matey then asks if it should go second. This may seem silly, but
the two questions give these possibilities:

        You first?      Me second?      White   Black
        ----------      ----------      -----   -----
            Y               Y           Person  Matey       (default)
            N               N           Matey   Person
            Y               N           Person  Person
            N               Y           Matey   Matey   

The third question sets the skill level, which determines how much Matey
thinks about its moves. Level 3 is a good place to start. (Type "3"
followed by the Enter key.) Levels 2 through 5 are the recommended range,
but 1 through 7 are accepted. Level 1 is so dumb that it's only useful
for debugging, and level 7 is so slow (hours per move) that it's only
useful for extreme situations, such as correspondence games.

The mouse is used to make a move. Click on the piece you want to move
then click on the square to move it to. A dragging motion also works.

There are a row of soft buttons at the bottom of the screen.

Clicking the mouse on the [Exit] button terminates the program. The Esc
key also terminates, and it works even while the computer is thinking
about its move. (If Level is set above 3, it may take several seconds to
respond.)

The [Undo] button undoes the last pair of moves (black's and white's).
Pressing [Undo] a second time, undoes the undo. In other words, it
restores the original positions.

The [Info] button turns on and off the display of the computer's possible
moves and their values. It's normally off. The current value is displayed
above the best value. A pawn is worth 100 points. (The Pause key may be
helpful.)

The [Sound] button turns the sound off and on. (Handy when sneaking a
game at work.)

The [Reset] button starts a new game.

To castle, move your king two squares either left or right. The rook will
move automatically. (Matey doesn't enforce all of the restrictions on the
castling move, such as not having previously moved the king or rook, or
not being in check for any of the intermediate positions).

To capture en passant, move your pawn diagonally, as though the
opponent's pawn only advanced a single square. (En passant is an unusual
move: If an opponent's pawn advances two squares instead of one to avoid
being captured by your pawn then you can, on the next move only, capture
the opponent's pawn as though it moved one square.)

The moves listed in the window at the left of the screen are also
recorded in a file called GAME.TXT. If you want to save this file, rename
it to something else, otherwise it will be overwritten by the next game.
The numbers in parenthesis in the file are the values of Matey's moves.


LIMITATIONS

Matey doesn't include castling in its search for a move. A human player
can castle, but Matey can't. (You might feel it's unfair to take
advantage of something that Matey can't do, but there are lots of things
that Matey can't do. It has its own peculiar way of playing the game.)

Pawns that reach the 8th rank are always converted to queens; there is no
option for selecting another piece, such as a knight.

Matey continues playing even after a king is captured. It does "Resign"
if it detects it's about to be checkmated, but it doesn't quit moving its
pieces. (When it plays against itself, it restarts the program when one
side resigns.)

Matey doesn't understand all the rules involving stalemates. It does
prevent you from moving into check, and it will declare a stalemate if
it is not in check and its only move is into check. However it's up to
you to declare a draw if a position has been repeated three times or if
50 moves have gone by without either side capturing a piece or moving a
pawn.

When Matey is playing against itself, the mouse is not active, thus the
[Exit] button cannot be clicked (hit Esc instead). This mode is useful
for debugging, or as a demo or teaser.


WINDOWS PROBLEMS

Windows 98 (and 95) may cause several problems. Windows XP, and of course
pure DOS, work just fine.

If you get a message that a file is missing, you might be running from
WinZip. If so, use CheckOut to run Matey from a folder that includes all
of its files, especially MATEY.BMP.

Windows 98 can sometimes run DOS applications at an unbelievably slow
speed by giving them only a few percent of the available CPU cycles.
There are many factors that affect speed, but if Matey at Level 4 on a
266 MHz Pentium is taking more than a minute per move then the reason
is probably Windows. This can be fixed either by booting into pure DOS
(not a DOS box under Windows), or by adjusting the "Idle Sensitivity" in
Windows 98 as follows:

   Run Matey in a window (Alt+Enter flips between full screen and a window).
   Click on the MSDOS icon in the upper-left corner of the window.
   Select Properties.
   Select Misc.
   Move the Idle sensitivity slider all the way to Low.
   Click OK.
   Hit Alt+Enter to get back to a full screen.

Matey should be run in full-screen mode for maximum speed and the proper
display.


HOW IT WORKS

Matey uses a recursive routine to search a large tree of possible moves.

In the 1950's it was thought that a computer playing grand-master-level
chess was just around the corner. However it took much longer than
expected, mostly because of the incredibly huge size of the search tree
required. There are about 10^120 moves in an average game, but only
3x10^18 nanoseconds in a century in which to search them.

Matey limits the size of the search tree by its Level setting. This is
the normal level, or depth, that the tree is generated. For example
setting this to 4, searches four half moves (equal to two full moves).
Assuming 30 possible moves per turn, this generates a tree with 837930
nodes (30^4 + 30^3 + 30^2 + 30).

Each node in the tree is assigned a value called its "static value".
These values are determined by captures, advancement across the board,
and the number and location of squares to which pieces can move (their
mobility). A penalty is assessed for moving the queen or king too early
in the game.

A depth-first tree search is used to generate nodes, thus only "Level"
number of nodes are saved in memory at one time. The state of each node
is automatically saved in the local variables of the search routine
(TryMove) as it recurses.

When the search reaches a terminal node, the minimax algorithm is
applied. This algorithm assumes that both the computer and the player
select the best move available. The best move for a terminal node is the
one with the highest static value. Thus this value is returned when the
search routine moves back up a level, and it's called the "BackupValue".

What's best for the player is worst for the computer, and vice versa, so
the signs of the backupValues alternate at each level in the tree. These
backupValues are added together, one for each level. The computer makes
the move that corresponds to the branch at level 1 that has the greatest
sum.

The easiest way to understand all this is to look at the accompanying
Tic-Tac-Toe program, TTT.XPL. Incidentally, the keypad is used to specify
where to place your X.

Matey has a few more complications. If a capture occurs at what would
normally be the terminal node, the search is extended another level. If
there are additional captures (exchanges), the extensions continue to the
maximum depths shown here:

        Level   Max. Extension   Max. Total Depth
          1      +      0       =        1
          2      +      1       =        3
          3      +      1       =        4
          4      +      2       =        6
          5      +      2       =        7
          6      +      3       =        9
          7      +      3       =       10

Another complication is the use of alpha-beta pruning. This dramatically
reduces the number of branches and nodes that must be searched. It's only
partially effective in Matey however because the most promising moves are
not necessarily searched first. (The moves are not sorted.)

Matey actually uses a minor variation of minimax called "negamax". The
accompanying files MINIMAX.TXT and NAGAMAX.TXT show the relationship and
provide an example of how alpha-beta pruning works.

It's left to you, the reader, to determine just how intelligent this tree
search makes the computer. At its level 4 it plays with an Elo rating of
about 1100 (as determined by competing against Chessmaster 9000). In the
case of Tic-Tac-Toe the tree is small enough that it can be completely
searched, thus the TTT program cannot be beaten.

If you're looking for a stronger chess player, an excellent example (that
includes source code) is Tom Kerrigan's Simple Chess Program (TSCP). It's
available on the Internet at: http://home.comcast.net/~tckerrigan/ (I wish
I had found this sooner; I would have done things a lot differently.)

Perhaps you'll be motivated to correct some of Matey's limitations or to
add other improvements. If so, I'll be very interested to hear about it.
In fact any comments regarding Matey or XPL0 would be appreciated.

Loren Blaney
loren_blaney@idcomm.com
