dracos has asked for the wisdom of the Perl Monks concerning the following question:
There must be a neater way... or is this the cleanest (which I doubt).#!/usr/bin/perl -w use strict; use warnings; sub fifteens { my $hand = shift; $hand =~ s/[SCHD]//g; $hand =~ s/[JQK]/10/g; $hand =~ s/A/1/g; my ( $c1, $c2, $c3, $c4, $c5 ) = split /,/, $hand; my $total = 0; # count the 2 card combinations that add up to 15 $total += 2 if ( ( $c1 + $c2 ) == 15 ); $total += 2 if ( ( $c1 + $c3 ) == 15 ); $total += 2 if ( ( $c1 + $c4 ) == 15 ); $total += 2 if ( ( $c1 + $c5 ) == 15 ); $total += 2 if ( ( $c2 + $c3 ) == 15 ); $total += 2 if ( ( $c2 + $c4 ) == 15 ); $total += 2 if ( ( $c2 + $c5 ) == 15 ); $total += 2 if ( ( $c3 + $c4 ) == 15 ); $total += 2 if ( ( $c3 + $c5 ) == 15 ); $total += 2 if ( ( $c4 + $c5 ) == 15 ); # count the 3 card combinations that add up to 15 $total += 2 if ( $c1 + $c2 + $c3 == 15 ); $total += 2 if ( $c1 + $c2 + $c4 == 15 ); $total += 2 if ( $c1 + $c2 + $c5 == 15 ); $total += 2 if ( $c1 + $c3 + $c4 == 15 ); $total += 2 if ( $c1 + $c3 + $c5 == 15 ); $total += 2 if ( $c1 + $c4 + $c5 == 15 ); $total += 2 if ( $c2 + $c3 + $c4 == 15 ); $total += 2 if ( $c2 + $c3 + $c5 == 15 ); $total += 2 if ( $c2 + $c4 + $c5 == 15 ); $total += 2 if ( $c3 + $c4 + $c5 == 15 ); # count the 4 card combinations that add up to 15 $total += 2 if ( $c1 + $c2 + $c3 + $c4 == 15 ); $total += 2 if ( $c1 + $c2 + $c3 + $c5 == 15 ); $total += 2 if ( $c1 + $c2 + $c4 + $c5 == 15 ); $total += 2 if ( $c1 + $c3 + $c4 + $c5 == 15 ); $total += 2 if ( $c2 + $c3 + $c4 + $c5 == 15 ); # See if all the cards add up to 15 $total += 2 if ( $c1 + $c2 + $c3 + $c4 + $c5 == 15 ); return $total; } ## end sub fifteens print "hand = A,2,3,4,5 \tTotal =", fifteens ( "A,2,3,4,5"), "\n"; print "hand = 5,5,5,J,5 \tTotal =", fifteens ( "5,5,5,J,5"), "\n"; print "hand = 6,7,8,9,5 \tTotal =", fifteens ( "6,7,8,9,5"), "\n";
2006-03-31 Retitled by planetscape, as per Monastery guidelines
Original title: 'there has to be a better way'
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: calculating cribbage points
by ikegami (Patriarch) on Mar 30, 2006 at 18:25 UTC | |
by blokhead (Monsignor) on Mar 30, 2006 at 18:54 UTC | |
by dracos (Sexton) on Mar 30, 2006 at 18:58 UTC | |
Re: calculating cribbage points
by Limbic~Region (Chancellor) on Mar 30, 2006 at 18:27 UTC | |
Re: calculating cribbage points
by ikegami (Patriarch) on Mar 30, 2006 at 19:49 UTC | |
by Limbic~Region (Chancellor) on Mar 31, 2006 at 16:53 UTC | |
by ikegami (Patriarch) on Mar 31, 2006 at 19:22 UTC | |
by dracos (Sexton) on Mar 31, 2006 at 14:48 UTC | |
by ikegami (Patriarch) on Mar 31, 2006 at 19:29 UTC | |
by thor (Priest) on Mar 31, 2006 at 15:07 UTC | |
Re: calculating cribbage points
by jdporter (Paladin) on Mar 30, 2006 at 22:46 UTC | |
Re: calculating cribbage points
by Roy Johnson (Monsignor) on Mar 31, 2006 at 17:18 UTC | |
Re: calculating cribbage points
by zer (Deacon) on Mar 30, 2006 at 19:06 UTC |