robertw has asked for the wisdom of the Perl Monks concerning the following question:

Dear perl monks, I have this module which has a sub that calculates the score of the poker hand (ScoreHand) it is in this link http://search.cpan.org/~pip/Games-Cards-Poker-1.2.565CHh5/Poker.pm, the synopsis goes like this:

use Games::Cards::Poker; # Deal Four (4) players hands and score them... my $players = 4; # number of players to get hands dealt my $hand_size = 5; # number of cards to deal to each player my @hands = ();# player hand data my @deck = Shuffle(Deck()); while($players--) { push(@{$hands[$players]}, pop(@deck)) foreach(1..$hand_size); printf("Player$players score:%4d hand:@{$hands[$players]}\n", ScoreHand(@{$hands[$players]})); }

what i want to know is how to use scorehand in this way:

#!/usr/bin/perl @userinput = <STDIN>; #What i will type e.g.: As,Qc,Td,6c,3h $score = ScoreHand(@userinput);

so far that only gives me the top score for everything I do, I don't fully get what exact form (scalar, hash , array) ScoreHand desires neither if it is supposed to be in string form. I also have the ScoreHand sub code:

sub ScoreHand { # returns the score of the passed in @hand or ShortHan +d my @hand = @_; return(7462) unless(@hand == 1 || @hand == 5); my $sh +rt; my $aflg = 0; $aflg = 1 if(ref($hand[0]) eq 'ARRAY'); my $aref; if( $aflg ) { $aref = $hand[0]; } else { $aref = \@hand; } if(@{$aref} == 1) { $shrt = $aref->[0]; } else { $shrt = ShortHand($aref); } if( $slow ) { return(SlowScoreHand($shrt)); } else { return( $zdnh{$shrt}); } }
Thank you so much in advance, I will of course be willing to supply much more information if that is necessary. Rob

Replies are listed 'Best First'.
Re: Poker module question
by jwkrahn (Abbot) on Jul 14, 2012 at 04:27 UTC
    sub ScoreHand { # returns the score of the passed in @hand or ShortHan +d my @hand = @_; return(7462) unless(@hand == 1 || @hand == 5); my $sh +rt; my $aflg = 0; $aflg = 1 if(ref($hand[0]) eq 'ARRAY'); my $aref; if( $aflg ) { $aref = $hand[0]; } else { $aref = \@hand; } if(@{$aref} == 1) { $shrt = $aref->[0]; } else { $shrt = ShortHand($aref); } if( $slow ) { return(SlowScoreHand($shrt)); } else { return( $zdnh{$shrt}); } }

    IMHO that would look better as:

    sub ScoreHand { # returns the score of the passed in @hand or ShortHan +d return 7462 unless @_ == 1 || @_ == 5; my @hand = @_; my $aref = ref $hand[ 0 ] eq 'ARRAY' ? $hand[ 0 ] : \@hand; my $shrt = @$aref == 1 ? $aref->[ 0 ] : ShortHand( $aref ); return $slow ? SlowScoreHand( $shrt ) : $zdnh{ $shrt }; }
Re: Poker module question
by frozenwithjoy (Priest) on Jul 14, 2012 at 04:23 UTC
    It seems that you need to pass ScoreHand() an array of your hand. The following worked for me (assuming the correct score of your sample hand is 6405):
    my $userinput = <STDIN>; #As,Qc,Td,6c,3h chomp $userinput; my @hand = split /,/, $userinput; my $score = ScoreHand(@hand); say $score; #6405

    edit: you can also pass it what the module refers to as a ShortHand. This seems to be a string containing all the cards in your hand, but appears to give errors very easily.