blib/lib/Game/RoyalUr/Rules.pm

filter: Statementstmt 100% 97 / 97 Branchbran 92.5% 37 / 40 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 26 / 26 Podpod 94.4% 17 / 18 totaltotal 94.7% 182 / 192 CC 24 SCAR 11.0
CC
24
Cov
96%
apply
6.0
BUILD
6.0
hand
3.2
CRAP 3.0
« blib/lib/Game/RoyalUr/Engine.pm ↑ Summary blib/lib/Game/RoyalUr/Error.pm »
1 package Game::RoyalUr::Rules;
2  
3 61 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 61 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 61 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 61 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 61 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 61 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 61 ▶use Game::RoyalUr::Engine qw(SIDE_LIGHT SIDE_DARK WON DRAWN BY_HOME BY_PLY_CAP);
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 61 our $VERSION = '0.02';
14  
15 61 my $E = 'Game::RoyalUr::Engine';
16  
17 61 my (@SIDE_NAME, %SIDE_OF);
18 61 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 61 @SIDE_NAME = ('light', 'dark');
20 61 %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK);
21 }
22  
23 61 has _given => (is => 'ro', init_arg => 'rules', private => 1);
24  
25 61 has _position => (is => 'ro', init_arg => 'position', private => 1);
26  
27 61 has _first => (is => 'ro', init_arg => 'first', private => 1);
28  
29 61 has _start_ply => (is => 'ro', init_arg => 'ply', private => 1);
30  
31 61 has _engine => (is => 'rw', private => 1);
32  
33 61 has _spelled => (is => 'rw', private => 1);
34  
35 61 has _stack => (is => 'rw', private => 1);
36  
37 1452 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
38 1452 my ($self) = @_;
39 1452 my $spelled = $E->rules($self->_given);
40 1451 $self->_spelled($spelled);
41 1451 $self->_stack([]);
42  
43 1451 my $position = $self->_position;
44 my $engine = defined $position
45 ? $E->new(position => $position)
46 1451 ▶ : $E->new(pieces => $spelled->{pieces});
Branch: 1 branch
BranchTrueFalseTotal
defined $position ? : 158 1293 1451
47  
48 1450 my $first = $self->_first;
49 1450 ▶ if (defined $first) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first) 1241 209 1450
50 Carp::croak("Game::RoyalUr::Rules: first is 'light' or 'dark'")
51 1241 ▶ unless exists $SIDE_OF{$first};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIDE_OF{$first} 1 1240 1241
52 1240 $engine->set_side($SIDE_OF{$first});
53 }
54  
55 1449 my $ply = $self->_start_ply;
56 1449 ▶ if (defined $ply) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ply) 5 1444 1449
57 5 ▶ Carp::croak('Game::RoyalUr::Rules: ply is a whole number from 0 up')
Branch: 1 branch
BranchTrueFalseTotal
unless $ply =~ /\A\d+\z/ 1 4 5
58 unless $ply =~ /\A\d+\z/;
59 4 $engine->set_ply($ply);
60 }
61  
62 1448 $self->_engine($engine);
63 1448 return;
64 }
65  
66 4 ▶sub rules { return { %{ $_[0]->_spelled } } }
Subroutine rules: called CC=1 SCAR=0.0
Pod documented
67  
68 693604 ▶sub side { $SIDE_NAME[ $_[0]->_engine->side ] }
Subroutine side: called CC=1 SCAR=0.0
Pod documented
69  
70 222685 ▶sub ply { $_[0]->_engine->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
71  
72 15962 ▶sub position { $_[0]->_engine->to_string }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
73  
74 7554 ▶sub key { $_[0]->_engine->key_hex }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
75  
76 56866 ▶sub board { $_[0]->_engine->clone }
Subroutine board: called CC=1 SCAR=0.0
Pod documented
77  
78 7122 ▶sub hand {
Subroutine hand: called CC=3 SCAR=11.7
Pod documented
79 7122 my ($self, $side) = @_;
80 7122 ▶ Carp::croak("Game::RoyalUr::Rules: a side is 'light' or 'dark'") unless exists $SIDE_OF{ $side // '' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIDE_OF{$side // ""} 0 7122 7122
Condition50%1/2
$side // ""
l!l
71220
MC/DC0%0/1
$side // ""
$side
Truth table50%1/2
A
A= $side
Aresult
11
00
81 7122 return $self->_engine->hand($SIDE_OF{$side});
82 }
83  
84 12274 ▶sub home {
Subroutine home: called CC=3 SCAR=11.7
Pod documented
85 12274 my ($self, $side) = @_;
86 12274 ▶ Carp::croak("Game::RoyalUr::Rules: a side is 'light' or 'dark'") unless exists $SIDE_OF{ $side // '' };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIDE_OF{$side // ""} 0 12274 12274
Condition50%1/2
$side // ""
l!l
122740
MC/DC0%0/1
$side // ""
$side
Truth table50%1/2
A
A= $side
Aresult
11
00
87 12274 return $self->_engine->home($SIDE_OF{$side});
88 }
89  
90 2164625 ▶sub status {
Subroutine status: called CC=3 SCAR=11.0
Pod documented
91 2164625 my ($self) = @_;
92 2164625 my $status = $self->_engine->status($self->_spelled);
93 2164625 ▶ return $status == WON ? 'won' : $status == DRAWN ? 'drawn' : 'ongoing';
Branch: 2 branches
BranchTrueFalseTotal
$status == 1 ? : 5126 2159499 2164625
$status == 2 ? : 4 2159495 2159499
94 }
95  
96 2564 ▶sub how {
Subroutine how: called CC=3 SCAR=11.0
Pod documented
97 2564 my ($self) = @_;
98 2564 my $how = $self->_engine->how($self->_spelled);
99 2564 ▶ return $how == BY_HOME ? 'home' : $how == BY_PLY_CAP ? 'ply_cap' : undef;
Branch: 2 branches
BranchTrueFalseTotal
$how == 1 ? : 2562 2 2564
$how == 2 ? : 1 1 2
100 }
101  
102 2568 ▶sub winner {
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
103 2568 my ($self) = @_;
104 2568 my $winner = $self->_engine->winner($self->_spelled);
105 2568 ▶ return $winner < 0 ? undef : $SIDE_NAME[$winner];
Branch: 1 branch
BranchTrueFalseTotal
$winner < 0 ? : 4 2564 2568
106 }
107  
108 2164605 ▶sub is_over { $_[0]->status ne 'ongoing' }
Subroutine is_over: called CC=1 SCAR=0.0
Pod documented
109  
110 621249 ▶sub moves {
Subroutine moves: called CC=3 SCAR=11.0
Pod documented
111 621249 my ($self, $roll) = @_;
112 621249 ▶ return wantarray ? () : 0 if $self->is_over;
Branch: 2 branches
BranchTrueFalseTotal
if $self->is_over 6 621243 621249
wantarray ? : 1 5 6
113 621243 return $self->_engine->moves($roll, $self->_spelled);
114 }
115  
116 202972 ▶sub apply {
Subroutine apply: called CC=6 SCAR=18.0
Pod documented
117 202972 my ($self, $move) = @_;
118 202972 ▶ Carp::croak('Game::RoyalUr::Rules: apply takes a move')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($move) and $move->isa("Game::RoyalUr::Move") 1 202971 202972
Condition66%2/3
builtin::blessed($move) and $move->isa("Game::RoyalUr::Move")
!ll&&!rl&&r
10202971
MC/DC50%1/2
builtin::blessed($move) and $move->isa("Game::RoyalUr::Move")
builtin::blessed($move) $move->isa("Game::RoyalUr::Move")
Truth table66%2/3
A and B
A= builtin::blessed($move)B= $move->isa("Game::RoyalUr::Move")
ABresult
0X0
100
111
119 unless Scalar::Util::blessed($move) && $move->isa('Game::RoyalUr::Move');
120 202971 ▶ return undef if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 202970 202971
121 202970 ▶ return undef if $move->side ne $self->side;
Branch: 1 branch
BranchTrueFalseTotal
if $move->side ne $self->side 1 202969 202970
122 202969 my $undo = $self->_engine->apply($move, $self->_spelled);
123 202969 ▶ return undef unless defined $undo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $undo 0 202969 202969
124 202969 push @{ $self->_stack }, $undo;
125 202969 return $move;
126 }
127  
128 8872 ▶sub forfeit {
Subroutine forfeit: called CC=2 SCAR=6.9
Pod documented
129 8872 my ($self) = @_;
130 8872 ▶ return 0 if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 8871 8872
131 8871 push @{ $self->_stack }, $self->_engine->forfeit;
132 8871 return 1;
133 }
134  
135 727 ▶sub undo {
Subroutine undo: called CC=2 SCAR=6.9
Pod documented
136 727 my ($self) = @_;
137 727 my $undo = pop @{ $self->_stack };
138 727 ▶ return 0 unless defined $undo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $undo 1 726 727
139 726 $self->_engine->unapply($undo);
140 726 return 1;
141 }
142  
143 204200 ▶sub depth { scalar @{ $_[0]->_stack } }
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
144  
145 61 1;
146  
147 __END__
« blib/lib/Game/RoyalUr/Engine.pm ↑ Summary blib/lib/Game/RoyalUr/Error.pm »