Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This one works, takes command line arguments like X X X X X X X X X X X X and runs the sample game if no args. There is some bounds checking but no other error checking.

It uses both strict and warnings. It does not validate that you entered sensible data. Nonsense data is scored as 0.

X can be either upper or lower but / has to be /

#!/usr/bin/perl use strict; use warnings; our @game; if(@ARGV>11 && @ARGV<22) { @game=@ARGV; } elsif(@ARGV) { print "Not possible to score this game\n"; exit; } else { print "No game specified using sample game\n"; @game=('5','/','6','3','X','X','7','0','4','3','X','4','/','8' +,'1','3','/','6'); } bowlscore(@game); sub bowlscore { my(@pins)=@_; my $f=0; my $sb; undef($sb); my $ef=0; my $pb=0; my $score=0; while(@pins) { my $ball=shift(@pins); if($ball eq 'X' || $ball eq 'x') { $f++; $score += 10; $score += points($pins[0]); $score += points($pins[1],$pins[0]); $ef=1; undef($sb); } elsif($ball eq '/') { $f++; $score += points($pins[0]); $score+= (10 - $sb); $ef=1; undef($sb); } else { $ball =~ s/[^0-9]//g; $ball ||= 0; die "$ball Invalid frame!\n", if((defined($sb) + && $sb+$ball>9) || $ball>9) ; $score+=$ball; if(defined($sb)) { $f++; $ef=1; undef($sb); } else { $sb=$ball; } } print "$ball "; print "@pins " if($f==10); print "F$f $score\n" if($ef); $ef=0; last if($f==10); } } sub points { my($pins, $pp)=@_; $pp = 0 unless($pins eq '/'); return 10-$pp if($pins eq 'X' || $pins eq '/' || $pins eq 'x +'); return $pins; }

Update: Notes that number of strokes is important as well as correct function. (still a novice at the monastery so have much to learn).

In reply to Re: (Golf) Let's go bowling by dga
in thread (Golf) Let's go bowling by virtualsue

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and !@monks...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (4)
    As of 2018-05-20 14:56 GMT
    Find Nodes?
      Voting Booth?