SlideShare uma empresa Scribd logo
1 de 47
Baixar para ler offline
An Exploration of Gameplay in Quarto
Matthew Kerner∗
Advisor: Dana Angluin†
April, 2001
Abstract
This paper examines the board game Quarto. Techniques for con-
structing an automated player are discussed. The feasibility of solving
Quarto is explored, and a proof is given that the 12,288 distinct trans-
formations considered exploit the maximum amount of symmetry in the
game. The behavior of one class of transformations is shown to be anal-
ogous to automorphisms of the hypercube. A procedure is proposed for
creating a normal form that characterizes a set of equivalent instances of
the game. An efficient representation of instances requiring fewer than
64 bits per instance is presented. Related work on the combinatorics of
Quarto is reviewed, and original combinatorial results are explored.
1 The Game of Quarto
Quarto is a two-player game that resembles tic-tac-toe. The board is a 4x4 grid,
and is accompanied by 16 distinct pieces, each of which is either tall or short,
round or square, dark or pale, and hollow or solid.
At the beginning of a game, player 1 selects a piece for the other player.
Player 2 places this piece on an unoccupied square on the board, and then
selects a piece for player 1. Play continues until either player completes a row,
column, or diagonal of four pieces that share at least one characteristic. Neither
player “owns” the pieces that she plays—the player who completes the first
group of four winning pieces is the winner. The game is a draw if every place
on the board is occupied by a piece, but no row, column, or diagonal contains
pieces with a common attribute.
2 Introduction
The game of Quarto is relatively simple compared to other two player games.
There are few pieces, the gameplay is uncomplicated, and the board is straight-
forward. The major twist is that each player selects the piece to be played by
her opponent.
∗matthew.kerner@yale.edu
†dana.angluin@yale.edu
1
Despite its simplicity, Quarto is an intriguing game. Any turn in which
one player is close to winning is followed by another turn in which the same
opportunities are offered to the opponent. Quarto is easy to understand but
hard to master, and there is a mathematical undercurrent to the game.
This paper will examine Quarto from the perspective of a computer scientist.
Section 3 presents the vocabulary used throughout the remainder of the paper,
and introduces the diagram used to depict the Quarto board.
Section 4 discusses various techniques that are used in automated game-
play. The concept of the game-state graph is introduced. The basic Minimax
algorithm is presented, and various enhancements are proposed, including α-β
pruning, caching, and parallel computing.
Section 5 discusses the concept of solving a game and gives a motivation for
the sections that follow.
Section 6 discusses the amount of work that would be necessary to solve
Quarto by examining the number of possible game states. The game-state
graph is shown to be prohibitively large for a complete search, and the need for
a reduction in the number of states examined is demonstrated.
Section 7 explores the symmetries of Quarto. A proof is presented that
establishes the symmetries considered as the maximal set of symmetries allowed
by the rules of the game. The behavior of one class of symmetries is shown to
be analogous to the behavior of the automorphisms of the hypercube. Finally,
an algorithm for determining the normal form of a set of equivalent instances is
proposed.
Section 8 presents an efficient representation of Quarto instances that re-
quires fewer than 64 bits per instance. The tradeoffs involved in a compressed
representation are discussed.
Section 9 reviews related work by other Quarto enthusiasts and presents
some original combinatorial results. An additional enhancement of the Minimax
algorithm is proposed to detect draws early.
Section 11, the Appendix, presents an enumeration of the 32 positional sym-
metries examined in Section 7.
3 Vocabulary
Attribute The four variables in the description of each piece: height, color,
shape, and hollowness. As each attribute can take on 2 values, we can
represent each piece as a distinct 4-bit binary number ranging from 0 to
15, or 0000 to 1111 in binary form.
Instance The set of pieces on the board and the piece selected for placement
on the next turn. Note that this includes enough information to determine
whether player 1 or player 2 will make the next move and to determine
the set of pieces in the pool. An instance is designated as n + 0 if it has n
pieces on the board and no piece has been selected for the next move. If
2
a piece has been selected, that instance is an n + 1 instance1
. This paper
will illustrate instances using the following diagram:
• (+1 position)
• • • •
• • • •
• • • •
• • • •
If a square is empty, it represents an unoccupied position. If a square
contains a capital letter, that letter is intended as a label for that square.
If a square contains a number, that number represents the corresponding
piece (see Attribute).
Quartet A set of four pieces. A quartet in which the pieces share at least one
characteristic is a winning quartet. A quartet in which the pieces share no
characteristic is a draw quartet.
Value The value of an attribute (i.e. 1 or 0 in the bitwise representation of a
piece).
Win A winning position and a quartet that inhabits it comprise a win if the
bitwise AND2
of the quartet is not 0 or the bitwise OR3
of the quartet
is not 15. That is, the quartet shares at least one attribute-value pair in
common.
Win Placement A win placement is a winning position and the set of pieces
that occupy it. A win placement is live if the value of any attribute is the
same across every piece in the win placement. A win placement is dead if
the value of every attribute differs across the pieces in the win placement.
Winning Position Four squares on the board that are arranged in a row,
column, or diagonal.
4 Building a Quarto Player
4.1 Internal Representation
The first steps in creating an automated player for any game are to select the
data required by the player, to specify how the player will gather the data
during play, and to develop a representation of the data that can be stored and
1This nomenclature was introduced by Goossens in [1].
2The bitwise AND function of two bits is 1 if the bits are identical and 0 otherwise.
Generalized to an n-bit string, the ith bit of the result contains the bitwise AND of the ith
bits of the inputs.
3The bitwise OR function of two bits is 1 if either or both inputs is 1, and 0 otherwise. It
generalizes to n-bit strings in the same manner as bitwise AND.
3
manipulated by a machine. Soccer, for example, includes a large amount of data
at any point, such as the location, velocity, and acceleration of every player as
well as for the ball, not to mention the wind speed and direction. An automated
soccer player requires frequent updates to keep up with the activity of the game.
Furthermore, any system that represents a soccer game accurately would require
massive computing resources and a nontrivial internal representation.
Fortunately Quarto is much simpler than soccer. An instance of Quarto is
characterized by the set of pieces on the board and their locations as well as the
piece selected to be placed on the next turn. The representation need only be
updated when a move is made by either player, clearly a reasonable requirement.
Furthermore, Quarto is a deterministic game of complete information. A
game like Monopoly is nondeterministic as the roll of the dice introduces an
element of chance. In deterministic games like Quarto, the only variability in
the game comes from the choices made by the players. Monopoly is a game of
incomplete information, as the cards are not visible to the players. In Quarto
and other games of complete information, each player knows exactly what the
board looks like at every point in the game. Aside from the opponent’s moves,
all of the relevant information is available to each player.
The simplest representation of the Quarto board is a two dimensional array
in computer memory that contains the pieces placed on the board. Pieces are
represented by 4-bit strings as explained under Attribute in Section 3.
We may want to keep additional information about an instance in order to
reconstruct the chain of moves that created the instance. We can either keep
a list of the moves made in a game, or keep a history of previous instances. A
game might go as follows:
(+1 position)
↓
4 (+1 position)
↓
(+1 position)
4
...
4
This sequence of instances suggests representing a Quarto game as a graph
of instances connected by moves.
4.2 The Game State Graph
A graph is a set of vertices and edges. Vertices (also known as nodes) are
points, and edges are ordered pairs of vertices. If an edge connects one vertex
to another, that edge is said to be incident to both of those vertices. Let us
call a graph that models the game states in Quarto a game-state graph. In it,
the vertices correspond to instances, and the edges correspond to moves that
transition between instances.
The game-state graph is a directed graph, or a graph in which the edges
travel in one direction only. This corresponds to the rule in Quarto that no
player may remove pieces from the board. The board only grows more full as
the game advances. This results in a graph that looks like the following:
0 + 1 instances: ◦
1 + 0 instances: ◦ ◦
1 + 1 instances: ◦ ◦ ◦
.·· ... .·· ... .·· ...
In this graph, the edges form paths. A path is an ordered set of edges such
that the ith
edge of the set originates at the node pointed to by the (i − 1)st
edge of the set. The initial edge is an outgoing edge of the node located at the
beginning of the path. A path that returns to its initial node is called a cycle.
The nodes in the graph are arranged in levels. The top level consists of a
single node with no incoming edges, known as the root. This node corresponds
to the instance with the empty board. Each node of a level n is path length n
from the root node.
There is at least one path from the root node of the graph to every other
node of the graph. In fact, there are multiple paths from the root node to many
of the other nodes in the graph.
The game-state graph is an acyclic graph, or a graph in which there are no
cycles. Thus it is impossible to encounter an instance twice in a single game of
Quarto, since a piece placed on the board must remain there.
The parent of a node n is any other node that is incident to an incoming
edge of node n. The child of a node n is any node incident to an outgoing edge
from node n. A descendant of a node n is any node that can be reached on a
path originating at node n (including n’s children). The descendant subgraph
of a node n is the set of all nodes that are descendants of node n and their
incident edges, as well as node n itself.
Every vertex in the graph is labelled with the result of the corresponding in-
stance. That is, a vertex corresponding to an instance that is a win is labelled as
5
a win. Similarly, other nodes are labelled as draws. Any node corresponding to
an instance in which there is neither a draw nor a win is labelled undetermined.
The game-state graph includes inconsistent states. An inconsistent state is
an instance that cannot be reached during normal gameplay. For example, if
player 1 wins on the top row of the board after 4 moves, the game should end.
If we placed another winning quartet on the bottom row of the board, that
instance would be an inconsistent state, as the game should end before that
board is reached. An inconsistent state occurs when there are at least two wins
on the board that do not share any piece. We include these states in the game-
state graph to simplify its analysis. For the most part, they may be ignored.
Inconsistent states are always labelled as wins.
The subgraph of descendants of any node labelled win or draw will share that
same label. The subgraphs of nodes labelled as undetermined may be labelled
differently at different nodes.
A leaf is any vertex that has no outgoing edges. Leaves in the game state
graph correspond to instances in which the board is full (i.e. a draw; a full board
with a single win, or multiple wins that share one piece; or an inconsistent state
labelled as a win).
Definition 1 (Game-State Graph) A game-state graph is a directed acyclic
graph in which the vertices correspond to instances and the edges to moves that
transition between those instances. The root of the graph corresponds to the
instance with the empty board. There is at least one path from the root node
to every other node in the graph. Each vertex is labelled as win, draw, or
undetermined, corresponding to the result of the instance represented by that
vertex. The nodes in the descendant subgraph of any vertex labelled win or draw
share that vertex’s label. The game-state graph includes inconsistent states (i.e.
states that cannot be reached during normal gameplay), that are labelled as wins.
The leaves of the graph correspond to instances in which the board is full.
The game-state graph is a useful tool for building a Quarto player, as it
contains every possible move that a player could make. By searching the game-
state graph before making a move, a player can choose a path to the desired
outcome. One method of searching the game-state graph is called the Minimax
algorithm.
4.3 The Minimax Algorithm
The Minimax algorithm is a method of searching the game-state graph in order
to choose the move that will result in the best possible outcome from a given
instance. Although the Minimax algorithm is a top-down algorithm, it is easier
to understand from the bottom up.
Consider the subgraph of the game-state graph that contains only consistent
states. The leaves of this subgraph will all be draws or wins. Now consider the
parent node of a leaf. We mark4
this node with the outcome of the move that
4This use of marks is not part of the labelling from Definition 1. It is an extension of that
idea for use in the Minimax algorithm.
6
would be chosen by a perfect player confronted with the corresponding instance.
A parent node i might have the following set of leaves as children:
Instance i: ◦
Leaves: ◦ ◦
(draw) (draw)
Node i is marked as a draw. If however, the set of child leaves contains a
win as in,
Instance j: ◦
Leaves: ◦ ◦
(draw) (win)
the parent node j is marked as a win.
This same technique can be applied to the next set of parents. However, the
player at this level is the opponent. This opponent is faced with making a move
that results in instance i or in instance j.
k (draw)
Player 2: ◦
Outcome for Player 1: i (draw) ◦ ◦ j (win)
Leaves: ◦ ◦ ◦ ◦
(draw) (draw) (draw) (win)
A perfect player will move to instance i, because she would rather draw
than lose. We continue this process until the marks propagate back to the
instance faced by a player, where the optimal move resulting from that instance
is chosen. This method of propagating the marks of child nodes to their parents
while switching players at each level is the Minimax algorithm. The name comes
from the fact that one level will find the minimum outcome for the opponent
and the next will find the maximum outcome for the player making the move.
Minimax is conducted as a search starting from the root of the game-state
graph. It examines each descendant recursively and propagates marks back up
the graph to the root. Thus, after the Minimax algorithm completes, every
node n is marked with the result of the instance corresponding to the node at
the end of the path that would result from a game between two perfect players
starting at node n5
.
One problem with the standard Minimax algorithm is the amount of time
it takes to make a decision. Starting from the root, the algorithm examines
every node in the game-state graph. In Section 5 it is shown that the size of
this graph is intractably large for a complete search. Thus it is necessary to
eliminate some subgraphs from consideration.
5See [2] for a fuller treatment of the Minimax algorithm.
7
4.4 Minimax Enhancements
Consider the following game-state graph:
k
Current Player’s move: ◦
j
Opponent’s move: i (draw) ◦ ◦
↓
Outcome for opponent: ◦ ◦ ◦ ◦
(draw) (win) (draw) (loss)
If i is marked first, the current player knows that she can draw in the worst
case. When she begins to examine j’s descendant subgraph, she immediately
discovers that the opponent can force her to lose if she chooses to move from k
to j. Regardless of the other possible outcomes resulting from j, the worst case
would be a loss. Thus the rest of j’s descendant subgraph is ignored, or pruned
from the search.
This technique for eliminating subgraphs is called α-β pruning6
. It keeps
a lower bound α and an upper bound β that track the worst and best case
outcomes discovered at any point. These bounds are initialized to −∞ and
+∞ to allow consideration of every value in between. Minimax begins as usual
starting from the root.
As the game-state graph is examined, the lower bound is continually updated
with the best possible outcome, and the upper bound is continually updated
with the worst possible outcome. They converge towards an intermediate value
that is optimal for both players. Any node whose descendant subgraph yields
a worse lower bound than the previous lower bound is immediately eliminated
from consideration. Similarly, any node whose descendant subgraph yields a
better upper bound than the previous upper bound is immediately eliminated
from consideration. This prunes sections of the game-state graph that are not
optimal for either of the two players, and results in a smaller and faster Minimax
search.
Another pruning technique useful for Quarto is to stop searching immedi-
ately upon finding any move marked as a win. The standard version of Quarto
awards the same benefit for a win early in the game as it does for a late win.
Thus, there is no downside to selecting a move that will result in a win some
large number of turns later even if another move would result in a win on the
next turn. We call this pruning technique win short-circuiting, and it results in
a slightly faster Minimax search.
Recall that many nodes in the game-state graph may be reached by multi-
ple paths from the root. This implies that some subgraphs may be examined
repeatedly during a Minimax search. It is therefore helpful to cache7
the marks
associated with each node in the game-state graph if it takes less time to store
6For a more complete treatment, see [2, 3].
7To cache a value means to save it in semi-temporary storage to avoid recomputing it for
subsequent uses.
8
and reaccess them than it does to recompute them. While a variety of different
caching schemes are possible, we found that the cache design must be optimized
for fast lookup in order for it to reduce the search time. One possible approach
for designing a minimax cache for Quarto is to use the efficient representation
proposed in Section 5.
Another technique for speeding up the search of the game-state space is to
divide up the search across multiple processors. If 2 processors each examine 1
2
of the game-state graph, the Minimax search will complete in about 1
2 the time
it would take a single processor to search the entire game-state graph. However,
since the two subgraphs might share some vertices, the processors ought to be
synchronized8
so that both do not examine the shared sections of the game-state
graph.
Synchronization is both complicated and inefficient, because it requires spen-
ding computing resources on something other than the search itself. Thus par-
allel techniques entail a tradeoff between the degree of parallelism exploited and
the fraction of computing resources spent on directly useful work. That is, in-
creasing the number of processors searching the game-state graph increases the
degree of parallelism, but decreases the fraction of total computing resources
spent on the search itself.
Perhaps the most simple and useful extension of the general Minimax algo-
rithm is to limit the depth of the search. That is, starting from some vertex at
level l with a depth limit of d, the Minimax algorithm will examine all of the
nodes in levels l, l + 1, . . . , l + d. If the nodes at level l + d are not leaves,
then an estimate of the winnability of the game from those nodes is calculated,
and the nodes are marked accordingly. The function generating the estimate is
known as a heuristic function, and it gives an approximation to the result of the
full Minimax search.
In order for a depth-limited Minimax search to be useful, the heuristic func-
tion must be designed to return a good estimate of the winnability of an instance.
Another important requirement of heuristic functions is that they require only
limited input and take limited amounts of time. A heuristic function that re-
quires significant amounts of time to evaluate an instance will be less successful
at reducing Minimax search time than a function that evaluates quickly.
In fact, there is a tradeoff in the design of heuristic functions between the
time required to evaluate an instance and the quality of the evaluation. The
solution that gives the best evaluation and takes the most time is simply the
complete Minimax search. The solution that takes the least time and gives
the worst evaluation is a random guess or some similar strategy. An ideal
heuristic function will evaluate immediately with an accurate assessment of the
winnability of the instance in question.
One approach for designing heuristic functions is to reduce an intuitive un-
derstanding of the strategy of a game to a mathematical characterization based
on the important features of the instances. In practice, this process is very dif-
ficult, and it often results in unexpected behavior. For this reason, the heuristic
8See [4] for a discussion of synchronization.
9
function is an area in which machine learning is often applied.
There are various machine learning techniques such as neural networks that
can extract trends from sample information and generalize those trends when
confronted with new data. This type of system could take an instance as input
and output the projected winnability of that instance based on the previous
experience of the machine.
A generic machine learning algorithm applied to a heuristic function accepts
features that are deemed important by the programmer, and assigns weights to
them based on its experience. These weighted features are combined in some
function, whose output is the estimate of winnability. This output is fed back
as additional input in future iterations when the estimate of winnability may
be confirmed or denied, and the weights are adjusted based on the accuracy of
the previous performance.
In order to properly train a machine learning mechanism, a large body of
example games is necessary. One way to generate these games is to have the
machine play itself. However, a novice machine player will learn very little
from playing another novice machine player. Ideally, a human Quarto expert
would play a machine in thousands of games until the machine extracted enough
information to play well.
Given that access to human Quarto experts is limited, another approach is
to solve the game completely, marking every node of the game-state graph with
the outcome of the game originating at the corresponding instance as played by
two perfect players. This completed game-state graph could then be used as
input to a machine learning mechanism that would extract the strategy9
.
Unfortunately, marking the entire game-state graph brings us back to the
point that Minimax takes a long time to complete when the search space is
large. The next few sections will examine the use of the game state graph to
solve the game completely rather than to create a perfect player.
5 Solving Quarto
To solve a game is to discover the outcome when the game is played by perfect
players. A perfect player will always select a move whose result is the best
possible outcome for that player.
Conveniently, a perfect player in a game of complete information knows
exactly what move a perfect opponent will make. She puts herself in her oppo-
nent’s position, and makes the perfect move. This reasoning is the motivation
behind the Minimax algorithm presented in Section 4. It is commonly used to
propagate an outcome to the root of the game-state graph, and thus to solve
the game.
9In some ways, this proposal begs the question. If the game-state graph is completely
marked, why pursue a heuristic function for Minimax? The task of Minimax has already been
completed, and the marked game-state graph already models a perfect player. Our response
is that it is uninteresting to play a perfect player, and thus developing a successful machine
learning strategy is a better use of the annotated game-state graph than a perfect player would
be.
10
Unfortunately, the outcome of a game between two perfect players provides
little insight into the intuitive strategy of a game. Minimax reports the move
that a perfect player would make from a certain point in a game, but provides
no insight into why this move is chosen. Despite this fact, solving games has a
certain allure.
I had the pleasure of attending a talk given by Guy Steele ([5]). Steele
is a well-known computer scientist whose list of accomplishments includes the
invention of both the Scheme and Java programming languages. The title of his
talk was “Teeko: Solving A Game,” and it detailed some recreational research
that he conducted while working at Sun Microsystems.
The game of Teeko is a two-player game not unlike Quarto. The board is
a 5x5 grid, and each player attempts to place her four pieces in a closed box
while preventing the other player from doing so. There is no hidden information
in the game other than the opponent’s choice of where to move. Steele began
investigating Teeko many years ago, and finally succeeded in solving Teeko with
a brute-force search.
When I heard Steele’s results, I felt that Quarto might be solvable too. Later
we discovered that Luc Goossens, a researcher at CERN10
, had solved the game
two years ago.11
In the summer of 1998, he conducted a search and found that
two perfect players will always draw.
Rather than dismissing the task of solving Quarto as pass´e, we felt that there
were additional results to be found, and that a better approach to conducting a
search of Quarto’s game space existed. The following section will examine the
feasibility of solving Quarto using a Minimax search.
6 Feasibility
In order for a complete search of a game to be feasible, the game-state graph
must fit into a computer’s memory (e.g. 232
bytes12
) and should ideally be
much smaller. The number of nodes in Quarto’s game state graph is includes
inconsistent states as described in Section 4. Thus it is an upper bound on the
number of instances that are encountered in regular play.
For an i + 0 instance, there are i pieces, i places, and an ordering of the
pieces. This gives the number of distinct i + 0 instances as
|i + 0| =
16
i
·
16
i
· i! (1)
Similarly, adding another factor for another choice of piece, the number of
distinct i + 1 instances is
|i + 1| =
16
i
·
16
i
· i! · (16 − i) (2)
10The European Organization for Nuclear Research, at http://www.cern.ch/
11See [1].
12A byte is eight bits.
11
An equivalent alternative to these equations is to work inductively. Suppose
that there are n distinct instances of i + 0 boards for some i. Choosing a piece
introduces a factor of (16 − i), and choosing a placement introduces another
factor of (16 − i).
However, in order to create the same instance, the last piece added could
have been chosen and placed on any earlier move, and any of the i previously
selected pieces could have been placed on this move instead. In other words, to
avoid counting a single instance multiple times, we must factor out the number
of paths from the root node to this state in the game-state graph. The number
of paths is exactly i!, since this is an ordering of the previously selected pieces.
It can easily be verified that this inductive method yields the same results as
the previous equations.
Therefore, the total number of nodes in the game-state graph is
16
i=0
16
i
·
16
i
· i! +
16
i
·
16
i
· i! · (16 − i) (3)
This yields about 2.68466 · 1016
, or 254.57
instances. The available address
space in today’s machines has a maximum index of about 232
. It would be
impossible to store enough Quarto instances in memory simultaneously to allow
a complete search of the game-state graph to finish in a reasonable time frame.
Assuming that a machine could examine 1,000 states per second, it would take
over 848,000 years to examine the entire game-state graph.
One approach to reducing the difficulty of this task is to examine fewer
instances. If we can divide all Quarto instances into several equivalent sets,
then we need only examine one instance from each set. We can then generalize
the results to all of the instances.
In order to decide which representative instances to examine, we need to
develop a set of transformations that map instances to each other, a notion of
equivalence, and a procedure for picking the representative instance, or the nor-
mal form, for each set of equivalent instances. These three steps are examined
in the following section.
7 Transformations, Equivalence, and the Nor-
mal Form
A transformation is a process that maps one instance into another. Transfor-
mations are useful in many games. For example, a chess player might rotate the
board to look at the layout of the pieces from the opponent’s point of view. This
rotation is a transformation that maintains the relationships between the pieces
on the board. Similarly, in Quarto, a player might apply a transformation by
swapping every tall piece with a corresponding short piece to create a symmet-
ric board. The transformations examined in this section have been proposed by
12
others in the past13
, and are considered here together for the first time.
In general, a transformation could be any mapping of instances to instances.
A Quarto transformation specified in that manner requires about 254
individual
mappings. Furthermore, it might do something strange and useless. Consider
the transformation that maps any instance to the instance containing the fol-
lowing board14
:
=⇒
1
1
1
1
Another possible transformation maps any instance to the instance contain-
ing the empty board:
=⇒
These two examples are useless when it comes to eliminating instances from
consideration. The first creates instances that are not legal in Quarto, and the
second brings any Quarto game back to the initial state.
We would like to define transformations in a concise way, so that all 254
mappings are not enumerated. We would like to ensure that a transformation
applied to a legal Quarto instance outputs another legal Quarto instance, and
that both the preimage and the output have the same number of pieces on the
board. These are all properties of permutations, so we define a transformation
accordingly:
Definition 2 (Transformation) A transformation is a function that accepts
a Quarto instance. It first applies a permutation of the board positions (and con-
sequently the pieces that occupy those positions) and then applies a permutation
of the 16 distinct piece names, returning another Quarto instance.
Since a transformation is a pair of permutations, it has some useful proper-
ties:
Property 3 Transformations are one-to-one mappings.
This property comes directly from permutations. It ensures that no piece
will be duplicated or eliminated by a transformation. It further ensures that
distinct inputs of a transformation result in distinct outputs, and that distinct
outputs have distinct preimages.
13See [6, 1].
14A square occupied by a number indicates that a corresponding piece occupies that square.
The number, expanded to a 4-bit binary form, codes the piece’s attribute/value pairs. See
Attribute in Section 3.
13
Property 4 Transformations are onto mappings.
An onto mapping is one that maps to every element in the range. Since the
domain and range of transformations are the same set of instances, Property 3
shows that they are onto as well.
Property 5 Transformations preserve cardinality.
This property also comes directly from permutations. Applying a transfor-
mation to any instance results in an instance that has the same number of pieces
on the board.
Property 6 Transformations have inverses.
Any permutation has an inverse that reorders the elements of the permuted
set to what they were before the transformation was applied. By applying the
inverse of a transformation to an instance, we can discover what the preimage
was before the original transformation was applied.
Property 7 Transformations can be composed to create additional transforma-
tions.
We can apply one transformation to an instance, and then apply another
transformation to the result of the first one. These two transformations may
be composed to create a single transformation that accomplishes in one pair of
permutations what the others did in two pairs of permutations.
In terms of the game-state graph, transformations map nodes to nodes, and
thus they also map subgraphs to subgraphs. In fact, a transformation is a
mapping of one subgraph of the game-state graph to another subgraph, and
these subgraphs will be isomorphic.
Two graphs G and G are isomorphic if and only if there exists a one-to-one
and onto function f mapping the vertices of G to the vertices of G such that for
every pair of vertices u, v of G, (u, v) is an edge of G if and only if (f(u), f(v))
is an edge of G .
Lemma 8 Given a transformation t and an instance i such that t(i) = i ,
the descendant subgraphs of the nodes corresponding to instances i and i are
isomorphic.
Proof The mapping t is a one-to-one and onto mapping by Properties 3
and 4. Thus it maps all nodes to one other node, and no node in the range is
left without a preimage. Thus nodes map to nodes under t.
Since t is a pair of permutations, i and i have the same number of pieces
on the board. Thus if there is a move available from i, there must be a cor-
responding move available from i (although it may involve a different piece or
square), and their corresponding nodes in the game-state graph will each have
an outgoing edge. Thus edges are preserved by the mapping t.
14
Since nodes and edges of the game-state graph are preserved by t, the de-
scendant subgraphs of the nodes corresponding to any two instances mapped
by t are isomorphic.
If a transformation maps in a well-behaved manner, we may be able to
examine a subgraph and generalize to the rest of the game-state graph. In
order for this to work, the transformation must preserve the structure of the
subgraph. That is, it must preserve equivalence.
7.1 Equivalence
Intuitively, two equivalent Quarto instances may be distinct, but the layout or
selection of pieces in the instance will result in the same strategy. Consider the
following example:
1 2 3 4 4 3 2 1
These two instances are equivalent because the strategy in the first is essen-
tially identical to that of the second. The only difference between the instances
is the layout of the pieces on the board. The interaction between every pair
of pieces is maintained. Flipping the first board about its vertical axis is one
transformation that maps the first board to the second board. This transforma-
tion also happens to be its own inverse, since flipping the second board about
its vertical axis maps it back to the first.
Definition 9 (Equivalence) An instance i is equivalent to instance i if and
only if the descendant subgraph of i is isomorphic to the descendant subgraph of
j and the nodes of these two subgraphs have identical labels.
The result of Definition 9 is that two instances are equivalent if and only if
the resulting gameplay from both of them is effectively the same.
Lemma 10 Let some transformation t map an instance i to another instance
i . These two instances are equivalent if and only if for every sequence of moves
s originating at i, the label of the node corresponding to the result of that game
is identical to the label of the node corresponding to the result of the game
originating at i with a sequence of moves s = t(s).
Proof The instances i and i have corresponding nodes in the game state
graph. Lemma 8 guarantees that the descendant subgraphs of each are isomor-
phic.
Each move is an edge transitioning between two instances. Thus a sequence
of moves is a path through the game-state graph. If any path starting at the
node corresponding to i reaches a node with a different label from the node
reached by the path’s image starting at the node corresponding to i , the two
15
descendant subgraphs are not identically labelled, so by Definition 9 the two
instances are not equivalent.
Conversely, if the nodes along every path originating at the node correspond-
ing to i is labelled identically to the nodes on every path originating at the node
corresponding to i , then the descendant subgraphs must be identically labelled,
and thus i is equivalent to i by Definition 9.
Lemma 10 is a restatement of Definition 9 that introduces transformations.
Together with transformations, equivalence has some useful properties. Certain
transformations are constructed in such a way that they only map between
equivalent instances.
Definition 11 (Equivalence-preserving Transformations) A transforma-
tion t guarantees equivalence if and only if for every instance i, t(i) = i is
equivalent to i.
Lemma 12 Equivalence-preserving transformations may be composed to create
additional equivalence-preserving transformations.
Proof Suppose that some equivalence-preserving transformation t1 maps i
to i . Let another equivalence-preserving transformation t2 map i to i . The de-
scendant subgraph of the node corresponding to i is isomorphic and identically
labelled to the node corresponding to i by Definition 11. Similarly, the de-
scendant subgraph of the node corresponding to i is isomorphic and identically
labelled to the node corresponding to i . Thus there is some transformation
t3 that maps the nodes, edges, and labels of the descendant subgraph of i to
the nodes edges, and labels to the descendant subgraph of i , and it is also
equivalence preserving. The transformation t3 is the composition t2(t1()).
Any transformation may be tested to determine whether it guarantees equiv-
alence by mapping every instance and determining whether these mappings
satisfy Lemma 10:
Lemma 13 A transformation t guarantees equivalence if and only if for every
instance i, the node of the game-state graph corresponding to instance t(i) = i
is labelled identically to the node corresponding to i.
Proof This is a restatement of Lemma 10 with the sequence s set to ∅.
The only transformations of interest when comparing game states are those
that guarantee equivalence. It would be necessary to check the equivalence of
every pair of instances mapped by a general transformation. If we constrain
transformations to be equivalence-preserving, that check is unnecessary.
General transformations are composed of “atomic” transformations that fall
into two classes - positional and piecewise. The first class contains transforma-
tions that reorder the pieces on the board. The second contains transformations
that alter the value of each piece in an instance.
16
7.2 Positional Transformations
Positional transformations reorder the pieces on the board. In general, there
are 16! different positional transformations corresponding to the permutations
of the squares of the board.
Definition 14 (Positional Transformation) A positional transformation is
a permutation of the squares of the board, or a general transformation whose
piece-name permutation is the identity.
One such transformation swaps the pieces occupying the first two upper-left
squares of the board15
:
A B C D B A C D
E F G H =⇒ E F G H
I J K L I J K L
M N O P M N O P
Instances mapped by this transformation are equivalent if their first two
upper-left squares are empty. Suppose instead that square A is initially occupied
by a piece and square B is empty. When this transformation is applied, the
resulting instance is different in a way that affects gameplay: the piece originally
on square A occupied a diagonal, but after the mapping it is off of the diagonal.
This change means that it occupies only two winning positions (column 1 and
row 2) instead of three (column 1, row 1, left to right diagonal). It is possible
to construct a winning instance that maps to a non-winning instance under this
transformation.
More generally, a positional transformation guarantees equivalence if and
only if it maintains the interactions between quartets that occupy winning po-
sitions. It does not matter which winning positions the quartets occupy as long
as they are all intact on some winning position. The order of the pieces in the
quartet can change freely, provided that other quartets that include each of the
pieces remain intact.
The diagonal is the key to discovering the equivalence preserving positional
transformations. Consider the left to right diagonal:
A B C D
E F G H
I J K L
M N O P
We notice the following characteristics of the board:
• The elements of this quartet each occupy three winning positions: a row,
a column, and a diagonal.
15A square occupied by a letter may or may not be occupied by a piece. The letter is a
label for the square, and is used to illustrate the manner in which a positional transformation
rearranges the squares of the board (and, correspondingly, their resident pieces).
17
• Squares A and P “share” squares D and M, which are both on the other
diagonal.
• Squares F and K “share” squares G and J, which are both on the other
diagonal.
Any transformation that guarantees equivalence must ensure that each of
the three points above holds true for the resulting states. This suggests the
following list of transformations:
1. Swap outer rows and columns, swap inner rows and columns, swap outer
rows and columns: this brings squares A and P into the inner part of the
diagonal, reorders them, and then returns them to the outer part of the
diagonal. It also keeps their rows and columns aligned.
(
A B C D
E F G H
I J K L
M N O P
)
⇓
P N O M
H F G E
L J K I
D B C A
2. Swap inner rows and columns: this reorders squares F and K, keeping
their rows and columns aligned.
(
A B C D
E F G H
I J K L
M N O P
) =⇒
A C B D
I K J L
E G F H
M O N P
3. Swap outer rows and columns: this brings squares A and P to the inner
two diagonal locations, and brings F and K to the outer two diagonal
locations. It also keeps their rows and columns aligned.
18
A B C D
E F G H
I J K L
M N O P
=⇒
F E H G
B A D C
N M P O
J I L K
4. Transpose over diagonal: this functions just like a matrix transpose.
A B C D
E F G H
I J K L
M N O P
=⇒
A E I M
B F J N
C G K O
D H L P
5. Rotate 90◦
clockwise: this swaps the two diagonals and aligns the other
rows and columns.
A B C D
E F G H
I J K L
M N O P
=⇒
M I E A
N J F B
O K G C
P L H D
These five transformations are the basis for the only positional transforma-
tions that guarantee equivalence. Each one maps squares that originally occu-
pied a diagonal position to another diagonal position. Because of the “sharing”
that goes on between the pair of outer diagonal elements and the pair of in-
ner diagonal elements, one of each pair cannot be interchanged: both must be
interchanged.
It can be shown that these positional transformations guarantee equivalence.
Furthermore, any positional transformation that guarantees equivalence must
be some composition of these five transformations. This proof follows.
Lemma 15 A positional transformation guarantees equivalence if and only if
it preserves win-placements.
Proof Let us assume that there is some transformation t that does not
preserve a win-placement. We construct a winning instance i such that the only
pieces on the board are the elements of the win-placement that is not preserved
by t. t(i) = i is clearly not a win, thus t does not guarantee equivalence by
Lemma 13.
Now consider some transformation t that preserves win-placements. Be-
cause it is a permutation, it must map the empty board to itself, clearly an
equivalence-preserving operation. Assume then that t preserves equivalence in
some mapping t(ip) = ip. Adding an additional piece to ip creates ip+1. If the
addition of this piece created a win, then t(ip+1) = ip+1 is also a win, as the
newly-completed win-placement is preserved by t. Similarly, if adding a piece
19
to ip makes it a draw, then t(ip+1) = ip+1 is a draw, since the newly added
piece participates in the same win-placements in ip+1 as it did without creating
the conditions for a win in ip+1. Thus, by induction on the number of pieces
on the board in an instance, t preserves labels of the nodes of the game-state
graph when mapping between any two instances. Thus, by Lemma 13, t is an
equivalence-preserving transformation.
Lemma 16 Any positional transformation that guarantees equivalence must
map the set of rows to rows or to columns, but not to a combination thereof,
and not to the diagonals.
Proof First observe that permutations preserve the cardinality of inter-
sections of sets: |A ∩ B| = |π(A ∩ B)|. The intersection of any two rows is
the empty set, and therefore any permutation of an instance must preserve this
property.
Let t be an equivalence-preserving transformation. If t maps a row to a
column while mapping another row to a row, they must share an element, which
contradicts the property of permutations above. t cannot preserve equivalence
in this case.
If t maps a row to a diagonal, there is only one winning position remaining
that does not share an element with the occupied diagonal: the other diagonal.
Therefore, the row mapped to the first diagonal must share an element with at
least two other rows. t cannot preserve equivalence in this case.
Thus t must map all rows to rows or it must map all rows to columns.
Corollary 17 Any positional transformation that guarantees equivalence must
map the set of columns to rows or to columns, but not to a combination thereof,
and not to the diagonals.
Proof We observe that the proof of Lemma 16 applies to columns as well
as to rows.
Corollary 18 Any positional transformation that guarantees equivalence must
map diagonals to diagonals.
Proof There are 10 winning positions on the board. A mapping assigns
8 of them to rows and columns by Lemma 16 and Corollary 17. The 2 that
remain for the diagonal win-placements are the diagonal winning positions.
Consider all 16! possible positional transformations. Any of these that map
diagonal elements to non-diagonal positions are eliminated by Corollary 18.
Those remaining that mix the diagonal elements between the two diagonals are
eliminated by Lemma 15.
All of the remaining positional transformations map the left to right diagonal
to itself or to the other diagonal. These form two cosets16
of positional transfor-
16A set may be partitioned into disjoint subsets of equal cardinality. If there is a one-to-one,
onto mapping from the elements of one of these sets to the elements of every other, then these
sets are cosets. In this case, the mapping of the left to right diagonal to the other diagonal is
that one-to-one, onto function. See [7] for a further treatment of cosets.
20
mations. Let us consider the case in which the left to right diagonal is mapped
to itself. It contains 4! transformations, corresponding to the permutations of
the elements in a diagonal win-placement.
Lemma 19 Any positional transformation that guarantees equivalence must
map the outer squares of a diagonal either to the outer squares of a diagonal or
to the inner squares of a diagonal, but not to a combination thereof.
Proof Let some positional transformation t swap one of the outer squares
of the diagonal with one of the inner squares of that same diagonal:
Instance i
A •
F
K
P
t
=⇒
Instance i
F
A •
K
• P
The row of A in instance i shares an element with the column of P, and
that element occupies a square on the other diagonal winning position. When
A is mapped to the inner part of the diagonal, its row must map with it by
Lemma 15. Furthermore, by Lemma 16, this row must end up as either a
row or column in i . In either case, the shared element no longer occupies a
diagonal square, violating Corollary 18. Thus t does not guarantee equivalence
and Lemma 19 is proven.
Lemma 19 eliminates a number of the 4! positional transformations under
consideration, leaving two cosets of transformations. The first maps rows to
rows, and the second maps rows to columns. Let us consider only the set that
maps rows to rows.
This set contains three positional transformations and their compositions.
They reorder the outer pair of diagonal elements, reorder the inner pair of
diagonal elements, and swap the outer and inner pairs of diagonal elements.
These are transformations (1)–(3) listed above.
The one-to-one function that maps the elements of this coset to their com-
plements in the other coset must maps rows to columns and vice versa. This is
transformation (4) listed above.
Lemma 20 If an equivalence-preserving positional transformation enumerates
the order of the elements of one of the two diagonals, specifies which diagonal
this quartet maps to, and specifies the placement of one other piece, it completely
determines the resulting instance.
Proof The nondiagonal piece was an element of the row of one of the
diagonal pieces in the original instance. Its placement determines the mapping
of rows to rows or rows to columns dictated by Lemma 16 and Corollary 17. The
remainder of the rows are mapped to the winning positions occupied by their
diagonal elements, and the columns are also mapped accordingly, as dictated by
Lemma 15. This fixes the square of every piece on the board at the intersection
21
of the row of one diagonal element and the column of another diagonal element.
Thus the instance is completely determined.
Now consider the original two cosets of transformations. The first mapped
the left to right diagonal to itself, and the other mapped it to the right to left
diagonal. The function that maps between them is the transformation that maps
one diagonal to the other diagonal. This is transformation (5) listed above.
Theorem 21 Any positional transformation that guarantees equivalence is a
composition of the five positional transformations proposed above.
Proof Suppose some equivalence-preserving transformation t maps in-
stance i to instance j.
1. If the outer squares of the left to right diagonal of i are reversed in the
corresponding diagonal of j, then apply transformation (1) listed above to
i, creating i1. Otherwise let i1 = i.
2. If the inner squares of the left to right diagonal of i1 are reversed in the
corresponding diagonal of j, then apply transformation (2) listed above to
i1, creating i2. Otherwise let i2 = i1.
3. If the outer squares of the left to right diagonal in i2 are located on the in-
ner squares of the corresponding diagonal of j, then apply transformation
(3) listed above to i2, creating i3. Otherwise let i3 = i2.
4. If the left to right diagonal of i3 is mapped to the other diagonal of j,
then apply transformation (5) listed above to i3, creating i4. Otherwise
let i4 = i3.
5. If the rows of i4 are mapped to the columns of j, then apply transformation
(4) listed above to i4, creating i5. Otherwise let i5 = i4
First we observe that the transformations mapping i to i5 are each individually
equivalence-preserving, and by Lemma 12 their composition preserves equiv-
alence. Let us call this composition t . t has enumerated the order of the
elements of the left to right diagonal of i, specified the mapping of the rows of i
to either rows or columns of i5, and specified whether the left to right diagonal
of i maps to itself or to the other diagonal of i5. Thus, by Lemma 20, t has
completely specified the board of i5, and i5 must be identical to j. Thus t
is indistinguishable from t, and any equivalence-preserving positional transfor-
mation may be decomposed into some composition of transformations (1)–(5)
proposed above17
.
Let us take an example transformation t:
17Note that steps (4) and (5) both apply transformations that map rows to columns and
vice versa. As a result of this, the rules for applying these transformations are slightly more
complicated than those listed above. The enumeration of these rules is left as an exercise for
the reader.
22
Instance i
A B C D
E F G H
I J K L
M N O P
t
=⇒
Instance j
P H L D
N F J B
O G K C
M E I A
For step (1), we observe that the outer squares of the left to right diagonal
in i are reversed in j, so we apply transformation (1):
Instance i
A B C D
E F G H
I J K L
M N O P
transformation (1)
=⇒
Instance i1
P N O M
H F G E
L J K I
D B C A
For step (2), we observe that the inner squares of the left to right diagonal
in i1 are not reversed in j, so we do not apply transformation (2):
Instance i1
P N O M
H F G E
L J K I
D B C A
(no transformation)
=⇒
Instance i2
P N O M
H F G E
L J K I
D B C A
For step (3), we observe that the outer squares of the diagonal in i2 map to
the outer squares of the diagonal in j, so we do not apply transformation (3):
Instance i2
P N O M
H F G E
L J K I
D B C A
(no transformation)
=⇒
Instance i3
P N O M
H F G E
L J K I
D B C A
For step (4), we observe that the left to right diagonal in i3 maps to itself in
j, so we do not apply transformation (5):
Instance i3
P N O M
H F G E
L J K I
D B C A
(no transformation)
=⇒
Instance i4
P N O M
H F G E
L J K I
D B C A
For step (5), we observe that the rows in i4 map to columns in j, so we apply
transformation (4):
Instance i4
P N O M
H F G E
L J K I
D B C A
transformation (4)
=⇒
Instance i5
P H L D
N F J B
O G K C
M E I A
23
Instance i5 is identical to instance j, and t is a composition of positional
transformations (1) and (4). These five transformations and their compositions
are the only candidates for positional equivalence-preserving transformations.
Theorem 22 The five transformations listed above and their compositions form
the maximal set of positional transformations that guarantee equivalence in
Quarto. This set contains 25
= 32 distinct elements.
Proof By inspection, the five transformations listed above preserve win-
placements. Thus, by Lemma 15 they are equivalence-preserving. By Lemma 12
their compositions are equivalence preserving. Finally, the 32 possible compo-
sitions are distinct as shown by their enumeration (See Appendix).
7.3 Piece Transformations
The other class of transformations that we consider are those that alter the
values of individual pieces without permuting the squares of the board. Piece
transformations are permutations of the piece names.
Definition 23 (Piece Transformation) A piece transformation is a permu-
tation of the piece names in a Quarto instance, or a general transformation
whose positional permutation is the identity.
One way of specifying this permutation is to consider the pieces as 4-bit
strings, and the transformation as a one-to-one onto mapping whose domain
and range are both the set of pieces [0, 15].
For example, one possible transformation is to exchange every dark piece in
an instance with the corresponding pale piece. This is equivalent to toggling
the bit in the piece names that corresponds to color.
Another example piece transformation might specify that the pattern of
short and tall pieces in an instance should be exchanged with that instance’s
pattern of round and square pieces. That is, every short piece should be replaced
by the corresponding round piece (and tall with square), while every round piece
should be replaced with the corresponding short piece (and square with tall).
These two classes of piece transformations can be expressed as functions on
the bits that represent the pieces, which, in turn, encode a permutation of the
piece names. We consider two classes of piece transformations as follows:
1. There are 16 equivalence-preserving piece transformations that XOR18
the
bits of every piece with some constant c such that 0 ≤ c ≤ 15.
2. There are 24 equivalence-preserving piece transformations that permute
the bits of every piece in one of 4! = 24 ways.
18XOR is a function of two 1-bit inputs that returns 1 if the inputs differ and 0 if they are
identical. When extending the inputs to n-bit strings, the ith bit of the output corresponds
to the ith bits of the inputs. XORing with a constant toggles bits of the input for each bit
where the constant has a 1 bit.
24
The first class toggles a set of bits of each piece. This corresponds to the
first example transformation mentioned above. The second class exchanges
attributes as in the second example transformation mentioned above.
It can be shown that these piece transformations and their compositions
guarantee equivalence. In fact, any equivalence-preserving piece transformation
can be constructed as a composition of these two classes of transformations.
This proof follows.
Lemma 24 A piece transformation t guarantees equivalence if and only if ∀
quartets Q with a value v (win or draw), t(Q) = Q has the same value v.
Proof Suppose a piece transformation t maps some winning quartet Q to a
draw quartet Q . We construct a winning instance i in which the board contains
Q on some winning position and nothing else. The instance t(i) = i is not a
winning instance, and thus by Lemma 13 t does not guarantee equivalence.
Suppose a piece transformation t maps some draw quartet Q to a winning
quartet Q . We construct a draw instance j in which the board contains Q
on some winning position and the rest of the winning positions contain draw
quartets. The instance t(j) = j is a winning instance, and thus by Lemma 13
t does not guarantee equivalence. Thus any piece transformation that does not
preserve the values of quartets does not guarantee equivalence.
Consider some piece transformation t that preserves the values of quartets. If
we construct a winning instance i, the instance t(i) = i must also contain a win,
as the winning win-placement in i must be mapped to a winning win-placement
in i . Similarly, if we construct a draw instance j, the instance t(j) = j must
also be a draw, as every draw win-placement in j must be mapped to a draw
win-placement in j . Thus any piece transformation that preserves the values
of quartets guarantees equivalence, and Lemma 24 is proven.
Definition 25 (Kill Piece) Suppose some set P of pieces shares some set of
bits across every element. A kill piece is any piece k such that the set P ∪ {k}
shares no bits across every element.
Definition 26 (Non-kill Piece) Suppose some set P of pieces shares some
set of bits across every element. A non-kill piece is any piece n such that the
set P ∪ {n} shares some set of bits across every element.
As an example, consider a set P = {10002, 11112}. Any two pieces of the
form 1 ∗ ∗∗2 will complete a winning quartet. These are non-kill pieces. If
a quartet containing P contains any piece of the form 0 ∗ ∗∗2 it cannot be a
winning quartet. These are kill pieces.
Lemma 27 If a piece transformation guarantees equivalence, it must preserve
the Hamming distances19
between every pair of pieces.
19Hamming distance measures the difference between two bit-strings. If a two strings have
a Hamming distance of 1, it means that a single bit is different (e.g. 1111 and 1101). A precise
definition is that the Hamming distance between two strings is the number of 1 bits in their
XOR.
25
Proof Let P1 be a pair of pieces with Hamming distance 1. Since they
share three bits, any kill piece for this set must differ on these three bits. There
is one bit remaining, determining 2 kill pieces. The complement of the set of
kill pieces contains 12 non-kill pieces.
Let P2 be a pair of pieces with Hamming distance 2. Since they share two
bits, any kill piece for this set must differ on these two bits. There are two bits
remaining, determining 4 kill pieces. The complement of the set of kill pieces
contains 10 non-kill pieces.
Let P3 be a pair of pieces with Hamming distance 3. Since they share one bit,
any kill piece for this set must differ on this bit. There are three bits remaining,
determining 8 kill pieces. The complement of the set of kill pieces contains 6
non-kill pieces.
Let P4 be a pair of pieces with Hamming distance 4. Each piece in P4 is the
complement20
of the other, and thus there is no winning quartet that can be
constructed containing P4.
Suppose that some piece transformation t1→2 maps P1 to P2. There are 4
kill pieces for P2 and 2 kill pieces for P1, so there are at least two kill pieces
for P2, kP2,1 and kP2,2, whose preimages under t1→2 are non-kill pieces for P1.
Thus we can construct a winning instance i such that the only occupied win-
placement on the board contains P1 and the preimages of kP2,1 and kP2,2. i is a
winning instance and t1→2(i) = i is not a winning instance. Thus by Lemma 13
any piece transformation that maps a pair of pieces with Hamming distance 1
to a pair of pieces with Hamming distance 2 does not preserve equivalence.
The inverse of t1→2, t−1
1→2, maps the non-winning instance i to the winning
instance i, and thus by Lemma 13, any piece transformation that maps a pair
of pieces with Hamming distance 2 to a pair of pieces with Hamming distance
1 does not preserve equivalence.
Suppose that some piece transformation t1→3 maps P1 to P3. There are 8
kill pieces for P3 and 2 kill pieces for P1, so there are at least two kill pieces
for P3, kP3,1 and kP3,2, whose preimages under t1→3 are non-kill pieces for P1.
Thus we can construct a winning instance i such that the only occupied win-
placement on the board contains P1 and the preimages of kP3,1 and kP3,2. i is a
winning instance and t1→3(i) = i is not a winning instance. Thus by Lemma 13
any piece transformation that maps a pair of pieces with Hamming distance 1
to a pair of pieces with Hamming distance 3 does not preserve equivalence.
The inverse of t1→3, t−1
1→3, maps the non-winning instance i to the winning
instance i, and thus by Lemma 13, any piece transformation that maps a pair
of pieces with Hamming distance 3 to a pair of pieces with Hamming distance
1 does not preserve equivalence.
Suppose that some piece transformation t2→3 maps P2 to P3. There are 4
kill pieces for P2 and 8 kill pieces for P3, so there are at least 2 kill pieces for P3,
kP3,1 and kP3,2, whose preimages under t2→3 are non-kill pieces for P2. Thus we
can construct a winning instance i such that the only occupied win-placement
on the board contains P2 and the preimages of kP3,1 and kP3,2 under t2→3. i is a
20A complement of a binary number is the result of flipping every bit.
26
winning instance and t2→3(i) = i is not a winning instance. Thus by Lemma 13
any piece transformation that maps a pair of pieces with Hamming distance 2
to a pair of pieces with Hamming distance 3 does not preserve equivalence.
The inverse of t2→3, t−1
2→3, maps the non-winning instance i to the winning
instance i, and thus by Lemma 13, any piece transformation that maps a pair
of pieces with Hamming distance 3 to a pair of pieces with Hamming distance
2 does not preserve equivalence.
Suppose some piece transformation tn→4 maps one of P1, P2, or P3 to P4. We
can construct a winning instance i such that the only occupied winning position
on the board contains one of the former three pairs of pieces and is completed
by two corresponding non-kill pieces. Instance i is a winning instance and
instance tn→4(i) = i is a non-winning instance. Thus by Lemma 13 any piece
transformation that maps a pair of pieces with Hamming distance less than 4
to a pair of pieces with Hamming distance 4 does not guarantee equivalence.
The inverse of tn→4, t−1
n→4, maps the non-winning instance i to the winning
instance i, and thus by Lemma 13, any piece transformation that maps a pair
of pieces with Hamming distance 4 to a pair of pieces with Hamming distance
less than 4 does not guarantee equivalence.
We do not consider pairs of pieces with Hamming distance 0, as these two
pieces are identical, and the rules of Quarto allow distinct pieces only. Thus any
piece transformation that does not preserve pairwise Hamming distances does
not guarantee equivalence.
Lemma 28 XORing any set of n-bit strings with the same n-bit constant both
preserves the pairwise Hamming distances and preserves equivalence.
Proof Consider the 1-bit strings 1 and 0. The pairwise Hamming distance
is 1. XORing them by 1 produces 0 and 1, and the pairwise Hamming distance
is still 1. Similarly, XORing them with 0 produces 1 and 0, and the pairwise
Hamming distance remains 1. Thus XOR by a constant preserves the pairwise
Hamming distance of 1-bit strings.
Any n-bit string is a contatenation of n 1-bit strings, and by preserving the
Hamming distances between the individual 1-bit strings, XOR preserves the
Hamming distance between the n-bit strings.
Consider an XOR transformation t and an instance i such that t(i) = i . If i
contains a win, then i contains a win, as the common bits of the pieces on the
winning win-placement are shared in i , although their value may be flipped.
Similarly, if i is a draw, then i must be a draw, although the values of some set
of bits of each piece may be toggled. Regardless, there is still no bit shared by
all 4 pieces of any win-placement. Thus by Lemma 13 t preserves equivalence.
Lemma 29 A permutation of the bits of a set of bit strings both preserves the
pairwise Hamming distances and preserves equivalence.
Proof Consider some permutation π of bits of a set of bit strings. For any
strings s1 and s2, the ith
bit of the string is mapped to the jth
bit of s1 and s2.
27
The Hamming distance between s1 and s2 will be identical to that of s1 and s2
as the individual bits are still aligned in the jth
position. Thus bit-permutations
preserve pairwise Hamming distances.
Consider some bitwise-permutation transformation t and an instance i such
that t(i) = i . If i contains a win, then i contains a win, as the common bits of
the pieces on the winning win-placement are shared in i , although the position
of those bits may have changed. Similarly, if i is a draw, then i must be a draw
with the positions of some set of bits of each piece permuted. There is still no
bit shared by all 4 pieces of any win-placement. Thus by Lemma 13 t preserves
equivalence.
Lemma 30 If the pairwise Hamming distances are known between some piece
p and each of the “elementary” pieces 00012, 00102, 01002 and 10002, as well
as the zero piece 00002, then p is completely determined.
Proof Suppose that d is the Hamming distance between p and 00002. If
the Hamming distance between p and the elementary piece with a 1 in the ith
position is d − 1, then the ith
bit of p must be a 1. If the Hamming distance
is d + 1, then the ith
bit of p must be a 0. This algorithm can be applied to
discover every bit of p since every elementary piece is available, and thus p is
completely determined.
Theorem 31 Any piece transformation that guarantees equivalence must be a
composition of an XOR by some 4-bit constant and a bitwise permutation of the
resulting 4 bits.
Proof Assume that some equivalence-preserving transformation t maps
instance i to instance j. Order the pieces of i, and match them with the corre-
sponding pieces of j. Piece 00002 in j has some preimage in i. XOR every piece
in i with the preimage of 00002 in j, yielding i , and call this transformation x.
Now, the piece in i that corresponds to the preimage of 00002 in j is 00002.
Originally we had
i j
... t
...
preimage =⇒ 00002
...
...
Now we have:
i i j
... x
...
...
preimage =⇒ 00002 00002
...
...
...
28
Since t guarantees equivalence, we know from Lemma 27 that the pairwise
Hamming distance between any two pieces in i is the same as the Hamming
distance between the corresponding pair in j. Similarly, XORing by a constant
preserves pairwise Hamming distances by Lemma 28. Thus the pairwise Ham-
ming distances are identical in i and j. Furthermore, i is equivalent to j, as
it is the image of j under the transformation x(t−1
(j)) = i (we know t−1
ex-
ists from Property 6 and we know the composition preserves equivalence from
Lemma 12).
Now examine the four pieces in i that are Hamming distance 1 from 00002.
Each of them contains a single 1 bit. Compose some bitwise permutation y such
that these four pieces in i are mapped to their corresponding pieces in j (which
must also be Hamming distance 1 away from 00002). Apply y to i to create i .
Now we have:
i i i j
... x
... y
...
...
preimage =⇒ 00002 =⇒ 00002 00002
preimage 00102 00012 00012
preimage 00012 00102 00102
preimage 10002 01002 01002
preimage 01002 10002 10002
...
...
...
...
Pairwise Hamming distances were the same between i and j, and since y is
a bitwise permutation, it preserves pairwise Hamming distances by Lemma 29.
Thus pairwise Hamming distances are identical in i and j. Furthermore, i and
j are equivalent, as i is equivalent to j, and the bitwise permutation applied to
i to produce i preserves equivalence by Lemma 29.
There are four elementary pieces in i that correspond to four elementary
pieces in j, and each of them also has a corresponding zero piece. Pairwise
Hamming distances are identical in i and j, and by Lemma 30 i is completely
determined. Thus it must be identical to j, and t(i) = y(x(i)) = j.
This proof constructed any equivalence-preserving piece transformation as
a composition of XOR with a constant and bitwise permutations by extracting
information from the preimage and result and using the property of pairwise
Hamming distance to constrain the intermediate states.
Remark An n-dimensional hypercube is an undirected graph with 2n
nodes labelled by n-bit strings in which an edge connects any two nodes with a
Hamming distance of one21
.
Suppose that we map the pieces to the 16 nodes of the 4-dimensional hy-
percube22
. Consider a winning quartet: we fix one bit across all 4 pieces, and
three free bits remain. These map to 8 nodes in the 4-dimensional hypercube
21See [8].
22See [9].
29
whose labels share one bit. Those nodes determine a 3-dimensional subcube of
the 4-dimensional hypercube. In fact, there are 8 subcubes, corresponding to
the 8 ways to construct a winning quartet (4 bits · 4 values).
An automorphism of a graph G is an isomorphism of G with itself. In any
automorphism of a hypercube, the subcubes are preserved23
. When mapped
back to Quarto, the transformation resulting in the automorphism of the hy-
percube preserves winning quartets and thus it must guarantee equivalence by
Lemma 24.
Harary states that any automorphism of an n-dimensional hypercube is a
composition of a bitwise permutation of the labels and an XOR of the labels
with some n-bit constant24
. Thus the result of Theorem 31 can be reached
by reducing the problem of Quarto transformations to automorphisms of the
hypercube.
Theorem 32 The piece transformations of XOR by a constant and bitwise per-
mutation listed above and their compositions form the maximal set of piece trans-
formations that guarantee equivalence in Quarto. This set contains 16·24 = 384
distinct elements.
Proof Lemma 12 shows that compositions of the proposed piece transfor-
mations guarantee equivalence. Theorem 31 shows that there are no equivalence-
preserving piece transformations that are not a composition of these two classes
of transformations. We have observed that they are distinct by an enumera-
tion25
.
7.4 General transformations
The addition of piece transformations to the positional transformations exam-
ined above yields in a total of 32·16·24 = 12, 288 possible equivalence-preserving
general transformations.
Theorem 33 The positional and piece transformations listed in the previous
two sections and their compositions form the maximal set of equivalence-preserv-
ing transformations in Quarto. This set contains 32 · 384 = 12, 288 distinct
transformations.
Proof This follows directly from the conjunction of Theorems 22 and
32, with the observation from Lemma 12 that compositions of equivalence-
preserving transformations are themselves equivalence-preserving. We have ob-
served that these 12,288 transformations are distinct in an enumeration26
. .
The distinctness of the 12,288 transformations we considered depends heavily
on the instances mapped. We have verified that the 12,288 transformations
23See [10].
24See [10].
25This enumeration is too large to be included in this paper.
26This enumeration is too large to be included in this paper.
30
are distinct for some instances in which the board is fully occupied. Other
fully-occupied boards only allow 384 distinct transformations—the number of
compositions of the piece transformations.
Remark In fact, these compositions form an algebraic group. A group
G is a set of elements and a binary operation ◦. The group must be closed
under ◦, such that ∀g1, g2 ∈ G, g1 ◦ g2 ∈ G. The binary operation ◦ must be
associative. There must be some identity element e such that ∀g ∈ G, g ◦ e = g.
Finally, ∀g ∈ G, ∃g−1
such that g ◦ g−1
= e. In this group, each element is
a transformation. The binary operation ◦ is composition, and the identity is
the identity transformation. The inverse of each element is the transformation
that permutes the piece names and their locations back to their original values
before the transformation27
.
7.5 Normalization
Equivalence-preserving transformations are useful in two ways. If we have a
single instance, we can apply each of these transformations to build a set of
distinct equivalent instances known as an equivalence class. Alternatively, if
we have an equivalence class, we can reduce this set to a single representative
instance—the normal form.
In order for the normal form to function as a representative instance, it must
be defined so that any two instances in the same equivalence class reduce to the
same normal form. If two instances reduce to different normal forms, it is a
signal that those two instances are not equivalent.
The normal form is defined by the sequence of steps that generate it, and
that sequence is chosen to ensure that all equivalent instances are reduced to
the same normal form. The sequence we use is as follows:
1. All of the positional symmetries are applied to the original instance, gen-
erating 32 equivalent, possibly distinct instances. Each of these instances
has an associated tag. This tag, or positional bit-mask, is a 17-bit string
in which the ith
bit is set if the ith
square of the board is occupied. The
squares are ordered as follows:
0 (+1 position)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Those instances that share the largest tag are candidates for the normal
form.
2. Each candidate instance is mapped with an XOR piece transformation.
The constant used is the value of the piece on the first occupied square
27See [7] for more on groups.
31
using the order from step (1). This results in the first occupied square
containing the piece 00002.
3. All 24 bitwise-permutation piece transformations are applied to each of
the candidate instances, and the resulting instances are compared to each
other with a string comparison. That is, the pieces occupying the positions
are compared in the order from step (1). They are treated as “characters”,
and the instance that results in the lexicographically least string is selected
as the normal form.
Thus the normalization procedure selects a positional layout for the pieces
on the board, toggles the bits of those pieces to bring the “first” piece to zero,
and then selects the bitwise permutation that generates the instance occuring
first in a lexicographic ordering.
With the tools of transformations and the normal form, the game-state graph
may be reduced to a subgraph containing only distinct nonequivalent instances.
The next step is to ensure that these instances can be stored efficiently. The
following section proposes an algorithm for encoding a Quarto instance in fewer
than 64 bits.
8 Efficient Representation
The state saved at any point in a search of the game-state graph must be able to
fit into the 232
bytes available on a modern machine. After reducing the number
of instances examined through pruning and normalization, we can reduce the
space required to store each instance by choosing an efficient representation for
instances.
Let us call a representation of a Quarto instance an instance-string. The
simplest way to store an instance is to create a string of 17 5-bit characters. The
first character corresponds to the +1 position, and the subsequent characters
each correspond to a square on the board. If the value of a character is in [0, 15],
then the corresponding Quarto piece is represented. Otherwise, the character
signals that no piece occupies that character’s position. This scheme requires
85 bits to store an instance. This is inefficient because it does not exploit the
characteristics of Quarto. We know, for example, that every piece appears at
most once on the board. We can use this knowledge to construct a more efficient
instance-string format.
The first step in constructing a more compact representation of an instance
is to assign some ordering to the squares of the board and the +1 position. We
construct a 17-bit tag in which the ith
bit is set if the corresponding square is
occupied. This tag forms the first part of the instance-string. We adopt the
following ordering:
0 (+1 position)
32
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
If we know a priori that the number of pieces on the board is p, we only
need to store as much of the tag as it takes to hold the p 1s. This results in a
variable-length tag that requires 17 bits in the worst case.
Now that we have a compact way of representing the positions occupied on
the board, we must represent the pieces corresponding to those positions. A
simple representation would hold one 4-bit character for each 1 in the tag.
It is easy to do this in fewer bits. We construct an ordered list of all Quarto
pieces (0 to 15). Instead of representing the piece values themselves, the bits
of the instance-string following the tag contain the indices into this list for the
pieces occupying the corresponding squares on the board. Each time that a piece
is indexed in the list, it is removed and the list grows shorter. The maximum
index into the list decreases as it shrinks. As a result, we can reduce the number
of bits required to index the list as pieces are removed. For a list of length l,
the number of bits required to index the list is log2 l .
If the board is full, the last piece does not need to be represented, as it
can be discovered by the process of elimination. Furthermore, if the board is
normalized, we know that the first piece encountered will always be 0, so we can
eliminate it from the instance-string and the ordered list. The resulting number
of bits required for this portion of the instance-string is
14
i=1
log2 i (4)
yielding 45 bits as follows:
Bits required: 4 4 4 4 4 4 4 3 3 3 3 2 2 1
Max list index: 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Using this scheme, there are at most 17 tag bits and 45 index bits, for a
total of 62 bits (a 27% reduction from the naive scheme).
A normalized board is more likely to have more pieces close to the beginning
of the tag order than at the end, due to the normalization algorithm (it selects
the equivalent instance with the largest tag). This means that the tag will likely
be shorter, saving bits.
An additional optimization would invert the meaning of the tag for boards
which are more than half full. That is, each 1 in the tag would correspond to an
empty position rather than a full position, and normalization could be altered
accordingly to prioritize instances with pieces farther from the beginning of the
tag order. Using the inverted-tag scheme, it is more likely that the tag will be
shorter, but the worst case scenario still requires a 17-bit tag.
The downside to using a compressed instance-string is the time required to
encode, decode, read, and write the strings. The encode and decode penalties
33
are the price paid for space savings. However, the read and write penalties
are significantly higher than those of the encode and decode procedures. If
a variable-length scheme is implemented, a collection of contiguous instance-
strings must be traversed sequentially to find the location of a specific instance-
string. Furthermore, each of these instance-strings must be decoded in order to
find the boundaries between them.
A fixed-length encoding allows for a quicker look-up, but unless the instance-
strings are byte-aligned, a specific string may still have to be extracted using bit
operations before being easily accessible. Given that the worst-case instance-
string length is 62 bits, we choose to allocate 64 bits per instance-string (wasting
only 2 bits for every 64 - a loss of only 3%). This allows for fast indexing and
aligned storage, as well as for convenient manipulation in a built-in data type
(C provides a 64-bit long integer type).
We decline to use the more complex inverted-tag scheme as the additional
savings only materialize with a variable-length scheme. We also store all 17 bits
of the tag regardless of the number of pieces on the board. This reduces the time
required to encode and to decode instance-strings and removes the requirement
of knowing how many pieces are on the board.
Furthermore, if all of the significant bits of the instance are pushed to the
least-significant end and all of the leftover bits are set to 0, integer comparison
operators can be used to distinguish boards rather than slower string comparison
operators. Finally, by splitting each 64-bit string into two 32-bit pieces, we
can index a sparse two-dimensional data structure holding the labels for the
corresponding nodes in the game-state graph. In this scheme, only the label
associated with an instance is stored, and the instance-string is encoded by its
location in memory.
Consider the following normalized board:
0 (+1)
2
3
This instance would have been represented by an 85-bit instance-string in
the naive implementation. Using the 64-bit compression scheme, the string is
the concatenation of the following bits:
11000001000000000 0010 0010 . . .
17-Bit Tag Implicit 0 Piece 1 Piece 2 39 Bits Ignored
This efficient representation requires only 64 bits per instance, but it is not
the optimal representation. There are 254
nodes in the game-state graph, so a
lower bound on the number of bits required to represent any node on the game-
state graph is therefore 54. Counting only normalized instances, that number
drops considerably. However, in order to achieve this lower bound, it is likely
that the representation format would be difficult to understand and implement.
34
Applying the normalization algorithm and using an efficient representation
of Quarto instances helps to reduce the search space of Quarto. There are
approximately 254
instances that must be examined in a complete search. Op-
timistically assuming that each of the 12,288 transformations is always distinct,
the number of states to be examined is still about 243
. At 64 bits per instance,
this amount of data is far larger than today’s memories, and the time required
to examine this many states is prohibitive on generally available machines.
However, the combination of pruning, normalization, parallelism, and an
efficient representation yields an algorithm that can search the game space in a
tractable amount of time. This is the general approach that allowed Goossens
to solve Quarto. His work, as well as that of others, is reviewed in the following
section.
9 Related Work
9.1 Goossens’ Work
In [1], Luc Goossens solves Quarto, determining that two perfect players will
always draw. In order to find this result, he searches the game space starting
from a small number of nonequivalent instances, and generalizes the results to
all equivalent instances.
Goossens uses all piece transformations and positional transformations (3)
and (4) presented in Section 7. He also uses rotation and mirroring opera-
tions, which achieve some subset of the set of 32 positional transformations we
examined. He does not use positional transformation (3).
He generates the numbers of equivalence classes for different instance types
by hand, reporting the results in Table 9.1.
Instance Equivalence Classes†
0 + 1 1
1 + 0 2
1 + 1 8
2 + 0 96†
2 + 1 336†
Table 1: Goossens’ results for the number of representative nonequivalent in-
stances for different instance types. †
We differ on results for 2 + 0 and 2 + 1
instances.
He begins by examining the choice of the first piece. This piece is reducible
to 00002 by the XOR transformation, and thus there is one equivalence class
for 0 + 1 instances. This piece may be placed on a diagonal or off a diagonal,
but no other choice matters. Thus there are two equivalence classes for 1 + 0
instances. The second piece can be reduced to 00012, 00112, 01112, or 11112
by bitwise permutation, yielding eight equivalence classes for 1 + 1 instances.
At this point, the number of cases tracked in the analysis becomes fairly large.
35
Suffice it to say that Goosens reports 336 equivalence classes for 2+1 instances,
and runs his search from these 336 initial states.
We arrive at some different results for the number of equivalence classes
by using the normalization procedure in Section 7. Beginning with the empty
board, a queue of possible successor states is generated, and each of those states
is reduced to its normal form. Every distinct normal form generated is enqueued
in the next iteration of the algorithm, producing a breadth first search that
reports the number of distinct normal forms for each type of instance. Our
results are in Table 9.1.
Instance Equivalence Classes Total Instances‡
Instances Per Class
0 + 1 1 16 16
1 + 0 2 256 128
1 + 1 8 3840 480
2 + 0 40 28,800 720
2 + 1 148 403,200 2,724
3 + 0 454 1,881,600 4,145
3 + 1 3382 24,460,800 7,233
4 + 0 10374 79,497,600 7,663
4 + 1 91558 953,971,200 10,419
5 + 0 216432 2,289,530,880 10,579
Table 2: Our results for the number of representative nonequivalent instances
of different types. Instances Per Class is the quotient of the number of equiv-
alence classes and the total number of instances. The breadth-first search that
generated these results was unable to handle the increase in the number of rep-
resentative nonequivalent instances past 5 + 0. ‡
The total number of instances
is calculated from Equations (1) and (2), and includes inconsistent states.
Goosens’ reasoning is solid until more than one piece is placed on the board.
At that point, there are multiple paths to each state. He counts a distinct state
for each path. He also does not include positional transformation (3).
While the 336 initial states in Goossens’ search do not comprise a minimal
set of representative nonequivalent instances, they are a superset, and thus his
results are accurate. He reports that his search takes about 30 million board
evaluations to make a decision for a 5+0 instance. Furthermore, each additional
search level multiplies the number of instances that must be examined by a factor
of about 10.
Goossens uses a minimax search, and in order for his search to be tractable,
he uses α-β pruning28
to eliminate suboptimal subgraphs of the game-state
graph from consideration.
28See Section 4.
36
9.2 Brown’s Work
Combinatorics is “the branch of mathematics that deals with permutations and
combinations,”29
or the branch of mathematics that deals with counting. Many
aspects of Quarto pose interesting combinatorial questions. In this section, we
present some of the more interesting results developed by Brown30
and augment
them with our own analysis.
Brown reports that there are 6,197,620,810,536 Quarto instances in which
the board is full and there are no winning rows. In order to generate this result,
the 16
4 = 1820 possible quartets are examined in a brute-force search, yielding
1284 quartets that are draws. Using a computer to automate the search, Brown
reports that it is possible to combine these in 778,339 ways to create an instance
without duplicating any pieces.
For each of these draw quartets, there are 4! permutations of the elements in
the quartet, yielding an additional factor of 4!4
. There are also 4! permutations
of the quartets themselves, yielding
778, 339 · (4!)5
= 6, 197, 620, 801, 536 (5)
for the number of distinct Quarto instances in which the board is full and no
row contains a win.
Theorem 34 There are 536 draw quartets in Quarto.
Proof We were able to confirm the 1284 draw quartets combinatorially
by calculating the total number of winning quartets and subtracting from the
total number of quartets. Let us call a quartet with one attribute/value pair in
common a single-attribute-win quartet, a quartet with two attribute/value pairs
in common a double-attribute-win quartet, and so on.
It is impossible to construct a quadruple-attribute-win quartet, as that would
require four identical pieces. Similarly, it is impossible to construct a triple-
attribute-win quartet, as there is one free bit, yielding two distinct pieces. Thus
winning quartets are either single- or double-attribute-win quartets.
Let us first consider double-attribute-win quartets. When fixing the first bit
and value, there are 4 bits with 2 possible values each, yielding 8 choices. After
fixing one of those choices, there remain 3 bits with 2 values each, yielding 6
choices. There is an additional factor of 1
2 to eliminate double counts, resulting
in 24 ways of fixing two bits.
Each of these 24 ways of fixing two bits may only be realized by a single
quartet, as there are only 2 free bits remaining to differentiate the pieces. That
is, the four distinct values of the bit strings correspond to one quartet.
Finding the number of single-attribute-win quartets is more complicated. We
know that there are 8 ways to choose the first common bit and value. Suppose
we fix that choice and examine the three remaining bits in each piece in the
29See [11].
30See [6].
37
quartet. The question is how to choose 4 different sets of 3 ordered bits so that
no single bit has an identical value across all 4 sets.
There are 8
4 = 70 sets of four distinct 3-bit numbers. We eliminate the 6
sets with one bit in common (3 bits · 2 values). This leaves 64 sets of four 3-bit
numbers with no bits in common. Thus the total number of single-attribute-
win quartets is 512 (8 bit/value selections · 64 3-bit completions). The number
of winning quartets is therefore 536 (24 double-attribute-win quartets + 512
single-attribute-win quartets).
Subtracting from the 1820 possible quartets yields 1284 draw quartets, the
number generated by Brown’s brute force search.
Brown proceeds by reporting a further result of 414,298,141,056 distinct
instances in Quarto in which there is a draw. This result was generated by an-
other search that took advantage of the piece transformations mentioned above,
and two of the positional transformations, (2) and (4). This result agrees with
Brown’s Monte Carlo31
simulations, which reported about 1 in 50.5017711 full-
board instances to be a draw. That is, 16! · 1
50.5017711 = 414, 298, 141, 080 ≈
414, 298, 141, 056.
9.3 Early Draw Prediction
The number of draws in Quarto is large relative to a computer memory, so
we investigate the possibility of detecting a draw condition before the board is
full. We have constructed an instance with 10 pieces on the board that always
evaluates to a draw in Figure 1.
1110 0001
1101 0010
0111 1010 0000
1000 0101 1111
or
14 1
13 2
7 10 0
8 5 15
Figure 1: This instance always evaluates to a draw. In fact, any determined
draw (See Definition 36) must have at least 10 pieces arranged in this layout.
Definition 35 (Definite Draw) An instance is a definite draw if and only if
every node in the descendant subgraph for its corresponding node in the game-
state graph is labelled draw or undetermined.
Definition 36 (Determined Draw) An instance is a determined draw if and
only if every win-placement on the board is dead.
31A Monte Carlo simulation is a randomized algorithm that reports an approximation of
some correct result. Presumably, Brown generated some large number of random Quarto
instances with full boards and checked each for a draw condition. The longer a Monte Carlo
simulation runs, the more accurate the approximation. This is opposed to a Las Vegas style
algorithm, which always returns the correct answer, but runs for a random amount of time,
possibly never terminating.
38
The set of determined draws in Quarto is a subset of the definite draws. We
believe that it may be possible to construct an instance that is not a determined
draw but is still a definite draw. In such an instance, at least one win-placement
is not dead, but the set of pieces that could complete that win-placement for a
win are deployed elsewhere on the board.
The entire set of definite draws is outside the scope of this paper. Instead,
we examine the format of a determined draw. A proof follows showing that any
determined draw must have at least 10 pieces on the board in the arrangement
of Figure 1.
Lemma 37 A win-placement containing exactly 2 pieces is dead if and only if
they are complements. A win-placement containing fewer than two pieces cannot
be dead.
Proof If a win-placement contains fewer than two pieces, we can select
another piece that preserves any possible wins, and thus continue to maintain
that possible win. If there are two pieces, and they are not complements of each
other, they share at least one bit in common, and thus a possible win exists.
If they are complements, they share no bits, and thus every possible win is
eliminated.
Lemma 38 If i is an instance with two win-placements that each contain ex-
actly two pieces, one of which is shared by both win-placements, then at least
one of the two win-placements is live, and thus i is not a determined draw.
Proof Let i be some instance in which two win-placements x and y con-
taining exactly 2 pieces each share a single piece p. In order for x to be a draw,
it must contain p, p’s complement by Lemma 37. Since x and y share p, and
there exists only one complement for every piece, y cannot contain p, and thus
it is not dead by Lemma 37. Thus i is not a determined draw, and Lemma 38
is proven.
Lemma 39 If an instance has 8 pieces or fewer on the board, it cannot be a
determined draw.
Proof Lemma 37 eliminates any instance with fewer than 8 pieces on the
board, as each of the 4 rows cannot contain the minimum 2 pieces. It also
eliminates any 8-piece instance in which any win-placement contains fewer than
2 pieces. Construct an instance such that every row contains 2 pieces. Arrange
these pieces such that every column also contains 2 pieces.
2 2 2 2
2
2
2
2
39
Each row and column contains 2 pieces, and each shares one piece with
another win-placement that contains only one other piece. By Lemma 38 this
board is not a determined draw.
Lemma 40 If an instance has 9 pieces or fewer on the board, it cannot be a
determined draw.
Proof Any instance with 8 pieces or fewer on the board is eliminated by
Lemma 39. Any 9-piece instance with fewer than 2 pieces in any win-placement
is eliminated by Lemma 37. Construct an 8-piece instance with 2 pieces in each
row and column as above. The 9th
piece can only occupy one column and one
row. Thus the distribution must be of the form:
2 2 2 3
2
2
2
3
There are 3 columns and 3 rows that share a single piece with a winning
position that contains one other piece. By Lemma 38 this instance is not a
determined draw.
Theorem 41 If an instance with 10 pieces on the board is a determined draw,
it must be of the form:
2 2 3 3
2
2
3
3
Proof Lemma 37 eliminates any 10-piece instance in which any win-
placement contains fewer than 2 pieces. Consider a 10-piece instance of the
form:
2 2 2 4
2
2
2
4
Each of the 2-piece columns shares one piece with the 4-piece row. Thus
they must share another piece with a 2-piece row, and this instance is not a
determined draw by Lemma 38.
Consider a 10-piece board of the form:
40
2 2 3 3
2
2
2
4
Each of the 2-piece columns shares a piece with the 4-piece row. Thus, each
must share a piece with a 2-piece row, and this instance is not a determined
draw by Lemma 38.
The only 10-piece instance type that remains is:
2 2 3 3
2
2
3
3
Figure 1 shows that such a determined draw can be constructed.
Early draw detection allows for pruning the search tree, but the largest term
in Equation 3 is when i = 8 or i = 9. Thus the majority of Minimax search time
will be spent examining states in the “middle” of the game, and draw pruning
will only result in incremental improvements.
10 Conclusion
This paper examined Quarto from a computer science perspective. Section 4
discussed the technique of Minimax searching to create an automated player,
and proposed several enhancements to the technique. Section 5 discussed the
idea of solving a game, and segued into Section 6, which examined the feasi-
bility of solving Quarto. Section 7 presented transformations and equivalence
in Quarto, the most significant contribution of this paper. The connections
between Quarto and the 4-dimensional hypercube were also explored in this
section. Finally, a normal form was proposed as a representative for every in-
stance in that normal form’s equivalence class. Section 8 proposed an efficient
representation for Quarto, requiring fewer than 64 bits per instance. Finally,
Section 9 reviewed related work by other Quarto enthusiasts and presented some
original combinatorial results.
There are additional areas where combinatorial analysis could be applied
to Quarto to yield interesting results. An analysis of the number of Quarto
wins would be a satisfying counterpart to the analysis of draws given by Kevin
Brown in [6]. Further connections between the hypercube and Quarto may
be exploitable to yield insights into the mathematical aspects of the game as
well. Finally, various sophisticated combinatorial techniques such as Burnside’s
and P´olya’s counting theorems32
could be applied to determine the number of
equivalence classes for instance types with larger numbers of pieces on the board.
32See [12].
41
We implemented an automated player based on Minimax with α-β pruning,
and found that the design of a consistently accurate heuristic function was a
limiting factor. Further work in this area could be directed towards developing
a machine learning based framework for heuristic functions.
42
11 Appendix
Enumeration of 32 distinct equivalent instances generated using positional sym-
metries from an instance with a full board.
1. The following instances map the left to right diagonal to itself.
(a) Outer diagonal squares map to outer diagonal squares.
i. Outer diagonal squares are in order, inner diagonal squares are
in order.
A B C D
E F G H
I J K L
M N O P
A E I M
B F J N
C G K O
D H L P
ii. Outer diagonal squares are in order, inner diagonal squares are
reversed.
A C B D
I K J L
E G F H
M O N P
A I E M
C K G O
B J F N
D L H P
iii. Outer diagonal squares are reversed, inner diagonal squares are
in order.
P N O M
H F G E
L J K I
D B C A
P H L D
N F J B
O G K C
M E I A
iv. Outer diagonal squares are reversed, inner diagonal squares are
reversed.
P O N M
L K J I
H G F E
D C B A
P L H D
O K G C
N J F B
M I E A
(b) Outer diagonal squares map to inner diagonal squares.
i. Outer diagonal squares are in order, inner diagonal squares are
in order.
F E H G
B A D C
N M P O
J I L K
F B N J
E A M I
H D P L
G C O K
ii. Outer diagonal squares are in order, inner diagonal squares are
reversed.
F H E G
N P M O
B D A C
J L I K
F N B J
H P D L
E M A I
G O C K
43
quarto
quarto
quarto
quarto

