tkMOO-light
Experimental Chess Game Application
This is an attempt to understand what it takes to make the client
support a useful game of chess (short of remote controlling an
xchess
session!). The result is about 300 lines of
Tcl and 200 lines of MOO code. The Tcl code provides the interface
between tkMOO-light's XMCP/1.1 module and the stateless chess
client. The MOO code provides the only persistant state for the
game, the client's internal state is entirely reset with each
request from the server to update the board. This approach also
greatly simplifies the client code.
On a separate note, I thought it would be a nice idea to make the
interface as realistic as possible. Everyone knows the rules of
chess so there's no internal checking to see if the moves made are
legal, which greatly simplifies the code. In short, people can
cheat, so it's important that you keep an eye on what your opponent
is up to! I've stopped short of modelling each piece as a separate
object, ie: just pieces being moved around on a surface, which would
be the most realistic form of this program.
XMCM/1.1 message format
Information about the state of the chess board is passed from server
to client by a single authenticated XMCP/1.1 message
chess-board
containing the following fields:
- object
- the MOO object identifier of the chess board object
- board
- a 64 character string representing the state of each square
in the game board. Empty squares are represented by the character
'.'. Game pieces are represented by the following letters, uppercase
letters are white pieces, lowercase are black:
- R
- Rook, or Castle
- B
- Bishop
- N
- Knight
- K
- King
- Q
- Queen
- P
- Pawn
- turn
- a boolean value. 1 means it's your go next, 0 means it's your
opponent's move. The client can use this information to update
the status message at the bottom of the chessboard window and can
can also use the information to prevent players moving their pieces
out of turn.
- colour
- a string containing the value
white
or
black
indicating the colour of the player's pieces.
The client uses this information to decide how to orient the contents
of the board
field when rendering the chessboard window
and can also use the information to prevent players moving each
others pieces.
- sequence
- an integer incremented by the server after each move has been
made. Under certain conditions it's possible for the client and
server to get out of sync. When the client moves a piece it sends
back the sequence number to the server. The server checks the
sequence number and if it matches it's own copy then executes the
move and updates the sequence number. If the sequence numbers
don't match then the server warns the player and sends a fresh copy
of the game's state, another
chess-board
message,
which the client then uses to reinitialise itself.
In this example, the fields of the message appear on separate lines
for the sake of clarity. In real life this message would be a
single line of text.
$#$chess-board 29360152
object: #269
board: RP....prNB.P..pn.P....pbKP....pkQP....pqBPN...p..P...p.nRP...bpr
turn: 0
colour: white
sequence: 5
Server-side support
The chess widget relies upon a MOO Chess Board object. The source code for such an object is available.
Possible Improvements
It'd be nice to have an undo feature, and be able to play games
from a preloaded starting state, rather then just a regular game.