blib/lib/Games/Hanabi.pm

filter: Statementstmt 73.8% 257 / 348 Branchbran 61.8% 94 / 152 Conditioncond 58.7% 27 / 46 MC/DCmcdc 53.3% 16 / 30 Subroutinesub 76.1% 16 / 21 Podpod 71.4% 10 / 14 totaltotal 68.7% 420 / 611 CC 126 SCAR 37.3
CC
126
Cov
69%
is_junk
182.0
is_valid_known_play
42.0
print_game_state
42.0
CRAP 41.5
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Games::Hanabi;
5 # ABSTRACT: 'hanabi' card game
6 1 $Games::Risk::VERSION = '0.001';
7  
8 1 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my @colors = qw(R G B Y W);
14 1 my @numbers = qw(1 2 3 4 5);
15 1 my %revealed_cache;
16  
17 # Needs players and variants
18 5 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod documented
19 5 my ( $class, %params ) = @_;
20 5 ▶ $params{players} //= 2;
Condition50%1/2
$params{"players"} //= 2
l!l
50
MC/DC0%0/1
$params{"players"} //= 2
$params{"players"}
Truth table50%1/2
A
A= $params{"players"}
Aresult
11
00
21 5 ▶ $params{derive} //= 1;
Condition50%1/2
$params{"derive"} //= 1
l!l
05
MC/DC0%0/1
$params{"derive"} //= 1
$params{"derive"}
Truth table50%1/2
A
A= $params{"derive"}
Aresult
11
00
22  
23 # R = red
24 # G = Green
25 # B = Blue
26 # W = White
27 # Y = Yellow
28 # M = Multicolor
29 5 my @cards = qw(
30 R1 R1 R1 R2 R2 R3 R3 R4 R4 R5
31 G1 G1 G1 G2 G2 G3 G3 G4 G4 G5
32 B1 B1 B1 B2 B2 B3 B3 B4 B4 B5
33 Y1 Y1 Y1 Y2 Y2 Y3 Y3 Y4 Y4 Y5
34 W1 W1 W1 W2 W2 W3 W3 W4 W4 W5
35 );
36 5 my @deck;
37 5 my ( %starting_count, %public_count );
38  
39 5 for my $card (@cards) {
40 250 my ( $color, $number ) = split //, $card;
41 250 push @deck, { color => $color, number => $number };
42 250 $starting_count{$color}{$number}++;
43 250 $public_count{$color}{$number} = 0;
44 }
45 5 @deck = shuffle @deck;
46  
47 my $self = {
48 players => $params{players},
49 derive => $params{derive},
50 deck => \@deck,
51 starting_deck => clone( \@deck ),
52 starting_count => \%starting_count,
53 public_count => \%public_count,
54 turn => int( rand( $params{players} ) ),
55 hints => 8,
56 bombs => 0,
57 debug => $params{debug},
58 5 score => 0,
59 discards => [],
60 top_card => {
61 'R' => 0,
62 'G' => 0,
63 'B' => 0,
64 'Y' => 0,
65 'W' => 0,
66 },
67 };
68  
69 5 bless $self, $class;
70  
71 # Draw starting hands
72 5 my $starting_cards = 4;
73 5 ▶ if ( $params{players} < 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"players"} < 4) 5 0 5
74 5 $starting_cards = 5;
75 }
76 5 for my $i ( 0 .. $params{players} - 1 ) {
77 10 for ( 1 .. $starting_cards ) {
78 50 $self->_draw($i);
79 }
80 }
81  
82 5 return $self;
83 }
84  
85 # Draw a new card and add it to the player's hand
86 96 ▶sub _draw {
Subroutine _draw: called CC=3 SCAR=11.0
87 96 my $self = shift;
88 96 my ($player) = @_;
89 96 ▶ return if @{ $self->{deck} } == 0; # empty deck
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"deck"};} == 0 1 95 96
90 95 my $card = shift @{ $self->{deck} };
91 $card->{known_information} =
92 95 { number_score => 0, color_score => 0, score => 0 };
93 95 push @{ $self->{hands}[$player] }, $card;
94  
95 # Deck just got empty, begin the end game
96 95 ▶ if ( scalar @{ $self->{deck} } == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"deck"};} == 0) 1 94 95
97 1 $self->{countdown} = $self->{players};
98 }
99 95 return;
100 }
101  
102 # The game state consists of what each player is holding,
103 # what cards are in play, the deck, and what information is known about
104 # the cards players are holding
105 10 ▶sub get_game_state {
Subroutine get_game_state: called CC=1 SCAR=0.0
Pod undocumented
106 10 my $self = shift;
107 return {
108 players => $self->{players},
109 deck => $self->{deck},
110 discards => $self->{discards},
111 hands => $self->{hands},
112 turn => $self->{turn},
113 hints => $self->{hints},
114 bombs => $self->{bombs},
115 top_card => $self->{top_card},
116 score => $self->{score},
117 countdown => $self->{countdown},
118 10 };
119 }
120  
121 # Return an array of valid moves.
122 # actions: play, discard, hint
123 # For play / discard, the 'card' value will be an int representing the index of the card in hand
124 # For hint, also pass 'player' index, and the 'value' which is either a number of color letter.
125 4 ▶sub get_valid_moves {
Subroutine get_valid_moves: called CC=12 SCAR=24.9
Pod documented
126 4 my $self = shift;
127 4 ▶ return if defined $self->{countdown} && $self->{countdown} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"countdown"} and $self->{"countdown"} == 0 1 3 4
Condition100%3/3
defined $self->{"countdown"} and $self->{"countdown"} == 0
!ll&&!rl&&r
211
MC/DC100%2/2
defined $self->{"countdown"} and $self->{"countdown"} == 0
defined $self->{"countdown"} $self->{"countdown"} == 0
Truth table100%3/3
A and B
A= defined $self->{"countdown"}B= $self->{"countdown"} == 0
ABresult
0X0
100
111
128 3 ▶ return if $self->{bombs} == 3;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bombs"} == 3 1 2 3
129 2 ▶ return if $self->{score} == 25;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"score"} == 25 1 1 2
130 1 my @moves;
131 1 my $current_player = $self->{turn};
132  
133 # Play or discard a card
134 1 for my $i ( 0 .. scalar @{ $self->{hands}[$current_player] } - 1 ) {
135 5 push @moves, { action => 'play', index => $i };
136 5 push @moves, { action => 'discard', index => $i };
137 }
138  
139 # Give a hint
140 1 ▶ if ( $self->{hints} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hints"}) 1 0 1
141 1 for my $player ( 0 .. $self->{players} - 1 ) {
142 2 ▶ next if $player == $current_player;
Branch: 1 branch
BranchTrueFalseTotal
if $player == $current_player 1 1 2
143 1 my %colors;
144 my %numbers;
145 1 for my $i ( 0 .. scalar @{ $self->{hands}[$player] } - 1 ) {
146 5 my $card = $self->{hands}[$player][$i];
147 5 $colors{ $card->{color} } = 1;
148 5 $numbers{ $card->{number} } = 1;
149 }
150 1 for my $c ( sort keys %colors ) {
151 3 push @moves,
152 { action => 'hint', player => $player, hint => $c };
153 }
154 1 for my $n ( sort keys %numbers ) {
155 1 push @moves,
156 { action => 'hint', player => $player, hint => $n };
157 }
158 }
159 }
160  
161 1 return @moves;
162 }
163  
164 # Perform an action. Return 1 for game on, 0 for game over.
165 7 ▶sub take_action {
Subroutine take_action: called CC=30 SCAR=36.5
Pod documented
166 7 my $self = shift;
167 7 my ($move) = @_;
168 7 my $current_player = $self->{turn};
169 7 ▶ if ( $move->{action} eq 'discard' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($move->{'action'} eq 'discard') { } 3 4 7
elsif ($move->{'action'} eq 'play') { } 2 2 4
elsif ($move->{'action'} eq 'hint') { } 2 0 2
170  
171 #print "Player: $current_player / index : $move->{index} \n";
172 3 my $card = $self->{hands}[$current_player][ $move->{index} ];
173 print "Player $current_player is discarding "
174 . $card->{color}
175 . $card->{number} . "\n"
176 3 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 3 3
177 3 push @{ $self->{discards} }, $card;
178 3 splice @{ $self->{hands}[$current_player] }, $move->{index}, 1;
179 3 $self->{public_count}{ $card->{color} }{ $card->{number} }++;
180 3 ▶ if ( $self->{hints} < 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hints"} < 8) 2 1 3
181 2 $self->{hints}++;
182 }
183 3 $self->_draw($current_player);
184 }
185 elsif ( $move->{action} eq 'play' ) {
186 2 my $card = $self->{hands}[$current_player][ $move->{index} ];
187 print "Player $current_player is playing "
188 . $card->{color}
189 . $card->{number} . "\n"
190 2 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
191 2 splice @{ $self->{hands}[$current_player] }, $move->{index}, 1;
192 2 $self->{public_count}{ $card->{color} }{ $card->{number} }++;
193  
194 2 ▶ if ( $self->is_valid_play($card) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_valid_play($card)) { } 1 1 2
195 1 ▶ print "It worked!\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
196 1 $self->{top_card}{ $card->{color} }++;
197 1 ▶ if ( $self->{top_card}{ $card->{color} } == 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"top_card"}{$card->{"color"}} == 5) 0 1 1
198 0 $self->{hints}++;
199 }
200 1 $self->{score}++;
201 }
202 else {
203 1 ▶ print "If did not work...\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
204 1 push @{ $self->{discards} }, $card;
205 1 $self->{bombs}++;
206 1 ▶ if ( $self->{bombs} == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bombs"} == 3) 0 1 1
207 0 return 0; # game over
208 }
209 }
210 2 $self->_draw($current_player);
211 }
212 elsif ( $move->{action} eq 'hint' ) {
213 2 $self->{hints}--;
214 croak "Used up a hint when there were none to use\n"
215 2 ▶ if $self->{hints} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"hints"} < 0 0 2 2
216 2 my $player = $move->{player};
217 print
218 "Player $current_player is giving a hint of $move->{hint} to player $player\n"
219 2 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
220 2 for my $i ( 0 .. scalar @{ $self->{hands}[$player] } - 1 ) {
221 10 my $card = $self->{hands}[$player][$i];
222 10 ▶ if ( $move->{hint} =~ /\d/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($move->{'hint'} =~ /\d/) { } 5 5 10
elsif ($move->{'hint'} =~ /[a-z]/i) { } 5 0 5
223 5 ▶ if ( $card->{number} == $move->{hint} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->{'number'} == $move->{'hint'}) { } 1 4 5
224 1 ▶ print "Found a match!\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 1 1
225 1 for my $number (@numbers) {
226 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"number"}{$number}) 5 0 5
227 !defined $card->{known_information}{number}{$number}
228 )
229 {
230 5 $card->{known_information}{number_score}++;
231 5 $card->{known_information}{number}{$number} = 0;
232 }
233 }
234 1 $card->{known_information}{number}{ $move->{hint} } = 1;
235 1 $card->{known_information}{number_score} = 10;
236 }
237 else {
238 4 ▶ if ( !defined $card->{known_information}{number}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"number"}{$move->{"hint"}}) 4 0 4
239 { $move->{hint} } )
240 {
241 4 $card->{known_information}{number_score}++;
242 4 $card->{known_information}{number}{ $move->{hint} } = 0;
243 }
244 }
245 }
246 elsif ( $move->{hint} =~ /[a-z]/i ) {
247 5 ▶ if ( $card->{color} eq $move->{hint} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->{'color'} eq $move->{'hint'}) { } 2 3 5
248 2 ▶ print "Found a match!\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
249 2 for my $color (@colors) {
250 10 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"color"}{$color}) 10 0 10
251 !defined $card->{known_information}{color}{$color} )
252 {
253 10 $card->{known_information}{color_score}++;
254 10 $card->{known_information}{color}{$color} = 0;
255 }
256  
257 }
258 2 $card->{known_information}{color}{ $move->{hint} } = 1;
259 2 $card->{known_information}{color_score} = 10;
260 }
261 else {
262 3 ▶ if ( !defined $card->{known_information}{color}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"color"}{$move->{"hint"}}) 3 0 3
263 { $move->{hint} } )
264 {
265 3 $card->{known_information}{color_score}++;
266 3 $card->{known_information}{color}{ $move->{hint} } = 0;
267 }
268 }
269 }
270 $card->{known_information}{score} =
271 $card->{known_information}{color_score} +
272 10 $card->{known_information}{number_score};
273 }
274 }
275 else {
276 0 croak "Unknown action: $move->{action}";
277 }
278  
279 # Advance the turn counter
280 7 $self->{turn} = ( $self->{turn} + 1 ) % $self->{players};
281 7 ▶ if ( defined $self->{countdown} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"countdown"}) 0 7 7
282 0 $self->{countdown}--;
283 0 ▶ if ( $self->{countdown} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"countdown"} == 0) 0 0 0
284 0 return 0; # game over
285 }
286 }
287  
288 7 ▶ if ( $self->{derive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"derive"}) 7 0 7
289 7 $self->derive_information();
290 }
291 7 return 1;
292 }
293  
294 2 ▶sub is_valid_play {
Subroutine is_valid_play: called CC=2 SCAR=6.9
Pod documented
295 2 my $self = shift;
296 2 my ($card) = @_;
297 2 ▶ if ( $card->{number} == $self->{top_card}{ $card->{color} } + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->{"number"} == $self->{"top_card"}{$card->{"color"}} + 1) 1 1 2
298 1 return 1;
299 }
300 1 return;
301 }
302  
303 # Is the play valid from the perspective of the player?
304 0 ▶sub is_valid_known_play {
Subroutine is_valid_known_play: not called CC=6 SCAR=37.4
Pod documented
305 0 my $self = shift;
306 0 my ($card) = @_;
307 0 ▶ return $self->is_valid_play($card) if $self->is_card_known($card);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_card_known($card) 0 0 0
308  
309 # If we know just the number, see if that number if valid for all possible cards
310 0 ▶ if ( $self->is_number_known($card) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_number_known($card)) { } 0 0 0
311  
312 #print "number is known for $card->{color}$card->{number}\n";
313 0 for my $color (@colors) {
314 0 ▶ if ( not defined $card->{known_information}{color}{$color} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"color"}{$color}) 0 0 0
315 0 ▶ if ( $card->{number} != $self->{top_card}{$color} + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->{"number"} != $self->{"top_card"}{$color} + 1) 0 0 0
316 0 return;
317 }
318 }
319 }
320 }
321 else {
322 0 return;
323 }
324 0 return 1;
325 }
326  
327 0 ▶sub is_junk {
Subroutine is_junk: not called CC=13 SCAR=52.0
Pod documented
328 0 my $self = shift;
329 0 my ( $card, $known ) = @_;
330 0 ▶ return 1 if $card->{known_information}{is_junk};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"known_information"}{"is_junk"} 0 0 0
331  
332 # A card is junk if it is known, and already played
333 0 ▶ if ( $known || $self->is_card_known($card) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($known or $self->is_card_known($card)) { } 0 0 0
elsif (my $color = $self->is_color_known($card)) { } 0 0 0
elsif (my $number = $self->is_number_known($card)) { } 0 0 0
Condition0%0/3
$known or $self->is_card_known($card)
l!l&&r!l&&!r
000
MC/DC0%0/2
$known or $self->is_card_known($card)
$known $self->is_card_known($card)
Truth table0%0/3
A or B
A= $knownB= $self->is_card_known($card)
ABresult
1X1
011
000
334 0 ▶ if ( $card->{number} <= $self->{top_card}{ $card->{color} } ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($card->{'number'} <= $self->{'top_card'}{$card->{'color'}}) { } 0 0 0
elsif ($card->{'number'} > $self->max_score_for_color($card->{'color'})) { } 0 0 0
elsif (scalar grep({$_->{'number'} == $card->{'number'} and $_->{'color'} eq $card->{'color'};} @{$self->{'hands'}[$self->{'turn'}];}) > 1) { } 0 0 0
335 0 $card->{known_information}{is_junk} = 1;
336 0 return 1;
337 }
338  
339 # or dead.
340 elsif ( $card->{number} > $self->max_score_for_color( $card->{color} ) )
341 {
342 0 $card->{known_information}{is_junk} = 1;
343 0 return 1;
344 }
345  
346 # or we have 2 copies
347 elsif (
348 scalar(
349 grep {
350 $_->{number} == $card->{number}
351 && $_->{color} eq $card->{color}
352 0 ▶ } @{ $self->{hands}[ $self->{turn} ] }
Condition0%0/3
$_->{'number'} == $card->{'number'} && $_->{'color'} eq $card->{'color'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'number'} == $card->{'number'} && $_->{'color'} eq $card->{'color'}
$_->{'number'} == $card->{'number'} $_->{'color'} eq $card->{'color'}
Truth table0%0/3
A && B
A= $_->{'number'} == $card->{'number'}B= $_->{'color'} eq $card->{'color'}
ABresult
0X0
100
111
353 ) > 1
354 )
355 {
356 0 $card->{known_information}{is_junk} = 1;
357 0 return 1;
358 }
359 }
360  
361 # A card is junk if a color is dead and the card has that color
362 elsif ( my $color = $self->is_color_known($card) ) {
363 0 ▶ if ( $self->{top_card}{$color} == $self->max_score_for_color($color) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"top_card"}{$color} == $self->max_score_for_color($color)) 0 0 0
364 0 $card->{known_information}{is_junk} = 1;
365 0 return 1;
366 }
367 }
368  
369 # A card is junk if a number is dead and the card has that number
370 elsif ( my $number = $self->is_number_known($card) ) {
371 0 for my $color ( keys %{ $self->{top_card} } ) {
372 0 ▶ if ( $self->{top_card}{$color} < $number ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"top_card"}{$color} < $number) 0 0 0
373 0 return;
374 }
375 }
376 0 $card->{known_information}{is_junk} = 1;
377 0 return 1;
378 }
379  
380 0 return;
381 }
382  
383 # What's the best score this color can get?
384 0 ▶sub max_score_for_color {
Subroutine max_score_for_color: not called CC=5 SCAR=34.0
Pod documented
385 0 my $self = shift;
386 0 my ($color) = @_;
387 0 my $max_score = $self->{top_card}{$color};
388 0 for my $i ( $max_score + 1 .. 5 ) {
389 0 ▶ my $matches = grep { $_->{color} eq $color && $_->{number} == $i }
Condition0%0/3
$_->{'color'} eq $color && $_->{'number'} == $i
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'color'} eq $color && $_->{'number'} == $i
$_->{'color'} eq $color $_->{'number'} == $i
Truth table0%0/3
A && B
A= $_->{'color'} eq $colorB= $_->{'number'} == $i
ABresult
0X0
100
111
390 0 @{ $self->{discards} };
391 0 ▶ my $starting = grep { $_->{color} eq $color && $_->{number} == $i }
Condition0%0/3
$_->{'color'} eq $color && $_->{'number'} == $i
!ll&&!rl&&r
000
MC/DC0%0/2
$_->{'color'} eq $color && $_->{'number'} == $i
$_->{'color'} eq $color $_->{'number'} == $i
Truth table0%0/3
A && B
A= $_->{'color'} eq $colorB= $_->{'number'} == $i
ABresult
0X0
100
111
392 0 @{ $self->{starting_deck} };
393 0 ▶ if ( $matches < $starting ) { $max_score++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($matches < $starting) 0 0 0
394 }
395 0 return $max_score;
396 }
397  
398 # Determine information about cards in our hand based on what else we can see
399 13 ▶sub derive_information {
Subroutine derive_information: called CC=39 SCAR=37.0
Pod undocumented
400 13 my $self = shift;
401 13 my ($retain_cache) = @_;
402 13 ▶ %revealed_cache = () if !$retain_cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $retain_cache 9 4 13
403 13 my $information_gained = 0;
404  
405 13 for my $player ( 0 .. $self->{players} - 1 ) {
406 26 for my $i ( 0 .. @{ $self->{hands}[$player] } - 1 ) {
407 130 my $card = $self->{hands}[$player][$i];
408  
409 130 my $color_known = $self->is_color_known($card);
410 130 my $number_known = $self->is_number_known($card);
411  
412 # If we know the number, try to figure out the color
413 130 ▶ if ( $number_known && !$color_known ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number_known and not $color_known) 26 104 130
Condition100%3/3
$number_known and not $color_known
!ll&&!rl&&r
103126
MC/DC100%2/2
$number_known and not $color_known
$number_known not $color_known
Truth table100%3/3
A and not(B)
A= $number_knownB= not $color_known
ABresult
0X0
100
111
414  
415 # Eliminate any colors that are fully revealed
416 26 for my $color (@colors) {
417 130 ▶ if ( not defined $card->{known_information}{color}{$color} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"color"}{$color}) 90 40 130
418 {
419 90 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->revealed_count($player, {"color", $color, "number", $number_known}) == $self->{"starting_count"}{$color}{$number_known}) 0 90 90
420 $self->revealed_count( $player,
421 { color => $color, number => $number_known } )
422 == $self->{starting_count}{$color}{$number_known}
423 )
424 {
425 0 $card->{known_information}{color}{$color} = 0;
426 0 $card->{known_information}{color_score}++;
427 0 $information_gained++;
428 }
429 }
430 }
431 }
432  
433 # If we know the color try to figure out the number
434 130 ▶ if ( $color_known && !$number_known ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color_known and not $number_known) 23 107 130
Condition100%3/3
$color_known and not $number_known
!ll&&!rl&&r
106123
MC/DC100%2/2
$color_known and not $number_known
$color_known not $number_known
Truth table100%3/3
A and not(B)
A= $color_knownB= not $number_known
ABresult
0X0
100
111
435  
436 # Eliminate any numbers that are fully revealed
437 23 for my $number (@numbers) {
438 115 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"number"}{$number}) 97 18 115
439 not defined $card->{known_information}{number}{$number}
440 )
441 {
442 97 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->revealed_count($player, {"color", $color_known, "number", $number}) == $self->{"starting_count"}{$color_known}{$number}) 0 97 97
443 $self->revealed_count( $player,
444 { color => $color_known, number => $number } )
445 == $self->{starting_count}{$color_known}{$number}
446 )
447 {
448 0 $card->{known_information}{number}{$number} = 0;
449 0 $card->{known_information}{number_score}++;
450 0 $information_gained++;
451 }
452 }
453 }
454  
455 }
456  
457 # If we know it's NOT every other color, then we know its color
458 130 ▶ if ( !$color_known ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color_known) 106 24 130
459 106 my $negative_colors = 0;
460 106 my $positive_color;
461 106 for my $color (@colors) {
462 530 ▶ if ( defined $card->{known_information}{color}{$color}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $card->{'known_information'}{'color'}{$color} and not $card->{'known_information'}{'color'}{$color}) { } 107 423 530
elsif (not defined $card->{'known_information'}{'color'}{$color}) { } 423 0 423
Condition66%2/3
defined $card->{"known_information"}{"color"}{$color} and not $card->{"known_information"}{"color"}{$color}
!ll&&!rl&&r
4230107
MC/DC50%1/2
defined $card->{"known_information"}{"color"}{$color} and not $card->{"known_information"}{"color"}{$color}
defined $card->{"known_information"}{"color"}{$color} not $card->{"known_information"}{"color"}{$color}
Truth table66%2/3
A and not(B)
A= defined $card->{"known_information"}{"color"}{$color}B= not $card->{"known_information"}{"color"}{$color}
ABresult
0X0
100
111
463 && !$card->{known_information}{color}{$color} )
464 {
465 107 $negative_colors++;
466 }
467 elsif ( !defined $card->{known_information}{color}{$color} )
468 {
469 423 $positive_color = $color;
470 }
471 }
472 106 ▶ if ( $negative_colors == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative_colors == 4) 10 96 106
473 10 $card->{known_information}{color}{$positive_color} = 1;
474 10 $card->{known_information}{color_score} = 10;
475 10 $information_gained++;
476 }
477 }
478  
479 # If we know it's NOT every other number, then we know its number
480 130 ▶ if ( !$number_known ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($number_known) 103 27 130
481 103 my $negative_numbers = 0;
482 103 my $positive_number;
483 103 for my $number (@numbers) {
484 515 ▶ if ( defined $card->{known_information}{number}{$number}
Branch: 2 branches
BranchTrueFalseTotal
if (defined $card->{'known_information'}{'number'}{$number} and not $card->{'known_information'}{'number'}{$number}) { } 78 437 515
elsif (not defined $card->{'known_information'}{'number'}{$number}) { } 437 0 437
Condition66%2/3
defined $card->{"known_information"}{"number"}{$number} and not $card->{"known_information"}{"number"}{$number}
!ll&&!rl&&r
437078
MC/DC50%1/2
defined $card->{"known_information"}{"number"}{$number} and not $card->{"known_information"}{"number"}{$number}
defined $card->{"known_information"}{"number"}{$number} not $card->{"known_information"}{"number"}{$number}
Truth table66%2/3
A and not(B)
A= defined $card->{"known_information"}{"number"}{$number}B= not $card->{"known_information"}{"number"}{$number}
ABresult
0X0
100
111
485 && !$card->{known_information}{number}{$number} )
486 {
487 78 $negative_numbers++;
488 }
489 elsif (
490 !defined $card->{known_information}{number}{$number} )
491 {
492 437 $positive_number = $number;
493 }
494 else {
495 0 $positive_number = $number;
496 }
497 }
498 103 ▶ if ( $negative_numbers == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative_numbers == 4) 6 97 103
499 6 $card->{known_information}{number}{$positive_number} = 1;
500 6 $card->{known_information}{number_score} = 10;
501 6 $information_gained++;
502 }
503 }
504 }
505  
506 # Is there a dead number?
507  
508 26 for my $number (@numbers) {
509 130 my $total = 0;
510 130 my $total_revealed = 0;
511 130 for my $color (@colors) {
512 650 $total_revealed += $self->revealed_count( $player,
513 { number => $number, color => $color } );
514 650 $total += $self->{starting_count}{$color}{$number};
515 }
516 130 ▶ if ( $total == $total_revealed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total == $total_revealed) 36 94 130
517 36 for my $i ( 0 .. @{ $self->{hands}[$player] } - 1 ) {
518 180 my $card = $self->{hands}[$player][$i];
519 180 ▶ if ( !defined $card->{known_information}{number}{$number} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"number"}{$number}) 40 140 180
520 {
521 40 $card->{known_information}{number}{$number} = 0;
522 40 $card->{known_information}{number_score}++;
523 40 $information_gained++;
524 }
525 }
526 }
527 }
528  
529 # Is there a dead color?
530 26 for my $color (@colors) {
531 130 my $total = 0;
532 130 my $total_revealed = 0;
533 130 for my $number (@numbers) {
534 650 $total_revealed += $self->revealed_count( $player,
535 { number => $number, color => $color } );
536 650 $total += $self->{starting_count}{$color}{$number};
537 }
538 130 ▶ if ( $total == $total_revealed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total == $total_revealed) 36 94 130
539 36 for my $i ( 0 .. @{ $self->{hands}[$player] } - 1 ) {
540 180 my $card = $self->{hands}[$player][$i];
541 180 ▶ if ( !defined $card->{known_information}{color}{$color} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card->{"known_information"}{"color"}{$color}) 50 130 180
542 50 $card->{known_information}{color}{$color} = 0;
543 50 $card->{known_information}{color_score}++;
544 50 $information_gained++;
545 }
546 }
547 }
548 }
549  
550 }
551  
552 13 ▶ if ($information_gained) {
Branch: 1 branch
BranchTrueFalseTotal
if ($information_gained) 4 9 13
553  
554 # Recompute the total scores and derive again
555 4 for my $player ( 0 .. $self->{players} - 1 ) {
556 8 for my $i ( 0 .. @{ $self->{hands}[$player] } - 1 ) {
557 40 my $card = $self->{hands}[$player][$i];
558 $card->{known_information}{score} =
559 $card->{known_information}{number_score} +
560 40 $card->{known_information}{color_score};
561 }
562 }
563  
564 4 $self->derive_information(1);
565 }
566 13 return;
567 }
568  
569 # How may copies of $card does $player know about?
570 1487 ▶sub revealed_count {
Subroutine revealed_count: called CC=7 SCAR=19.5
Pod undocumented
571 1487 my $self = shift;
572 1487 my ( $player, $card ) = @_;
573 return $revealed_cache{$player}{ $card->{color} }{ $card->{number} }
574 1487 ▶ if $revealed_cache{$player}{ $card->{color} }{ $card->{number} };
Branch: 1 branch
BranchTrueFalseTotal
if $revealed_cache{$player}{$card->{"color"}}{$card->{"number"}} 567 920 1487
575  
576 # If all copies of a card are revealed, stop looking
577 920 my $color = $card->{color};
578 920 my $number = $card->{number};
579 920 my $count = $self->{public_count}{$color}{$number};
580 920 ▶ if ( $self->{starting_count}{$color}{$number} == $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"starting_count"}{$color}{$number} == $count) 84 836 920
581 84 $revealed_cache{$player}{ $card->{color} }{ $card->{number} } = $count;
582 84 return $count;
583 }
584  
585 # Look for cards in players' hands... even our own
586 836 for my $p ( 0 .. $self->{players} - 1 ) {
587 1672 ▶ if ( $p == $player ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p == $player) { } 836 836 1672
588 $count += grep {
589 $_->{known_information}{color}{$color}
590 4180 ▶ && $_->{known_information}{number}{$number}
Condition100%3/3
$_->{'known_information'}{'color'}{$color} && $_->{'known_information'}{'number'}{$number}
!ll&&!rl&&r
37714081
MC/DC100%2/2
$_->{'known_information'}{'color'}{$color} && $_->{'known_information'}{'number'}{$number}
$_->{'known_information'}{'color'}{$color} $_->{'known_information'}{'number'}{$number}
Truth table100%3/3
A && B
A= $_->{'known_information'}{'color'}{$color}B= $_->{'known_information'}{'number'}{$number}
ABresult
0X0
100
111
591 836 } @{ $self->{hands}[$player] };
592 }
593 else {
594 4180 ▶ $count += grep { $_->{color} eq $color && $_->{number} == $number }
Condition100%3/3
$_->{'color'} eq $color && $_->{'number'} == $number
!ll&&!rl&&r
355953190
MC/DC100%2/2
$_->{'color'} eq $color && $_->{'number'} == $number
$_->{'color'} eq $color $_->{'number'} == $number
Truth table100%3/3
A && B
A= $_->{'color'} eq $colorB= $_->{'number'} == $number
ABresult
0X0
100
111
595 836 @{ $self->{hands}[$p] };
596 }
597 }
598 836 $revealed_cache{$player}{ $card->{color} }{ $card->{number} } = $count;
599 836 return $count;
600 }
601  
602 0 ▶sub is_card_known {
Subroutine is_card_known: not called CC=3 SCAR=24.8
Pod documented
603 0 my $self = shift;
604 0 my ($card) = @_;
605 return $card
606 if $card->{known_information}{score}
607 0 ▶ && $card->{known_information}{score} == 20;
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"known_information"}{"score"} and $card->{"known_information"}{"score"} == 20 0 0 0
Condition0%0/3
$card->{"known_information"}{"score"} and $card->{"known_information"}{"score"} == 20
!ll&&!rl&&r
000
MC/DC0%0/2
$card->{"known_information"}{"score"} and $card->{"known_information"}{"score"} == 20
$card->{"known_information"}{"score"} $card->{"known_information"}{"score"} == 20
Truth table0%0/3
A and B
A= $card->{"known_information"}{"score"}B= $card->{"known_information"}{"score"} == 20
ABresult
0X0
100
111
608 0 return;
609 }
610  
611 130 ▶sub is_color_known {
Subroutine is_color_known: called CC=3 SCAR=11.0
Pod undocumented
612 130 my $self = shift;
613 130 my ($card) = @_;
614 return $card->{color}
615 if $card->{known_information}{color_score}
616 130 ▶ && $card->{known_information}{color_score} == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"known_information"}{"color_score"} and $card->{"known_information"}{"color_score"} == 10 24 106 130
Condition100%3/3
$card->{"known_information"}{"color_score"} and $card->{"known_information"}{"color_score"} == 10
!ll&&!rl&&r
545224
MC/DC100%2/2
$card->{"known_information"}{"color_score"} and $card->{"known_information"}{"color_score"} == 10
$card->{"known_information"}{"color_score"} $card->{"known_information"}{"color_score"} == 10
Truth table100%3/3
A and B
A= $card->{"known_information"}{"color_score"}B= $card->{"known_information"}{"color_score"} == 10
ABresult
0X0
100
111
617 106 return;
618 }
619  
620 130 ▶sub is_number_known {
Subroutine is_number_known: called CC=3 SCAR=11.0
Pod documented
621 130 my $self = shift;
622 130 my ($card) = @_;
623 return $card->{number}
624 if $card->{known_information}{number_score}
625 130 ▶ && $card->{known_information}{number_score} == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"known_information"}{"number_score"} and $card->{"known_information"}{"number_score"} == 10 27 103 130
Condition100%3/3
$card->{"known_information"}{"number_score"} and $card->{"known_information"}{"number_score"} == 10
!ll&&!rl&&r
634027
MC/DC100%2/2
$card->{"known_information"}{"number_score"} and $card->{"known_information"}{"number_score"} == 10
$card->{"known_information"}{"number_score"} $card->{"known_information"}{"number_score"} == 10
Truth table100%3/3
A and B
A= $card->{"known_information"}{"number_score"}B= $card->{"known_information"}{"number_score"} == 10
ABresult
0X0
100
111
626 103 return;
627 }
628  
629 0 ▶sub print_game_state {
Subroutine print_game_state: not called CC=6 SCAR=37.4
Pod documented
630 0 my $self = shift;
631  
632 # Print the piles
633 0 print "-----------------------------------------\n";
634 0 print "Board: ";
635 0 for my $color ( keys %{ $self->{top_card} } ) {
636 0 print $color . $self->{top_card}{$color} . " ";
637 }
638 0 print "\n";
639  
640 # Print the hands
641 0 for my $i ( 0 .. $self->{players} - 1 ) {
642 0 print "Player $i: ";
643 0 for my $card ( @{ $self->{hands}[$i] } ) {
644 0 print $card->{color} . $card->{number} . ' ';
645 }
646 0 print "\n";
647 }
648 0 print "\n";
649  
650 # Print the discard pile
651 0 print "Discards: ";
652 0 for my $card ( @{ $self->{discards} } ) {
653 0 print $card->{color} . $card->{number} . ' ';
654 }
655 0 print "\n";
656  
657 # Print the deck size, turns left, and bombs
658 0 print "Turn: " . $self->{turn};
659 0 print "\tHints: " . $self->{hints};
660 0 print "\tScore: " . $self->{score};
661 0 print "\tDeck: " . scalar @{ $self->{deck} };
662 0 print "\tBombs: " . $self->{bombs};
663 0 ▶ print "\tTurns Left: " . $self->{countdown} if defined $self->{countdown};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"countdown"} 0 0 0
664 0 print "\n\n";
665 0 return;
666 }
667  
668 1 1;
669  
670 __END__
↑ Summary