heatblazer has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I am almost done with all the code for a real poker game but I am totally stuck with the winning condition matching... why? Well here is it:
sub winner { my $match; my (@hand) = (@_); foreach (@hand) { if ( /^(A|J|K|Q|2|3|4|5|6|7|8|9|10) of (clubs|diamonds|spades| +hearts)/ ){ print $&, "\n"; } } }
I don`t know how to make it to match for example if we have a pair of Kings or two pairs of any cards. A triple cards, and 5 cards of the same color.. Any ideas?
p.s. I was thinking of card by card comparing starting from index0 since... if we have 2 Kings then we can skip the 5 same colors checks since it`s never possible to have 5 color cards if at least 1 pair, but I think that regex could do it easier, just don`t know how.
Here is some algorihm I came up with... a noob one but still fixes the issue as matching pairs and finds the same color... kind of lame one but working. Opinions?
#!/usr/bin/perl -wT use strict; use utf8; my @hand = ('k of hearts', 'k of diamonds', 'j of spades', 'j of heart +s'); my $same_kind=0; my $same_color = 0; for ( my $i=0; $i<$#hand+1; $i++) { for ( my $j=$i+1; $j<$#hand+1; $j++) { my (@first) = split(" of ", $hand[$i]); my (@second) = split(" of ", $hand[$j]); print "Split1: $first[0]\n"; print "Split2: $second[0]\n"; if ( $first[0] eq $second[0] ) { #pair $same_kind +=1; } elsif ( $first[$#first] eq $second[$#second] ) { $same_color +=1; } } } print "You have same kind of cards: $same_kind\nand $same_color of sam +e color.\n";
Whew...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Checking for a special matching
by JavaFan (Canon) on Apr 01, 2012 at 23:17 UTC | |
by heatblazer (Scribe) on Apr 02, 2012 at 16:43 UTC | |
by JavaFan (Canon) on Apr 02, 2012 at 18:13 UTC | |
by heatblazer (Scribe) on Apr 03, 2012 at 14:33 UTC | |
|
Re: Checking for a special matching
by Marshall (Canon) on Apr 02, 2012 at 04:37 UTC | |
by heatblazer (Scribe) on Apr 02, 2012 at 11:57 UTC | |
by Marshall (Canon) on Apr 02, 2012 at 14:44 UTC | |
by heatblazer (Scribe) on Apr 03, 2012 at 11:39 UTC | |
by aaron_baugher (Curate) on Apr 04, 2012 at 09:15 UTC | |
by ww (Archbishop) on Apr 03, 2012 at 15:28 UTC | |
|