\providecommand{\N}{\|N}
\providecommand{\Z}{\|Z}
## Impartial Games

An _impartial game_ is a two-player game in which players take turns to make
moves, and where the moves available from a given position don't depend on
whose turn it is.

A player _loses_ if they can't make a move on their turn
(i.e. a player wins if they move to a position from which no move is
possible).

In games which are not impartial, the two players take on different roles
(e.g. one controls white pieces and the other black), so the moves available
in a given position depend on whose turn it is. Before we treat these more
complicated games, we first consider the special case of impartial games.

Any nim position is an impartial game. Write $*n$ for the nim position with a
single heap of size $n$.

In particular, $*0$ is the "zero game", written 0: the player to move
immediately loses.

We consider a position in a game as a game in itself.
We can specify a game by giving the set of its _options_ - the games to which
the first player can move.
e.g. $0 = {}$, $*1 = \{0\}$, $*n=\{*0,*1,*2,...,*(n-1)\}$.

For impartial games $G$ and $H$, $G+H$ is the game where the two games are
played side-by-side, with a player on their turn getting to decide which of
the two to move in.

So e.g. $*2 + *2 + *3$ is the nim position with two heaps of size 2 and one of
size 3.

If $G = \{G_1,...,G_n\}$ and $H=\{H_1,...,H_n\}$, then
:   $G+H = \{G_1+H,...,G_n+H,G+H_1,...,G+H_n\}$.

Remark:
    ***) $G+H = H+G$
    ** $(G+H)+K = G+(H+K)$
    *e)*

We refer to the player who is to move first as "Player 1", and their opponent
as "Player 2". So after a move, Player 1 in the new game was Player 2 in the
original game, and vice versa.

The _finite_ (or _short_) games are defined recursively by:\
***) 0 is finite
**  if $G_1,...,G_n$ are finite, then $\{G_1,...,G_n\}$ is finite.
*e)*

Lemma:
    In any finite impartial game $G$, either Player 1 has a winning strategy,
    or Player 2 has.

    We say that $G$ is a "first/second-player win" accordingly,
    and that the _outcome_ of $G$ is a win for Player 1/2.

Proof:
    Suppose inductively that the lemma holds for all options of $G$.

    If some option of $G$ is a win for Player 2, then Player 1 can win in $G$
    by making that move.

    Else, Player 2 can win in $G$, 
    since they have a winning strategy in whatever game Player 1 moves to.

# Green Hackenbush

A green hackenbush game consists of some dots joined by lines, with some dots
"on the ground"; a move comprises deleting a line, and then deleting all lines
which are no longer connected to the ground.

Example:
    \begin{verbatim}
:::    *           *         *         *         *
:::    |           |         |         |         |
:::    |           |         |         |         |
:::    *           *         *         *         *         *    
:::   / \           \       /         / \       / \       / \   
:::  /   \           \     /         /   \     /   \     /   \  
::: *     * = { *     * , *     * , *     * , *     * , *     * }
:::  \   /       \   /     \   /         /     \         \   /  
:::   \ /         \ /       \ /         /       \         \ /   
:::____*____   ____*____ ____*____ ____*____ ____*____ ____*____
    \end{verbatim}

    \begin{verbatim}
:::    *             *                                    
:::    |             |                                    
:::    |             |                                    
:::    *             *           *                             
:::   / \             \         / \                            
:::  /   \             \       /   \                           
::: *     *  = {        *  ,  *     *  ,        *  ,           }
:::      /             /           /           /               
:::     /             /           /           /                
:::____*____     ____*____   ____*____   ____*____   ____*____ 
    \end{verbatim}

$P+Q =$ [picture where we draw $P$ and $Q$ side by side, with no connections
    between them]


# Equivalence of impartial games

Definition:
    Two games $G$ and $H$ are _equivalent_, $G == H$,
    if for any game $K$,
    $G+K$ has the same outcome as $H+K$.

Remark:
    ***) If $G == H$ then $G$ has the same outcome as $H$;
    **   $0+G == G$;
    **   if $G == H$ then $G+K == H+K$ for any $K$.
    *e)*

Lemma:
    $G == 0$ iff $G$ is a second-player win.
Proof:
    $==>$:
	If $G == 0$, then $G=G+0$ has the same outcome as $0+0=0$, which is a
	second-player win.

    $<==$:
	Suppose $G$ is a second player win, and $K$ is any game.
	We want to show $G+K$ has the same outcome as $0+K=K$.
	The following is a winning strategy in $G+K$ for whichever player
	has the winning strategy in $K$:\

	On our turn, play the next move in the winning strategy for $K$,
	unless our opponent just played in $G$ - then play the next move
	in the winning strategy for $G$ as Player 2. \qed

Example:
    $G+G == 0$

    Indeed, the second player wins by mirroring any move made in one copy of $G$
    in the other copy of $G$.

Lemma:
    $G+H == 0$ iff $G == H$
Proof:
    If $G==H$, then $G+H == G+G == 0$ by the above example.

    If $G+H == 0$, then
    :	$G = G + 0 == G+H+H == 0 + H = H$. \qed

Exercise:
    Confirm, by finding a winning strategy for Player 2 in the sum, that
    \begin{verbatim}
:::        *
:::        |
:::        |
:::        *
:::       / \      ==  *1
:::      /   \
:::     *     *
:::      \   /
:::       \ /
:::    ____*____
    \end{verbatim}

Remark:
    One easily checks that games with equivalent options are equivalent,
    i.e. if $G_i == G'_i$, then $\{G_1,...,G_n\} == \{G'_1,...,G'_n\}$.

From now on, we will just write $=$ in place of $==$ - we are only interested
in games up to equivalence.

Our winning strategy for nim tells us how to add nim heaps:\

Proposition:
    $*n + *m = *( n (+) m )$
Proof:
    $n (+) m (+) ( n (+) m ) = 0$,
    so as we proved when discussing Nim,
    $*n + *m + *( n (+) m )$ is a second-player win. \qed

It follows that every nim game is equivalent to a single heap, with size the
nim sum of the sizes of the heaps in the original game.

Example:
    \begin{verbatim}
:::        *      *
:::        |       \
:::        |        \
:::        *         *
:::       / \       /    =  *1 + *3  =  *(1 (+) 3)  =  *2
:::      /   \     /
:::     *     *   *
:::      \   /    |
:::       \ /     |
:::    ____*______*_
    \end{verbatim}
    
# Impartial games are secret heaps

Now we show that not only every Nim game, but every impartial game is
equivalent to a Nim heap.

Theorem [Sprague-Grundy]:
    Any finite impartial game $G$ is equivalent to a nim heap;
    $G = *n$ where $n$ is the smallest non-negative integer such that
    $G$ has no option equivalent to $*n$.
Proof:
    Suppose inductively that the theorem holds for all options of $G$.

    We show that we can win in $G + *n$ as the second player.

    If Player 1 moves in $*n$, say to $*m$ with $m<n$,
    then by definition of $n$ we can move in $G$ to a game equivalent to $*m$,
    yielding a game equivalent to $*m + *m = 0$.
    So we win.

    If Player 1 moves in $G$, picking an option $G_i$ of $G$,
    then by the inductive hypothesis, $G_i$ is equivalent to some $*m$.

    So we have to win as first player in $*m + *n$,
    but by definition of $n$, $m != n$, so $*m + *n = *(m (+) n) != *0$.
    Again, we win. \qed

\newpage
Exercise:
    Confirm by using the Sprague-Grundy theorem that
    \begin{verbatim}
:::        *
:::        |
:::        |
:::        *
:::       / \      =  *1
:::      /   \
:::     *     *
:::      \   /
:::       \ /
:::    ____*____
    \end{verbatim}

Exercise:
    By using the Sprague-Grundy theorem to find the nim heaps equivalent to
    each component, and then applying the winning strategy from nim, find a
    winning move for the first player in the following Green Hackenbush game.

    \begin{verbatim}
:::              *
:::              |
:::              |
:::        *     *     *    *
:::       / \     \   /    / \
:::      /   \     \ /    /   \
:::     *     *     *    *-----*
:::      \   /      |     \   /
:::       \ /       |      \ /
:::    ____*________*_______*____
    \end{verbatim}

\newpage
## Bonus: Games

Now, we consider dropping the impartiality condition.

A (combinatorial) game is played by two players, Left and Right, taking turns.
A player who can't move loses.

A game is given by the moves available to Left (\underline{Left-options}) and
to Right (\underline{Right-options}).

We write $G=\{L_1,...,L_n | R_1,...,R_m \}$ for the game with Left-options
$L_i$ and Right-options $R_i$, which are themselves games.

