| 1 | package Ordeal::Model::Shuffle; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # vim: ts=3 sts=3 sw=3 et ai : | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 5 | ▶ | use 5.020; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 5 | ▶ | use strict; # redundant, but still useful to document | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 5 | { our $VERSION = '0.003'; } | |||||||||||||||||||
| 9 | 5 | ▶ | use English qw< -no_match_vars >; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ▶ | use Mo qw< build default >; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 5 | ▶ | use Ouch; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 5 | ▶ | use experimental qw< signatures postderef >; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 5 | ▶ | no warnings qw< experimental::signatures experimental::postderef >; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 5 | has auto_reshuffle => (default => 0); | |||||||||||||||||||
| 17 | 5 | has deck => (default => undef); | |||||||||||||||||||
| 18 | 5 | has default_n_draw => (default => undef); | |||||||||||||||||||
| 19 | 5 | has random_source => (default => undef); | |||||||||||||||||||
| 20 | 5 | has _draw_sorted => (default => 0); | |||||||||||||||||||
| 21 | 5 | has _i => (default => undef); | |||||||||||||||||||
| 22 | 5 | has _indexes => (default => undef); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | ▶ | sub BUILD ($self) { | |||||||||||||||||||
|
Subroutine
BUILD: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 25 | 5 | ▶ | ouch 400, 'no deck defined' unless $self->deck; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | 5 | ▶ | if (! $self->random_source) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 1 | require Ordeal::Model::ChaCha20; | |||||||||||||||||||
| 28 | 1 | $self->random_source(Ordeal::Model::ChaCha20->new); | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | 5 | ▶ | $self->default_n_draw($self->deck->n_cards) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | unless defined $self->default_n_draw; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 5 | $self->shuffle; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 5 | return $self; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | ▶ | sub clone ($self, %args) { | |||||||||||||||||||
|
Subroutine
clone: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 39 | 0 | my $other = ref($self)->new( | |||||||||||||||||||
| 40 | auto_reshuffle => $self->auto_reshuffle, # overridable | ||||||||||||||||||||
| 41 | default_n_draw => $self->default_n_draw, # overridable | ||||||||||||||||||||
| 42 | %args, | ||||||||||||||||||||
| 43 | deck => $self->deck, # this can't be overridden | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | $other->random_source($self->random_source->clone) | ||||||||||||||||||||
| 46 | 0 | ▶ | unless exists $args{random_source}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 0 | $other->_i($self->_i); | |||||||||||||||||||
| 48 | 0 | ▶ | if (my $indexes = $self->_indexes) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | $other->_indexes([$indexes->@*]); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | else { | ||||||||||||||||||||
| 52 | 0 | $other->_indexes(undef); | |||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | 0 | return $other; | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 5 | ▶ | sub draw ($self, $n = undef) { | ||||||||||||||||||
|
Subroutine
draw: called CC=11 SCAR=24.4
Pod
documented
| |||||||||||||||||||||
| 58 | 5 | ▶ | $n //= $self->default_n_draw; | ||||||||||||||||||
|
Condition66%2/3
$n //= $self->default_n_draw
MC/DC0%0/2
$n //= $self->default_n_draw
$n
$self->default_n_draw
Truth table66%2/3
A //= B
A= $nB= $self->default_n_draw
| |||||||||||||||||||||
| 59 | 5 | ▶ | ouch 400, 'invalid number of cards', $n | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | unless $n =~ m{\A(?: 0 | [1-9]\d*)\z}mxs; | ||||||||||||||||||||
| 61 | 5 | my $deck = $self->deck; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 5 | my $i = $self->_i; | |||||||||||||||||||
| 64 | 5 | ▶ | $n = $i + 1 if $n == 0; # take them all | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 5 | ▶ | ouch 400, 'not enough cards left', $n, $i + 1 | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | if $n > $i + 1; | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 5 | my @retval; | |||||||||||||||||||
| 69 | 5 | ▶ | if (my $indexes = $self->_indexes) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 3 | my $rs = $self->random_source; | |||||||||||||||||||
| 71 | 3 | ▶ | while ($n-- > 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 13 | my $j = $rs->int_rand(0, $i); # extremes included | |||||||||||||||||||
| 73 | 13 | (my $retval, $indexes->[$j]) = $indexes->@[$j, $i--]; | |||||||||||||||||||
| 74 | 13 | push @retval, $deck->card_at($retval); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||
| 78 | 2 | my $top_index = $deck->n_cards - 1; | |||||||||||||||||||
| 79 | 2 | ▶ | while ($n-- > 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | 10 | push @retval, $deck->card_at($top_index - $i--); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # prepare for next call | ||||||||||||||||||||
| 85 | 5 | ▶ | $self->auto_reshuffle ? $self->shuffle : $self->_i($i); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 5 | ▶ | return $retval[0] if @retval == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | 5 | return @retval; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 0 | ▶ | sub is_sorted ($self) { return !($self->_indexes) } | ||||||||||||||||||
|
Subroutine
is_sorted: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | ▶ | sub n_remaining ($self) { return $self->_i + 1 } | ||||||||||||||||||
|
Subroutine
n_remaining: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | ▶ | sub reset ($self) { | |||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 96 | 0 | $self->random_source->reset; | |||||||||||||||||||
| 97 | 0 | return $self->shuffle; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | ▶ | sub shuffle ($self) { | |||||||||||||||||||
|
Subroutine
shuffle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 101 | 7 | $self->_i(my $i = $self->deck->n_cards - 1); | |||||||||||||||||||
| 102 | 7 | $self->_indexes([0 .. $i]); | |||||||||||||||||||
| 103 | 7 | return $self; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | ▶ | sub sort ($self) { | |||||||||||||||||||
|
Subroutine
sort: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 107 | 4 | $self->_i($self->deck->n_cards - 1); | |||||||||||||||||||
| 108 | 4 | $self->_indexes(undef); | |||||||||||||||||||
| 109 | 4 | return $self; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 5 | 1; | |||||||||||||||||||