Last night I evaluated random things on IRC (freenode), but that is probably not a surprise to most. Someone else came along and did it too for the numbers 1 through 4. I also like the Lingua::EN::Inflect module. So, I took the person's random 1-4 and wrapped it in NUMWORDS and ORD. When I ran it through buubot the first time, I didn't get any fourths. I thought then said that no one made it home, referring to baseball.

About a month ago, Leverage, one of my favorite television programs, was centered around baseball. That is probably where the whole baseball thing came from.

So, here is where the oddball script comes into play. I wrote a little script that will randomly generate half an inning. The odder thing is, I don't like sports, so writing a script for baseball is really out there.

I have been told that I should work with other people on a project. I have gone about as far as I want to with this script. If anyone wants to tweak what is currently there or expand it, go for it. I am not a fan of baseball, so I don't know all the rules. What I have below is extraordinarily simplistic. All I know is that it was fun to write, and as fun to watch the results.

This script is notable because it is the first script that I have written from scratch that includes recursion. All other work with recursion I have done is with the help of others. That I recursed this one without help is one reason that I am so happy with and wanted to share it.

So, where did the idea for an oddball script you wrote come from? :)

use strict; use warnings; use Lingua::EN::Inflect qw(ORD NUMWORDS); my $pitch = 1; my $balls = 0; my $strikes = 0; my $outs = 0; my $score = 0; sub pitch { my $player = shift; my @pitch_results = qw(hit ball strike); my $pitch_result = $pitch_results[rand @pitch_results]; my $pitch_text = ucfirst ORD($pitch)." pitch:"; if ($pitch_result eq "ball") { ++$balls; if ($balls == 4) { my $base = ORD(NUMWORDS(1)); print "$pitch_text $player walks to $base base.\n"; } else { print "$pitch_text Ball ".NUMWORDS($balls)."\n"; ++$pitch; pitch($player); } } elsif ($pitch_result eq "strike") { ++$strikes; if ($strikes == 3) { print "$pitch_text $player stikes out!\n"; ++$outs; } else { print "$pitch_text Strike ".NUMWORDS($strikes)."!\n"; ++$pitch; pitch($player); } } else { my @hits = qw(good bad); my $hit = $hits[rand @hits]; if ($hit eq "bad") { my $strike_text = ""; if ($strikes < 2) { ++$strikes; $strike_text = "Strike ".NUMWORDS($strikes); } print "$pitch_text Foul ball! $strike_text\n"; ++$pitch; pitch($player); } else { my $base_num = (1..4)[rand 4]; my @bases = (1..$base_num); print "$pitch_text $player hits!\n"; for my $base (@bases) { my @tags = qw(yes no); my $tag = $tags[rand @tags]; if ($tag eq "no") { if ($base == 4) { print "\tHOME RUN!"; } else { print "\t$player makes it to ".NUMWORDS(ORD($base))." base +.\n"; } } else { if ($base == 4) { print "\t$player is out at the home plate.\n"; } else { print "\t$player is out at ".NUMWORDS(ORD($base))." base.\ +n"; ++$outs; last; } } } } } $pitch = 1; $balls = 0; $strikes = 0; } sub team { my $player_up = 1; until ($outs == 3) { print "Player ".NUMWORDS($player_up)."\n\n"; pitch("Player ".NUMWORDS($player_up)); print "\n"; ++$player_up; } print "Score: ".$score."\n"; $outs = 0; } team();

So, how did I do? :)

Have a nice day!
Lady Aleena

Replies are listed 'Best First'.
Re: Where ideas for oddball scripts come from...
by JavaFan (Canon) on Mar 16, 2010 at 14:26 UTC
    Considering that on a single play, more than 2 players can go out, I'd say the guard $out == 3 isn't correct. It should be $out >= 3.
Re: Where ideas for oddball scripts come from...
by hangon (Deacon) on Mar 16, 2010 at 19:35 UTC

    Interesting script, but you might find the following modification of the last line enlightening.

    ... team() until $score;

    On the other hand, by using objects for players & teams and setting up an event loop, you could make a pretty decent simulation for an entire baseball game. Of course simulating something that interests you would be more fun.

      I added the line you suggested, and I found it very interesting. Even though it hasn't survived to the current form in Re: Where ideas for oddball scripts come from..., it did help me get motivated to write the most of the rest of the game. In honor of you motivating me to do more with it, I named a team after you. :)

      About using objects and event loops, I am not entirely sure what you are talking about there.

      This is a nice diversion from role-playing and movie related scripts. Sometimes one has to go away from the main focus of one's work or do something completely different to be able to find little gems that work. For me in this script, I used recursion for the first time. In all the other scripts that I have written, I haven't had to do so. So even if I am not overly excited by the subject of the script, I and others can keep expanding it until it is a full game. This has been a great learning exercise for me.

      Have a nice day!
      Lady Aleena
Re: Where ideas for oddball scripts come from...
by Lady_Aleena (Priest) on Mar 16, 2010 at 23:03 UTC

    It's a game!

    It's got innings, extra_innings, and the game. You can even name your teams! :)

    There is the problem of game length. Right before posting this, I ran the longest game I have seen with 49 innings. I feel sorry for my virtual players. The script is still very simple.

    Have a nice day!
    Lady Aleena