| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||
| 2 | # a module for managing decks of cards | ||||||||||||||||||||||||||||
| 3 | package Game::Deckar; | ||||||||||||||||||||||||||||
| 4 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 5 | 3 | ▶ | use 5.26.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Object::Pad 0.66; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub fisher_yates_shuffle { | ||||||||||||||||||||||||||
|
Subroutine
fisher_yates_shuffle: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 1 | my ($deck) = @_; | |||||||||||||||||||||||||||
| 11 | 1 | my $i; | |||||||||||||||||||||||||||
| 12 | 1 | ▶ | for ( $i = @$deck; --$i; ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 6 | my $j = int rand( $i + 1 ); | |||||||||||||||||||||||||||
| 14 | 6 | ▶ | next if $i == $j; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | 5 | @$deck[ $i, $j ] = @$deck[ $j, $i ]; | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | class Game::Deckar::Card { | ||||||||||||||||||||||||||||
| 20 | 8 | ▶ | field $data :param :reader; | ||||||||||||||||||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | field %meta; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | BUILD { | ||||||||||||||||||||||||||||
| 24 | my (%param) = @_; | ||||||||||||||||||||||||||||
| 25 | %meta = $param{meta}->%* if exists $param{meta}; | ||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 7 | ▶ | method meta ($name) { $meta{$name} } | ||||||||||||||||||||||||||
|
Subroutine
meta: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3 | ▶ | method set_meta ( $name, $value ) { | ||||||||||||||||||||||||||
|
Subroutine
set_meta: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 3 | my $old = $meta{$name}; | |||||||||||||||||||||||||||
| 32 | 3 | $meta{$name} = $value; | |||||||||||||||||||||||||||
| 33 | 3 | ▶ | return sub { $self->set_meta( $name, $old ); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | 3 | } | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | class Game::Deckar { | |||||||||||||||||||||||||||
| 38 | 3 | ▶ | use Carp 'croak'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 3 | field %decks; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | BUILD { | ||||||||||||||||||||||||||||
| 42 | my (%param) = @_; | ||||||||||||||||||||||||||||
| 43 | if ( exists $param{decks} ) { | ||||||||||||||||||||||||||||
| 44 | for my $d ( $param{decks}->@* ) { | ||||||||||||||||||||||||||||
| 45 | $decks{$d} = []; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | if ( exists $param{initial} ) { | ||||||||||||||||||||||||||||
| 49 | for my $d ( keys $param{initial}->%* ) { | ||||||||||||||||||||||||||||
| 50 | croak "no such deck $d" unless exists $decks{$d}; | ||||||||||||||||||||||||||||
| 51 | push $decks{$d}->@*, $param{initial}->{$d}->@*; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | if ( exists $param{initial_cards} ) { | ||||||||||||||||||||||||||||
| 55 | for my $d ( keys $param{initial_cards}->%* ) { | ||||||||||||||||||||||||||||
| 56 | push $decks{$d}->@*, map { | ||||||||||||||||||||||||||||
| 57 | Game::Deckar::Card->new( | ||||||||||||||||||||||||||||
| 58 | data => $_, | ||||||||||||||||||||||||||||
| 59 | ( exists $param{meta} ? ( meta => $param{meta} ) : () ) | ||||||||||||||||||||||||||||
| 60 | ) | ||||||||||||||||||||||||||||
| 61 | } $param{initial_cards}->{$d}->@*; | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | method add_deck ($name) { | ||||||||||||||||||||||||||
|
Subroutine
add_deck: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | croak 'deck already exists' if exists $decks{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 1 | $decks{$name} = []; | |||||||||||||||||||||||||||
| 69 | 1 | ▶ | return sub { $self->del_deck($name); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 3 | } | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 3 | ▶ | method del_deck ($name) { | ||||||||||||||||||||||||||
|
Subroutine
del_deck: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | croak 'no such deck' unless exists $decks{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | ▶ | croak 'deck is not empty' if $decks{$name}->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 2 | my $orig = $decks{$name}; | |||||||||||||||||||||||||||
| 76 | 2 | delete $decks{$name}; | |||||||||||||||||||||||||||
| 77 | 2 | ▶ | return sub { $decks{$name} = $orig; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 3 | } | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | method collect ( $name, @rest ) { | ||||||||||||||||||||||||||
|
Subroutine
collect: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | croak 'not enough decks' unless @rest; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 2 | for my $d ( $name, @rest ) { | |||||||||||||||||||||||||||
| 83 | 5 | ▶ | croak "no such deck $d" unless exists $decks{$d}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 1 | my @rcards = [ $decks{$name}, [ $decks{$name}->@* ] ]; | |||||||||||||||||||||||||||
| 86 | 1 | for my $d (@rest) { | |||||||||||||||||||||||||||
| 87 | 2 | ▶ | next if $d eq $name; # so can collect on "get_decks" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 2 | push @rcards, [ $decks{$d}, [ $decks{$d}->@* ] ]; | |||||||||||||||||||||||||||
| 89 | # cards are put onto the "top" of the target deck, which is | ||||||||||||||||||||||||||||
| 90 | # how some humans might do it with real stacks of cards | ||||||||||||||||||||||||||||
| 91 | 2 | unshift $decks{$name}->@*, splice $decks{$d}->@*; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 1 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 94 | 1 | for my $r (@rcards) { $r->[0]->@* = $r->[1]->@* } | |||||||||||||||||||||||||||
| 95 | 1 | }; | |||||||||||||||||||||||||||
| 96 | 3 | } | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 10 | ▶ | method deal ( $src, $dst, $index = 0, $top = 1 ) { | ||||||||||||||||||||||||||
|
Subroutine
deal: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | croak 'no such deck' | ||||||||||||||||||||||||||||
| 100 | 10 | ▶ | unless exists $decks{$src} and exists $decks{$dst}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $decks{$src} and exists $decks{$dst}
MC/DC100%2/2
exists $decks{$src} and exists $decks{$dst}
exists $decks{$src}
exists $decks{$dst}
Truth table100%3/3
A and B
A= exists $decks{$src}B= exists $decks{$dst}
| |||||||||||||||||||||||||||||
| 101 | 8 | my ( $sref, $dref ) = @decks{ $src, $dst }; | |||||||||||||||||||||||||||
| 102 | 8 | ▶ | croak 'index out of range' if $index < 0 or $index > $sref->$#*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$index < 0 or $index > $sref->$#*
MC/DC100%2/2
$index < 0 or $index > $sref->$#*
$index < 0
$index > $sref->$#*
Truth table100%3/3
A or B
A= $index < 0B= $index > $sref->$#*
| |||||||||||||||||||||||||||||
| 103 | 6 | my $card = splice $sref->@*, $index, 1; | |||||||||||||||||||||||||||
| 104 | 6 | ▶ | splice $dref->@*, ( $top ? 0 : $dref->@* ), 0, $card; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 4 | ▶ | return $card, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 4 | ▶ | splice $sref->@*, $index, 0, splice $dref->@*, ( $top ? 0 : $dref->$#* ), 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 6 | }; | |||||||||||||||||||||||||||
| 108 | 3 | } | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 3 | ▶ | method empty ($name) { | ||||||||||||||||||||||||||
|
Subroutine
empty: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 3 | ▶ | croak 'no such deck' unless exists $decks{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | ▶ | croak 'deck is empty' unless $decks{$name}->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | my @orig = $decks{$name}->@*; | |||||||||||||||||||||||||||
| 114 | 1 | $decks{$name}->@* = (); | |||||||||||||||||||||||||||
| 115 | 1 | ▶ | return sub { $decks{$name}->@* = @orig; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 116 | 3 | } | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 9 | ▶ | method get_decks () { | ||||||||||||||||||||||||||
|
Subroutine
get_decks: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 9 | ▶ | croak 'no decks' unless %decks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 8 | return sort keys %decks; | |||||||||||||||||||||||||||
| 121 | 3 | } | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 47 | ▶ | method get ($name) { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 47 | ▶ | croak 'no such deck' unless exists $decks{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 46 | return $decks{$name}; | |||||||||||||||||||||||||||
| 126 | 3 | } | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 7 | ▶ | method move ( $src, $dst, $count, $index = 0, $top = 1 ) { | ||||||||||||||||||||||||||
|
Subroutine
move: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | croak 'no such deck' | ||||||||||||||||||||||||||||
| 130 | 7 | ▶ | unless exists $decks{$src} and exists $decks{$dst}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $decks{$src} and exists $decks{$dst}
MC/DC100%2/2
exists $decks{$src} and exists $decks{$dst}
exists $decks{$src}
exists $decks{$dst}
Truth table100%3/3
A and B
A= exists $decks{$src}B= exists $decks{$dst}
| |||||||||||||||||||||||||||||
| 131 | 5 | my ( $sref, $dref ) = @decks{ $src, $dst }; | |||||||||||||||||||||||||||
| 132 | 5 | ▶ | croak 'index out of range' if $index < 0 or $index > $sref->$#*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$index < 0 or $index > $sref->$#*
MC/DC100%2/2
$index < 0 or $index > $sref->$#*
$index < 0
$index > $sref->$#*
Truth table100%3/3
A or B
A= $index < 0B= $index > $sref->$#*
| |||||||||||||||||||||||||||||
| 133 | 3 | ▶ | croak 'count out of range' if $count < 1 or $index + $count > @$sref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count < 1 or $index + $count > @$sref
MC/DC50%1/2
$count < 1 or $index + $count > @$sref
$count < 1
$index + $count > @$sref
Truth table66%2/3
A or B
A= $count < 1B= $index + $count > @$sref
| |||||||||||||||||||||||||||||
| 134 | 2 | my @cards = splice $sref->@*, $index, $count; | |||||||||||||||||||||||||||
| 135 | 2 | ▶ | splice $dref->@*, ( $top ? 0 : $dref->@* ), 0, @cards; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 2 | ▶ | return \@cards, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 137 | 2 | ▶ | splice $decks{$src}->@*, $index, 0, splice $dref->@*, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | ( $top ? 0 : $dref->@* - $count ), $count; | ||||||||||||||||||||||||||||
| 139 | 2 | }; | |||||||||||||||||||||||||||
| 140 | 3 | } | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 10 | ▶ | method pick ( $src, $dst, $indices, $top = 1 ) { | ||||||||||||||||||||||||||
|
Subroutine
pick: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | croak 'no such deck' | ||||||||||||||||||||||||||||
| 144 | 10 | ▶ | unless exists $decks{$src} and exists $decks{$dst}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $decks{$src} and exists $decks{$dst}
MC/DC100%2/2
exists $decks{$src} and exists $decks{$dst}
exists $decks{$src}
exists $decks{$dst}
Truth table100%3/3
A and B
A= exists $decks{$src}B= exists $decks{$dst}
| |||||||||||||||||||||||||||||
| 145 | 8 | ▶ | croak 'no indices' unless $indices->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 7 | my ( $sref, $dref ) = @decks{ $src, $dst }; | |||||||||||||||||||||||||||
| 147 | 7 | ▶ | croak 'too many indices' if $indices->@* > $sref->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 6 | my ( @icard, %seen ); | |||||||||||||||||||||||||||
| 149 | 6 | for my $index ( $indices->@* ) { | |||||||||||||||||||||||||||
| 150 | 11 | ▶ | croak 'index out of range' if $index < 0 or $index > $sref->$#*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$index < 0 or $index > $sref->$#*
MC/DC100%2/2
$index < 0 or $index > $sref->$#*
$index < 0
$index > $sref->$#*
Truth table100%3/3
A or B
A= $index < 0B= $index > $sref->$#*
| |||||||||||||||||||||||||||||
| 151 | 9 | ▶ | croak 'duplicate index' if $seen{$index}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 8 | push @icard, [$index]; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 3 | for my $r ( sort { $b->[0] <=> $a->[0] } @icard ) { | |||||||||||||||||||||||||||
| 155 | 7 | $r->[1] = splice $sref->@*, $r->[0], 1; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 3 | my @cards = map { $_->[1] } @icard; | |||||||||||||||||||||||||||
| 158 | 3 | ▶ | splice $dref->@*, ( $top ? 0 : $dref->@* ), 0, @cards; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 3 | ▶ | return \@cards, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 160 | 3 | my $len = @icard; | |||||||||||||||||||||||||||
| 161 | 3 | ▶ | splice $dref->@*, ( $top ? 0 : -$len ), $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 3 | for my $r ( sort { $a->[0] <=> $b->[0] } @icard ) { | |||||||||||||||||||||||||||
| 163 | 7 | splice $sref->@*, $r->[0], 1, $r->[1]; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | 3 | }; | |||||||||||||||||||||||||||
| 166 | 3 | } | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 3 | ▶ | method shuffle ($name) { | ||||||||||||||||||||||||||
|
Subroutine
shuffle: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 3 | ▶ | croak 'no such deck' unless exists $decks{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 2 | my $deck = $decks{$name}; | |||||||||||||||||||||||||||
| 171 | 2 | ▶ | croak 'deck is empty' unless $deck->@*; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 1 | my @orig = $deck->@*; | |||||||||||||||||||||||||||
| 173 | 1 | fisher_yates_shuffle( $decks{$name} ); | |||||||||||||||||||||||||||
| 174 | 1 | ▶ | return sub { $deck->@* = @orig; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | 3 | } | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 3 | 1; | |||||||||||||||||||||||||||
| 179 | __END__ | ||||||||||||||||||||||||||||