Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: calculating cribbage points

by jdporter (Paladin)
on Mar 30, 2006 at 22:46 UTC ( [id://540306]=note: print w/replies, xml ) Need Help??


in reply to calculating cribbage points

Well, other people have talked about how to do loops, or use modules to assist in the problem. I'm going to take an approach which is more customized to the specific application. Even so, it wouldn't be hard to extend if, for example, you started playing 7-card cribbage. :-)

sub sum { my $sum; $sum += $_ for @_; $sum } my @combos = map { my @v = reverse split //, sprintf "%05b", $_; my @w = grep { $v[$_] } 0 .. $#v; @w > 1 ? \@w : () } 0 .. 31; sub fifteens { my $hand = shift; $hand =~ s/[SCHD]//g; $hand =~ s/[JQK]/10/g; $hand =~ s/A/1/g; my @hand = split /,/, $hand; ( grep { sum(@hand[@$_]) == 15 } @combos ) * 2 }

The key is the @combos array. It contains a set of "combination keys", such as

[ 1,2 ], [ 1,2,3 ], [ 1,2,4 ], . . .
for all the valid subsets of cards in a hand. It turns out there's only 26 of them. (It does not include any keys of length 1, since there's no way to get 15 from a single card.)
We rely on grep returning the number of matches — that's the number of fifteens found.

We're building the house of the future together.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://540306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found