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 87 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 87 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 87 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 87 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 87 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 87 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 87 ▶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 87 our $VERSION = '0.03';
14  
15 87 my $E = 'Game::Senet::Engine';
16  
17 87 my (@SIDE_NAME, %SIDE_OF);
18 87 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 87 @SIDE_NAME = ('light', 'dark');
20 87 %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK);
21 }
22  
23 87 has _given => (is => 'ro', init_arg => 'rules', private => 1);
24  
25 87 has _position => (is => 'ro', init_arg => 'position', private => 1);
26  
27 87 has _first => (is => 'ro', init_arg => 'first', private => 1, default => 'light');
28  
29 87 has _engine => (is => 'rw', private => 1);
30  
31 87 has _spelled => (is => 'rw', private => 1);
32  
33 87 has _stack => (is => 'rw', private => 1);
34  
35 87 has _bound => (is => 'rw', private => 1);
36  
37 7247 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.7
Pod undocumented
38 7247 my ($self) = @_;
39 7247 my $spelled = $E->rules($self->_given);
40 7247 $self->_spelled($spelled);
41 7247 $self->_stack([]);
42 7247 $self->_bound(0);
43  
44 7247 my $position = $self->_position;
45 7247 my $engine;
46 7247 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) { } 157 7090 7247
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 7090 my $first = $self->_first;
54 Carp::croak("Game::Senet::Rules: first is 'light' or 'dark'")
55 7090 ▶ unless defined $first && exists $SIDE_OF{$first};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and exists $SIDE_OF{$first} 0 7090 7090
Condition33%1/3
defined $first and exists $SIDE_OF{$first}
!ll&&!rl&&r
007090
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 7090 $engine = $E->new(pieces => $spelled->{pieces});
57 7090 ▶ $engine->put($_, $_ % 2 ? DARK : LIGHT) for 1 .. 2 * $spelled->{pieces};
Branch: 1 branch
BranchTrueFalseTotal
$_ % 2 ? : 35484 35484 70968
58 7090 $engine->set_side($SIDE_OF{$first});
59 }
60 7247 $self->_engine($engine);
61 7247 return;
62 }
63  
64 0 ▶sub rules { return { %{ $_[0]->_spelled } } }
Subroutine rules: not called CC=1 SCAR=6.9
Pod documented
65  
66 3365840 ▶sub side { $SIDE_NAME[ $_[0]->_engine->side ] }
Subroutine side: called CC=1 SCAR=0.0
Pod documented
67  
68 232735 ▶sub only { $_[0]->_bound }
Subroutine only: called CC=1 SCAR=0.0
Pod documented
69  
70 861466 ▶sub ply { $_[0]->_engine->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
71  
72 90301 ▶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 587805 ▶sub board { $_[0]->_engine->clone }
Subroutine board: called CC=1 SCAR=0.0
Pod documented
77  
78 15695 ▶sub home {
Subroutine home: called CC=3 SCAR=11.7
Pod documented
79 15695 my ($self, $side) = @_;
80 15695 ▶ 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 15695 15695
Condition50%1/2
$side // ""
l!l
156950
MC/DC0%0/1
$side // ""
$side
Truth table50%1/2
A
A= $side
Aresult
11
00
81 15695 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 7319102 ▶sub status {
Subroutine status: called CC=3 SCAR=11.0
Pod documented
89 7319102 my ($self) = @_;
90 7319102 my $status = $self->_engine->status;
91 7319102 ▶ return $status == WON ? 'won' : $status == DRAWN ? 'drawn' : 'ongoing';
Branch: 2 branches
BranchTrueFalseTotal
$status == 1 ? : 6791 7312311 7319102
$status == 2 ? : 3 7312308 7312311
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 7319099 ▶sub is_over { $_[0]->status ne 'ongoing' }
Subroutine is_over: called CC=1 SCAR=0.0
Pod documented
107  
108 854871 ▶sub moves {
Subroutine moves: called CC=3 SCAR=11.1
Pod documented
109 854871 my ($self, $roll) = @_;
110 854871 ▶ return wantarray ? () : 0 if $self->is_over;
Branch: 2 branches
BranchTrueFalseTotal
if $self->is_over 1 854870 854871
wantarray ? : 0 1 1
111 854870 return $self->_engine->moves($roll, $self->_given, $self->_bound);
112 }
113  
114 748861 ▶sub apply {
Subroutine apply: called CC=8 SCAR=21.3
Pod documented
115 748861 my ($self, $move) = @_;
116 748861 ▶ Carp::croak('Game::Senet::Rules: apply takes a move')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($move) and $move->isa("Game::Senet::Move") 0 748861 748861
Condition33%1/3
builtin::blessed($move) and $move->isa("Game::Senet::Move")
!ll&&!rl&&r
00748861
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 748861 ▶ return undef if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 748860 748861
119 748860 ▶ return undef if $move->side ne $self->side;
Branch: 1 branch
BranchTrueFalseTotal
if $move->side ne $self->side 0 748860 748860
120 748860 ▶ return undef if $self->_bound && $move->from != $self->_bound;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_bound and $move->from != $self->_bound 1 748859 748860
Condition100%3/3
$self->_bound and $move->from != $self->_bound
!ll&&!rl&&r
723773250861
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 748859 my ($undo, $bound) = $self->_engine->apply($move, $self->_given);
122 748859 ▶ return undef unless defined $undo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $undo 0 748859 748859
123 748859 push @{ $self->_stack }, [ $undo, $self->_bound ];
124 748859 $self->_bound($bound);
125 748859 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 749379 ▶sub depth { scalar @{ $_[0]->_stack } }
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
147  
148 87 1;
149  
150 __END__
« blib/lib/Game/Senet/Sticks.pm ↑ Summary blib/lib/Game/Senet/Engine.pm »