Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: (Golf) Let's go bowling

by dga (Hermit)
on Aug 09, 2001 at 01:12 UTC ( #103230=note: print w/replies, xml ) Need Help??

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

    Per my update above this one is less than 1/3 the other and still works if you use use strict; (and warnings)

    { my(@g)=@_; my(@c)=@g; my($t, $s, $m, $p, $f, $h); foreach (@c) { s/[Xx]/10/; s/\//10-$p/e; $p=$_; } while(@g) { $s=shift(@c); $_=shift(@g); /X/ && ($t += $s + $c[0] + $c[1], $f++, $h=0, $f>9?last:next) +; /\// && ( $t += $s + $c[0], $f++, $h=0, $f>9?last:next); $h=$h?($f++,0):1 $t+=$s; $h++; } $t; }

    Dropping the my lines will of course stop strict from letting it run and will erase the callers copy of all the arguments to the function. But it is more than 10% smaller...

    Update: Failed case 6 of virtualsue's test suite now works correctly.

      Rewriting your code in Golf-ese, just to give you an example, could yield (without really taking into account all the shortcuts) in 166 characters ...
      map{s/X/10/;s#/#10-$p#e;$p=$_}@c=@_;$t=$f=$h=0;while(@_){$s=shift@c;$_ +=shift;/[X\/]/&&($t+=$s+$c[0]+(/X/?$c[1]:0),$h=0,++$f>9?last:next);$f +++,$h=0 if$h;$t+=$s;$h++}$t
      Golf code isn't meant to be pretty or even readable. It's meant to be compact. :)

      /me wants to be the brightest bulb in the chandelier!

      Vote paco for President!

        Ok last draft, including dragonchild's compressions and optimizations and correcting the case 6 problem which actually saves 3 more bytes.

        map{s/X/10/;s#/#10-$p#e;$p=$_}@c=@_;$t=$f=$h=0;while(@_){$s=shift@c;$_ +=shift;/[X\/]/&&($t+=$s+$c[0]+(/X/?$c[1]:0),$h=0,++$f>9?last:next);$h +=$h?($f++,0):1;$t+=$s}$t

        163 bytes which is basically 1/2 of the previous length.

        Thanks again dragonchild.