Set.pm

filter: Statementstmt 89.2% 50 / 56 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 89.2% 75 / 84 CC 8 SCAR 10.3
CC
8
Cov
88%
shuffle
6.0
set
5.0
find_sets
3.0
CRAP 2.8
↑ Summary Set/Card.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Games::Set;
3 1 ▶use Games::Set::Card;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Algorithm::ChooseSubsets;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Algorithm::GenerateSequence;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Class::Accessor::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 __PACKAGE__->mk_accessors(qw( deck ));
10  
11 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $VERSION = '0.01';
13  
14 =head1 NAME
15  
16 Games::Set - The rules for the game of Set
17  
18 =head1 SYNOPSIS
19  
20 my $game = Games::Set->new({ deck => [ Games::Set->standard_deck ] });
21 $game->shuffle;
22 my @cards = map { $game->deal } 1..3; # give me 3 cards
23 print $game->set( @cards ) ? "set\n" : "no set\n";
24  
25 =head1 DESCRIPTION
26  
27 Games::Set understands some of the rules of the card game Set. It
28 also knows how to generate a standard deck.
29  
30 =head1 METHODS
31  
32 =head2 new
33  
34 Class::Accessor inherited constructor, returns a new gamestate
35  
36 =head2 deck
37  
38 The current deck. A reference to an array containing many
39 Games::Set::Card objects.
40  
41 =head2 shuffle
42  
43 Shuffle the current deck.
44  
45 =cut
46  
47 # the fisher-yates shuffle from perlfaq4
48  
49 0 ▶sub shuffle {
Subroutine shuffle: not called CC=2 SCAR=17.9
Pod documented
50 0 my $self = shift;
51 0 my $deck = $self->deck;
52  
53 0 my $i = @$deck;
54 0 ▶ while ($i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i--) 0 0 0
55 0 my $j = int rand ($i+1);
56 0 @$deck[$i,$j] = @$deck[$j,$i];
57 }
58 }
59  
60 =head2 deal
61  
62 Take a card from the top of the deck.
63  
64 =cut
65  
66 2 ▶sub deal {
Subroutine deal: called CC=1 SCAR=0.0
Pod documented
67 2 my $self = shift;
68 2 shift @{ $self->deck };
69 }
70  
71 =head2 standard_deck
72  
73 Calculates the standard deck as a list of Games::Set::Card objects.
74  
75 =cut
76  
77 1 ▶sub standard_deck {
Subroutine standard_deck: called CC=1 SCAR=0.0
Pod documented
78 1 my $self = shift;
79 1 my $iter = Algorithm::GenerateSequence->new(
80 values %Games::Set::Card::properties
81 );
82 map {
83 1 my %h; @h{ keys %Games::Set::Card::properties } = @$_;
84 81 Games::Set::Card->new(\%h)
85 } $iter->as_list;
86 }
87  
88  
89 =head2 set( @cards )
90  
91 Returns true if the cards make a set.
92  
93 =cut
94  
95 85323 ▶sub set {
Subroutine set: called CC=5 SCAR=16.1
Pod documented
96 85323 my $self = shift;
97 85323 for my $property ( keys %Games::Set::Card::properties ) {
98 126479 my %seen;
99 126479 $seen{ $_->$property() }++ for @_;
100 126479 ▶ next if (keys %seen) == 1; # all same
Branch: 1 branch
BranchTrueFalseTotal
if keys %seen == 1 13077 113402 126479
101 113402 ▶ next if (max values %seen) == 1; # all different
Branch: 1 branch
BranchTrueFalseTotal
if &max(values %seen) == 1 29161 84241 113402
102 84241 return;
103 }
104 1082 return 1;
105 }
106  
107 =head2 find_sets( @cards )
108  
109 returns all the possible sets within @cards as array references
110  
111 =cut
112  
113 1 ▶sub find_sets {
Subroutine find_sets: called CC=3 SCAR=11.0
Pod documented
114 1 my $self = shift;
115  
116 1 my @found;
117 1 my $iter = Algorithm::ChooseSubsets->new( \@_, 3 );
118 1 ▶ while (my $set = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $set = $iter->next) 85320 1 85321
119 85320 ▶ push @found, $set if $self->set(@$set);
Branch: 1 branch
BranchTrueFalseTotal
if $self->set(@$set) 1080 84240 85320
120 }
121 1 return @found;
122 }
123  
124  
125 1 1;
126 __END__
↑ Summary Set/Card.pm »