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).
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.