← The Engines

How Chopsticks’ computer knows every answer

A companion to the game: four fingers a hand, the whole game solved backwards from its end, and the draws that are left over

Download PDFPlay Chopsticks

Chopsticks has so few positions that the computer does not search at all. It knows the result of every one before the first move. This companion, the second of the arcade’s four, shows how. Ignoring which hand is which, the positions fit in a table of 196. Unlike tic-tac-toe, a game of Chopsticks can return to a position it has already been in, so there is no last move to work back from. Instead, the computer works backwards from the positions where the game is already over. Sweep after sweep, it labels each position a win or a loss for the player to move, with the number of moves the result takes. The positions never labelled are exactly the draws. The computer then reads its move from the table, and a repetition rule ends the drawn games that would otherwise never finish.

1 Hands as numbers

How many different positions can a game of Chopsticks reach?
Each player starts with one finger up on each hand. On a turn a player attacks, adding one of their hands to one of the opponent’s, or splits their fingers between their own two hands. A hand that reaches exactly five is out; past five it wraps round. A player with both hands out loses. Here blue attacks, and red’s left hand goes up to 2.
In wordsIn symbols
attack the hand 𝑏 with the hand 𝑎𝑏←(𝑎+𝑏)mod5
split two hands (𝑎,𝑏) into (𝑐,𝑑)𝑐+𝑑∈{𝑎+𝑏,𝑎+𝑏−5}
𝑐,𝑑≤4, {𝑐,𝑑}≠{𝑎,𝑏}
a player’s hands, in either order{𝑎,𝑏}, 0≤𝑎≤𝑏≤4
a position: the hands of the player to move, then the opponent’s(ℎme,ℎthem)

“Wraps round” means arithmetic modulo 5. A hand keeps the remainder of its total after dividing by five. A hand of 3 hit by a hand of 4 holds (3+4)mod5=2, and a hand that reaches exactly 5 holds 0, which is out. As in the first companion, a position is everything that decides how the game goes on, here both players’ hands and whose turn it is. A ply is one player’s move.

2 Which hand is which does not matter

How few positions does the computer need to keep?

The rules never ask whether a hand is the left or the right one: 1 and 3 plays exactly like 3 and 1. So a player’s hands can be written as a pair with the smaller hand first. This canonical form stands for both orders. Positions that differ only by a swap are the same position for every purpose of the game. This is a symmetry, and keeping one representative of each set of mirror images is called a symmetry reduction.

The grid holds every ordered pair of hands, with the first hand down the side and the second across the top. The table keeps the tinted pairs, smaller hand first. Each faint pair is the mirror image of a kept pair, and the arrow names that kept pair. Of 25 ordered pairs, 15 remain. One of those, with both hands out, only ever ends a game. That leaves 14 pairs with a live hand.

There is a second saving. Instead of recording whose turn it is, a position always lists the hands of the player to move first. A position with red to move and the same hands with blue to move are then one entry, read from the mover’s side. What matters is “my hands and yours”, not the colours. The engine’s key writes both pairs sorted, mover first, and the page counts repetitions with the same key, as Section 10 describes.

Proposition 1 (Size of the table). A player has (52)+5=15 unordered pairs of hands, 14 of them with a live hand, so the table has 142=196 positions. Ordered pairs with a record of whose turn it is would give 2⋅242=1,152.
Proof. An unordered pair from the five values 0 to 4 is either two different values, (52)=10 ways, or the same value twice, 5 ways; removing 0·0 leaves 14. A position in play has a live hand on each side, and the mover’s pair and the opponent’s are chosen independently. Ordered, each player has 52−1=24 live pairs, and the turn doubles the count. ∎

The table is 5.9 times smaller, and the reduction loses no information. The rules give a pair and its mirror image the same moves, mirrored, and the same games from then on.

3 Every move from a position

What can a player do on a turn?
Algorithm 1: Move generation: legalMoves in src/games/chopsticks/game.ts.
Require: the mover’s hands (𝑎,𝑏) and the opponent’s (𝑐,𝑑)
Ensure: every legal move
1for each live hand of the mover and each live hand of the opponent: an attack
2for each total 𝑡 in {𝑎+𝑏,𝑎+𝑏−5} with 𝑡>0the second drops five fingers
3for each 𝑥 with max(0,𝑡−4)≤𝑥≤𝑡/2
4if {𝑥,𝑡−𝑥}≠{𝑎,𝑏} then a split into (𝑥,𝑡−𝑥)a swap is not a move

An attack needs a live hand on each side. A position has up to four attacks, fewer when a hand is out or when two hands are equal. An attack of 1 onto 1 is the same move whichever 1 is used. A split rearranges the mover’s own fingers. Three details of the splits are easy to miss:

