#!/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; }