Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Welcome to the Monastery
 
PerlMonks  

Re^2: calculating cribbage points

by blokhead (Monsignor)
on Mar 30, 2006 at 13:54 UTC ( [id://540240]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: calculating cribbage points
in thread calculating cribbage points

If you want to abstract it a little step farther you can get rid of 5 levels of nested looping. All you're doing is looking at all subsets of cards. So using the power-set iterator from (tye)Re: Finding all Combinations, the code becomes much more high-level and reads more naturally: "For each subset of cards, check if their sum is 15" ... Whether it's overkill for cribbage, the OP must decide. I know cribbage is not usually generalized to >5 card hands ;)
use List::Util 'sum'; sub combinations { ... } ## from [id://128293] my @c = split /,/, $hand; my $total = 0; my $iter = combinations(@c); while (my @subset = $iter->()) { $total += 2 if 15 == sum @subset; }
You could also check for pairs inside that while loop, although runs would have to be calculated somewhere else.

blokhead

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://540240]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.