A swap is not a move
Splitting 1·3 into 3·1 changes nothing that matters, so it is not allowed. Line 4 compares the pairs as sets. Without this rule a player could pass forever.
Dropping five
The total may also go down by five, as if five fingers made a hand go out. 3·4 holds 7 fingers and can split into 0·2 or 1·1.
Reviving a hand
A split may give fingers to a hand that is out: 0·4 can become 1·3 or 2·2. A split may also put a hand out, 1·3 into 0·4.
Figure 1: The distinct moves of four positions, with red to move and red’s hands at the bottom: the start with its one attack and one split, 1·3 against 2·2, a hand revived from 0·4, and 3·4 dropping five.

Over the whole table a position has between 1 and 6 distinct moves, 3.5 on average. That is far fewer than tic-tac-toe’s opening nine, but a game of Chopsticks does not get shorter with each move.

4 Games that return to a position

Why can’t the computer just search to the end, as in tic-tac-toe?

In tic-tac-toe every move fills a square, so every game ends within nine plies. Backward induction can start from the last level of the tree. Chopsticks has no such limit. Splits move fingers back and forth, and wrapping round modulo 5 takes fingers away again. So a game can come back to a position it has already been in.

Figure 2: The shortest way back to the start takes 4 plies, all splits. After them the position, with red to move, is the starting one again.

Positions joined by moves then form a game graph rather than a tree, because the same position can be reached again from the positions after it. Minimax, which scores a position by first scoring every position after it, would follow the cycle in Figure 2 forever. The game is still finite in another sense, because it has only 196 positions. The next section uses that.

Definition 1 (Game graph). The game graph has a node for each position and an edge from each position to the position each legal move leads to. A cycle is a path that returns to its starting node. A game tree is a game graph without cycles, unfolded so that each path is drawn separately.

5 Solving backwards

How can the computer know the result of every position?
Algorithm 2: Retrograde analysis: solve in src/games/chopsticks/game.ts, run when the page loads.
Require: every position (ℎme,ℎthem) with a live hand on each side
Ensure: each position’s value: a win or a loss in 𝑛 moves, or a draw
1repeat until a sweep labels nothing new
2for each position not yet labelled
3if some move ends the game or leads to a loss for the opponentlabelled on an earlier sweep
4label it a win, in one more move than the fastest such loss
5else if every move leads to a win for the opponent
6label it a loss, in one more move than the slowest of those wins
7label every position still unlabelled a drawwith best play the game goes round in circles

A search starts from the position on the board and looks forwards. Retrograde analysis starts from the end. A position from which some move knocks out the opponent’s last hand is a win in one. A position whose every move hands the opponent such a win is a loss in two. The analysis goes on this way, and each sweep reads only what the sweeps before it decided. Here “win in 𝑛” and “loss in 𝑛” count plies, as in the first companion, and the count includes the winner’s final move.

Definition 2 (Win and loss in 𝑛). A position is a win in 𝑛 if the player to move can knock out the opponent’s last hand within 𝑛 plies whatever the opponent does, and 𝑛 is the least such number. It is a loss in 𝑛 if every move leads to a position that is a win for the opponent, and 𝑛 is the largest number of plies the mover can make the loss take. A position that is neither is a draw.
1Wins in oneAlgorithm 2
Require: every position (ℎme,ℎthem) with a live hand on each side
Ensure: each position’s value: a win or a loss in 𝑛 moves, or a draw
1repeat until a sweep labels nothing new
2for each position not yet labelled
3if some move ends the game or leads to a loss for the opponent
4label it a win, in one more move than the fastest such loss
5else if every move leads to a win for the opponent
6label it a loss, in one more move than the slowest of those wins
7label every position still unlabelled a draw
The first sweep finds the 20 positions where the mover can knock out the opponent’s last hand at once.
2Losses in twoAlgorithm 2
Require: every position (ℎme,ℎthem) with a live hand on each side
Ensure: each position’s value: a win or a loss in 𝑛 moves, or a draw
1repeat until a sweep labels nothing new
2for each position not yet labelled
3if some move ends the game or leads to a loss for the opponent
4label it a win, in one more move than the fastest such loss
5else if every move leads to a win for the opponent
6label it a loss, in one more move than the slowest of those wins
7label every position still unlabelled a draw
The second finds the 3 positions where every move leaves the opponent a win in one.
3Sweep until nothing changesAlgorithm 2
Require: every position (ℎme,ℎthem) with a live hand on each side
Ensure: each position’s value: a win or a loss in 𝑛 moves, or a draw
1repeat until a sweep labels nothing new
2for each position not yet labelled
3if some move ends the game or leads to a loss for the opponent
4label it a win, in one more move than the fastest such loss
5else if every move leads to a win for the opponent
6label it a loss, in one more move than the slowest of those wins
7label every position still unlabelled a draw
Wins and losses alternate, sweep by sweep, up to a win in 25 plies. Then a sweep labels nothing new, and the 102 positions still unlabelled are draws. The table holds 73 wins and 21 losses.
Figure 3: The positions each sweep decides, with wins in an odd number of plies above the line and losses in an even number below.

