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 84 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 84 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 84 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 84 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 84 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 84 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 84 ▶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 84 our $VERSION = '0.04';
14  
15 84 my $E = 'Game::RoyalUr::Engine';
16  
17 84 my (@SIDE_NAME, %SIDE_OF);
18 84 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 84 @SIDE_NAME = ('light', 'dark');
20 84 %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK);
21 }
22  
23 84 has _given => (is => 'ro', init_arg => 'rules', private => 1);
24  
25 84 has _position => (is => 'ro', init_arg => 'position', private => 1);
26  
27 84 has _first => (is => 'ro', init_arg => 'first', private => 1);
28  
29 84 has _start_ply => (is => 'ro', init_arg => 'ply', private => 1);
30  
31 84 has _engine => (is => 'rw', private => 1);
32  
33 84 has _spelled => (is => 'rw', private => 1);
34  
35 84 has _stack => (is => 'rw', private => 1);
36  
37 1655 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
38 1655 my ($self) = @_;
39 1655 my $spelled = $E->rules($self->_given);
40 1654 $self->_spelled($spelled);
41 1654 $self->_stack([]);
42  
43 1654 my $position = $self->_position;
44 my $engine = defined $position
45 ? $E->new(position => $position)
46 1654 ▶ : $E->new(pieces => $spelled->{pieces});
Branch: 1 branch
BranchTrueFalseTotal
defined $position ? : 175 1479 1654
47  
48 1653 my $first = $self->_first;
49 1653 ▶ if (defined $first) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first) 1441 212 1653
50 Carp::croak("Game::RoyalUr::Rules: first is 'light' or 'dark'")
51 1441 ▶ unless exists $SIDE_OF{$first};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIDE_OF{$first} 1 1440 1441
52 1440 $engine->set_side($SIDE_OF{$first});
53 }
54  
55 1652 my $ply = $self->_start_ply;
56 1652 ▶ if (defined $ply) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ply) 5 1647 1652
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 1651 $self->_engine($engine);
63 1651 return;
64 }
65  
66 4 ▶sub rules { return { %{ $_[0]->_spelled } } }
Subroutine rules: called CC=1 SCAR=0.0
Pod documented
67  
68 710525 ▶sub side { $SIDE_NAME[ $_[0]->_engine->side ] }
Subroutine side: called CC=1 SCAR=0.0
Pod documented
69  
70 228832 ▶sub ply { $_[0]->_engine->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
71  
72 16603 ▶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 64809 ▶sub board { $_[0]->_engine->clone }
Subroutine board: called CC=1 SCAR=0.0
Pod documented
77  
78 7896 ▶sub hand {
Subroutine hand: called CC=3 SCAR=11.7
Pod documented
79 7896 my ($self, $side) = @_;
80 7896 ▶ 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 7896 7896
Condition50%1/2
$side // ""
l!l
78960
MC/DC0%0/1
$side // ""
$side
Truth table50%1/2
A
A= $side
Aresult
11
00
81 7896 return $self->_engine->hand($SIDE_OF{$side});
82 }
83  
84 13134 ▶sub home {
Subroutine home: called CC=3 SCAR=11.7
Pod documented
85 13134 my ($self, $side) = @_;
86 13134 ▶ 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 13134 13134
Condition50%1/2
$side // ""
l!l
131340
MC/DC0%0/1
$side // ""
$side
Truth table50%1/2
A
A= $side
Aresult
11
00
87 13134 return $self->_engine->home($SIDE_OF{$side});
88 }
89  
90 2220811 ▶sub status {
Subroutine status: called CC=3 SCAR=11.0
Pod documented
91 2220811 my ($self) = @_;
92 2220811 my $status = $self->_engine->status($self->_spelled);
93 2220811 ▶ return $status == WON ? 'won' : $status == DRAWN ? 'drawn' : 'ongoing';
Branch: 2 branches
BranchTrueFalseTotal
$status == 1 ? : 5223 2215588 2220811
$status == 2 ? : 4 2215584 2215588
94 }
95  
96 2594 ▶sub how {
Subroutine how: called CC=3 SCAR=11.0
Pod documented
97 2594 my ($self) = @_;
98 2594 my $how = $self->_engine->how($self->_spelled);
99 2594 ▶ return $how == BY_HOME ? 'home' : $how == BY_PLY_CAP ? 'ply_cap' : undef;
Branch: 2 branches
BranchTrueFalseTotal
$how == 1 ? : 2592 2 2594
$how == 2 ? : 1 1 2
100 }
101  
102 2598 ▶sub winner {
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
103 2598 my ($self) = @_;
104 2598 my $winner = $self->_engine->winner($self->_spelled);
105 2598 ▶ return $winner < 0 ? undef : $SIDE_NAME[$winner];
Branch: 1 branch
BranchTrueFalseTotal
$winner < 0 ? : 4 2594 2598
106 }
107  
108 2220791 ▶sub is_over { $_[0]->status ne 'ongoing' }
Subroutine is_over: called CC=1 SCAR=0.0
Pod documented
109  
110 634333 ▶sub moves {
Subroutine moves: called CC=3 SCAR=11.0
Pod documented
111 634333 my ($self, $roll) = @_;
112 634333 ▶ return wantarray ? () : 0 if $self->is_over;
Branch: 2 branches
BranchTrueFalseTotal
if $self->is_over 6 634327 634333
wantarray ? : 1 5 6
113 634327 return $self->_engine->moves($roll, $self->_spelled);
114 }
115  
116 208563 ▶sub apply {
Subroutine apply: called CC=6 SCAR=18.0
Pod documented
117 208563 my ($self, $move) = @_;
118 208563 ▶ Carp::croak('Game::RoyalUr::Rules: apply takes a move')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($move) and $move->isa("Game::RoyalUr::Move") 1 208562 208563
Condition66%2/3
builtin::blessed($move) and $move->isa("Game::RoyalUr::Move")
!ll&&!rl&&r
10208562
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 208562 ▶ return undef if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 208561 208562
121 208561 ▶ return undef if $move->side ne $self->side;
Branch: 1 branch
BranchTrueFalseTotal
if $move->side ne $self->side 1 208560 208561
122 208560 my $undo = $self->_engine->apply($move, $self->_spelled);
123 208560 ▶ return undef unless defined $undo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $undo 0 208560 208560
124 208560 push @{ $self->_stack }, $undo;
125 208560 return $move;
126 }
127  
128 9098 ▶sub forfeit {
Subroutine forfeit: called CC=2 SCAR=6.9
Pod documented
129 9098 my ($self) = @_;
130 9098 ▶ return 0 if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 9097 9098
131 9097 push @{ $self->_stack }, $self->_engine->forfeit;
132 9097 return 1;
133 }
134  
135 720 ▶sub undo {
Subroutine undo: called CC=2 SCAR=6.9
Pod documented
136 720 my ($self) = @_;
137 720 my $undo = pop @{ $self->_stack };
138 720 ▶ return 0 unless defined $undo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $undo 1 719 720
139 719 $self->_engine->unapply($undo);
140 719 return 1;
141 }
142  
143 209781 ▶sub depth { scalar @{ $_[0]->_stack } }
Subroutine depth: called CC=1 SCAR=0.0
Pod documented
144  
145 84 1;
146  
147 __END__
« blib/lib/Game/RoyalUr/Engine.pm ↑ Summary blib/lib/Game/RoyalUr/Error.pm »