blib/lib/Game/Senet/Rules.pm

filter: Statementstmt 98.0% 101 / 103 Branchbran 76.3% 29 / 38 Conditioncond 54.5% 6 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 96.4% 27 / 28 Podpod 95.0% 19 / 20 totaltotal 88.8% 184 / 207 CC 25 SCAR 12.7
CC
25
Cov
89%
apply
8.4
BUILD
7.1
home
3.2
CRAP 3.6
« blib/lib/Game/Senet/Sticks.pm ↑ Summary blib/lib/Game/Senet/Engine.pm »
1 package Game::Senet::Rules;
2  
3 66 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 66 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 66 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 66 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 66 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 66 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 66 ▶use Game::Senet::Engine qw(SIDE_LIGHT SIDE_DARK LIGHT DARK WON DRAWN BY_HOME BY_PLY_CAP);
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 66 our $VERSION = '0.02';
14  
15 66 my $E = 'Game::Senet::Engine';
16  
17 66 my (@SIDE_NAME, %SIDE_OF);
18 66 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 66 @SIDE_NAME = ('light', 'dark');
20 66 %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK);
21 }
22  
23 66 has _given => (is => 'ro', init_arg => 'rules', private => 1);
24  
25 66 has _position => (is => 'ro', init_arg => 'position', private => 1);
26  
27 66 has _first => (is => 'ro', init_arg => 'first', private => 1, default => 'light');
28  
29 66 has _engine => (is => 'rw', private => 1);
30  
31 66 has _spelled => (is => 'rw', private => 1);
32  
33 66 has _stack => (is => 'rw', private => 1);
34  
35 66 has _bound => (is => 'rw', private => 1);
36  
37 7198 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.7
Pod undocumented
38 7198 my ($self) = @_;
39 7198 my $spelled = $E->rules($self->_given);
40 7198 $self->_spelled($spelled);
41 7198 $self->_stack([]);
42 7198 $self->_bound(0);
43  
44 7198 my $position = $self->_position;
45 7198 my $engine;
46 7198 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) { } 157 7041 7198
47 157 $engine = $E->new(position => $position);
48 Carp::croak('Game::Senet::Rules: the position has ' . $engine->pieces
49 . ' pieces a side and the rule set ' . $spelled->{pieces})
50 157 ▶ unless $engine->pieces == $spelled->{pieces};
Branch: 1 branch
BranchTrueFalseTotal
unless $engine->pieces == $spelled->{"pieces"} 0 157 157
51 }
52 else {
53 7041 my $first = $self->_first;
54 Carp::croak("Game::Senet::Rules: first is 'light' or 'dark'")
55 7041 ▶ unless defined $first && exists $SIDE_OF{$first};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and exists $SIDE_OF{$first} 0 7041 7041
Condition33%1/3
defined $first and exists $SIDE_OF{$first}
!ll&&!rl&&r
007041
MC/DC0%0/2
defined $first and exists $SIDE_OF{$first}
defined $first exists $SIDE_OF{$first}
Truth table33%1/3
A and B
A= defined $firstB= exists $SIDE_OF{$first}
ABresult
0X0
100
111
56 7041 $engine = $E->new(pieces => $spelled->{pieces});
57 7041 ▶ $engine->put($_, $_ % 2 ? DARK : LIGHT) for 1 .. 2 * $spelled->{pieces};
Branch: 1 branch
BranchTrueFalseTotal
$_ % 2 ? : 35239 35239 70478
58 7041 $engine->set_side($SIDE_OF{$first});
59 }
60 7198 $self->_engine($engine);
61 7198 return;
62 }
63  
64 0 ▶sub rules { return { %{ $_[0]->_spelled } } }
Subroutine rules: not called CC=1 SCAR=6.9
Pod documented
65  
66 3365170 ▶sub side { $SIDE_NAME[ $_[0]->_engine->side ] }
Subroutine side: called CC=1 SCAR=0.0
Pod documented
67  
68 232605 ▶sub only { $_[0]->_bound }
Subroutine only: called CC=1 SCAR=0.0
Pod documented
69  
70 861380 ▶sub ply { $_[0]->_engine->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
71  
72 90299 ▶sub position { $_[0]->_engine->to_string }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
73  
74 90159 ▶sub key { $_[0]->_engine->key_hex }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
75  
76 582075 ▶sub board { $_[0]->_engine->clone }
Subroutine board: called CC=1 SCAR=0.0
Pod documented
77  
78 15565 ▶sub home {
Subroutine home: called CC=3 SCAR=11.7
Pod documented
79 15565 my ($self, $side) = @_;
80 15565 ▶ Carp::croak("Game::Senet::Rules: a side is 'light' or 'dark'") unless exists $SIDE_OF{ $side // '' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIDE_OF{$side // ""} 0 15565 15565
Condition50%1/2
$side // ""
l!l
155650
MC/DC0%0/1
$side // ""
$side
Truth table50%1/2
A
A= $side
Aresult
11
00
81 15565 return $self->_engine->home($SIDE_OF{$side});
82 }
83  
84 2265 ▶sub protected { $_[0]->_engine->protected($_[1], $_[0]->_given) }
Subroutine protected: called CC=1 SCAR=0.0
Pod documented
85  
86 4202 ▶sub in_blockade { $_[0]->_engine->in_blockade($_[1], $_[0]->_given) }
Subroutine in_blockade: called CC=1 SCAR=0.0
Pod documented
87  
88 7317262 ▶sub status {
Subroutine status: called CC=3 SCAR=11.0
Pod documented
89 7317262 my ($self) = @_;
90 7317262 my $status = $self->_engine->status;
91 7317262 ▶ return $status == WON ? 'won' : $status == DRAWN ? 'drawn' : 'ongoing';
Branch: 2 branches
BranchTrueFalseTotal
$status == 1 ? : 6791 7310471 7317262
$status == 2 ? : 3 7310468 7310471
92 }
93  
94 1411 ▶sub how {
Subroutine how: called CC=3 SCAR=11.1
Pod documented
95 1411 my ($self) = @_;
96 1411 my $how = $self->_engine->how;
97 1411 ▶ return $how == BY_HOME ? 'home' : $how == BY_PLY_CAP ? 'ply_cap' : undef;
Branch: 2 branches
BranchTrueFalseTotal
$how == 1 ? : 1410 1 1411
$how == 2 ? : 1 0 1
98 }
99  
100 1412 ▶sub winner {
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
101 1412 my ($self) = @_;
102 1412 my $winner = $self->_engine->winner;
103 1412 ▶ return $winner < 0 ? undef : $SIDE_NAME[$winner];
Branch: 1 branch
BranchTrueFalseTotal
$winner < 0 ? : 1 1411 1412
104 }
105  
106 7317259 ▶sub is_over { $_[0]->status ne 'ongoing' }
Subroutine is_over: called CC=1 SCAR=0.0
Pod documented
107  
108 854737 ▶sub moves {
Subroutine moves: called CC=3 SCAR=11.1
Pod documented
109 854737 my ($self, $roll) = @_;
110 854737 ▶ return wantarray ? () : 0 if $self->is_over;
Branch: 2 branches
BranchTrueFalseTotal
if $self->is_over 1 854736 854737
wantarray ? : 0 1 1
111 854736 return $self->_engine->moves($roll, $self->_given, $self->_bound);
112 }
113  
114 748776 ▶sub apply {
Subroutine apply: called CC=8 SCAR=21.3
Pod documented
115 748776 my ($self, $move) = @_;
116 748776 ▶ Carp::croak('Game::Senet::Rules: apply takes a move')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($move) and $move->isa("Game::Senet::Move") 0 748776 748776
Condition33%1/3
builtin::blessed($move) and $move->isa("Game::Senet::Move")
!ll&&!rl&&r
00748776
MC/DC0%0/2
builtin::blessed($move) and $move->isa("Game::Senet::Move")
builtin::blessed($move) $move->isa("Game::Senet::Move")
Truth table33%1/3
A and B
A= builtin::blessed($move)B= $move->isa("Game::Senet::Move")
ABresult
0X0
100
111
117 unless Scalar::Util::blessed($move) && $move->isa('Game::Senet::Move');
118 748776 ▶ return undef if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 748775 748776
119 748775 ▶ return undef if $move->side ne $self->side;
Branch: 1 branch
BranchTrueFalseTotal
if $move->side ne $self->side 0 748775 748775
120 748775 ▶ return undef if $self->_bound && $move->from != $self->_bound;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_bound and $move->from != $self->_bound 1 748774 748775
Condition100%3/3
$self->_bound and $move->from != $self->_bound
!ll&&!rl&&r
723688250861
MC/DC100%2/2
$self->_bound and $move->from != $self->_bound
$self->_bound $move->from != $self->_bound
Truth table100%3/3
A and B
A= $self->_boundB= $move->from != $self->_bound
ABresult
0X0
100
111
121 748774 my ($undo, $bound) = $self->_engine->apply($move, $self->_given);
122 748774 ▶ return undef unless defined $undo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $undo 0 748774 748774
123 748774 push @{ $self->_stack }, [ $undo, $self->_bound ];
124 748774 $self->_bound($bound);
125 748774 return $move;
126 }
127  
128 21487 ▶sub forfeit {
Subroutine forfeit: called CC=2 SCAR=6.9
Pod documented
129 21487 my ($self, $roll) = @_;
130 21487 ▶ return 0 if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 21486 21487
131 21486 my $undo = $self->_engine->forfeit($roll);
132 21486 push @{ $self->_stack }, [ $undo, $self->_bound ];
133 21486 $self->_bound(0);
134 21486 return 1;
135 }
136  
137 627 ▶sub undo {
Subroutine undo: called CC=2 SCAR=7.0
Pod documented
138 627 my ($self) = @_;
139 627 my $top = pop @{ $self->_stack };
140 627 ▶ return 0 unless defined $top;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $top 0 627 627
141 627 $self->_engine->unapply($top->[0]);
142 627 $self->_bound($top->[1]);
143 627 return 1;
144 }
145  
146 749294 ▶sub depth { scalar @{ $_[0]->_stack } }
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
147  
148 66 1;
149  
150 __END__
« blib/lib/Game/Senet/Sticks.pm ↑ Summary blib/lib/Game/Senet/Engine.pm »