| 1 | package Game::Mahjong::Wall; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 34 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use Digest::SHA (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 34 | ▶ | use Game::Mahjong::Tiles; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 34 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 34 | ▶ | use constant DEALT => 53; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 34 | has seed => (is => 'ro', isa => Str, required => 1); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 34 | has hand => (is => 'ro', isa => Int, default => 1); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 34 | has tiles => (is => 'rw', isa => ArrayRef); | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 34 | has drawn => (is => 'rw', isa => Int, default => 0); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 34 | has replaced => (is => 'rw', isa => Int, default => 0); | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 34 | has dealt => (is => 'rw', isa => Int, default => 0); | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 362 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 362 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 362 | ▶ | die 'Game::Mahjong::Wall: a seed is a non-empty string' unless length $self->seed; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 361 | ▶ | die 'Game::Mahjong::Wall: a hand is 1 or more' unless $self->hand >= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 360 | ▶ | if (my $given = $self->tiles) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 46 | my %count; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 46 | for my $kind (@$given) { | |||||||||||||||||||||||||||||||||||||||||
| 36 | 280 | Game::Mahjong::Tiles::code_of($kind); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 279 | ▶ | my $cap = Game::Mahjong::Tiles::is_bonus($kind) ? 1 : Game::Mahjong::Tiles::PER_KIND; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | die 'Game::Mahjong::Wall: a written wall holds more of ' | ||||||||||||||||||||||||||||||||||||||||||
| 39 | . Game::Mahjong::Tiles::code_of($kind) . ' than the set does' | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 279 | ▶ | if ++$count{$kind} > $cap; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 43 | $self->tiles([@$given]); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 43 | return; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 314 | my @set = Game::Mahjong::Tiles::set(); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 314 | $self->tiles([ map { $set[ $_ - 1 ] } order_for($self->seed, $self->hand) ]); | |||||||||||||||||||||||||||||||||||||||||
| 48 | 314 | return; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 321 | ▶ | sub order_for { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
order_for: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 321 | my ($seed, $hand) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 321 | ▶ | die 'Game::Mahjong::Wall: order_for needs a seed and a hand number' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $seed and length $seed and defined $hand
defined $seed and length $seed
MC/DC33%1/3
defined $seed and length $seed and defined $hand
defined $seed
length $seed
defined $hand
Truth table50%2/4
A and B and C
A= defined $seedB= length $seedC= defined $hand
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | unless defined $seed && length $seed && defined $hand; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 320 | my ($counter, @words) = (0); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 45760 | ▶ | my $next = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 45760 | ▶ | unless (@words) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 5760 | my $digest = Digest::SHA::sha256($seed . "hand:$hand:" . $counter++); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 5760 | @words = unpack 'N8', $digest; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 45760 | return shift @words; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 320 | }; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 320 | my @order = (1 .. Game::Mahjong::Tiles::TILES); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 320 | ▶ | for (my $i = $#order; $i > 0; $i--) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 45760 | my $n = $i + 1; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 45760 | my $limit = int(4294967296 / $n) * $n; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 45760 | my $word; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 45760 | ▶ | do { $word = $next->() } while $word >= $limit; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 45760 | my $j = $word % $n; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 45760 | @order[$i, $j] = @order[$j, $i]; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 320 | return @order; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 319 | ▶ | sub deal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
deal: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 319 | my ($self, $dealer) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 319 | ▶ | die 'Game::Mahjong::Wall: the dealer is a seat 0 to 3' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $dealer and $dealer =~ /\A[0-3]\z/
MC/DC0%0/2
defined $dealer and $dealer =~ /\A[0-3]\z/
defined $dealer
$dealer =~ /\A[0-3]\z/
Truth table33%1/3
A and B
A= defined $dealerB= $dealer =~ /\A[0-3]\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | unless defined $dealer && $dealer =~ /\A[0-3]\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 319 | ▶ | die 'Game::Mahjong::Wall: this wall has been dealt' if $self->dealt; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 315 | ▶ | die 'Game::Mahjong::Wall: a wall of ' . $self->remaining . ' cannot deal ' . DEALT | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | if $self->remaining < DEALT; | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 314 | my @seats = map { ($dealer + $_) % 4 } 0 .. 3; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 314 | my %hands = map { $_ => [] } 0 .. 3; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 314 | my $tiles = $self->tiles; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 314 | for my $round (1 .. 3) { | |||||||||||||||||||||||||||||||||||||||||
| 90 | 942 | for my $seat (@seats) { | |||||||||||||||||||||||||||||||||||||||||
| 91 | 3768 | push @{ $hands{$seat} }, splice @$tiles, 0, 4; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 314 | for my $seat (@seats) { | |||||||||||||||||||||||||||||||||||||||||
| 95 | 1256 | push @{ $hands{$seat} }, shift @$tiles; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 314 | push @{ $hands{$dealer} }, shift @$tiles; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 314 | @{ $hands{$_} } = sort { $a <=> $b } @{ $hands{$_} } for keys %hands; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 314 | $self->dealt(1); | |||||||||||||||||||||||||||||||||||||||||
| 102 | 314 | return { hands => \%hands, dealer => $dealer }; | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 14359 | ▶ | sub draw { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 14359 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | die 'Game::Mahjong::Wall: the wall is empty, so there is nothing to draw' | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 14359 | ▶ | unless @{ $self->tiles }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 14358 | $self->drawn($self->drawn + 1); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 14358 | return shift @{ $self->tiles }; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | 1881 | ▶ | sub replace { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 1881 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 115 | die 'Game::Mahjong::Wall: the wall is empty, so there is no replacement' | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 1881 | ▶ | unless @{ $self->tiles }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1880 | $self->replaced($self->replaced + 1); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 1880 | return pop @{ $self->tiles }; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 18528 | ▶ | sub remaining { return scalar @{ $_[0]->tiles } } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
remaining: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 31568 | ▶ | sub is_empty { return @{ $_[0]->tiles } ? 0 : 1 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_empty: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 4228 | ▶ | sub peek { return [ @{ $_[0]->tiles } ] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
peek: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||||||||||||||||||||||