Mais conteúdo relacionado

Semelhante a quarto

This is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docxThis is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docxabhi353063
 
0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptx0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptxAhishektttPhm
 
Unit_I_Introduction(Part_III).ppt
Unit_I_Introduction(Part_III).pptUnit_I_Introduction(Part_III).ppt
Unit_I_Introduction(Part_III).pptganesh15478
 
AI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptx
AI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptxAI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptx
AI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptxAsst.prof M.Gokilavani
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - pythonSunjid Hasan
 
International journal of engineering issues vol 2015 - no 2 - paper1
International journal of engineering issues   vol 2015 - no 2 - paper1International journal of engineering issues   vol 2015 - no 2 - paper1
International journal of engineering issues vol 2015 - no 2 - paper1sophiabelthome
 
AI3391 Artificial Intelligence UNIT III Notes_merged.pdf
AI3391 Artificial Intelligence UNIT III Notes_merged.pdfAI3391 Artificial Intelligence UNIT III Notes_merged.pdf
AI3391 Artificial Intelligence UNIT III Notes_merged.pdfAsst.prof M.Gokilavani
 
MINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptxMINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptxNayanChandak1
 
LEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERSLEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERSkevig
 
LEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERSLEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERSkevig
 
I have to write a Connect Four game program When a player t.pdf
I have to write a Connect Four game program When a player t.pdfI have to write a Connect Four game program When a player t.pdf
I have to write a Connect Four game program When a player t.pdfaayushmaany2k14
 

