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...
In reply to Checking for a special matching by heatblazer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |