in reply to Keeping track of Pairs
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 | |
by mattr (Curate) on Jul 13, 2001 at 16:36 UTC |