| 1 | package Game::Mahjong::Meld; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Object::Proto::Sugar -types; | ||
| 8 | |||
| 9 | use Game::Mahjong::Tiles; | ||
| 10 | use Game::Mahjong::Notation; | ||
| 11 | |||
| 12 | our $VERSION = '0.04'; | ||
| 13 | |||
| 14 | our %KIND; | ||
| 15 | BEGIN { %KIND = (chow => 3, pung => 3, kong => 4) } | ||
| 16 | |||
| 17 | has kind => (is => 'ro', isa => Str, required => 1); | ||
| 18 | |||
| 19 | has tiles => (is => 'ro', isa => ArrayRef, required => 1); | ||
| 20 | |||
| 21 | has concealed => (is => 'ro', default => 0); | ||
| 22 | |||
| 23 | has claimed_from => (is => 'ro'); | ||
| 24 | |||
| 25 | has claimed_tile => (is => 'ro'); | ||
| 26 | |||
| 27 | has promoted => (is => 'ro', default => 0); | ||
| 28 | |||
| 29 | sub BUILD { | ||
| 30 | my ($self) = @_; | ||
| 31 | my $kind = $self->kind; | ||
| 32 | die "Game::Mahjong::Meld: no such kind '$kind'" unless $KIND{$kind}; | ||
| 33 | |||
| 34 | my @tiles = sort { $a <=> $b } @{ $self->tiles }; | ||
| 35 | die "Game::Mahjong::Meld: a $kind is $KIND{$kind} tiles, not " . scalar @tiles | ||
| 36 | unless @tiles == $KIND{$kind}; | ||
| 37 | Game::Mahjong::Tiles::code_of($_) for @tiles; | ||
| 38 | die 'Game::Mahjong::Meld: a bonus tile is never in a meld' | ||
| 39 | if grep { Game::Mahjong::Tiles::is_bonus($_) } @tiles; | ||
| 40 | |||
| 41 | if ($kind eq 'chow') { | ||
| 42 | my $suit = Game::Mahjong::Tiles::suit_of($tiles[0]); | ||
| 43 | die 'Game::Mahjong::Meld: a chow is three consecutive tiles of one suit' | ||
| 44 | unless defined $suit | ||
| 45 | && $tiles[1] == $tiles[0] + 1 && $tiles[2] == $tiles[1] + 1 | ||
| 46 | && Game::Mahjong::Tiles::rank_of($tiles[0]) <= 7; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | die "Game::Mahjong::Meld: a $kind is identical tiles" | ||
| 50 | if grep { $_ != $tiles[0] } @tiles; | ||
| 51 | } | ||
| 52 | |||
| 53 | die 'Game::Mahjong::Meld: a claimed meld names the tile it took' | ||
| 54 | if defined $self->claimed_from && !defined $self->claimed_tile; | ||
| 55 | die 'Game::Mahjong::Meld: the claimed tile is not in the meld' | ||
| 56 | if defined $self->claimed_tile && !grep { $_ == $self->claimed_tile } @tiles; | ||
| 57 | die 'Game::Mahjong::Meld: a concealed meld was claimed from nobody' | ||
| 58 | if $self->concealed && defined $self->claimed_from; | ||
| 59 | die 'Game::Mahjong::Meld: only a kong is promoted' | ||
| 60 | if $self->promoted && $kind ne 'kong'; | ||
| 61 | |||
| 62 | @{ $self->tiles } = @tiles; | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub is_chow { return $_[0]->kind eq 'chow' ? 1 : 0 } | ||
| 67 | |||
| 68 | sub is_pung { return $_[0]->kind eq 'chow' ? 0 : 1 } | ||
| 69 | |||
| 70 | sub is_kong { return $_[0]->kind eq 'kong' ? 1 : 0 } | ||
| 71 | |||
| 72 | sub size { return scalar @{ $_[0]->tiles } } | ||
| 73 | |||
| 74 | sub counts_as { return 3 } | ||
| 75 | |||
| 76 | sub tile { return $_[0]->tiles->[0] } | ||
| 77 | |||
| 78 | sub suit { return Game::Mahjong::Tiles::suit_of($_[0]->tiles->[0]) } | ||
| 79 | |||
| 80 | sub rank { return Game::Mahjong::Tiles::rank_of($_[0]->tiles->[0]) } | ||
| 81 | |||
| 82 | sub is_honour { return Game::Mahjong::Tiles::is_honour($_[0]->tiles->[0]) } | ||
| 83 | |||
| 84 | sub has_terminal { | ||
| 85 | my ($self) = @_; | ||
| 86 | return (grep { Game::Mahjong::Tiles::is_terminal($_) } @{ $self->tiles }) ? 1 : 0; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub is_outside { | ||
| 90 | my ($self) = @_; | ||
| 91 | return $self->is_honour || $self->has_terminal ? 1 : 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub is_exposed { | ||
| 95 | my ($self) = @_; | ||
| 96 | return $self->concealed ? 0 : 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub promote { | ||
| 100 | my ($self) = @_; | ||
| 101 | die 'Game::Mahjong::Meld: only an exposed pung is promoted to a kong' | ||
| 102 | unless $self->kind eq 'pung' && !$self->concealed; | ||
| 103 | return ref($self)->new( | ||
| 104 | kind => 'kong', | ||
| 105 | tiles => [ @{ $self->tiles }, $self->tile ], | ||
| 106 | claimed_from => $self->claimed_from, | ||
| 107 | claimed_tile => $self->claimed_tile, | ||
| 108 | promoted => 1, | ||
| 109 | ); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub equals { | ||
| 113 | my ($self, $other) = @_; | ||
| 114 | return 0 unless ref $other && $other->can('kind'); | ||
| 115 | return $self->kind eq $other->kind | ||
| 116 | && $self->concealed == $other->concealed | ||
| 117 | && join(',', @{ $self->tiles }) eq join(',', @{ $other->tiles }) ? 1 : 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub to_hash { | ||
| 121 | my ($self) = @_; | ||
| 122 | return { | ||
| 123 | kind => $self->kind, | ||
| 124 | tiles => [ @{ $self->tiles } ], | ||
| 125 | concealed => $self->concealed ? 1 : 0, | ||
| 126 | }; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub to_notation { | ||
| 130 | my ($self) = @_; | ||
| 131 | return Game::Mahjong::Notation::print_meld($self->to_hash); | ||
| 132 | } | ||
| 133 | |||
| 134 | 1; | ||
| 135 | |||
| 136 | __END__ |