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();