| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | 7 | my $column = 0; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 7 | ▶ | if ($m%2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | for (my $n = 1; $n < 20; $n++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 57 | ▶ | if ($n%2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 30 | print "|"; | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 27 | ▶ | if ($self->[$column][$row]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 76 | ▶ | if ($n%2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$cell and $cell =~ /^\d+$/o
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
| |||||||||||||||||||||||||||||
| 116 | 1842 | $rv--; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 147 | ▶ | if ($rv != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||