NAME
Game::Backgammon::Rules - the legal turn
SYNOPSIS
use Game::Backgammon::Rules qw(legal_turns);
my $turns = legal_turns($board, 'white', 3, 1);
$_->notation for @$turns; # '8/5 6/5', '24/21 6/5', ...
DESCRIPTION
A turn in backgammon is a set of moves, not a move, and two of the rules binding it are properties of the whole set:
both dice must be played if any legal sequence plays both;
if only one can be played, and either could be alone, the larger must be.
Neither can be applied one die at a time: a move that looks legal on its own can be the one that makes the other die unplayable, and then it never was. So legal_turns enumerates every sequence, keeps the longest, applies the larger-die rule to what is left, and deduplicates by the multiset of moves, because two orders reaching the same position are one choice.
A player who can do nothing gets exactly one turn back, an empty one. A forfeit is a turn, not an error.
Cost
Bounded: at most two dice from at most fifteen sources, or four on doubles. The worst realistic case is a doubles roll in a crowded position, which is a few hundred sequences. That is fine here and is the number the bot has to budget for.
FUNCTIONS
legal_turns($board, $player, @dice)
Every distinct legal turn, as Game::Backgammon::Turn objects. Always at least one: a player who can do nothing gets an empty turn.
single_moves($board, $player, $die)
Where one checker may go with one die. A building block of the enumeration above, and not a legal move on its own: only a whole turn is legal.
apply_move($board, $move)
The board that results, as a new object. Never changes the one it is given.