24 days of Perl code from RJBS! Feed

The Other Kind of Turkey

Games::Bowling::Scorecard - 2009-12-05

It's Elf Bowling Time Again

Every year, as Christmas approaches, it's time once again to update our email filters to bitbucket all the "elf bowling!!!" emails from friends and family. I've never understood the appeal of ELFBOWL.EXE, but I guess I can say one thing for it: it gets me thinking about bowling, again. I used to really enjoy bowling, but I haven't gone out to the lanes in ages. Living in New England made me stop thinking about it much, because almost all bowling up there is of a strange, abominable variation.

When the Nintendo Wii came out, though, I got hooked on bowling again, and realized that although I had always enjoyed the game, I really never understood how to score it. What better way to learn than to implement a CPAN module for tracking bowling scores? Probably many, but that's the one I picked!

Here's the sort of scorecard I might get to fill out on a good day. (I said I liked bowling, not that I was good at it!)


1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 

 

use Games::Bowling::Scorecard;

my $card = Games::Bowling::Scorecard->new;

$card->record(6,1); # slow start
$card->record(7,2); # getting better
$card->record(10); # strike!
$card->record(9,1); # picked up a spare

$card->record(10) for 1 .. 3; # turkey!

$card->record(0,0); # clearly distracted by something
$card->record(8,2); # amazingly picked up 7-10 split

$card->record(10, 9, 1); # pick up a bonus spare

say $card->score; # 156

 

If you're a much better bowler, you might get to make this excellent invocation:


1: 
2: 
3: 
4: 
5: 
6: 

 

my $card = Games::Bowling::Scorecard->new;
$card->record( (10) x 12 );

say $card->score; # 300

$alley->trigger_balloon_drop;

 

Displaying Score Cards

Of course, printing a number to the screen isn't very satisfying. Part of the joy of bowling is watching all the bizarre math and symbols get drawn on the big overhead screen. We could tack this onto my game above...


1: 
2: 
3: 

 

use Games::Bowling::Scorecard::AsText;

print Games::Bowling::Scorecard::AsText->card_as_text($card);

 

And we'll get this extremely satisfying output:

  +-----+-----+-----+-----+-----+-----+-----+-----+-----+-------+
  | 6 1 | 7 2 | X   | 9 / | X   | X   | X   | - - | 8 / | X 9 / |
  |   7 |  16 |  36 |  56 |  86 | 106 | 116 | 116 | 136 |   156 |

Practical Uses..?

If you know how to bowl, you probably know how to keep score, so you're not going to be saving a lot of time by keying in your scores to a Perl program. You could easily enough throw together a ReadLine-based program to keep track of turns and scores, but I think those uses are never going to be big wins over transparencies and grease pencils.

No, I think that the big hot application for Games::Bowling::Scorecard will come with the growing resurgence of SDL Perl. Pretty soon, dozens of graphical bowling games written in Perl will be in the works, and they'll all be easier to write thanks to Games::Bowling::Scorecard. I look forward to the day when the ELFBOWL2.EXE that I angrily delete is compiled from Perl, using my own code.

See Also