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.
use strict; use warnings; use Lingua::EN::Inflect qw(ORD NUMWORDS); my $pitch = 1; my $inning = 1; my $balls = 0; my $strikes = 0; my $outs = 0; my $score = 0; my $home_score = 0; my $visit_score = 0; my $home_team = "Aleenists"; my $visit_team = "Hangons"; 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 "\t\t\t$pitch_text $player walks to $base base.\n"; } else { print "\t\t\t$pitch_text Ball ".NUMWORDS($balls)."\n"; ++$pitch; pitch($player); } } elsif ($pitch_result eq "strike") { ++$strikes; if ($strikes == 3) { print "\t\t\t$pitch_text $player stikes out!\n"; ++$outs; } else { print "\t\t\t$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 "\t\t\t$pitch_text Foul ball! $strike_text\n"; ++$pitch; pitch($player); } else { my $base_num = (1..4)[rand 4]; my @bases = (1..$base_num); print "\t\t\t$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 "\t\t\t\tHOME RUN!"; ++$score; } else { my $base_text = NUMWORDS(ORD($base_num)); print "\t\t\t\t$player makes it to ".NUMWORDS(ORD($base)). +" base.\n"; } } else { if ($base == 4) { print "\t\t\t\t$player is out at the home plate.\n"; } else { print "\t\t\t\t$player is out at ".NUMWORDS(ORD($base))." +base.\n"; ++$outs; last; } } } } } $pitch = 1; $balls = 0; $strikes = 0; } sub batting { my ($team) = @_; my $player_up = 1; print "\t$team\n\n"; until ($outs >= 3) { print "\t\tPlayer ".NUMWORDS($player_up)."\n\n"; pitch("Player ".NUMWORDS($player_up)); print "\n"; ++$player_up; } if ($team eq $home_team) { $home_score += $score; print "\t$home_team score: $home_score\n\n"; } elsif ($team eq $visit_team) { $visit_score += $score; print "\t$visit_team score: $visit_score\n\n"; } $outs = 0; $score = 0; } sub inning { my ($home, $visitor) = @_; print ucfirst NUMWORDS(ORD($inning))." inning\n\n"; for my $team ($home, $visitor) { batting($team); } ++$inning; } sub extra_inning { my ($home, $visitor) = @_; while ($home_score == $visit_score) { inning($home, $visitor); extra_inning($home, $visitor); } } sub game { my ($home, $visitor) = @_; for (1..9) { inning($home, $visitor); } extra_inning($home, $visitor); print qq{Final score: $visit_team:\t$visit_score $home_team:\t$home_score }; } game($home_team, $visit_team);
In reply to Re: Where ideas for oddball scripts come from...
by Lady_Aleena
in thread Where ideas for oddball scripts come from...
by Lady_Aleena
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |