cphb/chapter25.tex

816 lines
22 KiB
TeX
Raw Normal View History

2016-12-28 23:54:51 +01:00
\chapter{Game theory}
2017-02-11 15:52:42 +01:00
In this chapter, we will focus on two-player
games that do not contain random elements.
2017-01-15 19:42:46 +01:00
Our goal is to find a strategy that we can
follow to win the game
no matter what the opponent does,
if such a strategy exists.
It turns out that there is a general strategy
for all such games,
and we can analyze the games using the \key{nim theory}.
2017-02-11 15:52:42 +01:00
First, we will analyze simple games where
2017-01-15 19:42:46 +01:00
players remove sticks from heaps,
2017-02-11 15:52:42 +01:00
and after this, we will generalize the strategy
used in those games to all other games.
2017-01-15 19:42:46 +01:00
\section{Game states}
2017-02-11 15:52:42 +01:00
Let us consider a game where there is initially
2017-01-15 19:42:46 +01:00
a heap of $n$ sticks.
Players $A$ and $B$ move alternatively,
and player $A$ begins.
On each move, the player has to remove
2017-02-11 15:52:42 +01:00
1, 2 or 3 sticks from the heap,
and the player who removes the last stick wins the game.
2017-01-15 19:42:46 +01:00
For example, if $n=10$, the game may proceed as follows:
2017-04-21 21:18:22 +02:00
\begin{itemize}[noitemsep]
2017-01-15 19:42:46 +01:00
\item Player $A$ removes 2 sticks (8 sticks left).
\item Player $B$ removes 3 sticks (5 sticks left).
\item Player $A$ removes 1 stick (4 sticks left).
\item Player $B$ removes 2 sticks (2 sticks left).
\item Player $A$ removes 2 sticks and wins.
2017-04-21 21:18:22 +02:00
\end{itemize}
2017-02-11 15:52:42 +01:00
2017-01-15 19:42:46 +01:00
This game consists of states $0,1,2,\ldots,n$,
where the number of the state corresponds to
the number of sticks left.
\subsubsection{Winning and losing states}
\index{winning state}
\index{losing state}
A \key{winning state} is a state where
2017-02-11 15:52:42 +01:00
the player will win the game if they
play optimally,
and a \key{losing state} is a state
where the player will lose the game if the
2017-01-15 19:42:46 +01:00
opponent plays optimally.
It turns out that we can classify all states
2017-02-11 15:52:42 +01:00
of a game so that each state is either
2017-01-15 19:42:46 +01:00
a winning state or a losing state.
In the above game, state 0 is clearly a
2017-02-11 15:52:42 +01:00
losing state, because the player cannot make
2017-01-15 19:42:46 +01:00
any moves.
States 1, 2 and 3 are winning states,
because we can remove 1, 2 or 3 sticks
and win the game.
State 4, in turn, is a losing state,
because any move leads to a state that
is a winning state for the opponent.
More generally, if there is a move that leads
from the current state to a losing state,
the current state is a winning state,
2017-04-21 21:18:22 +02:00
and otherwise the current state is a losing state.
2017-01-15 19:42:46 +01:00
Using this observation, we can classify all states
2017-02-11 15:52:42 +01:00
of a game starting with losing states where
2017-01-15 19:42:46 +01:00
there are no possible moves.
The states $0 \ldots 15$ of the above game
can be classified as follows
2017-02-11 15:52:42 +01:00
($W$ denotes a winning state and $L$ denotes a losing state):
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (0,0) grid (16,1);
2017-01-15 19:42:46 +01:00
\node at (0.5,0.5) {$L$};
\node at (1.5,0.5) {$W$};
\node at (2.5,0.5) {$W$};
\node at (3.5,0.5) {$W$};
\node at (4.5,0.5) {$L$};
\node at (5.5,0.5) {$W$};
\node at (6.5,0.5) {$W$};
\node at (7.5,0.5) {$W$};
\node at (8.5,0.5) {$L$};
\node at (9.5,0.5) {$W$};
\node at (10.5,0.5) {$W$};
\node at (11.5,0.5) {$W$};
\node at (12.5,0.5) {$L$};
\node at (13.5,0.5) {$W$};
\node at (14.5,0.5) {$W$};
\node at (15.5,0.5) {$W$};
2016-12-28 23:54:51 +01:00
\footnotesize
\node at (0.5,1.4) {$0$};
\node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$7$};
\node at (8.5,1.4) {$8$};
\node at (9.5,1.4) {$9$};
\node at (10.5,1.4) {$10$};
\node at (11.5,1.4) {$11$};
\node at (12.5,1.4) {$12$};
\node at (13.5,1.4) {$13$};
\node at (14.5,1.4) {$14$};
\node at (15.5,1.4) {$15$};
\end{tikzpicture}
\end{center}
2017-02-11 15:52:42 +01:00
It is easy to analyze this game:
2017-01-15 19:42:46 +01:00
a state $k$ is a losing state if $k$ is
divisible by 4, and otherwise it
2017-04-21 21:18:22 +02:00
is a winning state.
2017-01-15 19:42:46 +01:00
An optimal way to play the game is
to always choose a move after which
the number of sticks in the heap
is divisible by 4.
Finally, there are no sticks left and
the opponent has lost.
Of course, this strategy requires that
the number of sticks is \emph{not} divisible by 4
when it is our move.
If it is, there is nothing we can do,
2017-04-17 11:18:29 +02:00
and the opponent will win the game if
2017-01-15 19:42:46 +01:00
they play optimally.
\subsubsection{State graph}
2017-02-11 15:52:42 +01:00
Let us now consider another stick game,
where in each state $k$, it is allowed to remove
2017-01-15 19:42:46 +01:00
any number $x$ of sticks such that $x$
2017-02-11 15:52:42 +01:00
is smaller than $k$ and divides $k$.
2017-01-15 19:42:46 +01:00
For example, in state 8 we may remove
1, 2 or 4 sticks, but in state 7 the only
allowed move is to remove 1 stick.
The following picture shows the states
$1 \ldots 9$ of the game as a \key{state graph},
2017-02-11 15:52:42 +01:00
whose nodes are the states and edges are the moves between them:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1) at (0,0) {$1$};
\node[draw, circle] (2) at (2,0) {$2$};
\node[draw, circle] (3) at (3.5,-1) {$3$};
\node[draw, circle] (4) at (1.5,-2) {$4$};
\node[draw, circle] (5) at (3,-2.75) {$5$};
\node[draw, circle] (6) at (2.5,-4.5) {$6$};
\node[draw, circle] (7) at (0.5,-3.25) {$7$};
\node[draw, circle] (8) at (-1,-4) {$8$};
\node[draw, circle] (9) at (1,-5.5) {$9$};
\path[draw,thick,->,>=latex] (2) -- (1);
\path[draw,thick,->,>=latex] (3) edge [bend right=20] (2);
\path[draw,thick,->,>=latex] (4) edge [bend left=20] (2);
\path[draw,thick,->,>=latex] (4) edge [bend left=20] (3);
\path[draw,thick,->,>=latex] (5) edge [bend right=20] (4);
\path[draw,thick,->,>=latex] (6) edge [bend left=20] (5);
\path[draw,thick,->,>=latex] (6) edge [bend left=20] (4);
\path[draw,thick,->,>=latex] (6) edge [bend right=40] (3);
\path[draw,thick,->,>=latex] (7) edge [bend right=20] (6);
\path[draw,thick,->,>=latex] (8) edge [bend right=20] (7);
\path[draw,thick,->,>=latex] (8) edge [bend right=20] (6);
\path[draw,thick,->,>=latex] (8) edge [bend left=20] (4);
\path[draw,thick,->,>=latex] (9) edge [bend left=20] (8);
\path[draw,thick,->,>=latex] (9) edge [bend right=20] (6);
\end{tikzpicture}
\end{center}
2017-01-15 19:42:46 +01:00
The final state in this game is always state 1,
which is a losing state, because there are no
valid moves.
The classification of states $1 \ldots 9$
is as follows:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.7]
\draw (1,0) grid (10,1);
2017-01-15 19:42:46 +01:00
\node at (1.5,0.5) {$L$};
\node at (2.5,0.5) {$W$};
\node at (3.5,0.5) {$L$};
\node at (4.5,0.5) {$W$};
\node at (5.5,0.5) {$L$};
\node at (6.5,0.5) {$W$};
\node at (7.5,0.5) {$L$};
\node at (8.5,0.5) {$W$};
\node at (9.5,0.5) {$L$};
2016-12-28 23:54:51 +01:00
\footnotesize
\node at (1.5,1.4) {$1$};
\node at (2.5,1.4) {$2$};
\node at (3.5,1.4) {$3$};
\node at (4.5,1.4) {$4$};
\node at (5.5,1.4) {$5$};
\node at (6.5,1.4) {$6$};
\node at (7.5,1.4) {$7$};
\node at (8.5,1.4) {$8$};
\node at (9.5,1.4) {$9$};
\end{tikzpicture}
\end{center}
2017-02-18 16:13:06 +01:00
Surprisingly, in this game,
2017-01-15 19:42:46 +01:00
all even-numbered states are winning states,
and all odd-numbered states are losing states.
\section{Nim game}
\index{nim game}
The \key{nim game} is a simple game that
has an important role in game theory,
2017-02-11 15:52:42 +01:00
because many other games can be played using
2017-01-15 19:42:46 +01:00
the same strategy.
First, we focus on nim,
and then we generalize the strategy
to other games.
There are $n$ heaps in nim,
and each heap contains some number of sticks.
The players move alternatively,
and on each turn, the player chooses
a heap that still contains sticks
and removes any number of sticks from it.
The winner is the player who removes the last stick.
The states in nim are of the form
$[x_1,x_2,\ldots,x_n]$,
where $x_k$ denotes the number of sticks in heap $k$.
For example, $[10,12,5]$ is a game where
there are three heaps with 10, 12 and 5 sticks.
The state $[0,0,\ldots,0]$ is a losing state,
2017-02-11 15:52:42 +01:00
because it is not possible to remove any sticks,
2017-01-15 19:42:46 +01:00
and this is always the final state.
\subsubsection{Analysis}
\index{nim sum}
2017-02-11 15:52:42 +01:00
It turns out that we can easily classify
any nim state by calculating
2017-04-21 21:18:22 +02:00
the \key{nim sum} $s = x_1 \oplus x_2 \oplus \cdots \oplus x_n$,
2017-02-25 20:47:47 +01:00
where $\oplus$ is the xor operation\footnote{The optimal strategy
2017-02-26 10:57:14 +01:00
for nim was published in 1901 by C. L. Bouton \cite{bou01}.}.
2017-02-11 15:52:42 +01:00
The states whose nim sum is 0 are losing states,
2017-01-15 19:42:46 +01:00
and all other states are winning states.
For example, the nim sum for
$[10,12,5]$ is $10 \oplus 12 \oplus 5 = 3$,
so the state is a winning state.
But how is the nim sum related to the nim game?
2017-02-18 16:13:06 +01:00
We can explain this by looking at how the nim
2017-01-15 19:42:46 +01:00
sum changes when the nim state changes.
2016-12-28 23:54:51 +01:00
~\\
\noindent
2017-01-15 19:42:46 +01:00
\textit{Losing states:}
The final state $[0,0,\ldots,0]$ is a losing state,
and its nim sum is 0, as expected.
In other losing states, any move leads to
a winning state, because when a single value $x_k$ changes,
the nim sum also changes, so the nim sum
is different from 0 after the move.
2016-12-28 23:54:51 +01:00
~\\
\noindent
2017-01-15 19:42:46 +01:00
\textit{Winning states:}
We can move to a losing state if
there is any heap $k$ for which $x_k \oplus s < x_k$.
In this case, we can remove sticks from
heap $k$ so that it will contain $x_k \oplus s$ sticks,
which will lead to a losing state.
There is always such a heap, where $x_k$
2017-02-11 15:52:42 +01:00
has a one bit at the position of the leftmost
2017-01-15 19:42:46 +01:00
one bit in $s$.
2016-12-28 23:54:51 +01:00
~\\
\noindent
2017-03-30 21:58:01 +02:00
As an example, consider the state $[10,12,5]$.
2017-01-15 19:42:46 +01:00
This state is a winning state,
because its nim sum is 3.
Thus, there has to be a move which
leads to a losing state.
Next we will find out such a move.
2016-12-28 23:54:51 +01:00
\begin{samepage}
2017-01-15 19:42:46 +01:00
The nim sum of the state is as follows:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tabular}{r|r}
10 & \texttt{1010} \\
12 & \texttt{1100} \\
5 & \texttt{0101} \\
\hline
3 & \texttt{0011} \\
\end{tabular}
\end{center}
\end{samepage}
2017-01-15 19:42:46 +01:00
In this case, the heap with 10 sticks
is the only heap that has a one bit
2017-02-11 15:52:42 +01:00
at the position of the leftmost
2017-01-15 19:42:46 +01:00
one bit in the nim sum:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tabular}{r|r}
2017-04-21 21:18:22 +02:00
10 & \texttt{10\underline{1}0} \\
2016-12-28 23:54:51 +01:00
12 & \texttt{1100} \\
5 & \texttt{0101} \\
\hline
2017-04-21 21:18:22 +02:00
3 & \texttt{00\underline{1}1} \\
2016-12-28 23:54:51 +01:00
\end{tabular}
\end{center}
2017-01-15 19:42:46 +01:00
The new size of the heap has to be
$10 \oplus 3 = 9$,
so we will remove just one stick.
After this, the state will be $[9,12,5]$,
which is a losing state:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tabular}{r|r}
9 & \texttt{1001} \\
12 & \texttt{1100} \\
5 & \texttt{0101} \\
\hline
0 & \texttt{0000} \\
\end{tabular}
\end{center}
2017-01-15 19:42:46 +01:00
\subsubsection{Misère game}
2016-12-28 23:54:51 +01:00
2017-01-15 19:42:46 +01:00
\index{misère game}
2016-12-28 23:54:51 +01:00
2017-04-21 21:18:22 +02:00
In a \key{misère game}, the goal of the game
is opposite,
2017-01-15 19:42:46 +01:00
so the player who removes the last stick
loses the game.
It turns out that a misère nim game can be
optimally played almost like the standard nim game.
2016-12-28 23:54:51 +01:00
2017-01-15 19:42:46 +01:00
The idea is to first play the misère game
like a standard game, but change the strategy
at the end of the game.
2017-02-11 15:52:42 +01:00
The new strategy will be introduced in a situation
where each heap would contain at most one stick
after the next move.
2016-12-28 23:54:51 +01:00
2017-01-15 19:42:46 +01:00
In the standard game, we should choose a move
after which there is an even number of heaps with one stick.
However, in the misère game, we choose a move so that
there is an odd number of heaps with one stick.
2016-12-28 23:54:51 +01:00
2017-02-11 15:52:42 +01:00
This strategy works because a state where the
strategy changes always appears in the game,
2017-01-15 19:42:46 +01:00
and this state is a winning state, because
2017-02-11 15:52:42 +01:00
it contains exactly one heap that has more than one stick
2017-01-15 19:42:46 +01:00
so the nim sum is not 0.
2016-12-28 23:54:51 +01:00
2017-01-15 21:02:40 +01:00
\section{SpragueGrundy theorem}
2016-12-28 23:54:51 +01:00
2017-01-15 21:02:40 +01:00
\index{SpragueGrundy theorem}
2016-12-28 23:54:51 +01:00
2017-04-21 21:18:22 +02:00
The \key{SpragueGrundy theorem}\footnote{The theorem was
independently discovered by R. Sprague \cite{spr35} and P. M. Grundy \cite{gru39}.} generalizes the
2017-02-11 15:52:42 +01:00
strategy used in nim to all games that fulfil
2017-01-15 21:02:40 +01:00
the following requirements:
2016-12-28 23:54:51 +01:00
\begin{itemize}[noitemsep]
2017-01-15 21:02:40 +01:00
\item There are two players who move alternatively.
\item The game consists of states, and the possible moves
2017-02-11 15:52:42 +01:00
in a state do not depend on whose turn it is.
\item The game ends when a player cannot make a move.
2017-01-15 21:02:40 +01:00
\item The game surely ends sooner or later.
\item The players have complete information about
2017-02-11 15:52:42 +01:00
the states and allowed moves, and there is no randomness in the game.
2016-12-28 23:54:51 +01:00
\end{itemize}
2017-01-15 21:02:40 +01:00
The idea is to calculate for each game state
a Grundy number that corresponds to the number of
sticks in a nim heap.
When we know the Grundy numbers for all states,
2017-02-11 15:52:42 +01:00
we can play the game like the nim game.
2016-12-28 23:54:51 +01:00
2017-02-20 22:23:10 +01:00
\subsubsection{Grundy numbers}
2016-12-28 23:54:51 +01:00
2017-01-15 21:02:40 +01:00
\index{Grundy number}
\index{mex function}
2016-12-28 23:54:51 +01:00
2017-01-15 21:02:40 +01:00
The \key{Grundy number} for a game state is
2016-12-28 23:54:51 +01:00
\[\textrm{mex}(\{g_1,g_2,\ldots,g_n\}),\]
2017-01-15 21:02:40 +01:00
where $g_1,g_2,\ldots,g_n$ are Grundy numbers for
2017-02-11 15:52:42 +01:00
states to which we can move from the state,
2017-02-18 16:13:06 +01:00
and the mex function gives the smallest
2017-01-15 21:02:40 +01:00
nonnegative number that is not in the set.
For example, $\textrm{mex}(\{0,1,3\})=2$.
If there are no possible moves in a state,
its Grundy number is 0, because
$\textrm{mex}(\emptyset)=0$.
For example, in the state graph
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1) at (0,0) {\phantom{0}};
\node[draw, circle] (2) at (2,0) {\phantom{0}};
\node[draw, circle] (3) at (4,0) {\phantom{0}};
\node[draw, circle] (4) at (1,-2) {\phantom{0}};
\node[draw, circle] (5) at (3,-2) {\phantom{0}};
\node[draw, circle] (6) at (5,-2) {\phantom{0}};
\path[draw,thick,->,>=latex] (2) -- (1);
\path[draw,thick,->,>=latex] (3) -- (2);
\path[draw,thick,->,>=latex] (5) -- (4);
\path[draw,thick,->,>=latex] (6) -- (5);
\path[draw,thick,->,>=latex] (4) -- (1);
\path[draw,thick,->,>=latex] (4) -- (2);
\path[draw,thick,->,>=latex] (5) -- (2);
\path[draw,thick,->,>=latex] (6) -- (2);
\end{tikzpicture}
\end{center}
2017-01-15 21:02:40 +01:00
the Grundy numbers are as follows:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=0.9]
\node[draw, circle] (1) at (0,0) {0};
\node[draw, circle] (2) at (2,0) {1};
\node[draw, circle] (3) at (4,0) {0};
\node[draw, circle] (4) at (1,-2) {2};
\node[draw, circle] (5) at (3,-2) {0};
\node[draw, circle] (6) at (5,-2) {2};
\path[draw,thick,->,>=latex] (2) -- (1);
\path[draw,thick,->,>=latex] (3) -- (2);
\path[draw,thick,->,>=latex] (5) -- (4);
\path[draw,thick,->,>=latex] (6) -- (5);
\path[draw,thick,->,>=latex] (4) -- (1);
\path[draw,thick,->,>=latex] (4) -- (2);
\path[draw,thick,->,>=latex] (5) -- (2);
\path[draw,thick,->,>=latex] (6) -- (2);
\end{tikzpicture}
\end{center}
2017-01-15 21:02:40 +01:00
The Grundy number of a losing state is 0,
and the Grundy number of a winning state is
a positive number.
2017-02-11 15:52:42 +01:00
The Grundy number of a state corresponds to
a number of sticks in a nim heap.
2017-01-15 21:02:40 +01:00
If the Grundy number is 0, we can only move to
states whose Grundy numbers are positive,
and if the Grundy number is $x>0$, we can move
to states whose Grundy numbers incluce all numbers
$0,1,\ldots,x-1$.
2016-12-28 23:54:51 +01:00
~\\
\noindent
2017-02-11 15:52:42 +01:00
As an example, let us consider a game where
2017-01-15 21:02:40 +01:00
the players move a figure in a maze.
Each square in the maze is either floor or wall.
On each turn, the player has to move
the figure some number
2017-02-11 15:52:42 +01:00
of steps left or up.
2017-01-15 21:02:40 +01:00
The winner of the game is the player who
makes the last move.
2016-12-28 23:54:51 +01:00
\begin{samepage}
2017-01-15 21:02:40 +01:00
The following picture shows a possible initial state
of the game, where @ denotes the figure and *
denotes a square where it can move.
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=.65]
\begin{scope}
\fill [color=black] (0, 1) rectangle (1, 2);
\fill [color=black] (0, 3) rectangle (1, 4);
\fill [color=black] (2, 2) rectangle (3, 3);
\fill [color=black] (2, 4) rectangle (3, 5);
\fill [color=black] (4, 3) rectangle (5, 4);
\draw (0, 0) grid (5, 5);
\node at (4.5,0.5) {@};
\node at (3.5,0.5) {*};
\node at (2.5,0.5) {*};
\node at (1.5,0.5) {*};
\node at (0.5,0.5) {*};
\node at (4.5,1.5) {*};
\node at (4.5,2.5) {*};
\end{scope}
\end{tikzpicture}
\end{center}
\end{samepage}
2017-01-15 21:02:40 +01:00
The states of the game are all floor squares
in the maze.
2017-02-11 15:52:42 +01:00
In this situation, the Grundy numbers
2017-01-15 21:02:40 +01:00
are as follows:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tikzpicture}[scale=.65]
\begin{scope}
\fill [color=black] (0, 1) rectangle (1, 2);
\fill [color=black] (0, 3) rectangle (1, 4);
\fill [color=black] (2, 2) rectangle (3, 3);
\fill [color=black] (2, 4) rectangle (3, 5);
\fill [color=black] (4, 3) rectangle (5, 4);
\draw (0, 0) grid (5, 5);
\node at (0.5,4.5) {0};
\node at (1.5,4.5) {1};
\node at (2.5,4.5) {};
\node at (3.5,4.5) {0};
\node at (4.5,4.5) {1};
\node at (0.5,3.5) {};
\node at (1.5,3.5) {0};
\node at (2.5,3.5) {1};
\node at (3.5,3.5) {2};
\node at (4.5,3.5) {};
\node at (0.5,2.5) {0};
\node at (1.5,2.5) {2};
\node at (2.5,2.5) {};
\node at (3.5,2.5) {1};
\node at (4.5,2.5) {0};
\node at (0.5,1.5) {};
\node at (1.5,1.5) {3};
\node at (2.5,1.5) {0};
\node at (3.5,1.5) {4};
\node at (4.5,1.5) {1};
\node at (0.5,0.5) {0};
\node at (1.5,0.5) {4};
\node at (2.5,0.5) {1};
\node at (3.5,0.5) {3};
\node at (4.5,0.5) {2};
\end{scope}
\end{tikzpicture}
\end{center}
2017-02-11 15:52:42 +01:00
Thus, each state of the maze game
2017-01-15 21:02:40 +01:00
corresponds to a heap in the nim game.
For example, the Grundy number for
the lower-right square is 2,
so it is a winning state.
We can reach a losing state and
win the game by moving
either four steps left or
two steps up.
Note that unlike in the original nim game,
it may be possible to move to a state whose
Grundy number is larger than the Grundy number
of the current state.
However, the opponent can always choose a move
that cancels such a move, so it is not possible
to escape from a losing state.
\subsubsection{Subgames}
2017-02-11 15:52:42 +01:00
Next we will assume that our game consists
2017-01-15 21:02:40 +01:00
of subgames, and on each turn, the player
first chooses a subgame and then a move in the subgame.
2017-02-11 15:52:42 +01:00
The game ends when it is not possible to make any move
2017-01-15 21:02:40 +01:00
in any subgame.
In this case, the Grundy number of a game
is the nim sum of the Grundy numbers of the subgames.
The game can be played like a nim game by calculating
2017-02-11 15:52:42 +01:00
all Grundy numbers for subgames and then their nim sum.
2016-12-28 23:54:51 +01:00
~\\
\noindent
2017-02-11 15:52:42 +01:00
As an example, consider a game that consists
2017-01-15 21:02:40 +01:00
of three mazes.
2017-02-11 15:52:42 +01:00
In this game, on each turn the player chooses one
2017-01-15 21:02:40 +01:00
of the mazes and then moves the figure in the maze.
Assume that the initial state of the game is as follows:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tabular}{ccc}
\begin{tikzpicture}[scale=.55]
\begin{scope}
\fill [color=black] (0, 1) rectangle (1, 2);
\fill [color=black] (0, 3) rectangle (1, 4);
\fill [color=black] (2, 2) rectangle (3, 3);
\fill [color=black] (2, 4) rectangle (3, 5);
\fill [color=black] (4, 3) rectangle (5, 4);
\draw (0, 0) grid (5, 5);
\node at (4.5,0.5) {@};
\end{scope}
\end{tikzpicture}
&
\begin{tikzpicture}[scale=.55]
\begin{scope}
\fill [color=black] (1, 1) rectangle (2, 3);
\fill [color=black] (2, 3) rectangle (3, 4);
\fill [color=black] (4, 4) rectangle (5, 5);
\draw (0, 0) grid (5, 5);
\node at (4.5,0.5) {@};
\end{scope}
\end{tikzpicture}
&
\begin{tikzpicture}[scale=.55]
\begin{scope}
\fill [color=black] (1, 1) rectangle (4, 4);
\draw (0, 0) grid (5, 5);
\node at (4.5,0.5) {@};
\end{scope}
\end{tikzpicture}
\end{tabular}
\end{center}
2017-01-15 21:02:40 +01:00
The Grundy numbers for the mazes are as follows:
2016-12-28 23:54:51 +01:00
\begin{center}
\begin{tabular}{ccc}
\begin{tikzpicture}[scale=.55]
\begin{scope}
\fill [color=black] (0, 1) rectangle (1, 2);
\fill [color=black] (0, 3) rectangle (1, 4);
\fill [color=black] (2, 2) rectangle (3, 3);
\fill [color=black] (2, 4) rectangle (3, 5);
\fill [color=black] (4, 3) rectangle (5, 4);
\draw (0, 0) grid (5, 5);
\node at (0.5,4.5) {0};
\node at (1.5,4.5) {1};
\node at (2.5,4.5) {};
\node at (3.5,4.5) {0};
\node at (4.5,4.5) {1};
\node at (0.5,3.5) {};
\node at (1.5,3.5) {0};
\node at (2.5,3.5) {1};
\node at (3.5,3.5) {2};
\node at (4.5,3.5) {};
\node at (0.5,2.5) {0};
\node at (1.5,2.5) {2};
\node at (2.5,2.5) {};
\node at (3.5,2.5) {1};
\node at (4.5,2.5) {0};
\node at (0.5,1.5) {};
\node at (1.5,1.5) {3};
\node at (2.5,1.5) {0};
\node at (3.5,1.5) {4};
\node at (4.5,1.5) {1};
\node at (0.5,0.5) {0};
\node at (1.5,0.5) {4};
\node at (2.5,0.5) {1};
\node at (3.5,0.5) {3};
\node at (4.5,0.5) {2};
\end{scope}
\end{tikzpicture}
&
\begin{tikzpicture}[scale=.55]
\begin{scope}
\fill [color=black] (1, 1) rectangle (2, 3);
\fill [color=black] (2, 3) rectangle (3, 4);
\fill [color=black] (4, 4) rectangle (5, 5);
\draw (0, 0) grid (5, 5);
\node at (0.5,4.5) {0};
\node at (1.5,4.5) {1};
\node at (2.5,4.5) {2};
\node at (3.5,4.5) {3};
\node at (4.5,4.5) {};
\node at (0.5,3.5) {1};
\node at (1.5,3.5) {0};
\node at (2.5,3.5) {};
\node at (3.5,3.5) {0};
\node at (4.5,3.5) {1};
\node at (0.5,2.5) {2};
\node at (1.5,2.5) {};
\node at (2.5,2.5) {0};
\node at (3.5,2.5) {1};
\node at (4.5,2.5) {2};
\node at (0.5,1.5) {3};
\node at (1.5,1.5) {};
\node at (2.5,1.5) {1};
\node at (3.5,1.5) {2};
\node at (4.5,1.5) {0};
\node at (0.5,0.5) {4};
\node at (1.5,0.5) {0};
\node at (2.5,0.5) {2};
\node at (3.5,0.5) {5};
\node at (4.5,0.5) {3};
\end{scope}
\end{tikzpicture}
&
\begin{tikzpicture}[scale=.55]
\begin{scope}
\fill [color=black] (1, 1) rectangle (4, 4);
\draw (0, 0) grid (5, 5);
\node at (0.5,4.5) {0};
\node at (1.5,4.5) {1};
\node at (2.5,4.5) {2};
\node at (3.5,4.5) {3};
\node at (4.5,4.5) {4};
\node at (0.5,3.5) {1};
\node at (1.5,3.5) {};
\node at (2.5,3.5) {};
\node at (3.5,3.5) {};
\node at (4.5,3.5) {0};
\node at (0.5,2.5) {2};
\node at (1.5,2.5) {};
\node at (2.5,2.5) {};
\node at (3.5,2.5) {};
\node at (4.5,2.5) {1};
\node at (0.5,1.5) {3};
\node at (1.5,1.5) {};
\node at (2.5,1.5) {};
\node at (3.5,1.5) {};
\node at (4.5,1.5) {2};
\node at (0.5,0.5) {4};
\node at (1.5,0.5) {0};
\node at (2.5,0.5) {1};
\node at (3.5,0.5) {2};
\node at (4.5,0.5) {3};
\end{scope}
\end{tikzpicture}
\end{tabular}
\end{center}
2017-01-15 21:02:40 +01:00
In the initial state, the nim sum of the Grundy numbers
is $2 \oplus 3 \oplus 3 = 2$, so
the first player can win the game.
2017-04-21 21:18:22 +02:00
One optimal move is to move two steps up
2017-02-11 15:52:42 +01:00
in the first maze, which produces the nim sum
2017-01-15 21:02:40 +01:00
$0 \oplus 3 \oplus 3 = 0$.
2016-12-28 23:54:51 +01:00
2017-01-15 21:02:40 +01:00
\subsubsection{Grundy's game}
2016-12-28 23:54:51 +01:00
2017-01-15 21:02:40 +01:00
Sometimes a move in the game divides the game
into subgames that are independent of each other.
In this case, the Grundy number of the game is
2016-12-28 23:54:51 +01:00
\[\textrm{mex}(\{g_1, g_2, \ldots, g_n \}),\]
2017-01-15 21:02:40 +01:00
where $n$ is the number of possible moves and
2016-12-28 23:54:51 +01:00
\[g_k = a_{k,1} \oplus a_{k,2} \oplus \ldots \oplus a_{k,m},\]
2017-01-15 21:02:40 +01:00
where move $k$ generates subgames with
Grundy numbers $a_{k,1},a_{k,2},\ldots,a_{k,m}$.
\index{Grundy's game}
An example of such a game is \key{Grundy's game}.
Initially, there is a single heap that contains $n$ sticks.
On each turn, the player chooses a heap and divides
2017-02-11 15:52:42 +01:00
it into two nonempty heaps such that the heaps
are of different size.
2017-01-15 21:02:40 +01:00
The player who makes the last move wins the game.
Let $f(n)$ be the Grundy number of a heap
that contains $n$ sticks.
The Grundy number can be calculated by going
through all ways to divide the heap into
two heaps.
For example, when $n=8$, the possibilities
2017-02-11 15:52:42 +01:00
are $1+7$, $2+6$ and $3+5$, so
2016-12-28 23:54:51 +01:00
\[f(8)=\textrm{mex}(\{f(1) \oplus f(7), f(2) \oplus f(6), f(3) \oplus f(5)\}).\]
2017-02-18 16:13:06 +01:00
In this game, the value of $f(n)$ is based on the values
2017-02-11 15:52:42 +01:00
of $f(1),\ldots,f(n-1)$.
2017-01-15 21:02:40 +01:00
The base cases are $f(1)=f(2)=0$,
2017-02-18 16:13:06 +01:00
because it is not possible to divide the heaps
2017-01-15 21:02:40 +01:00
of 1 and 2 sticks.
The first Grundy numbers are:
2016-12-28 23:54:51 +01:00
\[
\begin{array}{lcl}
f(1) & = & 0 \\
f(2) & = & 0 \\
f(3) & = & 1 \\
f(4) & = & 0 \\
f(5) & = & 2 \\
f(6) & = & 1 \\
f(7) & = & 0 \\
f(8) & = & 2 \\
\end{array}
\]
2017-01-15 21:02:40 +01:00
The Grundy number for $n=8$ is 2,
2017-02-11 15:52:42 +01:00
so it is possible to win the game.
2017-01-15 21:02:40 +01:00
The winning move is to create heaps
$1+7$, because $f(1) \oplus f(7) = 0$.
2016-12-28 23:54:51 +01:00