blib/lib/Game/Brandubh/Rules.pm

filter: Statementstmt 100% 100 / 100 Branchbran 96.6% 29 / 30 Conditioncond 87.5% 21 / 24 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 100% 40 / 40 Podpod 93.1% 27 / 29 totaltotal 96.1% 226 / 235 CC 24 SCAR 13.9
CC
24
Cov
97%
search
13.1
outcome_name
4.0
BUILD
4.0
CRAP 4.0
« blib/lib/Game/Brandubh/Bot.pm ↑ Summary blib/lib/Game/Brandubh/Error.pm »
1 package Game::Brandubh::Rules;
2  
3 50 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 50 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 50 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 50 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 50 ▶use Exporter 'import';
Subroutine __ANON__: called CC=1 SCAR=0.0
9 50 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 50 ▶use Game::Brandubh::Engine ();
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 50 our $VERSION = '0.02';
14  
15 50 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
16 50 ONGOING => 0,
17 BY_CORNER => 1,
18 BY_CAPTURE => 2,
19 BY_NO_PIECES => 3,
20 DRAW_REPETITION => 4,
21 DRAW_NO_MOVE => 5,
22 DRAW_PLY_CAP => 6,
23 50 };
24  
25 50 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 50 PLAY_OK => 0,
27 PLAY_OVER => 1,
28 PLAY_ILLEGAL => 2,
29 50 };
30  
31 50 our @EXPORT_OK = qw(
32 ONGOING BY_CORNER BY_CAPTURE BY_NO_PIECES
33 DRAW_REPETITION DRAW_NO_MOVE DRAW_PLY_CAP
34 PLAY_OK PLAY_OVER PLAY_ILLEGAL
35 outcome_name
36 );
37 50 our %EXPORT_TAGS = (all => \@EXPORT_OK);
38  
39 50 my @NAME;
40 50 ▶BEGIN { @NAME = qw(ongoing corner capture no_pieces repetition no_move ply_cap) }
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 4692 ▶sub outcome_name {
Subroutine outcome_name: called CC=4 SCAR=13.9
Pod documented
43 4692 my ($outcome) = @_;
44 4692 ▶ return undef unless defined $outcome && $outcome =~ /\A[0-9]+\z/ && $outcome < @NAME;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outcome and $outcome =~ /\A[0-9]+\z/ and $outcome < @NAME 3 4689 4692
Condition100%6/6
defined $outcome and $outcome =~ /\A[0-9]+\z/ and $outcome < @NAME
!ll&&!rl&&r
214689
defined $outcome and $outcome =~ /\A[0-9]+\z/
!ll&&!rl&&r
114690
MC/DC100%3/3
defined $outcome and $outcome =~ /\A[0-9]+\z/ and $outcome < @NAME
defined $outcome $outcome =~ /\A[0-9]+\z/ $outcome < @NAME
Truth table100%4/4
A and B and C
A= defined $outcomeB= $outcome =~ /\A[0-9]+\z/C= $outcome < @NAME
ABCresult
0XX0
10X0
1100
1111
45 4689 return $NAME[$outcome];
46 }
47  
48 50 has _ptr => (is => 'rw', private => 1);
49  
50 50 has _start => (is => 'ro', init_arg => 'position', private => 1);
51  
52 50 has _rules => (is => 'ro', init_arg => 'variant', private => 1);
53  
54 50 my %ADOPTING;
55  
56 8923 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
57 8923 my ($self) = @_;
58 8923 my $given = $self->_ptr;
59 8923 ▶ if ($given) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given) 4 8919 8923
60 4 ▶ return if delete $ADOPTING{$given};
Branch: 1 branch
BranchTrueFalseTotal
if delete $ADOPTING{$given} 3 1 4
61 1 $self->_ptr(0);
62 1 Carp::croak("Game::Brandubh::Rules: a game is made by new or clone");
63 }
64  
65 8919 my ($ptr, $err) = _new_game($self->_start, $self->_rules);
66 8918 ▶ Carp::croak("Game::Brandubh::Rules: the position was refused, code $err")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 1 8917 8918
67 unless defined $ptr;
68 8917 $self->_ptr($ptr);
69 8917 return;
70 }
71  
72 3 ▶my $adopt = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 3 my ($class, $ptr) = @_;
74 3 $ADOPTING{$ptr} = 1;
75 3 return $class->new(_ptr => $ptr);
76 50 };
77  
78 3 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
79 3 my ($self) = @_;
80 3 return $adopt->(ref($self), _copy_game($self->_ptr));
81 }
82  
83 8920 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.0
Pod undocumented
84 8920 my ($self) = @_;
85 8920 my $ptr = $self->_ptr;
86 8920 ▶ return unless $ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $ptr 0 8920 8920
87 8920 _drop_game($ptr);
88 8920 $self->_ptr(0);
89 8920 return;
90 }
91  
92 21211 ▶sub position { _position($_[0]->_ptr) }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
93 7842 ▶sub key_hex { _key_hex($_[0]->_ptr) }
Subroutine key_hex: called CC=1 SCAR=0.0
Pod documented
94 14 ▶sub key_at { _key_at_hex($_[0]->_ptr, $_[1]) }
Subroutine key_at: called CC=1 SCAR=0.0
Pod documented
95 8779 ▶sub side { _side($_[0]->_ptr) }
Subroutine side: called CC=1 SCAR=0.0
Pod documented
96 2319061 ▶sub at { _at($_[0]->_ptr, $_[1]) }
Subroutine at: called CC=1 SCAR=0.0
Pod documented
97 31241 ▶sub count { _count($_[0]->_ptr, $_[1]) }
Subroutine count: called CC=1 SCAR=0.0
Pod documented
98 28111 ▶sub ply { _ply($_[0]->_ptr) }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
99 6 ▶sub ply_cap { _cap($_[0]->_ptr) }
Subroutine ply_cap: called CC=1 SCAR=0.0
Pod documented
100 7 ▶sub variant { _variant($_[0]->_ptr) }
Subroutine variant: called CC=1 SCAR=0.0
Pod documented
101  
102 5 ▶sub board {
Subroutine board: called CC=1 SCAR=0.0
Pod documented
103 5 my ($self) = @_;
104 5 return Game::Brandubh::Engine->new(position => _position($self->_ptr));
105 }
106  
107 4755 ▶sub outcome { _outcome($_[0]->_ptr) }
Subroutine outcome: called CC=1 SCAR=0.0
Pod documented
108 383552 ▶sub is_over { _outcome($_[0]->_ptr) != ONGOING }
Subroutine is_over: called CC=1 SCAR=0.0
Pod documented
109 9 ▶sub is_draw { _outcome($_[0]->_ptr) >= DRAW_REPETITION }
Subroutine is_draw: called CC=1 SCAR=0.0
Pod documented
110 6074 ▶sub repeats { _repeats($_[0]->_ptr) }
Subroutine repeats: called CC=1 SCAR=0.0
Pod documented
111  
112 4188 ▶sub winner {
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
113 4188 my $side = _winner($_[0]->_ptr);
114 4188 ▶ return $side < 0 ? undef : $side;
Branch: 1 branch
BranchTrueFalseTotal
$side < 0 ? : 605 3583 4188
115 }
116  
117 7 ▶sub winner_of {
Subroutine winner_of: called CC=2 SCAR=6.9
Pod documented
118 7 my $side = _winner_of($_[1]);
119 7 ▶ return $side < 0 ? undef : $side;
Branch: 1 branch
BranchTrueFalseTotal
$side < 0 ? : 4 3 7
120 }
121  
122 100965 ▶sub moves {
Subroutine moves: called CC=2 SCAR=6.9
Pod documented
123 100965 my @m = _moves($_[0]->_ptr);
124 100965 ▶ return wantarray ? @m : scalar @m;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 100415 550 100965
125 }
126  
127 249052 ▶sub play {
Subroutine play: called CC=2 SCAR=6.9
Pod documented
128 249052 my ($answer, $flags) = _play($_[0]->_ptr, $_[1]);
129 249052 ▶ return wantarray ? ($answer, $flags) : $answer;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 482 248570 249052
130 }
131  
132 23641 ▶sub undo { _undo($_[0]->_ptr) }
Subroutine undo: called CC=1 SCAR=0.0
Pod documented
133 1616571 ▶sub preview { _preview($_[0]->_ptr, $_[1]) }
Subroutine preview: called CC=1 SCAR=0.0
Pod documented
134 179634 ▶sub why_not { _why_not($_[0]->_ptr, $_[1], $_[2]) }
Subroutine why_not: called CC=1 SCAR=0.0
Pod documented
135  
136 7 ▶sub live { _games_live() }
Subroutine live: called CC=1 SCAR=0.0
Pod documented
137  
138 8186 ▶sub search {
Subroutine search: called CC=13 SCAR=25.7
Pod documented
139 8186 my ($self, %with) = @_;
140 8186 my $budget = $with{budget};
141 8186 ▶ Carp::croak('Game::Brandubh::Rules: search needs a budget, a whole number of nodes from 1 up')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $budget and not ref $budget and $budget =~ /\A[0-9]{1,9}\z/ and $budget >= 1 5 8181 8186
Condition88%8/9
defined $budget and not ref $budget and $budget =~ /\A[0-9]{1,9}\z/ and $budget >= 1
!ll&&!rl&&r
418181
defined $budget and not ref $budget and $budget =~ /\A[0-9]{1,9}\z/
!ll&&!rl&&r
138182
defined $budget and not ref $budget
!ll&&!rl&&r
108185
MC/DC75%3/4
defined $budget and not ref $budget and $budget =~ /\A[0-9]{1,9}\z/ and $budget >= 1
defined $budget not ref $budget $budget =~ /\A[0-9]{1,9}\z/ $budget >= 1
Truth table80%4/5
A and not(B) and C and D
A= defined $budgetB= not ref $budgetC= $budget =~ /\A[0-9]{1,9}\z/D= $budget >= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
142 unless defined $budget && !ref $budget && $budget =~ /\A[0-9]{1,9}\z/ && $budget >= 1;
143 8181 ▶ my $seed = defined $with{seed} ? $with{seed} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'seed'} ? : 7876 305 8181
144 8181 ▶ Carp::croak('Game::Brandubh::Rules: a search seed is a whole number below 4294967296')
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $seed and $seed =~ /\A[0-9]{1,10}\z/ and $seed < 4294967296 2 8179 8181
Condition83%5/6
not ref $seed and $seed =~ /\A[0-9]{1,10}\z/ and $seed < 4294967296
!ll&&!rl&&r
118179
not ref $seed and $seed =~ /\A[0-9]{1,10}\z/
!ll&&!rl&&r
018180
MC/DC66%2/3
not ref $seed and $seed =~ /\A[0-9]{1,10}\z/ and $seed < 4294967296
not ref $seed $seed =~ /\A[0-9]{1,10}\z/ $seed < 4294967296
Truth table75%3/4
not(A) and B and C
A= not ref $seedB= $seed =~ /\A[0-9]{1,10}\z/C= $seed < 4294967296
ABCresult
0XX0
10X0
1100
1111
145 unless !ref $seed && $seed =~ /\A[0-9]{1,10}\z/ && $seed < 4294967296;
146 8179 ▶ my $depth = defined $with{depth} ? $with{depth} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'depth'} ? : 1595 6584 8179
147 8179 ▶ Carp::croak('Game::Brandubh::Rules: a search depth is a whole number')
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $depth and $depth =~ /\A[0-9]{1,3}\z/ 1 8178 8179
Condition66%2/3
not ref $depth and $depth =~ /\A[0-9]{1,3}\z/
!ll&&!rl&&r
018178
MC/DC50%1/2
not ref $depth and $depth =~ /\A[0-9]{1,3}\z/
not ref $depth $depth =~ /\A[0-9]{1,3}\z/
Truth table66%2/3
not(A) and B
A= not ref $depthB= $depth =~ /\A[0-9]{1,3}\z/
ABresult
0X0
100
111
148 unless !ref $depth && $depth =~ /\A[0-9]{1,3}\z/;
149  
150 8178 my ($mv, $nodes, $reached, $score, $stopped) = _search($self->_ptr, $budget, $seed, $depth, $with{weights});
151 8176 ▶ return undef unless $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless $mv 2 8174 8176
152 8174 return { move => $mv, nodes => $nodes, depth => $reached, score => $score, stopped => $stopped };
153 }
154  
155 4517 ▶sub evaluate { _evaluate($_[0]->_ptr, $_[1]) }
Subroutine evaluate: called CC=1 SCAR=0.0
Pod documented
156  
157 1 ▶sub weights { _weights() }
Subroutine weights: called CC=1 SCAR=0.0
Pod documented
158  
159 50 1;
160  
161 __END__
« blib/lib/Game/Brandubh/Bot.pm ↑ Summary blib/lib/Game/Brandubh/Error.pm »