Journal · Games and mathematics

21 Flags: a free strategy game, and the pattern that wins it

Twenty-one flags. Take one, two or three a turn, and whoever takes the last flag wins. Play a friend or a computer at three levels. There is a way to win every round, and the game will not tell you what it is until you have had a go.

Keys: 1 to 6 take flags, H for a clue, U to undo, P for the pattern once it unlocks, N for a new round. Scores are kept in this browser only. Nothing is sent anywhere.

How to play

The game starts with 21 flags. On your turn you take 1, 2 or 3 of them. Then your opponent does the same. Whoever takes the last flag wins the round.

Press Set up to choose your opponent: a friend on the same device, or the computer as a novice (it moves at random, but never misses a win in one move), as clever (it plays well most of the time and slips now and then), or as perfect (it never makes a mistake it can avoid). You can also choose who goes first, change the number of flags, change how many may be taken at once, and flip the rule so that the last flag loses.

Work it out yourself first

There is a simple rule that wins every round it can be won, and a player who knows it will beat one who does not almost every time. The game keeps it from you on purpose. Hint starts with a clue, and only names a move if you press it three times. Show the pattern stays locked until you have played three rounds. After each round the move log marks every move as safe or a slip, so you can see where it turned and wonder why.

A good way in: play the perfect computer, lose, and study the counts it leaves you on. Then try the game with fewer flags, say 9, where you can think every move through.

Spoiler: the winning pattern. Play a few rounds before you open this.

The pattern, worked backward

Start at the end. If it is your turn and 1, 2 or 3 flags are left, you take them all and win. If exactly 4 are left, you are stuck: whatever you take, 1, 2 or 3, your opponent takes the rest. So 4 is a losing count for the player about to move.

Now 5, 6 and 7 are winning counts, because from each of them you can leave your opponent on 4. And 8 is losing again: every take from 8 leaves 5, 6 or 7, which hands your opponent the win. The pattern repeats every four flags.

Flags left on your turnYouSafe move
1Wintake 1, leaving 0
2Wintake 2, leaving 0
3Wintake 3, leaving 0
4Losenone: every take leaves 1, 2 or 3 more than a multiple of 4
5Wintake 1, leaving 4
6Wintake 2, leaving 4
7Wintake 3, leaving 4
8Losenone: every take leaves 1, 2 or 3 more than a multiple of 4
9Wintake 1, leaving 8
10Wintake 2, leaving 8
11Wintake 3, leaving 8
12Losenone: every take leaves 1, 2 or 3 more than a multiple of 4

Classic rules: take 1 to 3, last flag wins. Computed by the page builder from the same rule the game uses.

So the losing counts are 4, 8, 12, 16 and 20. 21 is not one of them, which means the player who moves first can win every time: take 1, leaving 20. After that, whatever your opponent takes, take the number that makes the pair add up to 4. They take 3, you take 1. They take 1, you take 3. The count goes 20, 16, 12, 8, 4, and then you take the last flag.

Try it

Set the computer to perfect and go first. Take 1, then answer every move with its partner to 4. You will win every round. Now let the computer go first, and it will do the same to you. Press Show the pattern and the flags group themselves into blocks of four, with the flags you should take set apart in gold.

Why the perfect computer can lose

The perfect computer finds the safe move whenever there is one. When it is handed a multiple of 4 there is no safe move to find, so it takes a random amount and waits for you to slip. That is the whole game: once one player reaches a safe count and keeps answering in pairs, nothing the other player does can change the result.

Change the rules and the pattern moves

If the most you can take is k, the blocks are k + 1 flags long. Take up to 4 and the losing counts are multiples of 5. Take up to 2 and they are multiples of 3.

Flip the rule so the last flag loses and the target shifts by one: now you want to leave your opponent one more than a multiple of the block size, so they are left holding the last, poisoned flag. With the classic numbers the losing counts become 1, 5, 9, 13, 17 and 21. Under that rule the player who moves first at 21 is the one in trouble, and the smart choice is to let the other side start.

Every combination the setup allows follows the same two rules, and the coach and the perfect computer use nothing else.

An old game

Games of this kind are much older than computers. In 1612 the French mathematician Claude Gaspard Bachet de Méziriac published one in a book of number puzzles: two players take turns adding a number from 1 to 10 to a running total, and whoever reaches 100 wins. It is the same game run in the other direction, and once you have found the pattern in 21 Flags you can work out Bachet's for yourself.

Thinking backward from the end

The method that solves 21 Flags is called backward induction: label the end state, then label every state that leads to it, and keep going until you reach the start. It is also how we check a business process for dead ends, where a record can get into a state that has no way out. The journal post on explicit and implicit state machines does that for orders and invoices instead of flags.

Where this game came from

21 Flags began as one of the browser demos in Will Kennedy's personal portfolio, earlier work made before Highland Willow: two players or teams, the classic rules, a scoreboard and a move log. This version keeps all of that and adds:

  • a computer opponent at three levels, and a choice of who moves first;
  • a coach, hints that start as clues, and a pattern view, unlocked after three rounds, that groups the flags the way a strong player counts them;
  • a move log that marks each move as safe or a slip once the round is over (or as you go, with the pattern on), so you can see where it turned;
  • undo, keyboard play, and rules you can change: from 5 to 60 flags, up to 6 at a time, and the last-flag-loses variant;
  • scores that really are kept between visits, in this browser, separately for each opponent and rule set.

It is one self-contained page with no libraries, no fonts to download and no tracking inside the game, and it follows your device's light or dark setting.