blib/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 SCAR 33.7
CC
29
Cov
99%
_print_card
8.0
_integrity_check
6.0
populate
6.0
CRAP 29.0
« blib/script/bingo.pl ↑ Summary blib/lib/Games/Bingo/ColumnCollection.pm »
1 package Games::Bingo::Card;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use 5.006; #perl 5.6.0
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Games::Bingo::Column;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Games::Bingo::ColumnCollection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 use Games::Bingo::Constants qw(
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 13 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 13 $VERSION = '0.17';
19  
20 sub new {
21 13 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22
23 13 my $self = bless [], $class;
24
25 13 return $self;
26 }
27  
28 sub get_all_numbers {
29 89 ▶ my $self = shift;
Subroutine get_all_numbers: called CC=4 SCAR=13.9
Pod documented
30
31 89 my @numbers = ();
32 89 foreach my $row (@{$self}) {
33 792 foreach my $number (@{$row}) {
34 1826 ▶ push(@numbers, $number) if $number;
Branch: 1 branch
BranchTrueFalseTotal
if $number 1111 715 1826
35 }
36 }
37 89 return @numbers;
38 }
39  
40 sub validate {
41 3 ▶ my ($self, $bingo) = @_;
Subroutine validate: called CC=5 SCAR=16.1
Pod documented
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 sub _print_card {
66 1 ▶ my $self = shift;
Subroutine _print_card: called CC=8 SCAR=20.8
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 sub _insert {
101 1302 ▶ my ($self, $row, $number) = @_;
Subroutine _insert: called CC=1 SCAR=0.0
102
103 1302 my $column = $self->_resolve_column($number);
104 1302 $self->[$column]->[$row] = $number;
105  
106 1302 return 1;
107 }
108  
109 sub _integrity_check {
110 87 ▶ my ($self) = @_;
Subroutine _integrity_check: called CC=6 SCAR=17.9
111
112 87 my $rv = NUMBER_OF_NUMBERS_IN_CARD;
113 87 foreach my $row (@{$self}) {
114 783 foreach my $cell (@{$row}) {
115 1802 ▶ if ($cell and $cell =~ m/^\d+$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cell and $cell =~ /^\d+$/o) 1095 707 1802
Condition66%2/3
$cell and $cell =~ /^\d+$/o
!ll&&!rl&&r
70701095
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 1095 $rv--;
117 }
118 }
119 }
120 87 ▶ if ($rv != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv != 0) { } 79 8 87
121 79 return 0;
122 } else {
123 8 return 1;
124 }
125 }
126  
127 sub _resolve_column {
128 1392 ▶ my ($self, $number) = @_;
Subroutine _resolve_column: called CC=3 SCAR=11.0
129
130 1392 my $result = ($number / 10);
131 1392 my ($column) = $result =~ m/^(\d{1})$/o;
132
133 1392 ▶ if ($result < 1) { #ones go in column 0
Branch: 2 branches
BranchTrueFalseTotal
if ($result < 1) { } 148 1244 1392
elsif ($result == 9) { } 7 1237 1244
134 148 $column = 0;
135 } elsif ($result == NUMBER_OF_COLUMNS_IN_CARD) { #9 go in column 8
136 7 $column = 8;
137 }
138 1392 return $column;
139 }
140  
141 sub _init {
142 86 ▶ my ($self) = @_;
Subroutine _init: called CC=3 SCAR=11.0
143
144 86 my @numbers;
145 86 my $bingo = Games::Bingo->new();
146 86 $bingo->init(\@numbers, NUMBER_OF_NUMBERS);
147  
148 #Creating the numeric set to pick from
149 86 my $temp_collection = Games::Bingo::ColumnCollection->new();
150 86 $temp_collection->divide(NUMBER_OF_COLUMNS_IN_CARD, @numbers);
151
152 86 my $final_collection = Games::Bingo::ColumnCollection->new();
153
154 #Getting the first 9 numbers
155 86 ▶ for (my $i = 0; $i < NUMBER_OF_COLUMNS_IN_CARD; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 9) 774 86 860
156 774 my $c = $temp_collection->get_column($i);
157 774 my $n = $c->get_random_number(1);
158  
159 774 my $fc = Games::Bingo::Column->new($i);
160 774 $fc->populate($n);
161
162 774 $final_collection->add_column($fc);
163 }
164  
165 #Getting the 3 extras so we have 12 numbers
166 86 ▶ for (my $i = NUMBER_OF_NUMBERS_IN_CARD
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 516 86 602
167 - NUMBER_OF_COLUMNS_IN_CARD; $i > 0; $i--) {
168 516 my $tc = $temp_collection->get_random_column(1);
169 516 my $n = $tc->get_random_number(1);
170 516 my $label = $tc->{label};
171
172 516 my $fc = $final_collection->get_column($label);
173
174 516 $fc->populate($n);
175 }
176 86 return $final_collection;
177 }
178  
179 sub populate {
180 6 ▶ my ($self) = @_;
Subroutine populate: called CC=6 SCAR=17.9
Pod documented
181  
182 84 HACK:
183
184 my $fcc = $self->_init();
185
186 84 ▶ for (my $row = NUMBER_OF_ROWS_IN_CARD-1; $row >= 0; $row--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row >= 0) 252 84 336
187 252 my $tcc = Games::Bingo::ColumnCollection->new();
188  
189 252 ▶ for (my $i = NUMBER_OF_NUMBERS_IN_ROW; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 1260 252 1512
190  
191 1260 my $c = $fcc->get_random_column(1);
192 1260 my $number = $c->get_highest_number(1);
193
194 1260 ▶ if ($c->count_numbers() > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->count_numbers > 0) { } 504 756 1260
195 504 $fcc->add_column($c);
196 } else {
197 #implicitly discarding empty columns
198 }
199 1260 $self->_insert($row, $number);
200 }
201 252 foreach my $column (@${fcc}) {
202 908 $tcc->add_column($column);
203 }
204 252 $fcc = $tcc;
205 }
206 84 my $amount = scalar $self->get_all_numbers();
207
208 84 ▶ unless ($self->_integrity_check) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_integrity_check) 78 6 84
209
210 78 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 78 $self = $self->_flush;
217
218 78 goto HACK;
219 }
220 6 return $self;
221 }
222  
223 sub _flush {
224 79 ▶ my $self = shift;
Subroutine _flush: called CC=1 SCAR=0.0
225
226 79 @{$self} = ();
227
228 79 return $self;
229 }
230  
231 13 1;
232  
233 __END__
« blib/script/bingo.pl ↑ Summary blib/lib/Games/Bingo/ColumnCollection.pm »