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

In reply to Where ideas for oddball scripts come from... by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.