Finiteness is defined as in the impartial case.

A game is impartial iff the set of Right-options is equal to the set of
Left-options, and all options are impartial.


# Real-life games

Chess nearly fits into this framework - but it's possible for the game to end
in a draw, which we haven't allowed in our formalism, and it isn't finite.

Go fits even better, though rare loopy situations make it technically
infinite.

Some other games of variable notoriety which fit, at least roughly, our
definition of a game: draughts/chequers, pente, gess, khet, dots-and-boxes,
sprouts, connect 4, gomoku, tic-tac-toe, hex, Y, shogi, xiàngqí, hnefatafl.
See 
::: {\tt https://en.wikipedia.org/wiki/List\_of\_abstract\_strategy\_games}
for many more.

# Red-Blue Hackenbush

Like Green Hackenbush, but each line is either red or blue. Only Right can cut
Red lines, only Left can cut bLue lines.

# Domineering

Played on an 8x8 (say) chess board; players take turns to place 2x1 dominoes
on free squares. Left places her dominoes vertically, Right places his
horizontally.


# Basic theory

We still have the impartial games $*n$,
:   $*n = \{ *0,*1,...,*(n-1) | *0,*1,...,*(n-1) \}$.
We call these _nimbers_.
We write $*$ for $*1 = \{ 0 | 0 \}$.

$G+H$ is the game where players choose which of $G$ and $H$ to move in;
Left plays as Left in both, Right as Right in both.

Outcomes:
    Now there are four possibilities:\
    ***) Right wins (whoever moves first);
    **   Left wins (whoever moves first);
    **   Player 1 (whether that's Left or Right) wins;
    **	 Player 2 (whether that's Left or Right) wins.
    *e)*

Exercise:
    Determine the outcomes of these four basic domineering games:
    \begin{verbatim}
:::                                  ***
:::    *        **        **         *
:::    *                  *          *
    \end{verbatim}

We define equivalence ("$G=H$") as in the impartial case, but with this
notion of outcome.

By the same arguments as in the impartial case, we have:\

Lemma:
    $G = 0$ iff $G$ is a second-player win.

Definition:
    The _negative_ of a game $G=\{L_1,...,L_n | R_1,...,R_m\}$ is
    the game $-G$ in which Left and Right switch roles, i.e.
    :	$-G := \{ -R_1,...,-R_m | -L_1,...,-L_n \}$.

Note that $G$ is impartial iff $-G=G$.

Write $G-H$ for $G+(-H)$.

$G-G = G+(-G)$ is a second-player win, by mirroring moves made in one component
in the other.

Just as in the impartial case, we have

Lemma:
    $G-H=0$ iff $G=H$
Proof:
    If $G=H$, then $G-H = H-H = 0$.

    If $G-H = 0$, then
    :	$G = G + 0 = G+H-H = G-H + H = 0+H = H$. \qed

Definition:
    For games $G$ and $H$, we say
    ***) $G>H$ if $G-H$ is a win for Left;
    **   $G<H$ if $G-H$ is a win for Right;
    **   $G=H$ if $G-H$ is a win for Player 2;
    **   $G || H$ ("$G$ is confused with $H$") if $G-H$ is a win for Player 1.
    *e)*

Lemma:
    $<=$ is a partial order, and addition respects it,
    i.e. $G <= H ==> G+K <= H+K$.

# Numbers

$1 = \{0 | \}$,
$2 = \{1 | \}$,
$3 = \{2 | \}$,
etc.

So
$-1 = \{ | 0 \}$,
$-2 = \{ | -1 \}$,
$-3 = \{ | -2 \}$,
etc.

$\frac 12 = \{ 0 | 1 \}$.

Exercise:
    By e.g. considering the following red-blue hackenbush position (L means
    bLue, R means Red), prove
    :	$\frac 12 + \frac 12 - 1 = 0$

    \begin{verbatim}
:::        *       *       
:::       R|      R|       
:::        |       |       
:::        *       *      *
:::       L|      L|     R|
:::        |       |      |
:::    ____*_______*______*____
    \end{verbatim}

$\frac 14 = \{ 0 | \frac 12 \}$.

Exercise:
    Confirm that
    \begin{verbatim}
:::        *
:::       R|
:::        |
:::        *       1
:::       R|   =   -
:::        |       4
:::        *
:::       L|
:::        |
:::    ____*___
    \end{verbatim}

