| 1 | package Game::Backgammon::Rules; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Game::Backgammon::Board; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Game::Backgammon::Move; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use Game::Backgammon::Turn; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | our @EXPORT_OK = qw(legal_turns apply_move single_moves); | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 409932 | ▶ | sub single_moves { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
single_moves: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 409932 | my ($board, $player, $die) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 409932 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 409932 | ▶ | if ($board->bar($player)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 39765 | my $to = 25 - $die; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 39765 | ▶ | return () if $board->is_blocked($player, $to); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 36742 | return Game::Backgammon::Move->new( | |||||||||||||||||||||||||||||||||||||||||
| 22 | player => $player, from => 'bar', to => $to, die => $die, | ||||||||||||||||||||||||||||||||||||||||||
| 23 | hit => $board->is_blot($player, $to)); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 370167 | my $home = $board->all_home($player); | |||||||||||||||||||||||||||||||||||||||||
| 27 | 370167 | ▶ | my $highest = $home ? $board->highest_occupied($player) : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 370167 | for my $n (1 .. 24) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 8884008 | ▶ | next unless $board->mine_on($player, $n); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1871600 | my $to = $n - $die; | |||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1871600 | ▶ | if ($to >= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 1689644 | ▶ | next if $board->is_blocked($player, $to); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1597250 | push @out, Game::Backgammon::Move->new( | |||||||||||||||||||||||||||||||||||||||||
| 36 | player => $player, from => $n, to => $to, die => $die, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | hit => $board->is_blot($player, $to)); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 1597250 | next; | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 181956 | ▶ | next unless $home; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 15017 | ▶ | if ($to == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 4024 | push @out, Game::Backgammon::Move->new( | |||||||||||||||||||||||||||||||||||||||||
| 44 | player => $player, from => $n, to => 'off', die => $die); | ||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | elsif ($n == $highest) { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 2887 | push @out, Game::Backgammon::Move->new( | |||||||||||||||||||||||||||||||||||||||||
| 48 | player => $player, from => $n, to => 'off', die => $die); | ||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 370167 | return @out; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 2245681 | ▶ | sub apply_move { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
apply_move: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 2245681 | my ($board, $move) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 2245681 | my $next = $board->clone; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 2245681 | my $player = $move->player; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 2245681 | my $them = Game::Backgammon::Board::other($player); | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 2245681 | ▶ | if ($move->is_bar) { $next->to_bar($player, -1) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2147885 | else { $next->add($player, $move->from, -1) } | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 2245681 | ▶ | if ($move->is_off) { $next->to_off($player) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 2236433 | ▶ | if ($next->is_blot($player, $move->to)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 158086 | $next->set_point($them, 25 - $move->to, 0); | |||||||||||||||||||||||||||||||||||||||||
| 67 | 158086 | $next->to_bar($them); | |||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 2236433 | $next->add($player, $move->to, 1); | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 2245681 | return $next; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 41767 | ▶ | sub legal_turns { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
legal_turns: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 41767 | my ($board, $player, @dice) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 41767 | my @seqs; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 41767 | _walk($board, $player, \@dice, [], \@seqs, {}); | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 41767 | my $most = 0; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 41767 | ▶ | for my $s (@seqs) { $most = @{ $s->{moves} } if @{ $s->{moves} } > $most } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 41767 | @seqs = grep { @{ $_->{moves} } == $most } @seqs; | |||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 41767 | ▶ | if ($most == 1 && @dice == 2 && $dice[0] != $dice[1]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$most == 1 and @dice == 2 and $dice[0] != $dice[1]
$most == 1 and @dice == 2
MC/DC66%2/3
$most == 1 and @dice == 2 and $dice[0] != $dice[1]
$most == 1
@dice == 2
$dice[0] != $dice[1]
Truth table75%3/4
A and B and C
A= $most == 1B= @dice == 2C= $dice[0] != $dice[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 534 | ▶ | my $larger = $dice[0] > $dice[1] ? $dice[0] : $dice[1]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 534 | my @big = grep { $_->{moves}[0]->die == $larger } @seqs; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 534 | ▶ | @seqs = @big if @big; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 41767 | ▶ | return [ Game::Backgammon::Turn->new(player => $player, dice => \@dice, moves => []) ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | unless @seqs; | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 41767 | my (%seen, @turns); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 41767 | for my $s (@seqs) { | |||||||||||||||||||||||||||||||||||||||||
| 95 | my $turn = Game::Backgammon::Turn->new( | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 650455 | player => $player, dice => \@dice, moves => $s->{moves}); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 650455 | ▶ | next if $seen{ $turn->key }++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 650455 | push @turns, $turn; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 41767 | return \@turns; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 1682662 | ▶ | sub _walk { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_walk: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1682662 | my ($board, $player, $dice, $sofar, $out, $seen) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 1682662 | ▶ | return if $seen->{ join(',', sort map { $_->notation } @$sofar) . '|' . join(',', sort @$dice) }++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1023821 | push @$out, { moves => [ @$sofar ] }; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 1023821 | ▶ | return unless @$dice; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 374828 | my %tried; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 374828 | for my $i (0 .. $#$dice) { | |||||||||||||||||||||||||||||||||||||||||
| 111 | 507019 | ▶ | next if $tried{ $dice->[$i] }++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 409923 | my @rest = @$dice; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 409923 | my ($die) = splice @rest, $i, 1; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 409923 | for my $move (single_moves($board, $player, $die)) { | |||||||||||||||||||||||||||||||||||||||||
| 115 | 1640895 | _walk(apply_move($board, $move), $player, \@rest, | |||||||||||||||||||||||||||||||||||||||||
| 116 | [ @$sofar, $move ], $out, $seen); | ||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 374828 | return; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||||||||||||