Changeset 17422

Show
Ignore:
Timestamp:
08/11/08 17:49:34 (6 years ago)
Author:
daisuke
Message:

implement sorting

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Acme-Mahjong/trunk/lib/Acme/Mahjong/Hand.pm

    r17418 r17422  
    4141} 
    4242 
     43sub sort { 
     44    my $self = shift; 
     45 
     46    my %suit; 
     47    my %dragon; 
     48    my %wind; 
     49 
     50    foreach my $tile (@{ $self->tiles }) { 
     51        if ($tile->isa('Acme::Mahjong::Tile::Suited')) { 
     52            $suit{ $tile->suit } ||= []; 
     53            push @{ $suit{ $tile->suit } }, $tile; 
     54        } elsif ( $tile->isa('Acme::Mahjong::Tile::Dragon')) { 
     55            $dragon{ $tile->suit } ||= []; 
     56            push @{ $dragon{ $tile->label } }, $tile; 
     57        } elsif ( $tile->isa('Acme::Mahjong::Tile::Wind')) { 
     58            $wind{ $tile->suit } ||= []; 
     59            push @{ $wind{ $tile->label } }, $tile; 
     60        } else { 
     61            confess "Don't know what to do with $tile"; 
     62        } 
     63    } 
     64 
     65    return ( 
     66        (map { sort @{ $suit{ $_ } } } keys %suit), 
     67        (map { @{$dragon{$_}} } keys %dragon), 
     68        (map { @{$wind{$_}} } keys %wind), 
     69    ); 
     70} 
     71 
    43721;