Baseball-Simulation

 view release on metacpan or  search on metacpan

lib/Baseball/Simulation.pm  view on Meta::CPAN

#
# 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 =~ /#/ )  {
	$LineToBeParsed = $`;
    }
    # Delete the ending spaces
    if ( $LineToBeParsed =~ /\s+$/ )  {
	$LineToBeParsed = $`;
    }
    
    return $LineToBeParsed;
}

##################################################
# CreateBatter
#
# Paramenters: The array consisting the cumalitve totals for:
#             At-Bats
#             Walks
#             Singles
#             Doubles
#             Triples
#             Homers
#             StolenBases
#
# Description: Calculates the averages for the batting statistics
#
# 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;

    return ($WalkChance, $SinglesChance, $DoublesChance, $TriplesChance, $HomersChance, $SacrificeChance, $StealsChance); 
}

##################################################
# CreateNewLineup
#
# Parameters: The file of user list
#
# Description: Reads the stats from a file, adding the additions
#              and subtracting the subtractions
#
# 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;
    my $MaxLine = @FileLines;
    while (($Line = $FileLines[$i++]) && ($i <= $MaxLine)){
	$Line = StripLine($Line);
	next unless ($Line);
	@TotalStats = split /\:/, $Line;
	if ($#TotalStats + 1 != 7) {
	    die "The following line does not contain 7 double colon seperated values: $Line";
	}
	last;
    }

    while (($Line = $FileLines[$i++]) && ($i <= $MaxLine)){
	$Line = StripLine($Line);
	last if ($Line =~ /additions/i);
    }

    my $l = 0;
    while (($Line = $FileLines[$i++]) && ($i <= $MaxLine)){
	$Line = StripLine($Line);
	last if ($Line =~ /sub/i);
	if ($Line) {
	    my $j = 0;
	    @PlayerStats = split /\:/, $Line;
	    if ($#PlayerStats + 1 != 7) {
		die "The following line does not contain 7 double colon seperated values: $Line";
	    }	 
	    for ($l = 0; $l < 7; $l++) {
		$TotalStats[$l] += $PlayerStats[$l];
	    }	
	}
    }

    while (($Line = $FileLines[$i++]) && ($i <= $MaxLine)){
	$Line = StripLine($Line);
	if ($Line) {
	    @PlayerStats = split /\:/, $Line;
	    if ($#PlayerStats + 1 != 7) {
		die "The following line does not contain 7 double colon seperated values: $Line";
	    }	 
	    for ($l = 0; $l < 7; $l++) {
		$TotalStats[$l] -= $PlayerStats[$l];
	    }	
	}
    }

    return @TotalStats;
}

##################################################
# AtBat
#
# Parameters: 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 single can be hit
#             StolenBaseChance - The percentage that a single can be hit
#
# Description: Simulates an at-bat
#
# 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)  = @_;
    
    $Random2 = (((int (rand(10000))) + (int (rand(2000))))
		  % 1000);

    if ($Random2 < $WalkChance) {
	return -1;
    } elsif ($Random2 < ($SingleChance + $WalkChance)) {
	return 1;
    } elsif ($Random2 < ($SingleChance + $WalkChance + $DoubleChance)) {
	return 2;
    } elsif ($Random2 < ($SingleChance + $WalkChance + $DoubleChance + $TripleChance)) {
	return 3;
    } elsif ($Random2 < ($SingleChance + $WalkChance + $DoubleChance + $TripleChance + $HomerChance)) {
	return 4;
    }

    return 0;
}

##################################################
# AdvanceRunner
#
# Parameters: Result - the result of the at bat
#             PlayerStealChance - the player's chance of stealing
#             FirstBase - whether someone is on first
#             FirstBaseStealChance - the guys on first's chance of stealing
#             SecondBase - whether someone is on second
#             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($$$$$$$$) {



( run in 0.978 second using v1.01-cache-2.11-cpan-39bf76dae61 )