| 1 | package Game::Dominoes::Hand; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 0 | ▶ | use Object::Proto::Sugar -types; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | |||
| 8 | our $VERSION = '0.04'; | ||
| 9 | |||
| 10 | has tiles => ( | ||
| 11 | is => 'rw', | ||
| 12 | isa => ArrayRef, | ||
| 13 | default => [] | ||
| 14 | ); | ||
| 15 | |||
| 16 | sub count { | ||
| 17 | return scalar @{ $_[0]->tiles }; | ||
| 18 | } | ||
| 19 | |||
| 20 | sub is_empty { | ||
| 21 | return $_[0]->count ? 0 : 1; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub pips { | ||
| 25 | my ($self) = @_; | ||
| 26 | my $total = 0; | ||
| 27 | $total += $_->pips for @{ $self->tiles }; | ||
| 28 | return $total; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub add { | ||
| 32 | my ($self, @tiles) = @_; | ||
| 33 | push @{ $self->tiles }, grep { defined } @tiles; | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub holds { | ||
| 38 | my ($self, $tile) = @_; | ||
| 39 | return 0 unless ref $tile; | ||
| 40 | return (grep { $_->id == $tile->id } @{ $self->tiles }) ? 1 : 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub remove { | ||
| 44 | my ($self, $tile) = @_; | ||
| 45 | return undef unless ref $tile; | ||
| 46 | my $tiles = $self->tiles; | ||
| 47 | for my $i (0 .. $#$tiles) { | ||
| 48 | next unless $tiles->[$i]->id == $tile->id; | ||
| 49 | return splice @$tiles, $i, 1; | ||
| 50 | } | ||
| 51 | return undef; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub matching { | ||
| 55 | my ($self, @faces) = @_; | ||
| 56 | my %want = map { $_ => 1 } grep { defined } @faces; | ||
| 57 | return [ grep { $want{ $_->high } || $want{ $_->low } } @{ $self->tiles } ]; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub sorted { | ||
| 61 | my ($self) = @_; | ||
| 62 | return [ sort { $a->id <=> $b->id } @{ $self->tiles } ]; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub clone { | ||
| 66 | my ($self) = @_; | ||
| 67 | return ref($self)->new(tiles => [ @{ $self->tiles } ]); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub stringify { | ||
| 71 | my ($self) = @_; | ||
| 72 | return join ' ', map { $_->stringify } @{ $self->sorted }; | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | __END__ | ||