NAME

Game::Backgammon - backgammon as a reusable engine

SYNOPSIS

use Game::Backgammon;

my $game = Game::Backgammon->new(seed => $bytes);
$game->turn;                       # who the opening roll sent first
$game->dice;                       # [5, 2], from the seed

for my $turn (@{ $game->legal_turns }) { print $turn->notation, "\n" }
$game->play('8/3 6/4') or die $@;

$game->status;                     # 'active' or 'finished'
$game->result->margin;             # 'single', 'gammon', 'backgammon'

DESCRIPTION

Pure rules: no input, no output, no clock, and no randomness beyond the seed. A game is its seed plus the turn chosen at each roll, and "replay" rebuilds it from exactly that.

It is the engine behind the backgammon at https://peer2peergames.com.

A turn is the unit

Two dice mean up to two checkers move and doubles mean four, and the rules binding which combinations are legal are properties of the whole set. So legal_turns offers whole turns and play takes one. See Game::Backgammon::Rules.

The dice are checkable but not predictable

Roll n is SHA-256($seed . "roll:$n:..."), so a finished game can be replayed and verified from its seed, while somebody who learns the seed mid-game learns nothing about what is coming. See Game::Backgammon::Dice.

No doubling cube, and no draws

There is no cube: it is not a wager, but it reads like one, and leaving it out costs the game little. Backgammon cannot be drawn, so a result always has a winner.

METHODS

new(seed => $bytes)

The opening roll decides who starts and gives them their dice.

status, result, turn, board, history, to_log

seed

The 32 bytes every roll is derived from.

rolls

How many rolls the game has consumed: the dice index replay needs.

pending

The dice rolled and not yet played.

other

The other player's name.

finish(winner => ..., reason => ...)

A finish imposed from outside: a resignation, a timeout, an abandoned game. A resignation concedes a single.

replay(seed => ..., turns => [ '8/5 6/5', ... ])

to_text

my $text = $game->to_text;

The game as a string that "from_text" builds the same game from: the seed in hexadecimal, every turn played with its player, its dice and its notation, and a result once there is one, whether the checkers were borne off or the game was finished from outside:

[seed 5f1c...]
1. white 31: 8/5 6/5
2. black 6666: 24/18(2) 13/7(2)
[result black single resign]

The dice rolled for the player to move and not yet played are not written: they are the seed's next roll, and are rolled again when the text is read.

from_text

my $game = Game::Backgammon->from_text($text);

A new game built from the text "to_text" wrote, by playing every turn in it again from the seed, with the same board, the same player to move, the same dice waiting and the same dice to come. It shares nothing with the game the text came from, and its to_text is the same text.

Dies with a sentence naming the line that is wrong when the text is not a game: a turn the seed did not roll, a player out of turn, a turn the rules do not offer, or a result the game did not end with.

SEE ALSO

Game::Backgammon::Board, Game::Backgammon::Rules, Game::Backgammon::Dice, Game::Backgammon::Result.

AUTHOR

LNATION, <email@lnation.org>

LICENSE AND COPYRIGHT

This software is Copyright (c) 2026 by LNATION.

This is free software, licensed under the Artistic License 2.0.