Every win in Figure 4 has an odd length and every loss an even one. A win ends with the winner’s own move, an odd-numbered ply counted from the position. A loss ends with the opponent’s move, an even-numbered ply. The script that builds these figures, figures.ts, checks this for every labelled position.

Lemma 1 (Sweep 𝑛 labels exactly the results of length 𝑛). After sweep 𝑛, the table holds exactly the positions that are a win or a loss in at most 𝑛 plies, each with its correct length.
Proof. The proof is by induction on 𝑛. Before sweep 1 the table is empty. Sweep 𝑛 reads a snapshot holding, by the induction hypothesis, exactly the results of length at most 𝑛−1. Take a position not in it. If it is a win in 𝑛 with 𝑛>1, one of its moves leads to a loss in 𝑛−1 for the opponent, which the snapshot holds. If it is a win in 1, one of its moves ends the game. No move leads to a shorter loss, or the position would be a shorter win. So line 4 labels it a win in exactly 𝑛. If it is a loss in 𝑛, every move leads to a win for the opponent of length at most 𝑛−1. All of those wins are in the snapshot, and the longest has length 𝑛−1. So line 6 labels it a loss in 𝑛. Conversely, every position the sweep labels has one of these two forms. The position was not labelled on an earlier sweep, so its shortest win or longest loss through the snapshot is exactly 𝑛. ∎
Proposition 2 (The sweeps stop). The analysis ends after at most 197 sweeps. On this table it runs 26 sweeps. The longest result has length 25, and one more sweep finds nothing new.
Proof. Every sweep but the last labels at least one position, and a label is never removed, so there can be at most 196 labelling sweeps. A sweep that labels nothing leaves the table as it found it, so the next sweep would read the same snapshot and label nothing either. The table has then reached a fixed point, a state that a sweep maps to itself, and stopping there loses nothing. ∎
Figure 4: Run the analysis sweep by sweep and watch the table fill. Tap a square to see that position’s moves and the table’s value for each. Those values show why the position is labelled on its sweep.

6 Why the leftovers are draws

A position the sweeps never label: why is it a draw, and not just unknown?

When the sweeps stop, a position is either labelled or not. Lemma 1 says every forced result of any length was labelled. The unlabelled positions are more than unknown. From each of them, neither player can be forced to lose.

The start is never labelled. Red has two distinct moves: 1 onto 1 (draw) and split to 0·2 (loses in 26). The split leads to a labelled position, a win for blue. The attack leads to another unlabelled position. So red can keep the game among unlabelled positions, and so can blue on the next turn.
Theorem 1 (Unlabelled means drawn). Let 𝑈 be the positions left unlabelled when the sweeps stop. From every position in 𝑈 the player to move has a move to a position in 𝑈, and no move that ends the game. So both players can keep the game in 𝑈 forever, and neither can force a win. With best play the game never ends.
Proof. Take 𝑃 in 𝑈. If some move from 𝑃 ended the game or led to a labelled loss, the next sweep would label 𝑃 a win. If every move led to a labelled win, it would label 𝑃 a loss. Neither happened, because the sweeps had stopped. So some move leads to a position that is neither a labelled win nor a labelled loss, which puts it in 𝑈. A player who always plays such a move never reaches a position from which the opponent can knock them out, since that position would be a labelled win for the opponent. A forced win from 𝑃 would be a win in some 𝑛, which Lemma 1 says would have been labelled. ∎

Staying in 𝑈 forever means coming back to some position again and again, since there are only 196 positions. The draws are exactly the cycles both players can keep the game in. In the live figure above, tap any grey square after the last sweep, and its moves include one marked “a draw”.

7 Reading a snapshot

Why does each sweep read a copy of the previous table instead of the table itself?

Each sweep starts by copying the table and reads only the copy, while writing its labels into the table. A sweep therefore never sees its own labels, and Lemma 1 holds. Sweep 𝑛 labels exactly the results of length 𝑛, so the sweep figures can show each position as soon as its sweep runs.

Reading the table while writing it would let a sweep use labels it wrote earlier in the same sweep. Its result would then depend on the order it visits positions in. Suppose position B, a loss in 4 for the opponent, is labelled early in a sweep, and position A, a loss in 2, is still unlabelled when the sweep reaches P. P is then labelled a win in 5, through B. Labels are never revisited, so the quicker win in 3, through A, is lost.

The copy costs one table of 196 entries per sweep, which is negligible at this size.

8 Fastest wins, slowest losses

Why record how long a win takes, and not just who wins?

