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