lib/Games/Bingo/Card.pm

filter: Statementstmt 100% 133 / 133 Branchbran 100% 42 / 42 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 18 / 18 Podpod 100% 4 / 4 totaltotal 99.0% 200 / 202 CC 29 SCAR 15.0
CC
29
Cov
99%
_print_card
8.0
_integrity_check
6.0
populate
6.0
CRAP 4.5
« script/bingo.pl ↑ Summary lib/Games/Bingo/ColumnCollection.pm »
1 package Games::Bingo::Card;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use 5.006; #perl 5.6.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Games::Bingo::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Games::Bingo::ColumnCollection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Games::Bingo::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 NUMBER_OF_NUMBERS_IN_CARD
12 NUMBER_OF_COLUMNS_IN_CARD
13 NUMBER_OF_ROWS_IN_CARD
14 NUMBER_OF_NUMBERS_IN_ROW
15 NUMBER_OF_NUMBERS
16 12 );
17  
18 12 $VERSION = '0.18';
19  
20 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 13 my ($class) = @_;
22
23 13 my $self = bless [], $class;
24
25 13 return $self;
26 }
27  
28 149 ▶sub get_all_numbers {
Subroutine get_all_numbers: called CC=4 SCAR=13.9
Pod documented
29 149 my $self = shift;
30
31 149 my @numbers = ();
32 149 foreach my $row (@{$self}) {
33 1332 foreach my $number (@{$row}) {
34 3074 ▶ push(@numbers, $number) if $number;
Branch: 1 branch
BranchTrueFalseTotal
if $number 1858 1216 3074
35 }
36 }
37 149 return @numbers;
38 }
39  
40 3 ▶sub validate {
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
41 3 my ($self, $bingo) = @_;
42
43 3 my $rv = 0;
44 3 my $trv = NUMBER_OF_NUMBERS_IN_CARD;
45
46 3 ▶ if ($bingo->{game}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bingo->{'game'}) { } 2 1 3
47 2 my @numbers = $self->get_all_numbers();
48
49 2 foreach my $number (@numbers) {
50 30 ▶ ++$rv if $bingo->pulled($number);
Branch: 1 branch
BranchTrueFalseTotal
if $bingo->pulled($number) 15 15 30
51 }
52 2 $trv = NUMBER_OF_NUMBERS_IN_CARD;
53 } else {
54 1 warn "bingo game not defined?\n";
55 }
56
57 3 ▶ if ($rv == $trv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv == $trv) { } 1 2 3
58 1 $bingo->{game}--;
59 1 return 1;
60 } else {
61 2 return 0;
62 }
63 }
64  
65 1 ▶sub _print_card {
Subroutine _print_card: called CC=8 SCAR=20.8
66 1 my $self = shift;
67  
68 1 my $row = 0;
69 1 ▶ for (my $m = 0; $m < 7; $m++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m < 7) 7 1 8
70 7 my $column = 0;
71
72 7 ▶ if ($m%2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m % 2) { } 3 4 7
73 3 ▶ for (my $n = 1; $n < 20; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 20) 57 3 60
74 57 ▶ if ($n%2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % 2) { } 30 27 57
75 30 print "|";
76 } else {
77 27 ▶ if ($self->[$column][$row]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[$column][$row]) { } 15 12 27
78 15 printf("%2d",$self->[$column][$row]);
79 } else {
80 12 print " ";
81 }
82 27 ++$column;
83 }
84 }
85 3 $row++;
86 } else {
87 4 ▶ for (my $n = 1; $n < 20; $n++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 20) 76 4 80
88 76 ▶ if ($n%2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n % 2) { } 40 36 76
89 40 print "+";
90 } else {
91 36 print "--";
92 }
93 }
94 }
95 7 print "\n";
96 }
97 1 return 1;
98 }
99  
100 2202 ▶sub _insert {
Subroutine _insert: called CC=1 SCAR=0.0
101 2202 my ($self, $row, $number) = @_;
102
103 2202 my $column = $self->_resolve_column($number);
104 2202 $self->[$column]->[$row] = $number;
105  
106 2202 return 1;
107 }
108  
109 147 ▶sub _integrity_check {
Subroutine _integrity_check: called CC=6 SCAR=17.9
110 147 my ($self) = @_;
111
112 147 my $rv = NUMBER_OF_NUMBERS_IN_CARD;
113 147 foreach my $row (@{$self}) {
114 1323 foreach my $cell (@{$row}) {
115 3046 ▶ if ($cell and $cell =~ m/^\d+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell and $cell =~ /^\d+$/o) 1842 1204 3046
Condition66%2/3
$cell and $cell =~ /^\d+$/o
!ll&&!rl&&r
120401842
MC/DC50%1/2
$cell and $cell =~ /^\d+$/o
$cell $cell =~ /^\d+$/o
Truth table66%2/3
A and B
A= $cellB= $cell =~ /^\d+$/o
ABresult
0X0
100
111
116 1842 $rv--;
117 }
118 }
119 }
120 147 ▶ if ($rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 0) { } 139 8 147
121 139 return 0;
122 } else {
123 8 return 1;
124 }
125 }
126  
127 2292 ▶sub _resolve_column {
Subroutine _resolve_column: called CC=3 SCAR=11.0
128 2292 my ($self, $number) = @_;
129
130 2292 my $result = ($number / 10);
131 2292 my ($column) = $result =~ m/^(\d{1})$/o;
132
133 2292 ▶ if ($result < 1) { #ones go in column 0
Branch: 2 branches
BranchTrueFalseTotal
if ($result < 1) { } 253 2039 2292
elsif ($result == 9) { } 17 2022 2039
134 253 $column = 0;
135 } elsif ($result == NUMBER_OF_COLUMNS_IN_CARD) { #9 go in column 8
136 17 $column = 8;
137 }
138 2292 return $column;
139 }
140  
141 146 ▶sub _init {
Subroutine _init: called CC=3 SCAR=11.0
142 146 my ($self) = @_;
143
144 146 my @numbers;
145 146 my $bingo = Games::Bingo->new();
146 146 $bingo->init(\@numbers, NUMBER_OF_NUMBERS);
147  
148 #Creating the numeric set to pick from
149 146 my $temp_collection = Games::Bingo::ColumnCollection->new();
150 146 $temp_collection->divide(NUMBER_OF_COLUMNS_IN_CARD, @numbers);
151
152 146 my $final_collection = Games::Bingo::ColumnCollection->new();
153
154 #Getting the first 9 numbers
155 146 ▶ for (my $i = 0; $i < NUMBER_OF_COLUMNS_IN_CARD; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 9) 1314 146 1460
156 1314 my $c = $temp_collection->get_column($i);
157 1314 my $n = $c->get_random_number(1);
158  
159 1314 my $fc = Games::Bingo::Column->new($i);
160 1314 $fc->populate($n);
161
162 1314 $final_collection->add_column($fc);
163 }
164  
165 #Getting the 3 extras so we have 12 numbers
166 146 ▶ for (my $i = NUMBER_OF_NUMBERS_IN_CARD
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 876 146 1022
167 - NUMBER_OF_COLUMNS_IN_CARD; $i > 0; $i--) {
168 876 my $tc = $temp_collection->get_random_column(1);
169 876 my $n = $tc->get_random_number(1);
170 876 my $label = $tc->{label};
171
172 876 my $fc = $final_collection->get_column($label);
173
174 876 $fc->populate($n);
175 }
176 146 return $final_collection;
177 }
178  
179 6 ▶sub populate {
Subroutine populate: called CC=6 SCAR=17.9
Pod documented
180 6 my ($self) = @_;
181  
182 144 HACK:
183
184 my $fcc = $self->_init();
185
186 144 ▶ for (my $row = NUMBER_OF_ROWS_IN_CARD-1; $row >= 0; $row--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row >= 0) 432 144 576
187 432 my $tcc = Games::Bingo::ColumnCollection->new();
188  
189 432 ▶ for (my $i = NUMBER_OF_NUMBERS_IN_ROW; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 2160 432 2592
190  
191 2160 my $c = $fcc->get_random_column(1);
192 2160 my $number = $c->get_highest_number(1);
193
194 2160 ▶ if ($c->count_numbers() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->count_numbers > 0) { } 864 1296 2160
195 864 $fcc->add_column($c);
196 } else {
197 #implicitly discarding empty columns
198 }
199 2160 $self->_insert($row, $number);
200 }
201 432 foreach my $column (@${fcc}) {
202 1532 $tcc->add_column($column);
203 }
204 432 $fcc = $tcc;
205 }
206 144 my $amount = scalar $self->get_all_numbers();
207
208 144 ▶ unless ($self->_integrity_check) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_integrity_check) 138 6 144
209
210 138 warn "Incomplete trying again... ($amount)\n";
211
212 #if the integrity check fails, meaning we don not have
213 #enough numbers to print a card we simply try again,
214 #please refer to the BUGS file or the B<BUGS> section below.
215
216 138 $self = $self->_flush;
217
218 138 goto HACK;
219 }
220 6 return $self;
221 }
222  
223 139 ▶sub _flush {
Subroutine _flush: called CC=1 SCAR=0.0
224 139 my $self = shift;
225
226 139 @{$self} = ();
227
228 139 return $self;
229 }
230  
231 12 1;
232  
233 __END__
« script/bingo.pl ↑ Summary lib/Games/Bingo/ColumnCollection.pm »