Skip to content

Commit

Permalink
detect full house
Browse files Browse the repository at this point in the history
  • Loading branch information
ericherman committed Oct 23, 2011
1 parent 8ac72e6 commit c65b7e1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 16 deletions.
60 changes: 50 additions & 10 deletions Hand.pm
Expand Up @@ -155,22 +155,23 @@ sub _straight_flush {
}

sub _n_of_a_kind {
my ( $self, $n ) = @_;
my ( $self, $n, $cards ) = @_;

my $cards = $self->sorted_cards();
my $end = $self->num_cards() - $n;
$cards ||= $self->sorted_cards();
my $num_cards = scalar @$cards;
my $end = $num_cards - $n;

for ( my $start = 0 ; $start < $end ; $start++ ) {
my @hand;
push @hand, $cards->[$start];
for ( my $i = $start + 1 ; $i < $self->num_cards() ; $i++ ) {
for ( my $i = $start + 1 ; $i < $num_cards ; $i++ ) {
my $ca = $cards->[$i];
if ( $cards->[$start]->rank() == $ca->rank() ) {
push @hand, $ca;
}
}
if ( ( scalar @hand ) >= $n ) {
for ( my $i = 0 ; $i < $self->num_cards() ; $i++ ) {
for ( my $i = 0 ; $i < $num_cards ; $i++ ) {
my $card = $cards->[$i];
if ( $card->rank() != $cards->[$start]->rank() ) {
push @hand, $card;
Expand All @@ -179,7 +180,7 @@ sub _n_of_a_kind {
cards => \@hand,
};
if ( ( scalar @hand == 5 )
or ( $i == $self->num_cards() - 1 ) )
or ( $i == $num_cards - 1 ) )
{
return $best;
}
Expand All @@ -190,6 +191,44 @@ sub _n_of_a_kind {
return undef;
}

sub _full_house {
my ($self) = @_;
if ( $self->num_cards() < 5 ) {
return undef;
}
my $cards = $self->sorted_cards();
my $end = $self->num_cards() - 5 + 1;
my $best = $self->_n_of_a_kind( 3, $cards );
if ( not defined $best ) {
return undef;
}
my @hand = @{ $best->{cards} }[ 0 .. 2 ];

# print join(', ', map { $_->two_char() } @hand ), "\n";

my @remainder;
foreach my $card (@$cards) {
if ( ( $card->compare_to( $hand[0] ) )
and ( $card->compare_to( $hand[1] ) )
and ( $card->compare_to( $hand[2] ) ) )
{

# this card is not in the @hand
push @remainder, $card;
}
}
$best = $self->_n_of_a_kind( 2, \@remainder );
if ( not defined $best ) {
return undef;
}
push @hand, @{ $best->{cards} }[ 0 .. 1 ];
$best = {
name => 'full house',
cards => \@hand,
};
return $best;
}

sub best_hand {
my ($self) = @_;
my $best;
Expand All @@ -202,16 +241,17 @@ sub best_hand {
return $best if $best;

#full house
#TODO FULL HOUSE

#straight
$best = $self->_straight();
$best = $self->_full_house();
return $best if $best;

#flush
$best = $self->_flush();
return $best if $best;

#straight
$best = $self->_straight();
return $best if $best;

#three of a kind
$best = $self->_n_of_a_kind(3);
return $best if $best;
Expand Down
31 changes: 25 additions & 6 deletions hand_test.pl
Expand Up @@ -13,16 +13,27 @@
my $ace_of_hearts = Card->new( 1, 'h' );

my $four_of_hearts = Card->new( 4, 'h' );
my $three_of_clubs = Card->new( 3, 'c' );
my $nine_of_spades = Card->new( 9, 's' );
my $five_of_spades = Card->new( 5, 's' );
my $ace_of_diamond = Card->new( 1, 'd' );
my $nine_of_clubs = Card->new( 9, 'c' );

sub expect_name {
my $hand_name = shift;
my @cards = @_;
my $hand = Hand->new(@cards);
my $best = $hand->best_hand();
my $data = Dumper( { hand => $hand, best => $best, } );
if ( $best->{name} ne $hand_name ) {
my $data = Dumper( { hand => $hand, best => $best, } );
die $data . ' not ' . $hand_name;
}

if (0) {
my $cards = join ", ",
map { ($_) ? $_->two_char() : () } @{ $best->{cards} };
print "Got: $cards ($hand_name)\n";
}
}

my @cards = ( $two_of_hearts, $ace_of_spades );
Expand All @@ -37,9 +48,17 @@ sub expect_name {
push @cards, $four_of_hearts;
expect_name( 'flush', @cards );

my $nine_of_spades = Card->new( 9, 's' );
my @cards = ( $two_of_hearts, $ace_of_spades );
push @cards, $ace_of_clubs, $nine_of_hearts, $six_of_hearts;
push @cards, $ace_of_hearts;
push @cards, $nine_of_spades;
push @cards, $ace_of_diamond;
expect_name( '4 of a kind', @cards );

@cards = (
$four_of_hearts, $two_of_hearts, $three_of_clubs,
$six_of_hearts, $five_of_spades
);
expect_name( 'straight', @cards );

my @cards = (
$two_of_hearts, $ace_of_spades, $nine_of_clubs, $nine_of_hearts,
$six_of_hearts, $ace_of_hearts, $nine_of_spades
);
expect_name( 'full house', @cards );

0 comments on commit c65b7e1

Please sign in to comment.