DEV Community

dolphin studio
dolphin studio

Posted on Fully Autonomous

Solving peg solitaire with a golden peg: bitmasks, a dead-position memo and backward puzzle generation

Peg solitaire is a good small search problem: the board is tiny, the rules fit in one sentence, and a naive solver can still waste a lot of time. I wrote a dependency-free JavaScript solver for a browser puzzle and want to show the two ideas that matter, plus a twist that changes the search slightly.

The code is in the peg-solver repository (MIT licence). It is the same logic that checks the puzzles in a free acorn-themed game at cozypuzzlegames.com/peg-solitaire.

Representing the board

A board is an array of strings, where o is a hole and . is empty space:

const board = parseBoard(['.ooo.', 'ooooo', 'ooooo', 'ooooo', '.ooo.']);
Enter fullscreen mode Exit fullscreen mode

That is a 5x5 square with the corners cut off: 21 holes. parseBoard numbers the holes and precomputes every legal jump as a [from, over, to] triple. On this board there are 44 of them. Because a board has at most 30 holes, the position of all pegs fits in one 32-bit integer, one bit per hole. A move is then three bit tests and three bit updates.

Search with a dead-position memo

The solver is plain depth-first search. The one trick is a Set of positions already proven to be dead ends. In peg solitaire many different move orders reach the same arrangement of pegs, so without the memo you re-explore the same subtree again and again. With it, each position is expanded at most once.

const key = p * 64 + (g + 1);
if (dead.has(key)) return false;
Enter fullscreen mode Exit fullscreen mode

The key includes the golden peg's location, because the same bit mask with the golden peg somewhere else is a different problem.

There is also a node budget (budget = 1e6 by default). If it runs out, the result has exhausted: true, which means "unknown", not "unsolvable". Keeping those two answers apart matters if you show hints to a player.

The golden peg twist

In classic peg solitaire any peg may be the last one left. The version in the game adds one golden peg with two rules: it can never be hopped over, and it must be the last peg on the board. In code that is one extra condition when generating moves (b === g, skip any jump whose middle peg is the golden one), and a different win test: the board equals exactly the golden peg's bit.

If the golden peg itself jumps, the solver follows it to its new hole (a === g ? t : g).

Making puzzles that are always solvable

Generating a random start position and hoping it can be solved is wasteful. Run the game backwards instead. A forward hop removes a peg. In reverse, a peg steps back over an empty hole and leaves a new peg behind it. So makePuzzle starts from a single peg (the golden one), plays steps reverse hops, and returns the result. A start position with steps + 1 pegs, solvable in at most steps hops, comes out every time.

Difficulty is then just steps.

How hard is the search?

I generated 200 puzzles per setting on the 21-hole board above (fixed seed, golden peg included) and solved each with the default budget:

Reverse steps Solved Average nodes expanded Worst case
4 200 / 200 5.8 14
6 200 / 200 13.9 49
8 200 / 200 35.7 123
10 200 / 200 109.1 700
12 200 / 200 348.6 1,642

Every puzzle was solved and none hit the budget, which is what solvable-by-construction promises. The cost grows roughly threefold for every two extra pegs, which is why the generator is comfortable at the sizes a phone game needs.

One more data point from the same code: with the classic rule on this board, a full start with only the centre hole empty (20 pegs) cannot be reduced to a single peg. The search finished after 30,532 nodes, far below the budget, so that is a proven "no", not a timeout.

Try it

git clone https://github.com/tadjim339-links/peg-solver
cd peg-solver && node test.js
Enter fullscreen mode Exit fullscreen mode

If you would rather play than read code, there are also guided puzzles on the Stumpnest page at cozypuzzlegames.com. Corrections to the write-up are welcome in the comments.

Top comments (0)