Semelhante a quarto (20)

This is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docxThis is an individual project, to be completed on your own. It i.docx
This is an individual project, to be completed on your own. It i.docx
 
Stratego
StrategoStratego
Stratego
 
0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptx0-miniproject sem 4 review 1(1)(2).pptx
0-miniproject sem 4 review 1(1)(2).pptx
 
Unit_I_Introduction(Part_III).ppt
Unit_I_Introduction(Part_III).pptUnit_I_Introduction(Part_III).ppt
Unit_I_Introduction(Part_III).ppt
 
AI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptx
AI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptxAI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptx
AI3391 Artificial Intelligence Session 18 Monto carlo search tree.pptx
 
Artificial intelligence - python
Artificial intelligence - pythonArtificial intelligence - python
Artificial intelligence - python
 
International journal of engineering issues vol 2015 - no 2 - paper1
International journal of engineering issues   vol 2015 - no 2 - paper1International journal of engineering issues   vol 2015 - no 2 - paper1
International journal of engineering issues vol 2015 - no 2 - paper1
 
AI Lesson 07
AI Lesson 07AI Lesson 07
AI Lesson 07
 
AI3391 Artificial Intelligence UNIT III Notes_merged.pdf
AI3391 Artificial Intelligence UNIT III Notes_merged.pdfAI3391 Artificial Intelligence UNIT III Notes_merged.pdf
AI3391 Artificial Intelligence UNIT III Notes_merged.pdf
 
