in reply to Keeping track of Pairs

Is this some pseudo-poker or are you trying to simulate a 52 card deck? What are all the rules, or do you only care about two of a kind?

I'd seriously suggest taking a step back and organizing how you want to represent a card, a deck, and some pseudocode (English) description of scoring rules. Also when you process those strings you are really going to be splitting them into an array anyway.. but how come no hashes? They are nice because you can check if a key exists and you can do something like foreach (sort keys %myhash). If this is an assignment then work it out with hashes yourself later okay?

You could give each card a two letter code, like 5H for five of hearts. It would even be faster if you add a letter for which color it is because you could use regular expressions to see what color a card is, to check for flushes and straights.

This solves the question you asked.

#!/usr/bin/perl -w use strict; my $handstr = "3S 4H 3C 4C 7D"; my @hand = split(' ',$handstr); my @sorthand = sort @hand; my $uniquevals = my $ans = ""; my ($faceval,$numdups); foreach my $card (@sorthand) { $faceval = substr($card,0,1); $uniquevals .= "$faceval " unless $uniquevals =~ /$faceval/; } foreach my $v (split (' ',$uniquevals)) { $numdups = $handstr =~ s/$v/$v/g; $ans .= "$numdups "; } chop $uniquevals; chop $ans; print "Your hand is ",join(' ',@sorthand),", unique face\n"; print "values $uniquevals. The hand reduces to $ans.\n";

Replies are listed 'Best First'.
Re: Re: Keeping track of Pairs
by ChemBoy (Priest) on Jul 12, 2001 at 17:02 UTC

    Of course, no Perlmonks thread would be complete without the obligatory CPAN referral... believe it or not, even this has its own module: Games::Cards. It's thoroughly generic, so you'll still have to write your own scoring function (or adapt the Poker Probability processor as suggested by kscwab) but hand management at least is taken care of for you.

    To reiterate what mattr said: this is what hashes are for. Why can't you use them? They will simplify your life greatly, really.

    And of course, whatever internal representation you choose, don't forget flushes. :-)



    If God had meant us to fly, he would *never* have give us the railroads.
        --Michael Flanders

      I monkchatted with Satira and he was very cool. It was for an assignment but I think he probably got a lot out of the thread. Next time he'll say so up front so we can help the learning process and not guess! Good luck Satira.