| 1 | package Game::Backgammon; | ||||||||||||
| 2 | |||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 6 | ▶ | use Object::Proto::Sugar -types; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 6 | ▶ | use Game::Backgammon::Board; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 6 | ▶ | use Game::Backgammon::Dice (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 6 | ▶ | use Game::Backgammon::Move; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 6 | ▶ | use Game::Backgammon::Notation (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 6 | ▶ | use Game::Backgammon::Result; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 6 | ▶ | use Game::Backgammon::Rules (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 6 | ▶ | use Game::Backgammon::Turn; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 6 | our $VERSION = '0.03'; | |||||||||||
| 17 | |||||||||||||
| 18 | has seed => ( | ||||||||||||
| 19 | is => 'ro', | ||||||||||||
| 20 | isa => Str | ||||||||||||
| 21 | 6 | ); | |||||||||||
| 22 | |||||||||||||
| 23 | has board => ( | ||||||||||||
| 24 | is => 'rw', | ||||||||||||
| 25 | isa => Object, | ||||||||||||
| 26 | 92 | ▶ | default => sub { Game::Backgammon::Board->new } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | 6 | ); | |||||||||||
| 28 | |||||||||||||
| 29 | has turn => ( | ||||||||||||
| 30 | is => 'rw', | ||||||||||||
| 31 | isa => Str | ||||||||||||
| 32 | 6 | ); | |||||||||||
| 33 | |||||||||||||
| 34 | has rolls => ( | ||||||||||||
| 35 | is => 'rw', | ||||||||||||
| 36 | isa => Int, | ||||||||||||
| 37 | default => 0 | ||||||||||||
| 38 | 6 | ); | |||||||||||
| 39 | |||||||||||||
| 40 | has pending => ( | ||||||||||||
| 41 | is => 'rw', | ||||||||||||
| 42 | isa => ArrayRef, | ||||||||||||
| 43 | default => [] | ||||||||||||
| 44 | 6 | ); | |||||||||||
| 45 | |||||||||||||
| 46 | has history => ( | ||||||||||||
| 47 | is => 'rw', | ||||||||||||
| 48 | isa => ArrayRef, | ||||||||||||
| 49 | default => [] | ||||||||||||
| 50 | 6 | ); | |||||||||||
| 51 | |||||||||||||
| 52 | has result => ( | ||||||||||||
| 53 | is => 'rw', | ||||||||||||
| 54 | isa => Any | ||||||||||||
| 55 | 6 | ); | |||||||||||
| 56 | |||||||||||||
| 57 | 92 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 58 | 92 | my ($self) = @_; | |||||||||||
| 59 | 92 | ▶ | return if $self->turn; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 92 | my ($first, $dice, $used) = Game::Backgammon::Dice::opening_for($self->seed); | |||||||||||
| 61 | 92 | $self->turn($first); | |||||||||||
| 62 | 92 | $self->rolls($used); | |||||||||||
| 63 | 92 | $self->pending([ sort { $b <=> $a } @$dice ]); | |||||||||||
| 64 | 92 | return; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 5123 | ▶ | sub status { return $_[0]->result ? 'finished' : 'active' } | ||||||||||
|
Subroutine
status: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||
| 68 | |||||||||||||
| 69 | 4850 | ▶ | sub other { return $_[0] eq 'white' ? 'black' : 'white' } | ||||||||||
|
Subroutine
other: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||
| 70 | |||||||||||||
| 71 | 10483 | ▶ | sub dice { | ||||||||||
|
Subroutine
dice: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 72 | 10483 | my ($self) = @_; | |||||||||||
| 73 | 10483 | ▶ | return [] if $self->result; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 10483 | ▶ | return [ @{ $self->pending } ] if @{ $self->pending }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 4847 | my @dice = Game::Backgammon::Dice::dice_for($self->seed, $self->rolls); | |||||||||||
| 76 | 4847 | $self->pending(\@dice); | |||||||||||
| 77 | 4847 | return [ @dice ]; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 10274 | ▶ | sub legal_turns { | ||||||||||
|
Subroutine
legal_turns: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 81 | 10274 | my ($self) = @_; | |||||||||||
| 82 | 10274 | ▶ | return [] if $self->result; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 83 | 10274 | return Game::Backgammon::Rules::legal_turns($self->board, $self->turn, @{ $self->dice }); | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 4916 | ▶ | sub play { | ||||||||||
|
Subroutine
play: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 87 | 4916 | my ($self, $which) = @_; | |||||||||||
| 88 | 4916 | ▶ | return _refuse('game_over') if $self->result; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 89 | |||||||||||||
| 90 | 4916 | my $legal = $self->legal_turns; | |||||||||||
| 91 | 4916 | my $want; | |||||||||||
| 92 | 4916 | ▶ | if (ref $which) { $want = $which } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | else { | ||||||||||||
| 94 | 59 | ▶ | $want = Game::Backgammon::Notation::parse_turn($which, player => $self->turn) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | or return _refuse('not_notation', $@); | ||||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 4915 | my ($turn) = grep { $_->key eq $want->key } @$legal; | |||||||||||
| 99 | 4915 | ▶ | return _refuse('not_legal') unless $turn; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | |||||||||||||
| 101 | 4913 | my $board = $self->board; | |||||||||||
| 102 | 4913 | $board = Game::Backgammon::Rules::apply_move($board, $_) for @{ $turn->moves }; | |||||||||||
| 103 | 4913 | $self->board($board); | |||||||||||
| 104 | 4913 | push @{ $self->history }, $turn; | |||||||||||
| 105 | |||||||||||||
| 106 | 4913 | $self->rolls($self->rolls + 1); | |||||||||||
| 107 | 4913 | $self->pending([]); | |||||||||||
| 108 | |||||||||||||
| 109 | 4913 | ▶ | return $turn if $self->_check_finished; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 4850 | $self->turn(other($self->turn)); | |||||||||||
| 111 | 4850 | return $turn; | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | 4913 | ▶ | sub _check_finished { | ||||||||||
|
Subroutine
_check_finished: called CC=3 SCAR=11.0
| |||||||||||||
| 115 | 4913 | my ($self) = @_; | |||||||||||
| 116 | 4913 | for my $player (qw(white black)) { | |||||||||||
| 117 | 9771 | ▶ | next unless $self->board->off($player) == Game::Backgammon::Board::CHECKERS; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 63 | $self->result(Game::Backgammon::Result->new( | |||||||||||
| 119 | winner => $player, | ||||||||||||
| 120 | margin => Game::Backgammon::Result->margin_for($self->board, $player), | ||||||||||||
| 121 | reason => 'borne_off')); | ||||||||||||
| 122 | 63 | return 1; | |||||||||||
| 123 | } | ||||||||||||
| 124 | 4850 | return 0; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 5 | ▶ | sub finish { | ||||||||||
|
Subroutine
finish: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 128 | 5 | my ($self, %o) = @_; | |||||||||||
| 129 | 5 | ▶ | return _refuse('game_over') if $self->result; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 4 | ▶ | my $reason = $o{reason} // 'resign'; | ||||||||||
|
Condition100%2/2
$o{"reason"} // "resign"
MC/DC100%1/1
$o{"reason"} // "resign"
$o{"reason"}
Truth table100%2/2
A
A= $o{"reason"}
| |||||||||||||
| 131 | $self->result(Game::Backgammon::Result->new( | ||||||||||||
| 132 | 4 | ▶ | winner => $o{winner}, margin => $o{margin} // 'single', reason => $reason)); | ||||||||||
|
Condition50%1/2
$o{"margin"} // "single"
MC/DC0%0/1
$o{"margin"} // "single"
$o{"margin"}
Truth table50%1/2
A
A= $o{"margin"}
| |||||||||||||
| 133 | 4 | return $self->result; | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 2 | ▶ | sub replay { | ||||||||||
|
Subroutine
replay: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||
| 137 | 2 | my ($class, %o) = @_; | |||||||||||
| 138 | 2 | my $self = $class->new(seed => $o{seed}); | |||||||||||
| 139 | 2 | ▶ | for my $line (@{ $o{turns} || [] }) { | ||||||||||
|
Condition50%1/2
$o{'turns'} || []
MC/DC0%0/1
$o{'turns'} || []
$o{'turns'}
Truth table50%1/2
A
A= $o{'turns'}
| |||||||||||||
| 140 | 57 | my $played = $self->play($line); | |||||||||||
| 141 | 57 | ▶ | unless ($played) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 1 | my $err = $@; | |||||||||||
| 143 | die 'Game::Backgammon: the log does not replay at turn ' | ||||||||||||
| 144 | 1 | ▶ | . (scalar @{ $self->history } + 1) . ': ' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 145 | . (ref $err ? $err->message : $err) . "\n"; | ||||||||||||
| 146 | } | ||||||||||||
| 147 | } | ||||||||||||
| 148 | 1 | return $self; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 5 | ▶ | sub to_log { return [ map { $_->notation } @{ $_[0]->history } ] } | ||||||||||
|
Subroutine
to_log: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 152 | |||||||||||||
| 153 | 4 | ▶ | sub _refuse { | ||||||||||
|
Subroutine
_refuse: called CC=1 SCAR=0.0
| |||||||||||||
| 154 | 4 | my ($code, $extra) = @_; | |||||||||||
| 155 | 4 | require Game::Backgammon::Error; | |||||||||||
| 156 | 4 | $@ = Game::Backgammon::Error->new(code => $code, detail => $extra); | |||||||||||
| 157 | 4 | return undef; | |||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | 6 | 1; | |||||||||||
| 161 | |||||||||||||
| 162 | __END__ | ||||||||||||