blib/lib/Game/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
« blib/lib/Game/Backgammon/Terminal.pm ↑ Summary blib/lib/Game/Backgammon/Rules.pm »
1 package Game::Backgammon;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use Game::Backgammon::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use Game::Backgammon::Dice ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Game::Backgammon::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Game::Backgammon::Notation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Game::Backgammon::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use Game::Backgammon::Rules ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 27 ▶use Game::Backgammon::Turn;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 27 our $VERSION = '0.05';
17  
18 has seed => (
19 is => 'ro',
20 isa => Str
21 27 );
22  
23 has board => (
24 is => 'rw',
25 isa => Object,
26 200 ▶ default => sub { Game::Backgammon::Board->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
27 27 );
28  
29 has turn => (
30 is => 'rw',
31 isa => Str
32 27 );
33  
34 has rolls => (
35 is => 'rw',
36 isa => Int,
37 default => 0
38 27 );
39  
40 has pending => (
41 is => 'rw',
42 isa => ArrayRef,
43 default => []
44 27 );
45  
46 has history => (
47 is => 'rw',
48 isa => ArrayRef,
49 default => []
50 27 );
51  
52 has result => (
53 is => 'rw',
54 isa => Any
55 27 );
56  
57 200 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
58 200 my ($self) = @_;
59 200 ▶ return if $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
if $self->turn 0 200 200
60 200 my ($first, $dice, $used) = Game::Backgammon::Dice::opening_for($self->seed);
61 200 $self->turn($first);
62 200 $self->rolls($used);
63 200 $self->pending([ sort { $b <=> $a } @$dice ]);
64 200 return;
65 }
66  
67 25478 ▶sub status { return $_[0]->result ? 'finished' : 'active' }
Subroutine status: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->result ? : 80 25398 25478
68  
69 15943 ▶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' ? : 7931 8012 15943
70  
71 53203 ▶sub dice {
Subroutine dice: called CC=3 SCAR=11.0
Pod documented
72 53203 my ($self) = @_;
73 53203 ▶ return [] if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 8 53195 53203
74 53195 ▶ return [ @{ $self->pending } ] if @{ $self->pending };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->pending;} 37289 15906 53195
75 15906 my @dice = Game::Backgammon::Dice::dice_for($self->seed, $self->rolls);
76 15906 $self->pending(\@dice);
77 15906 return [ @dice ];
78 }
79  
80 41853 ▶sub legal_turns {
Subroutine legal_turns: called CC=2 SCAR=6.9
Pod documented
81 41853 my ($self) = @_;
82 41853 ▶ return [] if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 6 41847 41853
83 41847 return Game::Backgammon::Rules::legal_turns($self->board, $self->turn, @{ $self->dice });
84 }
85  
86 16020 ▶sub play {
Subroutine play: called CC=7 SCAR=19.5
Pod documented
87 16020 my ($self, $which) = @_;
88 16020 ▶ return _refuse('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 16020 16020
89  
90 16020 my $legal = $self->legal_turns;
91 16020 my $want;
92 16020 ▶ if (ref $which) { $want = $which }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $which) { } 15206 814 16020
93 else {
94 814 ▶ $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", $@) 812 2 814
95 or return _refuse('not_notation', $@);
96 }
97  
98 16018 my ($turn) = grep { $_->key eq $want->key } @$legal;
99 16018 ▶ return _refuse('not_legal') unless $turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $turn 3 16015 16018
100  
101 16015 my $board = $self->board;
102 16015 $board = Game::Backgammon::Rules::apply_move($board, $_) for @{ $turn->moves };
103 16015 $self->board($board);
104 16015 push @{ $self->history }, $turn;
105  
106 16015 $self->rolls($self->rolls + 1);
107 16015 $self->pending([]);
108  
109 16015 ▶ return $turn if $self->_check_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_check_finished 72 15943 16015
110 15943 $self->turn(other($self->turn));
111 15943 return $turn;
112 }
113  
114 16015 ▶sub _check_finished {
Subroutine _check_finished: called CC=3 SCAR=11.0
115 16015 my ($self) = @_;
116 16015 for my $player (qw(white black)) {
117 31967 ▶ next unless $self->board->off($player) == Game::Backgammon::Board::CHECKERS;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->board->off($player) == 15 31895 72 31967
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 15943 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 319 ▶sub to_text {
Subroutine to_text: called CC=3 SCAR=11.0
Pod documented
154 319 my ($self) = @_;
155 319 my @lines = ('[seed ' . unpack('H*', $self->seed) . ']');
156 319 my $n = 0;
157 25581 push @lines, ++$n . '. ' . $_->player . ' ' . join('', @{ $_->dice }) . ': ' . $_->notation
158 319 for @{ $self->history };
159 319 my $result = $self->result;
160 319 ▶ push @lines, '[result ' . join(' ', $result->winner, $result->margin, $result->reason) . ']'
Branch: 1 branch
BranchTrueFalseTotal
if $result 14 305 319
161 if $result;
162 319 return join("\n", @lines) . "\n";
163 }
164  
165 83 ▶sub from_text {
Subroutine from_text: called CC=22 SCAR=30.9
Pod documented
166 83 my ($class, $text) = @_;
167 83 ▶ my $refuse = sub { die 'Game::Backgammon: the text was refused: ' . $_[0] . "\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
168 83 ▶ $refuse->('from_text wants the text to_text wrote, and was given '
Branch: 2 branches
BranchTrueFalseTotal
if not defined $text or ref $text 2 81 83
defined $text ? : 1 1 2
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
1181
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 81 ▶ $refuse->('it does not end with a newline') unless $text =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n\z/ 2 79 81
172  
173 79 my @lines = split /\n/, $text, -1;
174 79 pop @lines;
175 79 ▶ 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") 78 1 79
Condition50%1/2
shift @lines // ""
l!l
790
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 78 my $self = $class->new(seed => pack 'H*', $seed);
178  
179 78 my $ending;
180 78 for my $i (0 .. $#lines) {
181 766 my ($line, $at) = ($lines[$i], $i + 2);
182 766 ▶ $refuse->("line $at follows the result") if $ending;
Branch: 1 branch
BranchTrueFalseTotal
if $ending 1 765 766
183 765 ▶ 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 759 765
184 6 $ending = { winner => $1, margin => $2, reason => $3 };
185 6 next;
186 }
187 759 ▶ 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") 758 1 759
188 or $refuse->("line $at is not a turn");
189 1 $refuse->("line $at is numbered $n where " . (@{ $self->history } + 1) . ' was next')
190 758 ▶ unless $n == @{ $self->history } + 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @{$self->history;} + 1 1 757 758
191 757 ▶ $refuse->("line $at has a turn after the game ended") if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 757 757
192 757 ▶ $refuse->("line $at is $player to play where it is " . $self->turn . "'s turn")
Branch: 1 branch
BranchTrueFalseTotal
unless $player eq $self->turn 1 756 757
193 unless $player eq $self->turn;
194 756 my $rolled = join '', @{ $self->dice };
195 756 ▶ $refuse->("line $at rolls $dice where the seed rolls $rolled") unless $dice eq $rolled;
Branch: 1 branch
BranchTrueFalseTotal
unless $dice eq $rolled 1 755 756
196 755 ▶ next if $self->play($notation);
Branch: 1 branch
BranchTrueFalseTotal
if $self->play($notation) 753 2 755
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 71 my $result = $self->result;
204 71 ▶ if ($result) {
Branch: 2 branches
BranchTrueFalseTotal
if ($result) { } 3 68 71
elsif ($ending) { } 3 65 68
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 68 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 27 1;
226  
227 __END__
« blib/lib/Game/Backgammon/Terminal.pm ↑ Summary blib/lib/Game/Backgammon/Rules.pm »