Generally, we can define
:   $\frac 1{2^{n+1}} = \{ 0 | \frac 1{2^n} \}$.
and then for $m\in\N$,
:   $\frac m{2^n} = m \frac 1{2^n}$ = [the sum of $m$ copies of $\frac 1{2^n}$]
:   $\frac {-m}{2^n} = -(m \frac 1{2^n})$

Definition:
    A finite game is a _number_ if it is equivalent to one of these games
    $\frac m{2^n}$ (with $m\in\Z$, $n\in\N$).

Lemma:
    The usual ordering on numbers agrees with the definition of $<$ for games.

Lemma:
    If $G$ is a finite game,
    and every Left-option is less than every Right-option,
    then $G$ is the simplest number $\frac m{2^n}$ which is greater than every
    Left-option and less than every Right-option,
    where $\frac m{2^n}$ is _simpler_ than $\frac {m'}{2^{n'}}$ if $n<n'$, or
    if $n=n'$ and $|m|<|m'|$.
Proof:
    We show $\frac m{2^n}-G=0$.

    Say Left plays first.

    If she plays in $-G$, it is to a number $-k$ with $k>\frac m{2^n}$,
    so Right wins the resulting game $\frac m{2^n}-k$.

    If she plays in $\frac m{2^n}$, it follows from the definitions (exercise)
    that it must be to a simpler number $k < \frac m{2^n}$.

    So since $\frac m{2^n}$ is simplest,
    $k$ must be less than some Left-option $H$ of $G$.

    So Right can play the Right-option $-H$ of $-G$,
    and win the resulting game $k-H$.

Exercise:
    Use this lemma to confirm the following values of domineering games:\
    \begin{verbatim}
:::  **   1         -1   ***   3
:::  *  = -   *** =  -   * * = -
:::  *    2     *    2   *     4
    \end{verbatim}

Lemma:
    The usual addition of numbers agrees with addition of games.
    
Exercise:
    Confirm, both by using addition and by thinking through strategies, that
    \begin{verbatim}
:::  *** ***            
:::  * * * * ** ***  = 0
:::  *   *        *     
    \end{verbatim}


# More on Domineering

Exercise:
    Confirm
    \begin{verbatim}
:::               *  
:::  **          *** 
:::  ** = *      *   = { 1 | 1 } = 1 + *
:::              *   
    \end{verbatim}

There is however much more to games than numbers and nimbers.

Consider
    \begin{verbatim}
:::  **  
:::  **  = { 1 | -1 } =: +/- 1
:::      
    \end{verbatim}

Whoever moves in this game gets a free move for their efforts.

Exercise:
    Confirm that for a number $x$,
    ***) $\pm 1 > x$ if $x < 1$,
    **   $\pm 1 < x$ if $x > 1$,
    **   $\pm 1 || x$ if $-1 <= x <= 1$.
    *e)*

Games like $\pm 1$, where there is an advantage to moving, are called _hot_
games.

For a positive number $x$, define $\pm x := \{ x | -x \}$. These games are
called _switches_.

Lemma:
    If $x$ and $y$ are numbers with $x >= y$, then
    :	$\{ x | y \} = \frac{x+y}2 \pm \frac{x-y}2$.

Example:
    \begin{verbatim}
:::     
:::  *** 
:::  *** = { 2 | -1/2 } = 3/4 +/- 5/4.
:::      
    \end{verbatim}

Roughly, it is sound strategy to play in the "hottest" component first.

Certainly this is true of games which are sums of switches, numbers and
nimbers:\
on your turn, you should play in the largest switch if there is one,
else if there are impartial components you should use the nim strategy to deal
with them,
and finally you should (always) only play in a number if there's nothing else
left.

This advice is far from being enough to let you win any domineering game
(it's actually a hard game, tournaments have been played), but it's a start!

See pp114-121 of "On Numbers and Games" for much more on Domineering.
    
# Further reading
The classic texts are
***) E. Berlekamp, J. Conway, and R. Guy, "Winning Ways for your Mathematical
Plays, vol 1";
** John Conway, "On Numbers and Games".
*e)*

There are also two more recent books,
***) M. Albert, R. Nowakowski, D. Wolfe, "Lessons in Play: An Introduction to
Combinatorial Game Theory";
** Aaron Siegel, "Combinatorial Game Theory".
*e)*

