Baseball-Simulation
view release on metacpan or search on metacpan
lib/Baseball/Simulation.pm view on Meta::CPAN
##################################################
# Round
#
# Parameters: un unrounded number
#
# Description: Rounds a number
#
# Returns: The rounded number
#
##################################################
sub Round($) {
my $Float = $_[0];
$Float += 0.5;
return int($Float);
}
##################################################
# StripLine
#
# Parameters: A line with white surrounding white space and comments
#
# Description: Removes surrounding white space and comments
#
# Returns: A cleaned up line
#
##################################################
sub StripLine($) {
my $LineToBeParsed = $_[0]; # The text to be stripped
chomp $LineToBeParsed; # Get rid of line feed
# Delete leading spaces;
if ( $LineToBeParsed =~ /^\s+/ ) {
$LineToBeParsed = $'; #'
}
# Check for comment characters
if ( $LineToBeParsed =~ /#/ ) {
lib/Baseball/Simulation.pm view on Meta::CPAN
# Returns: The array consisting the cumalitve averages for:
# WalkChance - The percentage for a walk
# SingleChance - The percentage that a single can be hit
# DoubleChance - The percentage that a single can be hit
# TripleChance - The percentage that a single can be hit
# HomerChance - The percentage that a single can be hit
# SacChance - The percentage that a sacrifice occurs
# StolenBaseChance - The percentage that a stolen base occurs
#
##################################################
sub CreateBatterArray(@) {
my ($AtBats, $Hits, $Doubles, $Triples, $Homers, $Walks, $Steals) = @_;
my $TotalAtBats = $AtBats + $Walks;
my $Singles = $Hits - $Doubles - $Triples - $Homers;
my $WalkChance = int (($Walks / $TotalAtBats) * 1000);
my $SinglesChance = int (($Singles / $TotalAtBats) * 1000);
my $DoublesChance = int (($Doubles / $TotalAtBats) * 1000);
my $TriplesChance = int (($Triples / $TotalAtBats) * 1000);
my $HomersChance = int (($Homers / $TotalAtBats) * 1000);
my $StealsChance = int ($Steals / ($Walks + $Singles));
my $SacrificeChance = 0;
lib/Baseball/Simulation.pm view on Meta::CPAN
# Returns: The array consisting the cumalitve totals for:
# At-Bats
# Walks
# Singles
# Doubles
# Triples
# Homers
# StolenBases
#
##################################################
sub CreateNewLineup($) {
my $File = $_[0];
my @TotalStats = (0,0,0,0,0,0,0);;
my @PlayerStats = (0,0,0,0,0,0,0);
my $Line;
open(INFILE, "$File") || die "Cannot open $File";
my @FileLines = <INFILE>;
close(INFILE);
my $i = 0;
lib/Baseball/Simulation.pm view on Meta::CPAN
#
# Returns: The result - -1 = walk
# 0 = out
# 1 = single
# 2 = double
# 3 = triple
# 4 = home run
#
##################################################
#ignore double plays and sacrifices for now
sub AtBat(@) {
my $WalkChance = 0;
my $SingleChance = 0;
my $DoubleChance = 0;
my $TripleChance = 0;
my $HomerChance = 0;
my $SacChance = 0;
my $StolenBaseChance = 0;
my $Random2 = 0;
($WalkChance, $SingleChance, $DoubleChance, $TripleChance, $HomerChance, $SacChance, $StolenBaseChance) = @_;
lib/Baseball/Simulation.pm view on Meta::CPAN
# SecondBaseStealChance - the guys on third's chance of stealing
# ThirdBase - whether someone is on third
# Score - The score so far
#
# Description: Advances runners after an at-bat
#
# Returns: Updated values for: $FirstBase, $FirstBaseStealChance, $
# SecondBase, $SecondBaseStealChance, $ThirdBase, $Score
#
##################################################
sub AdvanceRunner($$$$$$$$) {
my ($Result, $PlayerStealChance, $FirstBase, $FirstBaseStealChance, $SecondBase, $SecondBaseStealChance, $ThirdBase, $Score) = @_;
if ($Result == -1) {
if ($FirstBase && $SecondBase && $ThirdBase) {
#Advance all one
$Score++;
$SecondBaseStealChance = $FirstBaseStealChance;
} elsif ($FirstBase && $SecondBase) {
#Advance the first two one
$SecondBaseStealChance = $FirstBaseStealChance;
lib/Baseball/Simulation.pm view on Meta::CPAN
# Inning
#
# Parameters: Who is batting - 1 if the team is batting
# - 0 if the other team is batting
#
# Description: Simulates an inning
#
# Returns: Returns the score from that inning
#
##################################################
sub Inning($) {
my $Who = $_[0];
my $Outs = 0;
my $FirstBase = 0;
my $FirstBaseStealChance = 0;
my $SecondBase = 0;
my $SecondBaseStealChance = 0;
my $ThirdBase = 0;
my $Score = 0;
my $Result = 0;
my @Player = "";
lib/Baseball/Simulation.pm view on Meta::CPAN
# Parameters: None
#
# Description: Simulates the season
#
# Returns: Average victories per season
# Average defeats per season
# Average runs scored per season
# Average runs allowed per season
#
##################################################
sub Simulate() {
my $TotalVictories = 0;
my $TotalDefeats = 0;
my $SingleOtherScore = 0;
my $TotalScore = 0;
my $TotalOtherScore = 0;
my $i = 0;
my $k = 0;
for ($k = 0; $k < $NumOfSeasons; $k++) {
( run in 0.301 second using v1.01-cache-2.11-cpan-65fba6d93b7 )