Backgammon.pm

filter: Statementstmt 100% 158 / 158 Branchbran 92.6% 63 / 68 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 25 / 25 Podpod 90.9% 10 / 11 totaltotal 96.4% 269 / 279 CC 45 SCAR 22.1
CC
45
Cov
97%
from_text
22.0
play
7.0
replay
5.0
CRAP 9.2
« Backgammon/Terminal.pm ↑ Summary Backgammon/Rules.pm »
1 package Game::Backgammon;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use Game::Backgammon::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Game::Backgammon::Dice ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Game::Backgammon::Notation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Game::Backgammon::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Game::Backgammon::Rules ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 our $VERSION = '0.04';
17  
18 has seed => (
19 is => 'ro',
20 isa => Str
21 9 );
22  
23 has board => (
24 is => 'rw',
25 isa => Object,
26 182 ▶ default => sub { Game::Backgammon::Board->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
27 9 );
28  
29 has turn => (
30 is => 'rw',
31 isa => Str
32 9 );
33  
34 has rolls => (
35 is => 'rw',
36 isa => Int,
37 default => 0
38 9 );
39  
40 has pending => (
41 is => 'rw',
42 isa => ArrayRef,
43 default => []
44 9 );
45  
46 has history => (
47 is => 'rw',
48 isa => ArrayRef,
49 default => []
50 9 );
51  
52 has result => (
53 is => 'rw',
54 isa => Any
55 9 );
56  
57 182 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
58 182 my ($self) = @_;
59 182 ▶ return if $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
if $self->turn 0 182 182
60 182 my ($first, $dice, $used) = Game::Backgammon::Dice::opening_for($self->seed);
61 182 $self->turn($first);
62 182 $self->rolls($used);
63 182 $self->pending([ sort { $b <=> $a } @$dice ]);
64 182 return;
65 }
66  
67 25419 ▶sub status { return $_[0]->result ? 'finished' : 'active' }
Subroutine status: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->result ? : 80 25339 25419
68  
69 15891 ▶sub other { return $_[0] eq 'white' ? 'black' : 'white' }
Subroutine other: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'white' ? : 7906 7985 15891
70  
71 53031 ▶sub dice {
Subroutine dice: called CC=3 SCAR=11.0
Pod documented
72 53031 my ($self) = @_;
73 53031 ▶ return [] if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 8 53023 53031
74 53023 ▶ return [ @{ $self->pending } ] if @{ $self->pending };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->pending;} 37169 15854 53023
75 15854 my @dice = Game::Backgammon::Dice::dice_for($self->seed, $self->rolls);
76 15854 $self->pending(\@dice);
77 15854 return [ @dice ];
78 }
79  
80 41752 ▶sub legal_turns {
Subroutine legal_turns: called CC=2 SCAR=6.9
Pod documented
81 41752 my ($self) = @_;
82 41752 ▶ return [] if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 6 41746 41752
83 41746 return Game::Backgammon::Rules::legal_turns($self->board, $self->turn, @{ $self->dice });
84 }
85  
86 15968 ▶sub play {
Subroutine play: called CC=7 SCAR=19.5
Pod documented
87 15968 my ($self, $which) = @_;
88 15968 ▶ return _refuse('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 15968 15968
89  
90 15968 my $legal = $self->legal_turns;
91 15968 my $want;
92 15968 ▶ if (ref $which) { $want = $which }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $which) { } 15184 784 15968
93 else {
94 784 ▶ $want = Game::Backgammon::Notation::parse_turn($which, player => $self->turn)
Branch: 1 branch
BranchTrueFalseTotal
$want = Game::Backgammon::Notation::parse_turn($which, "player", $self->turn) or return _refuse("not_notation", $@) 782 2 784
95 or return _refuse('not_notation', $@);
96 }
97  
98 15966 my ($turn) = grep { $_->key eq $want->key } @$legal;
99 15966 ▶ return _refuse('not_legal') unless $turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $turn 3 15963 15966
100  
101 15963 my $board = $self->board;
102 15963 $board = Game::Backgammon::Rules::apply_move($board, $_) for @{ $turn->moves };
103 15963 $self->board($board);
104 15963 push @{ $self->history }, $turn;
105  
106 15963 $self->rolls($self->rolls + 1);
107 15963 $self->pending([]);
108  
109 15963 ▶ return $turn if $self->_check_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_finished 72 15891 15963
110 15891 $self->turn(other($self->turn));
111 15891 return $turn;
112 }
113  
114 15963 ▶sub _check_finished {
Subroutine _check_finished: called CC=3 SCAR=11.0
115 15963 my ($self) = @_;
116 15963 for my $player (qw(white black)) {
117 31863 ▶ next unless $self->board->off($player) == Game::Backgammon::Board::CHECKERS;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->board->off($player) == 15 31791 72 31863
118 72 $self->result(Game::Backgammon::Result->new(
119 winner => $player,
120 margin => Game::Backgammon::Result->margin_for($self->board, $player),
121 reason => 'borne_off'));
122 72 return 1;
123 }
124 15891 return 0;
125 }
126  
127 9 ▶sub finish {
Subroutine finish: called CC=4 SCAR=13.9
Pod documented
128 9 my ($self, %o) = @_;
129 9 ▶ return _refuse('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 1 8 9
130 8 ▶ my $reason = $o{reason} // 'resign';
Condition100%2/2
$o{"reason"} // "resign"
l!l
62
MC/DC100%1/1
$o{"reason"} // "resign"
$o{"reason"}
Truth table100%2/2
A
A= $o{"reason"}
Aresult
11
00
131 $self->result(Game::Backgammon::Result->new(
132 8 ▶ winner => $o{winner}, margin => $o{margin} // 'single', reason => $reason));
Condition100%2/2
$o{"margin"} // "single"
l!l
35
MC/DC100%1/1
$o{"margin"} // "single"
$o{"margin"}
Truth table100%2/2
A
A= $o{"margin"}
Aresult
11
00
133 8 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'} || []
l!l
20
MC/DC0%0/1
$o{'turns'} || []
$o{'turns'}
Truth table50%1/2
A
A= $o{'turns'}
Aresult
11
00
140 57 my $played = $self->play($line);
141 57 ▶ unless ($played) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($played) 1 56 57
142 1 my $err = $@;
143 die 'Game::Backgammon: the log does not replay at turn '
144 1 ▶ . (scalar @{ $self->history } + 1) . ': '
Branch: 1 branch
BranchTrueFalseTotal
ref $err ? : 1 0 1
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 269 ▶sub to_text {
Subroutine to_text: called CC=3 SCAR=11.0
Pod documented
154 269 my ($self) = @_;
155 269 my @lines = ('[seed ' . unpack('H*', $self->seed) . ']');
156 269 my $n = 0;
157 25471 push @lines, ++$n . '. ' . $_->player . ' ' . join('', @{ $_->dice }) . ': ' . $_->notation
158 269 for @{ $self->history };
159 269 my $result = $self->result;
160 269 ▶ push @lines, '[result ' . join(' ', $result->winner, $result->margin, $result->reason) . ']'
Branch: 1 branch
BranchTrueFalseTotal
if $result 14 255 269
161 if $result;
162 269 return join("\n", @lines) . "\n";
163 }
164  
165 73 ▶sub from_text {
Subroutine from_text: called CC=22 SCAR=30.9
Pod documented
166 73 my ($class, $text) = @_;
167 73 ▶ my $refuse = sub { die 'Game::Backgammon: the text was refused: ' . $_[0] . "\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
168 73 ▶ $refuse->('from_text wants the text to_text wrote, and was given '
Branch: 2 branches
BranchTrueFalseTotal
if not defined $text or ref $text 2 71 73
defined $text ? : 1 1 2
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
1171
MC/DC100%2/2
not defined $text or ref $text
not defined $text ref $text
Truth table100%3/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
169 . (defined $text ? 'a reference' : 'nothing'))
170 if !defined $text || ref $text;
171 71 ▶ $refuse->('it does not end with a newline') unless $text =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n\z/ 2 69 71
172  
173 69 my @lines = split /\n/, $text, -1;
174 69 pop @lines;
175 69 ▶ my ($seed) = (shift(@lines) // '') =~ /\A\[seed ((?:[0-9a-f]{2})+)\]\z/
Branch: 1 branch
BranchTrueFalseTotal
my($seed) = (shift @lines // "") =~ /\A\[seed ((?:[0-9a-f]{2})+)\]\z/ or &$refuse("line 1 is not the seed") 68 1 69
Condition50%1/2
shift @lines // ""
l!l
690
MC/DC0%0/1
shift @lines // ""
shift @lines
Truth table50%1/2
A
A= shift @lines
Aresult
11
00
176 or $refuse->('line 1 is not the seed');
177 68 my $self = $class->new(seed => pack 'H*', $seed);
178  
179 68 my $ending;
180 68 for my $i (0 .. $#lines) {
181 736 my ($line, $at) = ($lines[$i], $i + 2);
182 736 ▶ $refuse->("line $at follows the result") if $ending;
Branch: 1 branch
BranchTrueFalseTotal
if $ending 1 735 736
183 735 ▶ if ($line =~ /\A\[result (white|black) (single|gammon|backgammon) (\S+)\]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A\[result (white|black) (single|gammon|backgammon) (\S+)\]\z/) 6 729 735
184 6 $ending = { winner => $1, margin => $2, reason => $3 };
185 6 next;
186 }
187 729 ▶ my ($n, $player, $dice, $notation) = $line =~ /\A([1-9][0-9]*)\. (white|black) ([1-6]{2}|[1-6]{4}): (\S.*)\z/
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /\A([1-9][0-9]*)\. (white|black) ([1-6]{2}|[1-6]{4}): (\S.*)\z/ or &$refuse("line $at is not a turn") 728 1 729
188 or $refuse->("line $at is not a turn");
189 1 $refuse->("line $at is numbered $n where " . (@{ $self->history } + 1) . ' was next')
190 728 ▶ unless $n == @{ $self->history } + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @{$self->history;} + 1 1 727 728
191 727 ▶ $refuse->("line $at has a turn after the game ended") if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 727 727
192 727 ▶ $refuse->("line $at is $player to play where it is " . $self->turn . "'s turn")
Branch: 1 branch
BranchTrueFalseTotal
unless $player eq $self->turn 1 726 727
193 unless $player eq $self->turn;
194 726 my $rolled = join '', @{ $self->dice };
195 726 ▶ $refuse->("line $at rolls $dice where the seed rolls $rolled") unless $dice eq $rolled;
Branch: 1 branch
BranchTrueFalseTotal
unless $dice eq $rolled 1 725 726
196 725 ▶ next if $self->play($notation);
Branch: 1 branch
BranchTrueFalseTotal
if $self->play($notation) 723 2 725
197 2 my $err = $@;
198 2 ▶ my $why = ref $err ? $err->message : $err;
Branch: 1 branch
BranchTrueFalseTotal
ref $err ? : 2 0 2
199 2 $why =~ s/\s+\z//;
200 2 $refuse->("line $at does not play: $why");
201 }
202  
203 61 my $result = $self->result;
204 61 ▶ if ($result) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result) { } 3 58 61
elsif ($ending) { } 3 55 58
205 3 ▶ $refuse->('the game ended but the text has no result') unless $ending;
Branch: 1 branch
BranchTrueFalseTotal
unless $ending 1 2 3
206 $refuse->('the result is not the one the game ended with')
207 2 ▶ unless join(' ', @{$ending}{qw/winner margin reason/})
Branch: 1 branch
BranchTrueFalseTotal
unless join(" ", @{$ending;}{"winner", "margin", "reason"}) eq join(" ", $result->winner, $result->margin, $result->reason) 1 1 2
208 eq join(' ', $result->winner, $result->margin, $result->reason);
209 }
210 elsif ($ending) {
211 $refuse->('the result says the checkers were borne off, and they were not')
212 3 ▶ if $ending->{reason} eq 'borne_off';
Branch: 1 branch
BranchTrueFalseTotal
if $ending->{"reason"} eq "borne_off" 1 2 3
213 2 $self->finish(%$ending);
214 }
215 58 return $self;
216 }
217  
218 6 ▶sub _refuse {
Subroutine _refuse: called CC=1 SCAR=0.0
219 6 my ($code, $extra) = @_;
220 6 require Game::Backgammon::Error;
221 6 $@ = Game::Backgammon::Error->new(code => $code, detail => $extra);
222 6 return undef;
223 }
224  
225 9 1;
226  
227 __END__
« Backgammon/Terminal.pm ↑ Summary Backgammon/Rules.pm »