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.

edit

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.