When I step through the program using the debugger it works fine and the displays the correct results. However, when I run the program it displays some extremely high number for homeTeamRuns and awayTeamRuns. I can't seem to figure out the problem.
class BallGame
{
//Fields
private string output = "";
int outs = 0;
int runs = 0;
int homeTeamRuns = 0;
int awayTeamRuns = 0;
bool homeTeam = false;
bool awayTeam = false;
int[] innings = new int[12]; //Innings
//Properties
public string Output
{
get
{
return output;
}
set
{
output = value;
}
}
//Game Simulator
public void playBall()
{
int[] play = new int[4]; //Bases
for(int i = 1; i <= 2; i++)
{
homeTeam = false;
awayTeam = false;
if(i%2 == 0)
homeTeam = true;
else
awayTeam = true;
//Half Inning
while (outs < 3)
{
bool yourOut = false, single = false, double1 = false, triple = false, homerun = false;
Random rnd1 = new Random();
int randomNum = rnd1.Next(1, 101);
//
if (randomNum >= 1 && randomNum <= 50) //50% chance
yourOut = true;
else if (randomNum > 50 && randomNum <= 60) //10% chance
single = true;
else if (randomNum > 60 && randomNum <= 92) //42% chance
double1 = true;
else if (randomNum > 92 && randomNum <= 97) //5% chance
triple = true;
else if (randomNum > 97 && randomNum <= 100) //%3 chance
homerun = true;
if (yourOut == true)
{
outs++;
}
else if (single == true)
{
Shift(play);
runScored(play);
play[0] = 1;
}
else if (double1 == true)
{
Shift(play);
runScored(play);
Shift(play);
runScored(play);
play[1] = 1;
}
else if (triple == true)
{
Shift(play);
runScored(play);
Shift(play);
runScored(play);
Shift(play);
runScored(play);
play[2] = 1;
}
else if (homerun == true)
{
Shift(play);
runScored(play);
Shift(play);
runScored(play);
Shift(play);
runScored(play);
Shift(play);
runScored(play);
play[3] = 1;
}
play[3] = 0;
if (outs == 3)
{
play[0] = 0;
play[1] = 0;
play[2] = 0;
play[3] = 0;
}
}
outs = 0;
}
output = "Home Team Runs: " + homeTeamRuns + " Away Team Runs: " + awayTeamRuns;
}
//Shift Array
public int[] Shift(int[] array)
{
for (int i = array.Length -1; i >= 1; i--)
{
array[i] = array[i - 1];
}
array[0] = 0;
return array;
}
public void runScored(int[] array)
{
if(array[3] == 1)
{
if(awayTeam == true)
awayTeamRuns++;
else if(homeTeam == true)
homeTeamRuns++;
}
}
}