The table could store only win, loss or draw. It stores lengths too, for the same reason tic-tac-toe’s score counts plies. A computer that knows it is winning should take the quickest route. Otherwise it may keep playing winning moves without ever finishing the game. A computer that knows it is lost should hold out as long as it can, since every extra move is a chance for a human opponent to go wrong.

Red holds 1·2 against 0·2 and has lost, because every move gives blue a forced win. But the moves are not equally bad: 2 onto 2 loses in 22, 1 onto 2 loses in 4 and split to 0·3 loses in 2. The table’s lengths let the computer pick the slowest loss, which makes blue find the win 21 plies deep instead of at once.
In wordsIn symbols
a win: one more than the opponent’s quickest loss𝑊(1+min𝑚𝐿𝑚)
a loss: one more than the opponent’s slowest win𝐿(1+max𝑚𝑊𝑚)
a move’s rank for a win in 𝑛, where larger is better1000−𝑛
a draw0
a loss in 𝑛−1000+𝑛

The engine’s rank maps the three kinds of value to one number, so choosing a move means taking a maximum. Any win outranks any draw, and any draw outranks any loss, as long as lengths stay under 1000. Lengths here are at most 25.

9 What the table says

If nobody can force a win from the start, can the first move still lose?

The start, with one finger on each of four hands, is one of the 102 draws. Neither player can force a win from it, and the page calls the game a draw when a position comes up for the third time. The table also gives a value to each of the first player’s moves:

1 onto 1
draw for the player who makes it.
split to 0·2
loses in 26 for the player who makes it.

Splitting to 0·2 looks harmless, but it loses. The opponent can force a win, though the win takes 25 more plies. A player has to know the table to find that win.

10 Repetition ends the cycles

If perfect play goes round in circles, how does a game ever end?

The page adds a rule that the solution does not use. The game is a draw when the same position comes up for the third time. “The same position” means the same hands for both players, in either order, with the same player to move. The page counts each position by the engine’s key together with whose turn it is, and it counts the starting position too.

Figure 5: A game between two perfect players from the start, with one square per ply, shaded by how often its position has come up: grey the first time, yellow the second, red the third, which ends the game after 64 plies.

Both players in this game play from the table and break ties among equally good moves at random. So the game passes through many drawn positions before one comes up three times. Here the one that does is the start itself. Counting to three rather than two gives a player who returns to a position once the chance to try another move. Chess’s threefold repetition rule works the same way.

The table does not use the rule. Its values assume the game could go on forever, and the computer never plays towards or away from a repetition. For a draw it makes no difference, since a draw by repetition and an endless draw score the same.

11 Playing from the table

With every answer known, how does the computer choose?

The computer looks up the position each of its moves leads to. It plays the fastest win if there is one, otherwise a draw, otherwise the loss that takes longest, so a human opponent has the most chances to go wrong. Among equals it picks at random. Tic-tac-toe’s computer takes the first of equal squares, so it always plays the same game. Chopsticks’ computer can answer the same position differently from one game to the next.

Figure 6: A forced win in 5 plies. Red splits to 0·3 so that blue’s only move raises that hand to four, and red then knocks out blue’s last hand.

In Figure 6 red, holding 1·1 against 1·4, has 3 different moves: 1 onto 4 (wins in 5), split to 0·2 (draw) and 1 onto 1 (loses in 8). The table names the one that wins and how long it takes.

Easy plays a uniformly random legal move 50% of the time and the table’s choice otherwise. “Legal move” here counts each hand separately. From the start Easy chooses among 5 moves that are only 2 different ones: four attacks of 1 onto 1 and one split. It plays the split, the losing move, with probability 0.5⋅1/5=0.1, since the table’s choice is never the split. When the computer takes over a player’s seat in a Play with friends room, it plays at full strength.

Figure 7: Every move’s value from the table. Play both sides, from this position or the start. A position’s third appearance ends the game in a draw, as on the page.

12 What solving costs

Is solving the whole game in the browser expensive?

No. The analysis runs once, when the page’s code loads, before the first move. Its 26 sweeps look at every move of every position not yet labelled, 17,074 table lookups in all. They take about 14 milliseconds on the machine that built this document. After that, every move the computer makes is a handful of lookups, one per legal move.

Tic-tac-toe’s computer solves nothing in advance and searches up to half a million positions for its first move. Its tree is small enough to search on every turn, so nothing has to be kept. Chopsticks’ game graph has cycles, so a search would not end. But the graph has only 196 positions, so all of them fit in a table. Retrograde analysis needs every position in memory at once, and the later games are far too large for that. Four in a Row, the third companion, searches instead, with pruning and a memory. It stores only the positions near the start, solved ahead of time. Chowka Bhara, the fourth, is played with cowrie shells, and its computer judges positions with a trained network.