\documentclass{article}
%%toolkit zrm
\usepackage[units]{zeta}
\usepackage{epic}
\usepackage{eepic}
\usepackage{url}
\usepackage[numbers]{natbib}

% the tables are wide
\topmargin=0pt
\oddsidemargin=-0.25in
\evensidemargin=-0.25in
\textwidth=6.5in
\textheight=8.5in

% table of requests
\newenvironment{reqtab}[2]{%
\begin{table}%
\centering
\caption{#2}%
\label{table-#1}%
\begin{tabular}{rll}%
\multicolumn{1}{c}{\bf name}&%
\multicolumn{1}{c}{\bf request}&%
\multicolumn{1}{c}{\bf meaning}\\\hline
}{\end{tabular}%
\end{table}}

% table of responses
\newenvironment{resptab}[2]{
\begin{table}%
\centering
\caption{#2}%
\label{table-#1}%
\begin{tabular}{rll}%
\multicolumn{1}{c}{\bf name}&%
\multicolumn{1}{c}{\bf response}&%
\multicolumn{1}{c}{\bf meaning}\\\hline
}{\end{tabular}%
\end{table}}
% tokens
\newcommand{\stok}[1]{{$\langle${\em #1}$\rangle$}}
\newcommand{\bfid}[1]{{\bf #1}}
\newcommand{\itid}[1]{{\it #1}}

\title{A Networked {\em Lines Of Action} Referee: Specification}
\author{Bart Massey}

\begin{document}
\maketitle

{\em Lines Of Action} (LOA) is a fairly obscure but
fascinating game of skill. It is played on an ordinary
checkerboard, and has something of the feel of Othello or
Go.  In this document, we will describe a networked server
to which human and computer LOA players can connect to play
the game in a refereed fashion.

\section{The Game Of {\em Lines Of Action}}

The rules of LOA are complicated and have many variants.
This discussion is based partly on Dyer's account on his
excellent LOA webpage~\cite{dyer-loa-web}, and partly on the
Fifth Computer Olympiad (2000)~\cite{olympiad-2000}
rules~\cite{olympiad-loa-rules}, the latter being normative
in the case of conflicts.

LOA is a two player game, played by players conventionally
designated as $white$ and $black$.
\begin{zed}
PLAYER ::= black | white
\end{zed}
\begin{axdef}
  opponent : PLAYER \bij PLAYER
\where
  opponent~white = black \\
  opponent~black = white
\end{axdef}

The board,
shown in figure \ref{fig-board}, is an ordinary
checkerboard (whose squares are
conventionally designated using standard algebraic
notation).
\begin{zed}
DIGIT == 1 \upto 8 \\
% LETTER ::= a | b | c | d | e | f | g | h \\
% LETTERS == \langle a, b, c, d, e, f, g, h \rangle \\
% NAME == LETTER \cross DIGIT \\
SQUARE == DIGIT \cross DIGIT
\end{zed}
%\begin{axdef}
%  square\_name : SQUARE \bij NAME
%\where
%  \forall x, y : DIGIT @ square\_name~(x, y) = (LETTERS(x), y)
%\end{axdef}
At any given point in the game, the board position can
be given by noting whether each square is blank or
contains a colored checker.
\begin{zed}
SQUAREVAL ::= checker \ldata PLAYER \rdata | blank \\
BOARD == SQUARE \fun SQUAREVAL
\end{zed}
\begin{schema}{LOAPosition}
  board : BOARD \\
  to\_move : PLAYER
\end{schema}
As shown in Figure~\ref{fig-board}, each player starts with
12 checkers, placed on the six central edge squares of
opposing sides of the board.
\begin{zed}
  CDIGIT == 2 \upto 7
\end{zed}
\begin{schema}{InitLOAPosition}
  LOAPosition '
\where
  board' = (SQUARE \cross \{ blank \} ) \oplus \\
    \t1 (((\{ 1 \} \cross CDIGIT) \cross \{ checker~white \}) \cup \\
    \t1 ((\{ 8 \} \cross CDIGIT) \cross \{ checker~white \}) \cup \\
    \t1 ((CDIGIT \cross \{ 1 \} ) \cross \{ checker~black \}) \cup \\
    \t1 ((CDIGIT \cross \{ 8 \} ) \cross \{ checker~black \}))
\also
  to\_move' = black
\end{schema}

\begin{figure}
\begin{center}
\input{loa-board.eepic}
\end{center}
\caption{LOA Board In Initial Configuration}
\label{fig-board}
\end{figure}


The players alternate in moving one of their own checkers
a certain distance along its row, column, or diagonal axis
(``line of action'').
\begin{zed}
  DIST == -8 \upto 8 \\
  UNIT == -1 \upto 1 \\
  UVEC == (UNIT \cross UNIT) \setminus \{(0, 0)\} \\
  AXIS == SQUARE \fun DIST \pfun SQUARE
\end{zed}
\begin{axdef}
  line : UVEC \fun AXIS \\
  lines : \power AXIS
\where
  \forall ux, uy : UNIT | (ux, uy) \in UVEC @ \\
    \t1 \forall x, y : DIGIT;  @ \\
      \t2 \forall d : DIST | (x + d * ux, y + d * uy) \in SQUARE  @ \\
        \t3 line~(ux, uy)~(x, y)~d = (x + d, y)
\also
  lines = line\limg UVEC \rimg
\end{axdef}
A move is illegal unless the number of pieces (including the
moving piece) lying along the move axis is equal to the
distance moved by the piece.  It is also illegal if there is
an intervening piece of the other color in a square between
the source and destination of the move, or the destination
square contains a piece of the same color.  (If the
destination square contains a piece of opposite color it is
captured and removed from the game.)
\begin{zed}
  MOVE == SQUARE \cross SQUARE
\end{zed}
%\begin{axdef}
%  drange : DIST \fun \power DIST
%\where
%  \forall d : DIST @ drange~d = \\
%    \t1 \IF~d < 0~\THEN~(d+1) \upto -1 \\
%    \t1 \ELSE~1 \upto (d-1)
%\end{axdef}
\begin{schema}{LOAAction}
  LOAPosition \\
  naxis : AXIS \fun SQUARE \fun DIST \\
  target : AXIS \fun MOVE \\
  moves : \power MOVE \\
  make\_move : MOVE \fun BOARD
\where
  \forall f : AXIS @ \\
    \t1 naxis~f = (\lambda x : SQUARE @ \\
      \t2 \#(\dom(board \nrres \{blank\})
          \cap ((f~x)\limg DIST \rimg)))
\also
  \forall f : AXIS; x : SQUARE; d : DIST | \\
    \t1 d = naxis~f~x \land \\
    \t1 board~x = checker~to\_move \land \\
    \t1 board~(f~x~d) \neq checker~to\_move \land \\
    \t1 checker~(opponent~to\_move) \notin
	board\limg (f~x)\limg 1\upto(d-1)\rimg\rimg @ \\
    \t1 target~f = (x, f~x~d)
\also
  moves = target\limg lines \rimg
\also
  \forall s, d : SQUARE @ \\
    \t1 make\_move~(s, d) =
        board \oplus \{s \mapsto blank\} \oplus \{d \mapsto checker(to\_move)\}
\end{schema}

The goal of the game is to make a move that connects all of
one's pieces.  A player's pieces are connected if there is a
path from any piece to any other piece via adjacent
horizontal, vertical, or diagonal squares.
\begin{axdef}
  adjacent : SQUARE \rel SQUARE \\
  connections : \power \power SQUARE \\
  connected : PLAYER \rel BOARD
\where
  adjacent = \{~ x1, y1, x2, y2 : DIGIT | \\
    \t1 \abs(x2 - x1) \leq 1  \land \abs(y2 - y1) \leq 1 @ \\
    \t1 (x1, y1) \mapsto (x2, y2) ~\}
\also
  connections = \{~ p : \power SQUARE | \\
    \t1 \forall p' : \power p \setminus \{\emptyset\} \setminus \{p\} @
        (p' \dres adjacent \rres (p \setminus p')) \neq \emptyset ~\}
\also
  \forall p : PLAYER; b : BOARD @ \\
    \t1 (p, b) \in connected \iff
        \dom(b \rres \{checker(p)\}) \in connections
\end{axdef}

The Computer Olympiad rules list a
number of conditions which can
terminate the game.  Most of these have other variants which are
commonly played, but which will not be considered here.
Specifically, the Computer Olympiad rules state that:
\begin{enumerate}
\item\label{connection} {\em The game ends when one player
(or both) is connected}, and
  \begin{enumerate}
  \item\label{move-connects} {\em If just one player
  is connected, that player is the winner.}
 \item\label{both-connect} {\em If both players are
  connected, the player who moved last wins.}
  \end{enumerate}
Rules~\ref{move-connects} and~\ref{both-connect} can be
understood together in this context as ``The winner is the
first player to be connected at either the start or end of
their turn.''
\item\label{repeated-position} {\em Repeated positions with the
same player to move are drawn.}  While the language is
somewhat ambiguous, it is assumed that this means any
repetition, not just immediate repetition.
\item\label{no-move} {\em If a player has no move, that
player loses.}  It is also noted that this exception was
added after the fact, which suggests an advantage of formal
characterization and analysis of tournament game rules.
\end{enumerate}

There are a number of possible outcomes of a player's
turn.
\begin{zed}
  RESULT ::= win \ldata PLAYER \rdata | draw | not\_done | illegal\_move
\end{zed}
Because of rule~\ref{repeated-position}, it is necessary to
keep track of all the positions seen in the game.
A move in the game consists of altering the board and
recording the resulting board position.
\begin{schema}{LOAHistory}
  positions : \power LOAPosition
\end{schema}
\begin{schema}{LOARecordHistory}
  LOAPosition \\
  \Delta LOAHistory
\where
  positions' = positions \cup \{\theta LOAPosition\}
\end{schema}

Conditions are checked for the player on move at the start of the turn.
\begin{enumerate}
\item\label{check-win} If the player is connected,
they win.
\item\label{check-draw} If the current position
is a repeat of a previously seen position, they draw.
\item\label{check-lose} If there is no move available, they lose.
\end{enumerate}
(Note that condition~\ref{check-draw} is
mutually exclusive with conditions~\ref{check-win}
and~\ref{check-lose}, and the wording of the
Computer Olympiad rules implies
that condition~\ref{check-win} is supposed
to take precedence over condition~\ref{check-lose}.
So all of the special cases are disjoint.)
\begin{schema}{LOAPrecheck}
  LOAAction \\
  LOAHistory \\
  precheck : RESULT
\where
  precheck = \\
    \t1 \IF~(to\_move, board) \in connected \THEN~win(to\_move) \\
    \t1 \ELSE~\IF~\theta LOAPosition \in positions \THEN~draw \\
    \t1 \ELSE~\IF~moves = \emptyset \THEN~win(opponent~to\_move) \\
    \t1 \ELSE~not\_done
\end{schema}
Otherwise, some normal move must be made.
In this case, the result of making the move is
a win when the position is now connected for the
player to move.  Otherwise, the game continues.
\begin{schema}{LOATurn}
  LOAPrecheck \\
  \Delta LOAPosition \\
  LOARecordHistory \\
  move? : MOVE \\
  result! : RESULT
\where
  to\_move' = opponent~to\_move
\also
  precheck \neq not\_done \implies \\
    \t1 result! = precheck
\also
  precheck = not\_done \land move? \in moves \implies \\
    \t1 board' = make\_move~move? \land \\
    \t1 result! = \\
      \t2 \IF~(to\_move, board') \in connected \THEN~win(to\_move) \\
      \t2 \ELSE~not\_done
\also
  precheck = not\_done \land move? \notin moves \implies \\
    \t1 result! = illegal\_move
\end{schema}
\begin{schema}{InitLOATurn}
  InitLOAPosition \\
  LOAHistory '
\where
  positions' = \{~ \theta LOAPosition ' ~\}
\end{schema}

\clearpage
\section{Server}

The {\em Lines Of Action} server listens on a port in the
range $29057\ldots29067$ for a
connection.\footnote{All numbers in this section
will be base 10 (decimal) unless otherwise stated.}
All input to the server will be in the form of
ASCII text lines, terminated with a CR character (ASCII code
13).  All server responses will be in the form of ASCII text
lines, terminated with a CR and then an LF character (ASCII
code 10).  Responses will begin with a 3-digit numerical
code, and be followed by whitespace and a (non-standard)
explanatory text message.  Requests and responses not currently
implemented by the server will have their identifier
in italics: those implemented will have boldface identifiers.

Any number of observers may connect to the server, as well
as the two players.
The server will always be in a state determined by the input
it has seen.  This state will determine which messages it
will accept, and which responses it will return.  The server
may be in different states for different connections: it
must synchronize the connections at key points.
\begin{zed}
  STATE ::= initial | seated | playing | done \\
  ENTITY ::= player \ldata PLAYER \rdata | observer \ldata \nat_1 \rdata \\
  observer \in \seq ENTITY
\end{zed}
\begin{axdef}
  cstate : ENTITY \pfun STATE
\end{axdef}


Upon connection to the server, an entity will
receive a greeting in the form indicated by Table~\ref{table-greeting}.
The version number is a pair of integers separated by a
decimal point.  This document describes version 0.9.

\begin{resptab}{greeting}{Greeting}
\bfid{greeting} & 000 LOA \stok{version-number} &Greeting message
\end{resptab}


The initial message sent to the server must be as
shown in Table~\ref{table-initreq}.
Responses are shown in Table~\ref{table-initresp}.
The \stok{optional-name} is an optional
double-quoted string (with the convention that two
consecutive double-quotes "" inside the string escape to
a single double-quote ") of up to 31 characters used to
identify the entity.  The version number is as above,
and is used to identify the client version.  The client
version must be no greater (under the usual ordering)
than the server version.
If both players indicate ``player ?'', the server will
randomly select a white and black player.

\begin{reqtab}{initreq}{Initial Requests}
\bfid{want\_white} & \stok{version} player white \stok{optional-name}
  & Will play white \\
\bfid{want\_black} & \stok{version} player black \stok{optional-name}
  & Will play black \\
\itid{want\_side} & \stok{version} player ? \stok{optional-name}
  & Will play either \\
\bfid{want\_observe} & \stok{version} observer \stok{optional-name}
  & Will observe
\end{reqtab}

\begin{resptab}{initresp}{Initial Responses}
\bfid{seat\_granted}	& 100	& Request accepted \\
\bfid{seat\_granted\_tc}& 101 \stok{secs}
			      \stok{opp-secs} &
				  Request accepted with time
			          controls \\
			& 19x	& Request not accepted \\
\bfid{seat\_taken}	& 191	& Other player holds requested side \\
\bfid{seat\_full}	& 192	& There are already two players \\
\bfid{seat\_private}	& 193	& Cannot observe \\
\bfid{seat\_illegal}    & 198	& Illegal version number \\
\bfid{seat\_garbled}    & 199	& Request not understood
\end{resptab}

Once both a white player and a black player have
connected, the setup phase will be over.  The server may
indicate to each entity the other entities involved, by
sending messages as shown in
Table~\ref{table-configresp}. \stok{name} and \stok{optional-name} are
double-quoted strings as described below. \stok{number} is a
decimal number. (All entities should be prepared to deal
with numbers up to 3 decimal digits, and to discard an
arbitrary number).

\begin{resptab}{configresp}{Configuration Messages}
\itid{config\_white}	    & 341 \stok{name}	  & White player is \stok{name} \\
\itid{config\_black}	    & 342 \stok{name}	  & Black player is \stok{name} \\
\itid{config\_observer}  & 343 \stok{number} \stok{name} & Observer \stok{number} is \stok{name} \\
\itid{config\_nobserver} & 344 \stok{number}	  & There are \stok{number} observers
\end{resptab}

The server will then signal the start of game by sending
a message to each connected entity, as shown
in Table~\ref{table-startresp}.

\begin{resptab}{startresp}{Starting Messages}
\bfid{role\_white}    & 351 & You will play white \\
\bfid{role\_black}    & 352 & You will play black \\
\bfid{role\_observer} & 353 & You will observe
\end{resptab}

After this, the server will accept moves from players
in alternation, of the form shown
in Table~\ref{table-moves},
where the \stok{move number} is a standard decimal number
indicating the ply of the move, the \stok{ellipses-if-white}
will be the string ... for a move by white and the
empty string for a move by black, and \stok{move} will be
a move in the notation described above.

\begin{reqtab}{moves}{Move Syntax}
\bfid{action\_move} & \stok{move number} \stok{ellipses-if-white} \stok{move}
  & Make a move
\end{reqtab}

Instead of a move, the following inputs may
also be 
accepted as shown in Table~\ref{table-altmovereq}.
Responses to actions are shown in Table~\ref{table-altmoveresp}.

\begin{reqtab}{altmovereq}{Alternatives To Moving}
\itid{action\_resign}    & resign & Player resigns \\
\itid{action\_draw\_req} & draw?  & Player offers a draw \\
\itid{action\_draw\_ack} & draw   & Player accepts a draw \\
\itid{action\_draw\_nak} & nodraw & Player refuses a draw
\end{reqtab}

\begin{resptab}{altmoveresp}{Responses To Actions}
                        & 20x & Action accepted \\
\bfid{result\_continue} & 200 & Continue playing \\
\bfid{result\_continue} & 207 \stok{secs} & Continue with time left \\
\bfid{result\_win}      & 201 & You win \\
\bfid{result\_lost}     & 202 & You lose \\
\bfid{result\_drawn}    & 203 & You draw \\
\itid{result\_resigned} & 204 & Resignation accepted \\
\itid{result\_drawreq}  & 205 & Received draw request \\
\itid{result\_drawnak}  & 206 & Received draw refusal \\
                        & 29x & Action not accepted \\
\bfid{result\_illegal}  & 291 & Illegal request \\
\bfid{result\_garbled}  & 299 & Request not understood
\end{resptab}

After each accepted action, a message will
be sent to each connected entity, as
shown in Table~\ref{table-statusmsgs}.
Upon termination of the game, the server will close all
connections.

\begin{resptab}{statusmsgs}{Status Messages}
& 31x			     & Game continues \\
\bfid{status\_moves\_black} & 311 \stok{move-number} \stok{move}     & Black move \\
\bfid{status\_moves\_white} & 312 \stok{move-number} ... \stok{move} & White move \\
\bfid{status\_moves\_black\_tc} & 313 \stok{move-number}
                                  \stok{move}
				  \stok{secs}
				& Black move and time\\
\bfid{status\_moves\_white\_tc} & 314 \stok{move-number}
			  ... \stok{move}
			  \stok{secs}
			& White move and time \\
& 32x, 36x			     & Game over \\
\bfid{status\_winsmove\_black} & 321 \stok{move-number} \stok{move}     & Black wins by move \\
\bfid{status\_losesmove\_black} & 322 \stok{move-number} \stok{move}     & Black loses by move \\
\bfid{status\_winsmove\_white} & 323 \stok{move-number} ... \stok{move} & White wins by move \\
\bfid{status\_losesmove\_white} & 324 \stok{move-number} ... \stok{move} & White loses by move \\
\bfid{status\_drawsmove\_black} & 325 \stok{move-number} \stok{move} & Drawn by Black move \\
\bfid{status\_drawsmove\_white} & 326 \stok{move-number} ... \stok{move} & Drawn by White move \\
\itid{status\_resigns\_white} & 327			     & Black wins by resignation \\
\itid{status\_resigns\_black} & 328			     & White wins by resignation \\
\itid{status\_draw\_agreed} & 329 & Draw accepted \\
\bfid{status\_flagfell\_white} & 361 & Black wins by White time expiring \\
\bfid{status\_flagfell\_black} & 362 & White wins by Black time expiring \\
$$ & 33x			     & Draw protocol \\
\itid{status\_reqsdraw\_black} & 331			     & Draw requested by Black \\
\itid{status\_reqsdraw\_white} & 332			     & Draw requested by White \\
\itid{status\_refsdraw\_black} & 333			     & Draw refused by Black \\
\itid{status\_refsdraw\_white} & 334			     & Draw refused by White \\
& 34x		             & (see above) \\
& 35x			     & (see above) \\
& 39x			     & Bad status \\
\itid{status\_disconnect\_black} & 391			     & Black disconnected \\
\itid{status\_disconnect\_white} & 392			     & White disconnected \\
\bfid{status\_garble} & 399			     & Unknown problem
\end{resptab}

If the participant is an observer, every status message will
be followed by two state display messages showing the
current state of the game, as in Table~\ref{table-sdispmsgs}.
The times will be in seconds, and the \stok{to-move} value
will be either ``b'', ``w'', or ``.'' indicating 
Black, White, or the game is over.  The \bfid{sdisp\_board}
message will be immediately followed by 8 lines of
8 printable characters indicating the board state.
Each character will be as above: ``b'', ``w'', or
``.'' indicating a blank square.

\begin{resptab}{sdispmsgs}{State Display Messages}
                    & 38x & state display \\
\bfid{sdisp\_status}    & 380 \stok{move-number}
			    \stok{to-move} & state \\
\bfid{sdisp\_status\_tc}    & 381 \stok{move-number}
			    \stok{time-b}
			    \stok{time-w}
			    \stok{to-move} & state and time \\
\bfid{sdisp\_board} & 382 & board
\end{resptab}

The draw protocol is as one would expect: a draw may be
offered by a player whose turn it is to move.  If it
is rejected by the other player, it may not be offered
again until the next move.

% Pending Issues:
%+ Multiple games on a single server. (i.e. sub-connection
%protocol)
%+ Partial board positions.  (e.g. for saving game status
%across disconnects.)
%+ 4-player game.
%+ Chat.
%+ Late observers.

\bibliographystyle{plainnat}
\bibliography{loa-server}

\end{document}
