| 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
| |||||||||||
| 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
| |||||||||||
| 101 | 113402 | ▶ | next if (max values %seen) == 1; # all different | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 119 | 85320 | ▶ | push @found, $set if $self->set(@$set); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | } | ||||||||||
| 121 | 1 | return @found; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | 1 | 1; | |||||||||
| 126 | __END__ | ||||||||||