in reply to (Golf) Let's go bowling
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).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: (Golf) Let's go bowling
by dga (Hermit) on Aug 11, 2001 at 00:23 UTC | |
by dragonchild (Archbishop) on Aug 11, 2001 at 00:54 UTC | |
by dga (Hermit) on Aug 15, 2001 at 00:32 UTC |
In Section
Meditations