Play5sDeveloper notes

How a Backgammon Computer Counts Shots From 36 Rolls

The Play5s backgammon opponent measures risk by counting, for every lone checker, how many of the 36 dice rolls would hit it, and turns that count into an expected loss in pips. Each blot gets two 18-bit masks, one per half of the roll table. A hand-written evaluation adds the pip lead, the shape of the points held and the expected loss, and three levels choose among the plays by looking at less or more of it. There is no neural network and nothing to download.

This note covers only how the computer player chooses its play. It does not cover the rules, the board drawing or the dice.

What is a shot, and how do you count one?

A lone checker 6 points away: 17 of 36 rolls112233445566A lone checker 8 points away: 6 of 36 rolls112233445566Rows: first die. Columns: second die.a roll that hits (no blocked points)
A lone checker 6 points away: 17 of 36 rolls112233445566A lone checker 8 points away: 6 of 36 rolls112233445566a roll that hits. Rows: first die. Columns: second die.
Each cell is one roll of two dice. The shaded rolls hit a lone checker six points away (17 of 36) and eight points away (6 of 36), with no blocked points in between.

A single checker on a point is a blot. An opponent hits it by landing a checker on that point, and the hit sends it back to the bar. Wikipedia's backgammon article defines the rules of hitting. How likely a hit is depends on how many of the 36 equally likely dice rolls let the opponent do it. Players call each hitting roll a shot.

The count has two parts. If the hitter is six points or fewer behind the blot, any roll with a die of exactly that distance hits it directly: 11 rolls out of 36. Combination shots need both dice, or the repeated die of a double. A roll of 5 and 1 hits at distance six by moving 5 and then 1, or the other way round, but only if the point where the first checker would stop is not held by two opposing checkers. The code, hitMask handles all of this. For distances up to 24 it precomputes two tables, DIRECT and COMBO, at start-up. A double can hit at multiples of its pip value, up to four steps, and every intermediate point must be open. A non-double needs one of its two orders to pass through an open point.

The figure shows the answers when nothing is blocked: a blot six away is hit by 17 of the 36 rolls, one eight away by 6. Those are the numbers backgammon players learn as a first lesson. The program computes them from the same rules instead of storing them.

Why store a roll table as two 18-bit masks?

A roll is a pair of dice, so the table has 36 entries, with roll number (a - 1) * 6 + (b - 1). A set of rolls is a set of numbers from 0 to 35, and the neat storage for a set of small numbers is one bit per number. JavaScript's bitwise operators work on 32-bit integers, as MDN explains, so 36 bits do not fit in one value. The code splits the table into two halves of 18 bits, rolls 0 to 17 and rolls 18 to 35, and keeps each blot's shots as a pair of numbers.

Combining the shots of several attackers or several blots is then a bitwise OR, and counting shots is a population count, bits(l) + bits(h). Both are a few machine instructions. The earlier tables mean no dice are ever rolled during the computer's thinking, since it reasons about all 36 outcomes at once and each costs almost nothing.

A checker waiting on the bar changes the count, because it must enter first. hitMaskBar handles that case: a hit needs the entering die to open a point and the other die to finish the hit, or the entering checker itself to land on the blot. With one checker on the bar the mask is exact. With more, the source calls it a close estimate that counts only the hits made while entering.

How do shots turn into a loss in pips?

A blot that is hit costs its owner the distance it had already travelled and the tempo of having to enter again. blotRisk sets that cost at 25 - q + 6 + 2.2 * oppHome pips for a blot on point q, where the last term adds 2.2 for every home-board point the opponent already holds, since each one makes re-entering harder. Blots that no opposing checker can reach any more are skipped as safe.

The expected loss is then an average over all 36 rolls. For each roll, the program takes the worst cost among the blots that roll can hit, so a roll is counted once, at its most expensive victim, and the 36 numbers are summed and divided by 36. The result is a single figure in pips that goes straight into the evaluation. It is a plain expectation, so it compares risks that look unlike each other, such as one blot at distance 6 against two blots at distance 9.

What does the evaluation add up?

evalPos scores a position for the player who has just moved, in pips, so every term has the same unit. Wikipedia's article describes the pip count as the measure of a racing lead. The program starts from that difference and adds the rest:

  • Pip lead: the opponent's pip count minus the player's. In a pure race, with no contact left, this and a small bonus per checker borne off are all that count.
  • Structure: a table of values for points held by two or more checkers, in which the 6-point and the 5-point are worth most, then the bar point, anchors in the opponent's home board are rewarded, a run of three or more adjacent points earns a bonus that grows with its square, and a stack of more than three on one point is penalised, except on the 6, 8 and 13 points. The opponent's structure counts against the player at 0.85.
  • Blot risk: the expected loss above, subtracted.
  • Bar: 5 pips for every opposing checker waiting to enter.
  • Threats: for each opposing blot the player can hit next turn, its shots out of 36 times its value, discounted to 0.35 because the opponent moves first.

Everything is hand-set and small enough to read. That is a deliberate choice for a page: there is no model file, and every number in the list can be changed and re-tested.

What changes between the three levels?

The levels choose among the plays the evaluation ranks. Easy adds random noise of up to 16 pips to every score, skips the threat term and never doubles. Normal plays the best-scoring play. Sharp takes the top three plays that lie within 5 pips of the best score, except in a pure race, then looks at the opponent's replies: for each of the 21 distinct rolls, weighted 1 for doubles and 2 for the others so the weights make 36, it finds the opponent's best answer by the quick evaluation and scores the position after it. The final order blends the shallow score with 0.9 of that deeper number. That second look is the only search the program does, and it covers one ply.

The doubling cube follows from a win probability. In a race, the chance to win comes from a normal distribution of the pip difference, with a few pips for the roll and a spread that grows with the square root of the race length. With contact it is a squashed version of the evaluation. The normal curve is evaluated with the polynomial that Abramowitz and Stegun give as equation 7.1.26, listed among the approximations of the error function. Normal and Sharp double when the chance to win is at least 72 and 69 percent, never above 97, and accept a double from 22 and 25 percent. Easy never doubles and accepts from 12.

Backgammon is one of five board games open on 27 November 2026, next to chess, Gomoku, Tic Tac Toe and Long Nardy. For another opponent that thinks in a browser tab, the chess note explains why Play5s limits search by nodes instead of seconds.