python.pptx
python.pptxpython.pptx
python.pptx
 
MINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptxMINI-MAX ALGORITHM.pptx
MINI-MAX ALGORITHM.pptx
 
Two player games
Two player gamesTwo player games
Two player games
 
Thesis paper
Thesis paperThesis paper
Thesis paper
 
Tic Tac Toe
Tic Tac ToeTic Tac Toe
Tic Tac Toe
 
LEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERSLEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERS
 
LEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERSLEARNING CHESS AND NIM WITH TRANSFORMERS
LEARNING CHESS AND NIM WITH TRANSFORMERS
 
Tic tac toe simple ai game
Tic tac toe simple ai gameTic tac toe simple ai game
Tic tac toe simple ai game
 
I have to write a Connect Four game program When a player t.pdf
I have to write a Connect Four game program When a player t.pdfI have to write a Connect Four game program When a player t.pdf
I have to write a Connect Four game program When a player t.pdf
 
Sol90
Sol90Sol90
Sol90
 
Sol90
Sol90Sol90
Sol90
 

quarto

  • 1. An Exploration of Gameplay in Quarto Matthew Kerner∗ Advisor: Dana Angluin† April, 2001 Abstract This paper examines the board game Quarto. Techniques for con- structing an automated player are discussed. The feasibility of solving Quarto is explored, and a proof is given that the 12,288 distinct trans- formations considered exploit the maximum amount of symmetry in the game. The behavior of one class of transformations is shown to be anal- ogous to automorphisms of the hypercube. A procedure is proposed for creating a normal form that characterizes a set of equivalent instances of the game. An efficient representation of instances requiring fewer than 64 bits per instance is presented. Related work on the combinatorics of Quarto is reviewed, and original combinatorial results are explored. 1 The Game of Quarto Quarto is a two-player game that resembles tic-tac-toe. The board is a 4x4 grid, and is accompanied by 16 distinct pieces, each of which is either tall or short, round or square, dark or pale, and hollow or solid. At the beginning of a game, player 1 selects a piece for the other player. Player 2 places this piece on an unoccupied square on the board, and then selects a piece for player 1. Play continues until either player completes a row, column, or diagonal of four pieces that share at least one characteristic. Neither player “owns” the pieces that she plays—the player who completes the first group of four winning pieces is the winner. The game is a draw if every place on the board is occupied by a piece, but no row, column, or diagonal contains pieces with a common attribute. 2 Introduction The game of Quarto is relatively simple compared to other two player games. There are few pieces, the gameplay is uncomplicated, and the board is straight- forward. The major twist is that each player selects the piece to be played by her opponent. ∗matthew.kerner@yale.edu †dana.angluin@yale.edu 1
  • 2. Despite its simplicity, Quarto is an intriguing game. Any turn in which one player is close to winning is followed by another turn in which the same opportunities are offered to the opponent. Quarto is easy to understand but hard to master, and there is a mathematical undercurrent to the game. This paper will examine Quarto from the perspective of a computer scientist. Section 3 presents the vocabulary used throughout the remainder of the paper, and introduces the diagram used to depict the Quarto board. Section 4 discusses various techniques that are used in automated game- play. The concept of the game-state graph is introduced. The basic Minimax algorithm is presented, and various enhancements are proposed, including α-β pruning, caching, and parallel computing. Section 5 discusses the concept of solving a game and gives a motivation for the sections that follow. Section 6 discusses the amount of work that would be necessary to solve Quarto by examining the number of possible game states. The game-state graph is shown to be prohibitively large for a complete search, and the need for a reduction in the number of states examined is demonstrated. Section 7 explores the symmetries of Quarto. A proof is presented that establishes the symmetries considered as the maximal set of symmetries allowed by the rules of the game. The behavior of one class of symmetries is shown to be analogous to the behavior of the automorphisms of the hypercube. Finally, an algorithm for determining the normal form of a set of equivalent instances is proposed. Section 8 presents an efficient representation of Quarto instances that re- quires fewer than 64 bits per instance. The tradeoffs involved in a compressed representation are discussed. Section 9 reviews related work by other Quarto enthusiasts and presents some original combinatorial results. An additional enhancement of the Minimax algorithm is proposed to detect draws early. Section 11, the Appendix, presents an enumeration of the 32 positional sym- metries examined in Section 7. 3 Vocabulary Attribute The four variables in the description of each piece: height, color, shape, and hollowness. As each attribute can take on 2 values, we can represent each piece as a distinct 4-bit binary number ranging from 0 to 15, or 0000 to 1111 in binary form. Instance The set of pieces on the board and the piece selected for placement on the next turn. Note that this includes enough information to determine whether player 1 or player 2 will make the next move and to determine the set of pieces in the pool. An instance is designated as n + 0 if it has n pieces on the board and no piece has been selected for the next move. If 2
  • 3. a piece has been selected, that instance is an n + 1 instance1 . This paper will illustrate instances using the following diagram: • (+1 position) • • • • • • • • • • • • • • • • If a square is empty, it represents an unoccupied position. If a square contains a capital letter, that letter is intended as a label for that square. If a square contains a number, that number represents the corresponding piece (see Attribute). Quartet A set of four pieces. A quartet in which the pieces share at least one characteristic is a winning quartet. A quartet in which the pieces share no characteristic is a draw quartet. Value The value of an attribute (i.e. 1 or 0 in the bitwise representation of a piece). Win A winning position and a quartet that inhabits it comprise a win if the bitwise AND2 of the quartet is not 0 or the bitwise OR3 of the quartet is not 15. That is, the quartet shares at least one attribute-value pair in common. Win Placement A win placement is a winning position and the set of pieces that occupy it. A win placement is live if the value of any attribute is the same across every piece in the win placement. A win placement is dead if the value of every attribute differs across the pieces in the win placement. Winning Position Four squares on the board that are arranged in a row, column, or diagonal. 4 Building a Quarto Player 4.1 Internal Representation The first steps in creating an automated player for any game are to select the data required by the player, to specify how the player will gather the data during play, and to develop a representation of the data that can be stored and 1This nomenclature was introduced by Goossens in [1]. 2The bitwise AND function of two bits is 1 if the bits are identical and 0 otherwise. Generalized to an n-bit string, the ith bit of the result contains the bitwise AND of the ith bits of the inputs. 3The bitwise OR function of two bits is 1 if either or both inputs is 1, and 0 otherwise. It generalizes to n-bit strings in the same manner as bitwise AND. 3
  • 4. manipulated by a machine. Soccer, for example, includes a large amount of data at any point, such as the location, velocity, and acceleration of every player as well as for the ball, not to mention the wind speed and direction. An automated soccer player requires frequent updates to keep up with the activity of the game. Furthermore, any system that represents a soccer game accurately would require massive computing resources and a nontrivial internal representation. Fortunately Quarto is much simpler than soccer. An instance of Quarto is characterized by the set of pieces on the board and their locations as well as the piece selected to be placed on the next turn. The representation need only be updated when a move is made by either player, clearly a reasonable requirement. Furthermore, Quarto is a deterministic game of complete information. A game like Monopoly is nondeterministic as the roll of the dice introduces an element of chance. In deterministic games like Quarto, the only variability in the game comes from the choices made by the players. Monopoly is a game of incomplete information, as the cards are not visible to the players. In Quarto and other games of complete information, each player knows exactly what the board looks like at every point in the game. Aside from the opponent’s moves, all of the relevant information is available to each player. The simplest representation of the Quarto board is a two dimensional array in computer memory that contains the pieces placed on the board. Pieces are represented by 4-bit strings as explained under Attribute in Section 3. We may want to keep additional information about an instance in order to reconstruct the chain of moves that created the instance. We can either keep a list of the moves made in a game, or keep a history of previous instances. A game might go as follows: (+1 position) ↓ 4 (+1 position) ↓ (+1 position) 4 ... 4
  • 5. This sequence of instances suggests representing a Quarto game as a graph of instances connected by moves. 4.2 The Game State Graph A graph is a set of vertices and edges. Vertices (also known as nodes) are points, and edges are ordered pairs of vertices. If an edge connects one vertex to another, that edge is said to be incident to both of those vertices. Let us call a graph that models the game states in Quarto a game-state graph. In it, the vertices correspond to instances, and the edges correspond to moves that transition between instances. The game-state graph is a directed graph, or a graph in which the edges travel in one direction only. This corresponds to the rule in Quarto that no player may remove pieces from the board. The board only grows more full as the game advances. This results in a graph that looks like the following: 0 + 1 instances: ◦ 1 + 0 instances: ◦ ◦ 1 + 1 instances: ◦ ◦ ◦ .·· ... .·· ... .·· ... In this graph, the edges form paths. A path is an ordered set of edges such that the ith edge of the set originates at the node pointed to by the (i − 1)st edge of the set. The initial edge is an outgoing edge of the node located at the beginning of the path. A path that returns to its initial node is called a cycle. The nodes in the graph are arranged in levels. The top level consists of a single node with no incoming edges, known as the root. This node corresponds to the instance with the empty board. Each node of a level n is path length n from the root node. There is at least one path from the root node of the graph to every other node of the graph. In fact, there are multiple paths from the root node to many of the other nodes in the graph. The game-state graph is an acyclic graph, or a graph in which there are no cycles. Thus it is impossible to encounter an instance twice in a single game of Quarto, since a piece placed on the board must remain there. The parent of a node n is any other node that is incident to an incoming edge of node n. The child of a node n is any node incident to an outgoing edge from node n. A descendant of a node n is any node that can be reached on a path originating at node n (including n’s children). The descendant subgraph of a node n is the set of all nodes that are descendants of node n and their incident edges, as well as node n itself. Every vertex in the graph is labelled with the result of the corresponding in- stance. That is, a vertex corresponding to an instance that is a win is labelled as 5
  • 6. a win. Similarly, other nodes are labelled as draws. Any node corresponding to an instance in which there is neither a draw nor a win is labelled undetermined. The game-state graph includes inconsistent states. An inconsistent state is an instance that cannot be reached during normal gameplay. For example, if player 1 wins on the top row of the board after 4 moves, the game should end. If we placed another winning quartet on the bottom row of the board, that instance would be an inconsistent state, as the game should end before that board is reached. An inconsistent state occurs when there are at least two wins on the board that do not share any piece. We include these states in the game- state graph to simplify its analysis. For the most part, they may be ignored. Inconsistent states are always labelled as wins. The subgraph of descendants of any node labelled win or draw will share that same label. The subgraphs of nodes labelled as undetermined may be labelled differently at different nodes. A leaf is any vertex that has no outgoing edges. Leaves in the game state graph correspond to instances in which the board is full (i.e. a draw; a full board with a single win, or multiple wins that share one piece; or an inconsistent state labelled as a win). Definition 1 (Game-State Graph) A game-state graph is a directed acyclic graph in which the vertices correspond to instances and the edges to moves that transition between those instances. The root of the graph corresponds to the instance with the empty board. There is at least one path from the root node to every other node in the graph. Each vertex is labelled as win, draw, or undetermined, corresponding to the result of the instance represented by that vertex. The nodes in the descendant subgraph of any vertex labelled win or draw share that vertex’s label. The game-state graph includes inconsistent states (i.e. states that cannot be reached during normal gameplay), that are labelled as wins. The leaves of the graph correspond to instances in which the board is full. The game-state graph is a useful tool for building a Quarto player, as it contains every possible move that a player could make. By searching the game- state graph before making a move, a player can choose a path to the desired outcome. One method of searching the game-state graph is called the Minimax algorithm. 4.3 The Minimax Algorithm The Minimax algorithm is a method of searching the game-state graph in order to choose the move that will result in the best possible outcome from a given instance. Although the Minimax algorithm is a top-down algorithm, it is easier to understand from the bottom up. Consider the subgraph of the game-state graph that contains only consistent states. The leaves of this subgraph will all be draws or wins. Now consider the parent node of a leaf. We mark4 this node with the outcome of the move that 4This use of marks is not part of the labelling from Definition 1. It is an extension of that idea for use in the Minimax algorithm. 6
  • 7. would be chosen by a perfect player confronted with the corresponding instance. A parent node i might have the following set of leaves as children: Instance i: ◦ Leaves: ◦ ◦ (draw) (draw) Node i is marked as a draw. If however, the set of child leaves contains a win as in, Instance j: ◦ Leaves: ◦ ◦ (draw) (win) the parent node j is marked as a win. This same technique can be applied to the next set of parents. However, the player at this level is the opponent. This opponent is faced with making a move that results in instance i or in instance j. k (draw) Player 2: ◦ Outcome for Player 1: i (draw) ◦ ◦ j (win) Leaves: ◦ ◦ ◦ ◦ (draw) (draw) (draw) (win) A perfect player will move to instance i, because she would rather draw than lose. We continue this process until the marks propagate back to the instance faced by a player, where the optimal move resulting from that instance is chosen. This method of propagating the marks of child nodes to their parents while switching players at each level is the Minimax algorithm. The name comes from the fact that one level will find the minimum outcome for the opponent and the next will find the maximum outcome for the player making the move. Minimax is conducted as a search starting from the root of the game-state graph. It examines each descendant recursively and propagates marks back up the graph to the root. Thus, after the Minimax algorithm completes, every node n is marked with the result of the instance corresponding to the node at the end of the path that would result from a game between two perfect players starting at node n5 . One problem with the standard Minimax algorithm is the amount of time it takes to make a decision. Starting from the root, the algorithm examines every node in the game-state graph. In Section 5 it is shown that the size of this graph is intractably large for a complete search. Thus it is necessary to eliminate some subgraphs from consideration. 5See [2] for a fuller treatment of the Minimax algorithm. 7
  • 8. 4.4 Minimax Enhancements Consider the following game-state graph: k Current Player’s move: ◦ j Opponent’s move: i (draw) ◦ ◦ ↓ Outcome for opponent: ◦ ◦ ◦ ◦ (draw) (win) (draw) (loss) If i is marked first, the current player knows that she can draw in the worst case. When she begins to examine j’s descendant subgraph, she immediately discovers that the opponent can force her to lose if she chooses to move from k to j. Regardless of the other possible outcomes resulting from j, the worst case would be a loss. Thus the rest of j’s descendant subgraph is ignored, or pruned from the search. This technique for eliminating subgraphs is called α-β pruning6 . It keeps a lower bound α and an upper bound β that track the worst and best case outcomes discovered at any point. These bounds are initialized to −∞ and +∞ to allow consideration of every value in between. Minimax begins as usual starting from the root. As the game-state graph is examined, the lower bound is continually updated with the best possible outcome, and the upper bound is continually updated with the worst possible outcome. They converge towards an intermediate value that is optimal for both players. Any node whose descendant subgraph yields a worse lower bound than the previous lower bound is immediately eliminated from consideration. Similarly, any node whose descendant subgraph yields a better upper bound than the previous upper bound is immediately eliminated from consideration. This prunes sections of the game-state graph that are not optimal for either of the two players, and results in a smaller and faster Minimax search. Another pruning technique useful for Quarto is to stop searching immedi- ately upon finding any move marked as a win. The standard version of Quarto awards the same benefit for a win early in the game as it does for a late win. Thus, there is no downside to selecting a move that will result in a win some large number of turns later even if another move would result in a win on the next turn. We call this pruning technique win short-circuiting, and it results in a slightly faster Minimax search. Recall that many nodes in the game-state graph may be reached by multi- ple paths from the root. This implies that some subgraphs may be examined repeatedly during a Minimax search. It is therefore helpful to cache7 the marks associated with each node in the game-state graph if it takes less time to store 6For a more complete treatment, see [2, 3]. 7To cache a value means to save it in semi-temporary storage to avoid recomputing it for subsequent uses. 8
  • 9. and reaccess them than it does to recompute them. While a variety of different caching schemes are possible, we found that the cache design must be optimized for fast lookup in order for it to reduce the search time. One possible approach for designing a minimax cache for Quarto is to use the efficient representation proposed in Section 5. Another technique for speeding up the search of the game-state space is to divide up the search across multiple processors. If 2 processors each examine 1 2 of the game-state graph, the Minimax search will complete in about 1 2 the time it would take a single processor to search the entire game-state graph. However, since the two subgraphs might share some vertices, the processors ought to be synchronized8 so that both do not examine the shared sections of the game-state graph. Synchronization is both complicated and inefficient, because it requires spen- ding computing resources on something other than the search itself. Thus par- allel techniques entail a tradeoff between the degree of parallelism exploited and the fraction of computing resources spent on directly useful work. That is, in- creasing the number of processors searching the game-state graph increases the degree of parallelism, but decreases the fraction of total computing resources spent on the search itself. Perhaps the most simple and useful extension of the general Minimax algo- rithm is to limit the depth of the search. That is, starting from some vertex at level l with a depth limit of d, the Minimax algorithm will examine all of the nodes in levels l, l + 1, . . . , l + d. If the nodes at level l + d are not leaves, then an estimate of the winnability of the game from those nodes is calculated, and the nodes are marked accordingly. The function generating the estimate is known as a heuristic function, and it gives an approximation to the result of the full Minimax search. In order for a depth-limited Minimax search to be useful, the heuristic func- tion must be designed to return a good estimate of the winnability of an instance. Another important requirement of heuristic functions is that they require only limited input and take limited amounts of time. A heuristic function that re- quires significant amounts of time to evaluate an instance will be less successful at reducing Minimax search time than a function that evaluates quickly. In fact, there is a tradeoff in the design of heuristic functions between the time required to evaluate an instance and the quality of the evaluation. The solution that gives the best evaluation and takes the most time is simply the complete Minimax search. The solution that takes the least time and gives the worst evaluation is a random guess or some similar strategy. An ideal heuristic function will evaluate immediately with an accurate assessment of the winnability of the instance in question. One approach for designing heuristic functions is to reduce an intuitive un- derstanding of the strategy of a game to a mathematical characterization based on the important features of the instances. In practice, this process is very dif- ficult, and it often results in unexpected behavior. For this reason, the heuristic 8See [4] for a discussion of synchronization. 9
  • 10. function is an area in which machine learning is often applied. There are various machine learning techniques such as neural networks that can extract trends from sample information and generalize those trends when confronted with new data. This type of system could take an instance as input and output the projected winnability of that instance based on the previous experience of the machine. A generic machine learning algorithm applied to a heuristic function accepts features that are deemed important by the programmer, and assigns weights to them based on its experience. These weighted features are combined in some function, whose output is the estimate of winnability. This output is fed back as additional input in future iterations when the estimate of winnability may be confirmed or denied, and the weights are adjusted based on the accuracy of the previous performance. In order to properly train a machine learning mechanism, a large body of example games is necessary. One way to generate these games is to have the machine play itself. However, a novice machine player will learn very little from playing another novice machine player. Ideally, a human Quarto expert would play a machine in thousands of games until the machine extracted enough information to play well. Given that access to human Quarto experts is limited, another approach is to solve the game completely, marking every node of the game-state graph with the outcome of the game originating at the corresponding instance as played by two perfect players. This completed game-state graph could then be used as input to a machine learning mechanism that would extract the strategy9 . Unfortunately, marking the entire game-state graph brings us back to the point that Minimax takes a long time to complete when the search space is large. The next few sections will examine the use of the game state graph to solve the game completely rather than to create a perfect player. 5 Solving Quarto To solve a game is to discover the outcome when the game is played by perfect players. A perfect player will always select a move whose result is the best possible outcome for that player. Conveniently, a perfect player in a game of complete information knows exactly what move a perfect opponent will make. She puts herself in her oppo- nent’s position, and makes the perfect move. This reasoning is the motivation behind the Minimax algorithm presented in Section 4. It is commonly used to propagate an outcome to the root of the game-state graph, and thus to solve the game. 9In some ways, this proposal begs the question. If the game-state graph is completely marked, why pursue a heuristic function for Minimax? The task of Minimax has already been completed, and the marked game-state graph already models a perfect player. Our response is that it is uninteresting to play a perfect player, and thus developing a successful machine learning strategy is a better use of the annotated game-state graph than a perfect player would be. 10
  • 11. Unfortunately, the outcome of a game between two perfect players provides little insight into the intuitive strategy of a game. Minimax reports the move that a perfect player would make from a certain point in a game, but provides no insight into why this move is chosen. Despite this fact, solving games has a certain allure. I had the pleasure of attending a talk given by Guy Steele ([5]). Steele is a well-known computer scientist whose list of accomplishments includes the invention of both the Scheme and Java programming languages. The title of his talk was “Teeko: Solving A Game,” and it detailed some recreational research that he conducted while working at Sun Microsystems. The game of Teeko is a two-player game not unlike Quarto. The board is a 5x5 grid, and each player attempts to place her four pieces in a closed box while preventing the other player from doing so. There is no hidden information in the game other than the opponent’s choice of where to move. Steele began investigating Teeko many years ago, and finally succeeded in solving Teeko with a brute-force search. When I heard Steele’s results, I felt that Quarto might be solvable too. Later we discovered that Luc Goossens, a researcher at CERN10 , had solved the game two years ago.11 In the summer of 1998, he conducted a search and found that two perfect players will always draw. Rather than dismissing the task of solving Quarto as pass´e, we felt that there were additional results to be found, and that a better approach to conducting a search of Quarto’s game space existed. The following section will examine the feasibility of solving Quarto using a Minimax search. 6 Feasibility In order for a complete search of a game to be feasible, the game-state graph must fit into a computer’s memory (e.g. 232 bytes12 ) and should ideally be much smaller. The number of nodes in Quarto’s game state graph is includes inconsistent states as described in Section 4. Thus it is an upper bound on the number of instances that are encountered in regular play. For an i + 0 instance, there are i pieces, i places, and an ordering of the pieces. This gives the number of distinct i + 0 instances as |i + 0| = 16 i · 16 i · i! (1) Similarly, adding another factor for another choice of piece, the number of distinct i + 1 instances is |i + 1| = 16 i · 16 i · i! · (16 − i) (2) 10The European Organization for Nuclear Research, at http://www.cern.ch/ 11See [1]. 12A byte is eight bits. 11
  • 12. An equivalent alternative to these equations is to work inductively. Suppose that there are n distinct instances of i + 0 boards for some i. Choosing a piece introduces a factor of (16 − i), and choosing a placement introduces another factor of (16 − i). However, in order to create the same instance, the last piece added could have been chosen and placed on any earlier move, and any of the i previously selected pieces could have been placed on this move instead. In other words, to avoid counting a single instance multiple times, we must factor out the number of paths from the root node to this state in the game-state graph. The number of paths is exactly i!, since this is an ordering of the previously selected pieces. It can easily be verified that this inductive method yields the same results as the previous equations. Therefore, the total number of nodes in the game-state graph is 16 i=0 16 i · 16 i · i! + 16 i · 16 i · i! · (16 − i) (3) This yields about 2.68466 · 1016 , or 254.57 instances. The available address space in today’s machines has a maximum index of about 232 . It would be impossible to store enough Quarto instances in memory simultaneously to allow a complete search of the game-state graph to finish in a reasonable time frame. Assuming that a machine could examine 1,000 states per second, it would take over 848,000 years to examine the entire game-state graph. One approach to reducing the difficulty of this task is to examine fewer instances. If we can divide all Quarto instances into several equivalent sets, then we need only examine one instance from each set. We can then generalize the results to all of the instances. In order to decide which representative instances to examine, we need to develop a set of transformations that map instances to each other, a notion of equivalence, and a procedure for picking the representative instance, or the nor- mal form, for each set of equivalent instances. These three steps are examined in the following section. 7 Transformations, Equivalence, and the Nor- mal Form A transformation is a process that maps one instance into another. Transfor- mations are useful in many games. For example, a chess player might rotate the board to look at the layout of the pieces from the opponent’s point of view. This rotation is a transformation that maintains the relationships between the pieces on the board. Similarly, in Quarto, a player might apply a transformation by swapping every tall piece with a corresponding short piece to create a symmet- ric board. The transformations examined in this section have been proposed by 12
  • 13. others in the past13 , and are considered here together for the first time. In general, a transformation could be any mapping of instances to instances. A Quarto transformation specified in that manner requires about 254 individual mappings. Furthermore, it might do something strange and useless. Consider the transformation that maps any instance to the instance containing the fol- lowing board14 : =⇒ 1 1 1 1 Another possible transformation maps any instance to the instance contain- ing the empty board: =⇒ These two examples are useless when it comes to eliminating instances from consideration. The first creates instances that are not legal in Quarto, and the second brings any Quarto game back to the initial state. We would like to define transformations in a concise way, so that all 254 mappings are not enumerated. We would like to ensure that a transformation applied to a legal Quarto instance outputs another legal Quarto instance, and that both the preimage and the output have the same number of pieces on the board. These are all properties of permutations, so we define a transformation accordingly: Definition 2 (Transformation) A transformation is a function that accepts a Quarto instance. It first applies a permutation of the board positions (and con- sequently the pieces that occupy those positions) and then applies a permutation of the 16 distinct piece names, returning another Quarto instance. Since a transformation is a pair of permutations, it has some useful proper- ties: Property 3 Transformations are one-to-one mappings. This property comes directly from permutations. It ensures that no piece will be duplicated or eliminated by a transformation. It further ensures that distinct inputs of a transformation result in distinct outputs, and that distinct outputs have distinct preimages. 13See [6, 1]. 14A square occupied by a number indicates that a corresponding piece occupies that square. The number, expanded to a 4-bit binary form, codes the piece’s attribute/value pairs. See Attribute in Section 3. 13
  • 14. Property 4 Transformations are onto mappings. An onto mapping is one that maps to every element in the range. Since the domain and range of transformations are the same set of instances, Property 3 shows that they are onto as well. Property 5 Transformations preserve cardinality. This property also comes directly from permutations. Applying a transfor- mation to any instance results in an instance that has the same number of pieces on the board. Property 6 Transformations have inverses. Any permutation has an inverse that reorders the elements of the permuted set to what they were before the transformation was applied. By applying the inverse of a transformation to an instance, we can discover what the preimage was before the original transformation was applied. Property 7 Transformations can be composed to create additional transforma- tions. We can apply one transformation to an instance, and then apply another transformation to the result of the first one. These two transformations may be composed to create a single transformation that accomplishes in one pair of permutations what the others did in two pairs of permutations. In terms of the game-state graph, transformations map nodes to nodes, and thus they also map subgraphs to subgraphs. In fact, a transformation is a mapping of one subgraph of the game-state graph to another subgraph, and these subgraphs will be isomorphic. Two graphs G and G are isomorphic if and only if there exists a one-to-one and onto function f mapping the vertices of G to the vertices of G such that for every pair of vertices u, v of G, (u, v) is an edge of G if and only if (f(u), f(v)) is an edge of G . Lemma 8 Given a transformation t and an instance i such that t(i) = i , the descendant subgraphs of the nodes corresponding to instances i and i are isomorphic. Proof The mapping t is a one-to-one and onto mapping by Properties 3 and 4. Thus it maps all nodes to one other node, and no node in the range is left without a preimage. Thus nodes map to nodes under t. Since t is a pair of permutations, i and i have the same number of pieces on the board. Thus if there is a move available from i, there must be a cor- responding move available from i (although it may involve a different piece or square), and their corresponding nodes in the game-state graph will each have an outgoing edge. Thus edges are preserved by the mapping t. 14
  • 15. Since nodes and edges of the game-state graph are preserved by t, the de- scendant subgraphs of the nodes corresponding to any two instances mapped by t are isomorphic. If a transformation maps in a well-behaved manner, we may be able to examine a subgraph and generalize to the rest of the game-state graph. In order for this to work, the transformation must preserve the structure of the subgraph. That is, it must preserve equivalence. 7.1 Equivalence Intuitively, two equivalent Quarto instances may be distinct, but the layout or selection of pieces in the instance will result in the same strategy. Consider the following example: 1 2 3 4 4 3 2 1 These two instances are equivalent because the strategy in the first is essen- tially identical to that of the second. The only difference between the instances is the layout of the pieces on the board. The interaction between every pair of pieces is maintained. Flipping the first board about its vertical axis is one transformation that maps the first board to the second board. This transforma- tion also happens to be its own inverse, since flipping the second board about its vertical axis maps it back to the first. Definition 9 (Equivalence) An instance i is equivalent to instance i if and only if the descendant subgraph of i is isomorphic to the descendant subgraph of j and the nodes of these two subgraphs have identical labels. The result of Definition 9 is that two instances are equivalent if and only if the resulting gameplay from both of them is effectively the same. Lemma 10 Let some transformation t map an instance i to another instance i . These two instances are equivalent if and only if for every sequence of moves s originating at i, the label of the node corresponding to the result of that game is identical to the label of the node corresponding to the result of the game originating at i with a sequence of moves s = t(s). Proof The instances i and i have corresponding nodes in the game state graph. Lemma 8 guarantees that the descendant subgraphs of each are isomor- phic. Each move is an edge transitioning between two instances. Thus a sequence of moves is a path through the game-state graph. If any path starting at the node corresponding to i reaches a node with a different label from the node reached by the path’s image starting at the node corresponding to i , the two 15
  • 16. descendant subgraphs are not identically labelled, so by Definition 9 the two instances are not equivalent. Conversely, if the nodes along every path originating at the node correspond- ing to i is labelled identically to the nodes on every path originating at the node corresponding to i , then the descendant subgraphs must be identically labelled, and thus i is equivalent to i by Definition 9. Lemma 10 is a restatement of Definition 9 that introduces transformations. Together with transformations, equivalence has some useful properties. Certain transformations are constructed in such a way that they only map between equivalent instances. Definition 11 (Equivalence-preserving Transformations) A transforma- tion t guarantees equivalence if and only if for every instance i, t(i) = i is equivalent to i. Lemma 12 Equivalence-preserving transformations may be composed to create additional equivalence-preserving transformations. Proof Suppose that some equivalence-preserving transformation t1 maps i to i . Let another equivalence-preserving transformation t2 map i to i . The de- scendant subgraph of the node corresponding to i is isomorphic and identically labelled to the node corresponding to i by Definition 11. Similarly, the de- scendant subgraph of the node corresponding to i is isomorphic and identically labelled to the node corresponding to i . Thus there is some transformation t3 that maps the nodes, edges, and labels of the descendant subgraph of i to the nodes edges, and labels to the descendant subgraph of i , and it is also equivalence preserving. The transformation t3 is the composition t2(t1()). Any transformation may be tested to determine whether it guarantees equiv- alence by mapping every instance and determining whether these mappings satisfy Lemma 10: Lemma 13 A transformation t guarantees equivalence if and only if for every instance i, the node of the game-state graph corresponding to instance t(i) = i is labelled identically to the node corresponding to i. Proof This is a restatement of Lemma 10 with the sequence s set to ∅. The only transformations of interest when comparing game states are those that guarantee equivalence. It would be necessary to check the equivalence of every pair of instances mapped by a general transformation. If we constrain transformations to be equivalence-preserving, that check is unnecessary. General transformations are composed of “atomic” transformations that fall into two classes - positional and piecewise. The first class contains transforma- tions that reorder the pieces on the board. The second contains transformations that alter the value of each piece in an instance. 16
  • 17. 7.2 Positional Transformations Positional transformations reorder the pieces on the board. In general, there are 16! different positional transformations corresponding to the permutations of the squares of the board. Definition 14 (Positional Transformation) A positional transformation is a permutation of the squares of the board, or a general transformation whose piece-name permutation is the identity. One such transformation swaps the pieces occupying the first two upper-left squares of the board15 : A B C D B A C D E F G H =⇒ E F G H I J K L I J K L M N O P M N O P Instances mapped by this transformation are equivalent if their first two upper-left squares are empty. Suppose instead that square A is initially occupied by a piece and square B is empty. When this transformation is applied, the resulting instance is different in a way that affects gameplay: the piece originally on square A occupied a diagonal, but after the mapping it is off of the diagonal. This change means that it occupies only two winning positions (column 1 and row 2) instead of three (column 1, row 1, left to right diagonal). It is possible to construct a winning instance that maps to a non-winning instance under this transformation. More generally, a positional transformation guarantees equivalence if and only if it maintains the interactions between quartets that occupy winning po- sitions. It does not matter which winning positions the quartets occupy as long as they are all intact on some winning position. The order of the pieces in the quartet can change freely, provided that other quartets that include each of the pieces remain intact. The diagonal is the key to discovering the equivalence preserving positional transformations. Consider the left to right diagonal: A B C D E F G H I J K L M N O P We notice the following characteristics of the board: • The elements of this quartet each occupy three winning positions: a row, a column, and a diagonal. 15A square occupied by a letter may or may not be occupied by a piece. The letter is a label for the square, and is used to illustrate the manner in which a positional transformation rearranges the squares of the board (and, correspondingly, their resident pieces). 17
  • 18. • Squares A and P “share” squares D and M, which are both on the other diagonal. • Squares F and K “share” squares G and J, which are both on the other diagonal. Any transformation that guarantees equivalence must ensure that each of the three points above holds true for the resulting states. This suggests the following list of transformations: 1. Swap outer rows and columns, swap inner rows and columns, swap outer rows and columns: this brings squares A and P into the inner part of the diagonal, reorders them, and then returns them to the outer part of the diagonal. It also keeps their rows and columns aligned. ( A B C D E F G H I J K L M N O P ) ⇓ P N O M H F G E L J K I D B C A 2. Swap inner rows and columns: this reorders squares F and K, keeping their rows and columns aligned. ( A B C D E F G H I J K L M N O P ) =⇒ A C B D I K J L E G F H M O N P 3. Swap outer rows and columns: this brings squares A and P to the inner two diagonal locations, and brings F and K to the outer two diagonal locations. It also keeps their rows and columns aligned. 18
  • 19. A B C D E F G H I J K L M N O P =⇒ F E H G B A D C N M P O J I L K 4. Transpose over diagonal: this functions just like a matrix transpose. A B C D E F G H I J K L M N O P =⇒ A E I M B F J N C G K O D H L P 5. Rotate 90◦ clockwise: this swaps the two diagonals and aligns the other rows and columns. A B C D E F G H I J K L M N O P =⇒ M I E A N J F B O K G C P L H D These five transformations are the basis for the only positional transforma- tions that guarantee equivalence. Each one maps squares that originally occu- pied a diagonal position to another diagonal position. Because of the “sharing” that goes on between the pair of outer diagonal elements and the pair of in- ner diagonal elements, one of each pair cannot be interchanged: both must be interchanged. It can be shown that these positional transformations guarantee equivalence. Furthermore, any positional transformation that guarantees equivalence must be some composition of these five transformations. This proof follows. Lemma 15 A positional transformation guarantees equivalence if and only if it preserves win-placements. Proof Let us assume that there is some transformation t that does not preserve a win-placement. We construct a winning instance i such that the only pieces on the board are the elements of the win-placement that is not preserved by t. t(i) = i is clearly not a win, thus t does not guarantee equivalence by Lemma 13. Now consider some transformation t that preserves win-placements. Be- cause it is a permutation, it must map the empty board to itself, clearly an equivalence-preserving operation. Assume then that t preserves equivalence in some mapping t(ip) = ip. Adding an additional piece to ip creates ip+1. If the addition of this piece created a win, then t(ip+1) = ip+1 is also a win, as the newly-completed win-placement is preserved by t. Similarly, if adding a piece 19
  • 20. to ip makes it a draw, then t(ip+1) = ip+1 is a draw, since the newly added piece participates in the same win-placements in ip+1 as it did without creating the conditions for a win in ip+1. Thus, by induction on the number of pieces on the board in an instance, t preserves labels of the nodes of the game-state graph when mapping between any two instances. Thus, by Lemma 13, t is an equivalence-preserving transformation. Lemma 16 Any positional transformation that guarantees equivalence must map the set of rows to rows or to columns, but not to a combination thereof, and not to the diagonals. Proof First observe that permutations preserve the cardinality of inter- sections of sets: |A ∩ B| = |π(A ∩ B)|. The intersection of any two rows is the empty set, and therefore any permutation of an instance must preserve this property. Let t be an equivalence-preserving transformation. If t maps a row to a column while mapping another row to a row, they must share an element, which contradicts the property of permutations above. t cannot preserve equivalence in this case. If t maps a row to a diagonal, there is only one winning position remaining that does not share an element with the occupied diagonal: the other diagonal. Therefore, the row mapped to the first diagonal must share an element with at least two other rows. t cannot preserve equivalence in this case. Thus t must map all rows to rows or it must map all rows to columns. Corollary 17 Any positional transformation that guarantees equivalence must map the set of columns to rows or to columns, but not to a combination thereof, and not to the diagonals. Proof We observe that the proof of Lemma 16 applies to columns as well as to rows. Corollary 18 Any positional transformation that guarantees equivalence must map diagonals to diagonals. Proof There are 10 winning positions on the board. A mapping assigns 8 of them to rows and columns by Lemma 16 and Corollary 17. The 2 that remain for the diagonal win-placements are the diagonal winning positions. Consider all 16! possible positional transformations. Any of these that map diagonal elements to non-diagonal positions are eliminated by Corollary 18. Those remaining that mix the diagonal elements between the two diagonals are eliminated by Lemma 15. All of the remaining positional transformations map the left to right diagonal to itself or to the other diagonal. These form two cosets16 of positional transfor- 16A set may be partitioned into disjoint subsets of equal cardinality. If there is a one-to-one, onto mapping from the elements of one of these sets to the elements of every other, then these sets are cosets. In this case, the mapping of the left to right diagonal to the other diagonal is that one-to-one, onto function. See [7] for a further treatment of cosets. 20
  • 21. mations. Let us consider the case in which the left to right diagonal is mapped to itself. It contains 4! transformations, corresponding to the permutations of the elements in a diagonal win-placement. Lemma 19 Any positional transformation that guarantees equivalence must map the outer squares of a diagonal either to the outer squares of a diagonal or to the inner squares of a diagonal, but not to a combination thereof. Proof Let some positional transformation t swap one of the outer squares of the diagonal with one of the inner squares of that same diagonal: Instance i A • F K P t =⇒ Instance i F A • K • P The row of A in instance i shares an element with the column of P, and that element occupies a square on the other diagonal winning position. When A is mapped to the inner part of the diagonal, its row must map with it by Lemma 15. Furthermore, by Lemma 16, this row must end up as either a row or column in i . In either case, the shared element no longer occupies a diagonal square, violating Corollary 18. Thus t does not guarantee equivalence and Lemma 19 is proven. Lemma 19 eliminates a number of the 4! positional transformations under consideration, leaving two cosets of transformations. The first maps rows to rows, and the second maps rows to columns. Let us consider only the set that maps rows to rows. This set contains three positional transformations and their compositions. They reorder the outer pair of diagonal elements, reorder the inner pair of diagonal elements, and swap the outer and inner pairs of diagonal elements. These are transformations (1)–(3) listed above. The one-to-one function that maps the elements of this coset to their com- plements in the other coset must maps rows to columns and vice versa. This is transformation (4) listed above. Lemma 20 If an equivalence-preserving positional transformation enumerates the order of the elements of one of the two diagonals, specifies which diagonal this quartet maps to, and specifies the placement of one other piece, it completely determines the resulting instance. Proof The nondiagonal piece was an element of the row of one of the diagonal pieces in the original instance. Its placement determines the mapping of rows to rows or rows to columns dictated by Lemma 16 and Corollary 17. The remainder of the rows are mapped to the winning positions occupied by their diagonal elements, and the columns are also mapped accordingly, as dictated by Lemma 15. This fixes the square of every piece on the board at the intersection 21
  • 22. of the row of one diagonal element and the column of another diagonal element. Thus the instance is completely determined. Now consider the original two cosets of transformations. The first mapped the left to right diagonal to itself, and the other mapped it to the right to left diagonal. The function that maps between them is the transformation that maps one diagonal to the other diagonal. This is transformation (5) listed above. Theorem 21 Any positional transformation that guarantees equivalence is a composition of the five positional transformations proposed above. Proof Suppose some equivalence-preserving transformation t maps in- stance i to instance j. 1. If the outer squares of the left to right diagonal of i are reversed in the corresponding diagonal of j, then apply transformation (1) listed above to i, creating i1. Otherwise let i1 = i. 2. If the inner squares of the left to right diagonal of i1 are reversed in the corresponding diagonal of j, then apply transformation (2) listed above to i1, creating i2. Otherwise let i2 = i1. 3. If the outer squares of the left to right diagonal in i2 are located on the in- ner squares of the corresponding diagonal of j, then apply transformation (3) listed above to i2, creating i3. Otherwise let i3 = i2. 4. If the left to right diagonal of i3 is mapped to the other diagonal of j, then apply transformation (5) listed above to i3, creating i4. Otherwise let i4 = i3. 5. If the rows of i4 are mapped to the columns of j, then apply transformation (4) listed above to i4, creating i5. Otherwise let i5 = i4 First we observe that the transformations mapping i to i5 are each individually equivalence-preserving, and by Lemma 12 their composition preserves equiv- alence. Let us call this composition t . t has enumerated the order of the elements of the left to right diagonal of i, specified the mapping of the rows of i to either rows or columns of i5, and specified whether the left to right diagonal of i maps to itself or to the other diagonal of i5. Thus, by Lemma 20, t has completely specified the board of i5, and i5 must be identical to j. Thus t is indistinguishable from t, and any equivalence-preserving positional transfor- mation may be decomposed into some composition of transformations (1)–(5) proposed above17 . Let us take an example transformation t: 17Note that steps (4) and (5) both apply transformations that map rows to columns and vice versa. As a result of this, the rules for applying these transformations are slightly more complicated than those listed above. The enumeration of these rules is left as an exercise for the reader. 22
  • 23. Instance i A B C D E F G H I J K L M N O P t =⇒ Instance j P H L D N F J B O G K C M E I A For step (1), we observe that the outer squares of the left to right diagonal in i are reversed in j, so we apply transformation (1): Instance i A B C D E F G H I J K L M N O P transformation (1) =⇒ Instance i1 P N O M H F G E L J K I D B C A For step (2), we observe that the inner squares of the left to right diagonal in i1 are not reversed in j, so we do not apply transformation (2): Instance i1 P N O M H F G E L J K I D B C A (no transformation) =⇒ Instance i2 P N O M H F G E L J K I D B C A For step (3), we observe that the outer squares of the diagonal in i2 map to the outer squares of the diagonal in j, so we do not apply transformation (3): Instance i2 P N O M H F G E L J K I D B C A (no transformation) =⇒ Instance i3 P N O M H F G E L J K I D B C A For step (4), we observe that the left to right diagonal in i3 maps to itself in j, so we do not apply transformation (5): Instance i3 P N O M H F G E L J K I D B C A (no transformation) =⇒ Instance i4 P N O M H F G E L J K I D B C A For step (5), we observe that the rows in i4 map to columns in j, so we apply transformation (4): Instance i4 P N O M H F G E L J K I D B C A transformation (4) =⇒ Instance i5 P H L D N F J B O G K C M E I A 23
  • 24. Instance i5 is identical to instance j, and t is a composition of positional transformations (1) and (4). These five transformations and their compositions are the only candidates for positional equivalence-preserving transformations. Theorem 22 The five transformations listed above and their compositions form the maximal set of positional transformations that guarantee equivalence in Quarto. This set contains 25 = 32 distinct elements. Proof By inspection, the five transformations listed above preserve win- placements. Thus, by Lemma 15 they are equivalence-preserving. By Lemma 12 their compositions are equivalence preserving. Finally, the 32 possible compo- sitions are distinct as shown by their enumeration (See Appendix). 7.3 Piece Transformations The other class of transformations that we consider are those that alter the values of individual pieces without permuting the squares of the board. Piece transformations are permutations of the piece names. Definition 23 (Piece Transformation) A piece transformation is a permu- tation of the piece names in a Quarto instance, or a general transformation whose positional permutation is the identity. One way of specifying this permutation is to consider the pieces as 4-bit strings, and the transformation as a one-to-one onto mapping whose domain and range are both the set of pieces [0, 15]. For example, one possible transformation is to exchange every dark piece in an instance with the corresponding pale piece. This is equivalent to toggling the bit in the piece names that corresponds to color. Another example piece transformation might specify that the pattern of short and tall pieces in an instance should be exchanged with that instance’s pattern of round and square pieces. That is, every short piece should be replaced by the corresponding round piece (and tall with square), while every round piece should be replaced with the corresponding short piece (and square with tall). These two classes of piece transformations can be expressed as functions on the bits that represent the pieces, which, in turn, encode a permutation of the piece names. We consider two classes of piece transformations as follows: 1. There are 16 equivalence-preserving piece transformations that XOR18 the bits of every piece with some constant c such that 0 ≤ c ≤ 15. 2. There are 24 equivalence-preserving piece transformations that permute the bits of every piece in one of 4! = 24 ways. 18XOR is a function of two 1-bit inputs that returns 1 if the inputs differ and 0 if they are identical. When extending the inputs to n-bit strings, the ith bit of the output corresponds to the ith bits of the inputs. XORing with a constant toggles bits of the input for each bit where the constant has a 1 bit. 24
  • 25. The first class toggles a set of bits of each piece. This corresponds to the first example transformation mentioned above. The second class exchanges attributes as in the second example transformation mentioned above. It can be shown that these piece transformations and their compositions guarantee equivalence. In fact, any equivalence-preserving piece transformation can be constructed as a composition of these two classes of transformations. This proof follows. Lemma 24 A piece transformation t guarantees equivalence if and only if ∀ quartets Q with a value v (win or draw), t(Q) = Q has the same value v. Proof Suppose a piece transformation t maps some winning quartet Q to a draw quartet Q . We construct a winning instance i in which the board contains Q on some winning position and nothing else. The instance t(i) = i is not a winning instance, and thus by Lemma 13 t does not guarantee equivalence. Suppose a piece transformation t maps some draw quartet Q to a winning quartet Q . We construct a draw instance j in which the board contains Q on some winning position and the rest of the winning positions contain draw quartets. The instance t(j) = j is a winning instance, and thus by Lemma 13 t does not guarantee equivalence. Thus any piece transformation that does not preserve the values of quartets does not guarantee equivalence. Consider some piece transformation t that preserves the values of quartets. If we construct a winning instance i, the instance t(i) = i must also contain a win, as the winning win-placement in i must be mapped to a winning win-placement in i . Similarly, if we construct a draw instance j, the instance t(j) = j must also be a draw, as every draw win-placement in j must be mapped to a draw win-placement in j . Thus any piece transformation that preserves the values of quartets guarantees equivalence, and Lemma 24 is proven. Definition 25 (Kill Piece) Suppose some set P of pieces shares some set of bits across every element. A kill piece is any piece k such that the set P ∪ {k} shares no bits across every element. Definition 26 (Non-kill Piece) Suppose some set P of pieces shares some set of bits across every element. A non-kill piece is any piece n such that the set P ∪ {n} shares some set of bits across every element. As an example, consider a set P = {10002, 11112}. Any two pieces of the form 1 ∗ ∗∗2 will complete a winning quartet. These are non-kill pieces. If a quartet containing P contains any piece of the form 0 ∗ ∗∗2 it cannot be a winning quartet. These are kill pieces. Lemma 27 If a piece transformation guarantees equivalence, it must preserve the Hamming distances19 between every pair of pieces. 19Hamming distance measures the difference between two bit-strings. If a two strings have a Hamming distance of 1, it means that a single bit is different (e.g. 1111 and 1101). A precise definition is that the Hamming distance between two strings is the number of 1 bits in their XOR. 25
  • 26. Proof Let P1 be a pair of pieces with Hamming distance 1. Since they share three bits, any kill piece for this set must differ on these three bits. There is one bit remaining, determining 2 kill pieces. The complement of the set of kill pieces contains 12 non-kill pieces. Let P2 be a pair of pieces with Hamming distance 2. Since they share two bits, any kill piece for this set must differ on these two bits. There are two bits remaining, determining 4 kill pieces. The complement of the set of kill pieces contains 10 non-kill pieces. Let P3 be a pair of pieces with Hamming distance 3. Since they share one bit, any kill piece for this set must differ on this bit. There are three bits remaining, determining 8 kill pieces. The complement of the set of kill pieces contains 6 non-kill pieces. Let P4 be a pair of pieces with Hamming distance 4. Each piece in P4 is the complement20 of the other, and thus there is no winning quartet that can be constructed containing P4. Suppose that some piece transformation t1→2 maps P1 to P2. There are 4 kill pieces for P2 and 2 kill pieces for P1, so there are at least two kill pieces for P2, kP2,1 and kP2,2, whose preimages under t1→2 are non-kill pieces for P1. Thus we can construct a winning instance i such that the only occupied win- placement on the board contains P1 and the preimages of kP2,1 and kP2,2. i is a winning instance and t1→2(i) = i is not a winning instance. Thus by Lemma 13 any piece transformation that maps a pair of pieces with Hamming distance 1 to a pair of pieces with Hamming distance 2 does not preserve equivalence. The inverse of t1→2, t−1 1→2, maps the non-winning instance i to the winning instance i, and thus by Lemma 13, any piece transformation that maps a pair of pieces with Hamming distance 2 to a pair of pieces with Hamming distance 1 does not preserve equivalence. Suppose that some piece transformation t1→3 maps P1 to P3. There are 8 kill pieces for P3 and 2 kill pieces for P1, so there are at least two kill pieces for P3, kP3,1 and kP3,2, whose preimages under t1→3 are non-kill pieces for P1. Thus we can construct a winning instance i such that the only occupied win- placement on the board contains P1 and the preimages of kP3,1 and kP3,2. i is a winning instance and t1→3(i) = i is not a winning instance. Thus by Lemma 13 any piece transformation that maps a pair of pieces with Hamming distance 1 to a pair of pieces with Hamming distance 3 does not preserve equivalence. The inverse of t1→3, t−1 1→3, maps the non-winning instance i to the winning instance i, and thus by Lemma 13, any piece transformation that maps a pair of pieces with Hamming distance 3 to a pair of pieces with Hamming distance 1 does not preserve equivalence. Suppose that some piece transformation t2→3 maps P2 to P3. There are 4 kill pieces for P2 and 8 kill pieces for P3, so there are at least 2 kill pieces for P3, kP3,1 and kP3,2, whose preimages under t2→3 are non-kill pieces for P2. Thus we can construct a winning instance i such that the only occupied win-placement on the board contains P2 and the preimages of kP3,1 and kP3,2 under t2→3. i is a 20A complement of a binary number is the result of flipping every bit. 26
  • 27. winning instance and t2→3(i) = i is not a winning instance. Thus by Lemma 13 any piece transformation that maps a pair of pieces with Hamming distance 2 to a pair of pieces with Hamming distance 3 does not preserve equivalence. The inverse of t2→3, t−1 2→3, maps the non-winning instance i to the winning instance i, and thus by Lemma 13, any piece transformation that maps a pair of pieces with Hamming distance 3 to a pair of pieces with Hamming distance 2 does not preserve equivalence. Suppose some piece transformation tn→4 maps one of P1, P2, or P3 to P4. We can construct a winning instance i such that the only occupied winning position on the board contains one of the former three pairs of pieces and is completed by two corresponding non-kill pieces. Instance i is a winning instance and instance tn→4(i) = i is a non-winning instance. Thus by Lemma 13 any piece transformation that maps a pair of pieces with Hamming distance less than 4 to a pair of pieces with Hamming distance 4 does not guarantee equivalence. The inverse of tn→4, t−1 n→4, maps the non-winning instance i to the winning instance i, and thus by Lemma 13, any piece transformation that maps a pair of pieces with Hamming distance 4 to a pair of pieces with Hamming distance less than 4 does not guarantee equivalence. We do not consider pairs of pieces with Hamming distance 0, as these two pieces are identical, and the rules of Quarto allow distinct pieces only. Thus any piece transformation that does not preserve pairwise Hamming distances does not guarantee equivalence. Lemma 28 XORing any set of n-bit strings with the same n-bit constant both preserves the pairwise Hamming distances and preserves equivalence. Proof Consider the 1-bit strings 1 and 0. The pairwise Hamming distance is 1. XORing them by 1 produces 0 and 1, and the pairwise Hamming distance is still 1. Similarly, XORing them with 0 produces 1 and 0, and the pairwise Hamming distance remains 1. Thus XOR by a constant preserves the pairwise Hamming distance of 1-bit strings. Any n-bit string is a contatenation of n 1-bit strings, and by preserving the Hamming distances between the individual 1-bit strings, XOR preserves the Hamming distance between the n-bit strings. Consider an XOR transformation t and an instance i such that t(i) = i . If i contains a win, then i contains a win, as the common bits of the pieces on the winning win-placement are shared in i , although their value may be flipped. Similarly, if i is a draw, then i must be a draw, although the values of some set of bits of each piece may be toggled. Regardless, there is still no bit shared by all 4 pieces of any win-placement. Thus by Lemma 13 t preserves equivalence. Lemma 29 A permutation of the bits of a set of bit strings both preserves the pairwise Hamming distances and preserves equivalence. Proof Consider some permutation π of bits of a set of bit strings. For any strings s1 and s2, the ith bit of the string is mapped to the jth bit of s1 and s2. 27
  • 28. The Hamming distance between s1 and s2 will be identical to that of s1 and s2 as the individual bits are still aligned in the jth position. Thus bit-permutations preserve pairwise Hamming distances. Consider some bitwise-permutation transformation t and an instance i such that t(i) = i . If i contains a win, then i contains a win, as the common bits of the pieces on the winning win-placement are shared in i , although the position of those bits may have changed. Similarly, if i is a draw, then i must be a draw with the positions of some set of bits of each piece permuted. There is still no bit shared by all 4 pieces of any win-placement. Thus by Lemma 13 t preserves equivalence. Lemma 30 If the pairwise Hamming distances are known between some piece p and each of the “elementary” pieces 00012, 00102, 01002 and 10002, as well as the zero piece 00002, then p is completely determined. Proof Suppose that d is the Hamming distance between p and 00002. If the Hamming distance between p and the elementary piece with a 1 in the ith position is d − 1, then the ith bit of p must be a 1. If the Hamming distance is d + 1, then the ith bit of p must be a 0. This algorithm can be applied to discover every bit of p since every elementary piece is available, and thus p is completely determined. Theorem 31 Any piece transformation that guarantees equivalence must be a composition of an XOR by some 4-bit constant and a bitwise permutation of the resulting 4 bits. Proof Assume that some equivalence-preserving transformation t maps instance i to instance j. Order the pieces of i, and match them with the corre- sponding pieces of j. Piece 00002 in j has some preimage in i. XOR every piece in i with the preimage of 00002 in j, yielding i , and call this transformation x. Now, the piece in i that corresponds to the preimage of 00002 in j is 00002. Originally we had i j ... t ... preimage =⇒ 00002 ... ... Now we have: i i j ... x ... ... preimage =⇒ 00002 00002 ... ... ... 28
  • 29. Since t guarantees equivalence, we know from Lemma 27 that the pairwise Hamming distance between any two pieces in i is the same as the Hamming distance between the corresponding pair in j. Similarly, XORing by a constant preserves pairwise Hamming distances by Lemma 28. Thus the pairwise Ham- ming distances are identical in i and j. Furthermore, i is equivalent to j, as it is the image of j under the transformation x(t−1 (j)) = i (we know t−1 ex- ists from Property 6 and we know the composition preserves equivalence from Lemma 12). Now examine the four pieces in i that are Hamming distance 1 from 00002. Each of them contains a single 1 bit. Compose some bitwise permutation y such that these four pieces in i are mapped to their corresponding pieces in j (which must also be Hamming distance 1 away from 00002). Apply y to i to create i . Now we have: i i i j ... x ... y ... ... preimage =⇒ 00002 =⇒ 00002 00002 preimage 00102 00012 00012 preimage 00012 00102 00102 preimage 10002 01002 01002 preimage 01002 10002 10002 ... ... ... ... Pairwise Hamming distances were the same between i and j, and since y is a bitwise permutation, it preserves pairwise Hamming distances by Lemma 29. Thus pairwise Hamming distances are identical in i and j. Furthermore, i and j are equivalent, as i is equivalent to j, and the bitwise permutation applied to i to produce i preserves equivalence by Lemma 29. There are four elementary pieces in i that correspond to four elementary pieces in j, and each of them also has a corresponding zero piece. Pairwise Hamming distances are identical in i and j, and by Lemma 30 i is completely determined. Thus it must be identical to j, and t(i) = y(x(i)) = j. This proof constructed any equivalence-preserving piece transformation as a composition of XOR with a constant and bitwise permutations by extracting information from the preimage and result and using the property of pairwise Hamming distance to constrain the intermediate states. Remark An n-dimensional hypercube is an undirected graph with 2n nodes labelled by n-bit strings in which an edge connects any two nodes with a Hamming distance of one21 . Suppose that we map the pieces to the 16 nodes of the 4-dimensional hy- percube22 . Consider a winning quartet: we fix one bit across all 4 pieces, and three free bits remain. These map to 8 nodes in the 4-dimensional hypercube 21See [8]. 22See [9]. 29
  • 30. whose labels share one bit. Those nodes determine a 3-dimensional subcube of the 4-dimensional hypercube. In fact, there are 8 subcubes, corresponding to the 8 ways to construct a winning quartet (4 bits · 4 values). An automorphism of a graph G is an isomorphism of G with itself. In any automorphism of a hypercube, the subcubes are preserved23 . When mapped back to Quarto, the transformation resulting in the automorphism of the hy- percube preserves winning quartets and thus it must guarantee equivalence by Lemma 24. Harary states that any automorphism of an n-dimensional hypercube is a composition of a bitwise permutation of the labels and an XOR of the labels with some n-bit constant24 . Thus the result of Theorem 31 can be reached by reducing the problem of Quarto transformations to automorphisms of the hypercube. Theorem 32 The piece transformations of XOR by a constant and bitwise per- mutation listed above and their compositions form the maximal set of piece trans- formations that guarantee equivalence in Quarto. This set contains 16·24 = 384 distinct elements. Proof Lemma 12 shows that compositions of the proposed piece transfor- mations guarantee equivalence. Theorem 31 shows that there are no equivalence- preserving piece transformations that are not a composition of these two classes of transformations. We have observed that they are distinct by an enumera- tion25 . 7.4 General transformations The addition of piece transformations to the positional transformations exam- ined above yields in a total of 32·16·24 = 12, 288 possible equivalence-preserving general transformations. Theorem 33 The positional and piece transformations listed in the previous two sections and their compositions form the maximal set of equivalence-preserv- ing transformations in Quarto. This set contains 32 · 384 = 12, 288 distinct transformations. Proof This follows directly from the conjunction of Theorems 22 and 32, with the observation from Lemma 12 that compositions of equivalence- preserving transformations are themselves equivalence-preserving. We have ob- served that these 12,288 transformations are distinct in an enumeration26 . . The distinctness of the 12,288 transformations we considered depends heavily on the instances mapped. We have verified that the 12,288 transformations 23See [10]. 24See [10]. 25This enumeration is too large to be included in this paper. 26This enumeration is too large to be included in this paper. 30
  • 31. are distinct for some instances in which the board is fully occupied. Other fully-occupied boards only allow 384 distinct transformations—the number of compositions of the piece transformations. Remark In fact, these compositions form an algebraic group. A group G is a set of elements and a binary operation ◦. The group must be closed under ◦, such that ∀g1, g2 ∈ G, g1 ◦ g2 ∈ G. The binary operation ◦ must be associative. There must be some identity element e such that ∀g ∈ G, g ◦ e = g. Finally, ∀g ∈ G, ∃g−1 such that g ◦ g−1 = e. In this group, each element is a transformation. The binary operation ◦ is composition, and the identity is the identity transformation. The inverse of each element is the transformation that permutes the piece names and their locations back to their original values before the transformation27 . 7.5 Normalization Equivalence-preserving transformations are useful in two ways. If we have a single instance, we can apply each of these transformations to build a set of distinct equivalent instances known as an equivalence class. Alternatively, if we have an equivalence class, we can reduce this set to a single representative instance—the normal form. In order for the normal form to function as a representative instance, it must be defined so that any two instances in the same equivalence class reduce to the same normal form. If two instances reduce to different normal forms, it is a signal that those two instances are not equivalent. The normal form is defined by the sequence of steps that generate it, and that sequence is chosen to ensure that all equivalent instances are reduced to the same normal form. The sequence we use is as follows: 1. All of the positional symmetries are applied to the original instance, gen- erating 32 equivalent, possibly distinct instances. Each of these instances has an associated tag. This tag, or positional bit-mask, is a 17-bit string in which the ith bit is set if the ith square of the board is occupied. The squares are ordered as follows: 0 (+1 position) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Those instances that share the largest tag are candidates for the normal form. 2. Each candidate instance is mapped with an XOR piece transformation. The constant used is the value of the piece on the first occupied square 27See [7] for more on groups. 31
  • 32. using the order from step (1). This results in the first occupied square containing the piece 00002. 3. All 24 bitwise-permutation piece transformations are applied to each of the candidate instances, and the resulting instances are compared to each other with a string comparison. That is, the pieces occupying the positions are compared in the order from step (1). They are treated as “characters”, and the instance that results in the lexicographically least string is selected as the normal form. Thus the normalization procedure selects a positional layout for the pieces on the board, toggles the bits of those pieces to bring the “first” piece to zero, and then selects the bitwise permutation that generates the instance occuring first in a lexicographic ordering. With the tools of transformations and the normal form, the game-state graph may be reduced to a subgraph containing only distinct nonequivalent instances. The next step is to ensure that these instances can be stored efficiently. The following section proposes an algorithm for encoding a Quarto instance in fewer than 64 bits. 8 Efficient Representation The state saved at any point in a search of the game-state graph must be able to fit into the 232 bytes available on a modern machine. After reducing the number of instances examined through pruning and normalization, we can reduce the space required to store each instance by choosing an efficient representation for instances. Let us call a representation of a Quarto instance an instance-string. The simplest way to store an instance is to create a string of 17 5-bit characters. The first character corresponds to the +1 position, and the subsequent characters each correspond to a square on the board. If the value of a character is in [0, 15], then the corresponding Quarto piece is represented. Otherwise, the character signals that no piece occupies that character’s position. This scheme requires 85 bits to store an instance. This is inefficient because it does not exploit the characteristics of Quarto. We know, for example, that every piece appears at most once on the board. We can use this knowledge to construct a more efficient instance-string format. The first step in constructing a more compact representation of an instance is to assign some ordering to the squares of the board and the +1 position. We construct a 17-bit tag in which the ith bit is set if the corresponding square is occupied. This tag forms the first part of the instance-string. We adopt the following ordering: 0 (+1 position) 32
  • 33. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 If we know a priori that the number of pieces on the board is p, we only need to store as much of the tag as it takes to hold the p 1s. This results in a variable-length tag that requires 17 bits in the worst case. Now that we have a compact way of representing the positions occupied on the board, we must represent the pieces corresponding to those positions. A simple representation would hold one 4-bit character for each 1 in the tag. It is easy to do this in fewer bits. We construct an ordered list of all Quarto pieces (0 to 15). Instead of representing the piece values themselves, the bits of the instance-string following the tag contain the indices into this list for the pieces occupying the corresponding squares on the board. Each time that a piece is indexed in the list, it is removed and the list grows shorter. The maximum index into the list decreases as it shrinks. As a result, we can reduce the number of bits required to index the list as pieces are removed. For a list of length l, the number of bits required to index the list is log2 l . If the board is full, the last piece does not need to be represented, as it can be discovered by the process of elimination. Furthermore, if the board is normalized, we know that the first piece encountered will always be 0, so we can eliminate it from the instance-string and the ordered list. The resulting number of bits required for this portion of the instance-string is 14 i=1 log2 i (4) yielding 45 bits as follows: Bits required: 4 4 4 4 4 4 4 3 3 3 3 2 2 1 Max list index: 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Using this scheme, there are at most 17 tag bits and 45 index bits, for a total of 62 bits (a 27% reduction from the naive scheme). A normalized board is more likely to have more pieces close to the beginning of the tag order than at the end, due to the normalization algorithm (it selects the equivalent instance with the largest tag). This means that the tag will likely be shorter, saving bits. An additional optimization would invert the meaning of the tag for boards which are more than half full. That is, each 1 in the tag would correspond to an empty position rather than a full position, and normalization could be altered accordingly to prioritize instances with pieces farther from the beginning of the tag order. Using the inverted-tag scheme, it is more likely that the tag will be shorter, but the worst case scenario still requires a 17-bit tag. The downside to using a compressed instance-string is the time required to encode, decode, read, and write the strings. The encode and decode penalties 33
  • 34. are the price paid for space savings. However, the read and write penalties are significantly higher than those of the encode and decode procedures. If a variable-length scheme is implemented, a collection of contiguous instance- strings must be traversed sequentially to find the location of a specific instance- string. Furthermore, each of these instance-strings must be decoded in order to find the boundaries between them. A fixed-length encoding allows for a quicker look-up, but unless the instance- strings are byte-aligned, a specific string may still have to be extracted using bit operations before being easily accessible. Given that the worst-case instance- string length is 62 bits, we choose to allocate 64 bits per instance-string (wasting only 2 bits for every 64 - a loss of only 3%). This allows for fast indexing and aligned storage, as well as for convenient manipulation in a built-in data type (C provides a 64-bit long integer type). We decline to use the more complex inverted-tag scheme as the additional savings only materialize with a variable-length scheme. We also store all 17 bits of the tag regardless of the number of pieces on the board. This reduces the time required to encode and to decode instance-strings and removes the requirement of knowing how many pieces are on the board. Furthermore, if all of the significant bits of the instance are pushed to the least-significant end and all of the leftover bits are set to 0, integer comparison operators can be used to distinguish boards rather than slower string comparison operators. Finally, by splitting each 64-bit string into two 32-bit pieces, we can index a sparse two-dimensional data structure holding the labels for the corresponding nodes in the game-state graph. In this scheme, only the label associated with an instance is stored, and the instance-string is encoded by its location in memory. Consider the following normalized board: 0 (+1) 2 3 This instance would have been represented by an 85-bit instance-string in the naive implementation. Using the 64-bit compression scheme, the string is the concatenation of the following bits: 11000001000000000 0010 0010 . . . 17-Bit Tag Implicit 0 Piece 1 Piece 2 39 Bits Ignored This efficient representation requires only 64 bits per instance, but it is not the optimal representation. There are 254 nodes in the game-state graph, so a lower bound on the number of bits required to represent any node on the game- state graph is therefore 54. Counting only normalized instances, that number drops considerably. However, in order to achieve this lower bound, it is likely that the representation format would be difficult to understand and implement. 34
  • 35. Applying the normalization algorithm and using an efficient representation of Quarto instances helps to reduce the search space of Quarto. There are approximately 254 instances that must be examined in a complete search. Op- timistically assuming that each of the 12,288 transformations is always distinct, the number of states to be examined is still about 243 . At 64 bits per instance, this amount of data is far larger than today’s memories, and the time required to examine this many states is prohibitive on generally available machines. However, the combination of pruning, normalization, parallelism, and an efficient representation yields an algorithm that can search the game space in a tractable amount of time. This is the general approach that allowed Goossens to solve Quarto. His work, as well as that of others, is reviewed in the following section. 9 Related Work 9.1 Goossens’ Work In [1], Luc Goossens solves Quarto, determining that two perfect players will always draw. In order to find this result, he searches the game space starting from a small number of nonequivalent instances, and generalizes the results to all equivalent instances. Goossens uses all piece transformations and positional transformations (3) and (4) presented in Section 7. He also uses rotation and mirroring opera- tions, which achieve some subset of the set of 32 positional transformations we examined. He does not use positional transformation (3). He generates the numbers of equivalence classes for different instance types by hand, reporting the results in Table 9.1. Instance Equivalence Classes† 0 + 1 1 1 + 0 2 1 + 1 8 2 + 0 96† 2 + 1 336† Table 1: Goossens’ results for the number of representative nonequivalent in- stances for different instance types. † We differ on results for 2 + 0 and 2 + 1 instances. He begins by examining the choice of the first piece. This piece is reducible to 00002 by the XOR transformation, and thus there is one equivalence class for 0 + 1 instances. This piece may be placed on a diagonal or off a diagonal, but no other choice matters. Thus there are two equivalence classes for 1 + 0 instances. The second piece can be reduced to 00012, 00112, 01112, or 11112 by bitwise permutation, yielding eight equivalence classes for 1 + 1 instances. At this point, the number of cases tracked in the analysis becomes fairly large. 35
  • 36. Suffice it to say that Goosens reports 336 equivalence classes for 2+1 instances, and runs his search from these 336 initial states. We arrive at some different results for the number of equivalence classes by using the normalization procedure in Section 7. Beginning with the empty board, a queue of possible successor states is generated, and each of those states is reduced to its normal form. Every distinct normal form generated is enqueued in the next iteration of the algorithm, producing a breadth first search that reports the number of distinct normal forms for each type of instance. Our results are in Table 9.1. Instance Equivalence Classes Total Instances‡ Instances Per Class 0 + 1 1 16 16 1 + 0 2 256 128 1 + 1 8 3840 480 2 + 0 40 28,800 720 2 + 1 148 403,200 2,724 3 + 0 454 1,881,600 4,145 3 + 1 3382 24,460,800 7,233 4 + 0 10374 79,497,600 7,663 4 + 1 91558 953,971,200 10,419 5 + 0 216432 2,289,530,880 10,579 Table 2: Our results for the number of representative nonequivalent instances of different types. Instances Per Class is the quotient of the number of equiv- alence classes and the total number of instances. The breadth-first search that generated these results was unable to handle the increase in the number of rep- resentative nonequivalent instances past 5 + 0. ‡ The total number of instances is calculated from Equations (1) and (2), and includes inconsistent states. Goosens’ reasoning is solid until more than one piece is placed on the board. At that point, there are multiple paths to each state. He counts a distinct state for each path. He also does not include positional transformation (3). While the 336 initial states in Goossens’ search do not comprise a minimal set of representative nonequivalent instances, they are a superset, and thus his results are accurate. He reports that his search takes about 30 million board evaluations to make a decision for a 5+0 instance. Furthermore, each additional search level multiplies the number of instances that must be examined by a factor of about 10. Goossens uses a minimax search, and in order for his search to be tractable, he uses α-β pruning28 to eliminate suboptimal subgraphs of the game-state graph from consideration. 28See Section 4. 36
  • 37. 9.2 Brown’s Work Combinatorics is “the branch of mathematics that deals with permutations and combinations,”29 or the branch of mathematics that deals with counting. Many aspects of Quarto pose interesting combinatorial questions. In this section, we present some of the more interesting results developed by Brown30 and augment them with our own analysis. Brown reports that there are 6,197,620,810,536 Quarto instances in which the board is full and there are no winning rows. In order to generate this result, the 16 4 = 1820 possible quartets are examined in a brute-force search, yielding 1284 quartets that are draws. Using a computer to automate the search, Brown reports that it is possible to combine these in 778,339 ways to create an instance without duplicating any pieces. For each of these draw quartets, there are 4! permutations of the elements in the quartet, yielding an additional factor of 4!4 . There are also 4! permutations of the quartets themselves, yielding 778, 339 · (4!)5 = 6, 197, 620, 801, 536 (5) for the number of distinct Quarto instances in which the board is full and no row contains a win. Theorem 34 There are 536 draw quartets in Quarto. Proof We were able to confirm the 1284 draw quartets combinatorially by calculating the total number of winning quartets and subtracting from the total number of quartets. Let us call a quartet with one attribute/value pair in common a single-attribute-win quartet, a quartet with two attribute/value pairs in common a double-attribute-win quartet, and so on. It is impossible to construct a quadruple-attribute-win quartet, as that would require four identical pieces. Similarly, it is impossible to construct a triple- attribute-win quartet, as there is one free bit, yielding two distinct pieces. Thus winning quartets are either single- or double-attribute-win quartets. Let us first consider double-attribute-win quartets. When fixing the first bit and value, there are 4 bits with 2 possible values each, yielding 8 choices. After fixing one of those choices, there remain 3 bits with 2 values each, yielding 6 choices. There is an additional factor of 1 2 to eliminate double counts, resulting in 24 ways of fixing two bits. Each of these 24 ways of fixing two bits may only be realized by a single quartet, as there are only 2 free bits remaining to differentiate the pieces. That is, the four distinct values of the bit strings correspond to one quartet. Finding the number of single-attribute-win quartets is more complicated. We know that there are 8 ways to choose the first common bit and value. Suppose we fix that choice and examine the three remaining bits in each piece in the 29See [11]. 30See [6]. 37
  • 38. quartet. The question is how to choose 4 different sets of 3 ordered bits so that no single bit has an identical value across all 4 sets. There are 8 4 = 70 sets of four distinct 3-bit numbers. We eliminate the 6 sets with one bit in common (3 bits · 2 values). This leaves 64 sets of four 3-bit numbers with no bits in common. Thus the total number of single-attribute- win quartets is 512 (8 bit/value selections · 64 3-bit completions). The number of winning quartets is therefore 536 (24 double-attribute-win quartets + 512 single-attribute-win quartets). Subtracting from the 1820 possible quartets yields 1284 draw quartets, the number generated by Brown’s brute force search. Brown proceeds by reporting a further result of 414,298,141,056 distinct instances in Quarto in which there is a draw. This result was generated by an- other search that took advantage of the piece transformations mentioned above, and two of the positional transformations, (2) and (4). This result agrees with Brown’s Monte Carlo31 simulations, which reported about 1 in 50.5017711 full- board instances to be a draw. That is, 16! · 1 50.5017711 = 414, 298, 141, 080 ≈ 414, 298, 141, 056. 9.3 Early Draw Prediction The number of draws in Quarto is large relative to a computer memory, so we investigate the possibility of detecting a draw condition before the board is full. We have constructed an instance with 10 pieces on the board that always evaluates to a draw in Figure 1. 1110 0001 1101 0010 0111 1010 0000 1000 0101 1111 or 14 1 13 2 7 10 0 8 5 15 Figure 1: This instance always evaluates to a draw. In fact, any determined draw (See Definition 36) must have at least 10 pieces arranged in this layout. Definition 35 (Definite Draw) An instance is a definite draw if and only if every node in the descendant subgraph for its corresponding node in the game- state graph is labelled draw or undetermined. Definition 36 (Determined Draw) An instance is a determined draw if and only if every win-placement on the board is dead. 31A Monte Carlo simulation is a randomized algorithm that reports an approximation of some correct result. Presumably, Brown generated some large number of random Quarto instances with full boards and checked each for a draw condition. The longer a Monte Carlo simulation runs, the more accurate the approximation. This is opposed to a Las Vegas style algorithm, which always returns the correct answer, but runs for a random amount of time, possibly never terminating. 38
  • 39. The set of determined draws in Quarto is a subset of the definite draws. We believe that it may be possible to construct an instance that is not a determined draw but is still a definite draw. In such an instance, at least one win-placement is not dead, but the set of pieces that could complete that win-placement for a win are deployed elsewhere on the board. The entire set of definite draws is outside the scope of this paper. Instead, we examine the format of a determined draw. A proof follows showing that any determined draw must have at least 10 pieces on the board in the arrangement of Figure 1. Lemma 37 A win-placement containing exactly 2 pieces is dead if and only if they are complements. A win-placement containing fewer than two pieces cannot be dead. Proof If a win-placement contains fewer than two pieces, we can select another piece that preserves any possible wins, and thus continue to maintain that possible win. If there are two pieces, and they are not complements of each other, they share at least one bit in common, and thus a possible win exists. If they are complements, they share no bits, and thus every possible win is eliminated. Lemma 38 If i is an instance with two win-placements that each contain ex- actly two pieces, one of which is shared by both win-placements, then at least one of the two win-placements is live, and thus i is not a determined draw. Proof Let i be some instance in which two win-placements x and y con- taining exactly 2 pieces each share a single piece p. In order for x to be a draw, it must contain p, p’s complement by Lemma 37. Since x and y share p, and there exists only one complement for every piece, y cannot contain p, and thus it is not dead by Lemma 37. Thus i is not a determined draw, and Lemma 38 is proven. Lemma 39 If an instance has 8 pieces or fewer on the board, it cannot be a determined draw. Proof Lemma 37 eliminates any instance with fewer than 8 pieces on the board, as each of the 4 rows cannot contain the minimum 2 pieces. It also eliminates any 8-piece instance in which any win-placement contains fewer than 2 pieces. Construct an instance such that every row contains 2 pieces. Arrange these pieces such that every column also contains 2 pieces. 2 2 2 2 2 2 2 2 39
  • 40. Each row and column contains 2 pieces, and each shares one piece with another win-placement that contains only one other piece. By Lemma 38 this board is not a determined draw. Lemma 40 If an instance has 9 pieces or fewer on the board, it cannot be a determined draw. Proof Any instance with 8 pieces or fewer on the board is eliminated by Lemma 39. Any 9-piece instance with fewer than 2 pieces in any win-placement is eliminated by Lemma 37. Construct an 8-piece instance with 2 pieces in each row and column as above. The 9th piece can only occupy one column and one row. Thus the distribution must be of the form: 2 2 2 3 2 2 2 3 There are 3 columns and 3 rows that share a single piece with a winning position that contains one other piece. By Lemma 38 this instance is not a determined draw. Theorem 41 If an instance with 10 pieces on the board is a determined draw, it must be of the form: 2 2 3 3 2 2 3 3 Proof Lemma 37 eliminates any 10-piece instance in which any win- placement contains fewer than 2 pieces. Consider a 10-piece instance of the form: 2 2 2 4 2 2 2 4 Each of the 2-piece columns shares one piece with the 4-piece row. Thus they must share another piece with a 2-piece row, and this instance is not a determined draw by Lemma 38. Consider a 10-piece board of the form: 40
  • 41. 2 2 3 3 2 2 2 4 Each of the 2-piece columns shares a piece with the 4-piece row. Thus, each must share a piece with a 2-piece row, and this instance is not a determined draw by Lemma 38. The only 10-piece instance type that remains is: 2 2 3 3 2 2 3 3 Figure 1 shows that such a determined draw can be constructed. Early draw detection allows for pruning the search tree, but the largest term in Equation 3 is when i = 8 or i = 9. Thus the majority of Minimax search time will be spent examining states in the “middle” of the game, and draw pruning will only result in incremental improvements. 10 Conclusion This paper examined Quarto from a computer science perspective. Section 4 discussed the technique of Minimax searching to create an automated player, and proposed several enhancements to the technique. Section 5 discussed the idea of solving a game, and segued into Section 6, which examined the feasi- bility of solving Quarto. Section 7 presented transformations and equivalence in Quarto, the most significant contribution of this paper. The connections between Quarto and the 4-dimensional hypercube were also explored in this section. Finally, a normal form was proposed as a representative for every in- stance in that normal form’s equivalence class. Section 8 proposed an efficient representation for Quarto, requiring fewer than 64 bits per instance. Finally, Section 9 reviewed related work by other Quarto enthusiasts and presented some original combinatorial results. There are additional areas where combinatorial analysis could be applied to Quarto to yield interesting results. An analysis of the number of Quarto wins would be a satisfying counterpart to the analysis of draws given by Kevin Brown in [6]. Further connections between the hypercube and Quarto may be exploitable to yield insights into the mathematical aspects of the game as well. Finally, various sophisticated combinatorial techniques such as Burnside’s and P´olya’s counting theorems32 could be applied to determine the number of equivalence classes for instance types with larger numbers of pieces on the board. 32See [12]. 41
  • 42. We implemented an automated player based on Minimax with α-β pruning, and found that the design of a consistently accurate heuristic function was a limiting factor. Further work in this area could be directed towards developing a machine learning based framework for heuristic functions. 42
  • 43. 11 Appendix Enumeration of 32 distinct equivalent instances generated using positional sym- metries from an instance with a full board. 1. The following instances map the left to right diagonal to itself. (a) Outer diagonal squares map to outer diagonal squares. i. Outer diagonal squares are in order, inner diagonal squares are in order. A B C D E F G H I J K L M N O P A E I M B F J N C G K O D H L P ii. Outer diagonal squares are in order, inner diagonal squares are reversed. A C B D I K J L E G F H M O N P A I E M C K G O B J F N D L H P iii. Outer diagonal squares are reversed, inner diagonal squares are in order. P N O M H F G E L J K I D B C A P H L D N F J B O G K C M E I A iv. Outer diagonal squares are reversed, inner diagonal squares are reversed. P O N M L K J I H G F E D C B A P L H D O K G C N J F B M I E A (b) Outer diagonal squares map to inner diagonal squares. i. Outer diagonal squares are in order, inner diagonal squares are in order. F E H G B A D C N M P O J I L K F B N J E A M I H D P L G C O K ii. Outer diagonal squares are in order, inner diagonal squares are reversed. F H E G N P M O B D A C J L I K F N B J H P D L E M A I G O C K 43