Programming baseball innings

SDS

I’d appreciate the input of any programmers out there on the following.

What would be the best way to structure the code for the looping of baseball innings, and why?

So far, I can come up with the following ideas:

  1. A for loop to repeat the first eight innings, then a while loop for innings 9+ that repeats the content while the game is tied
  2. A while loop for the whole game that repeats while inning is (less than 9) OR (more than 8 AND scores are equal)
  3. A for loop to repeat the first eight, then a single block specifically for the 9th, then a while loop for innings 10+

I’m thinking #2 simply to avoid repeating code, but it doesn’t take into account the fact that the 9th inning is unique from a winner checking POV so open to better ideas.

Cheers

SDS

bool gameEnd = false;
int inningCnt = 0;

WHILE (gameEnd == false)
{
inningCnt++;
playRoadTeamBat();
if (inningCnt >= 9 && homeScr > roadScr)
{
gameEnd = true;
break;
}
if (rain == true && inningCnt > 5 && homeScr > roadScr)
{
gameEnd = true;
break;
}
playHomeTeamBat();
if (inningCnt >= 9 && homeScr > roadScr)
{
gameEnd = true;
break;
}
}

SDS

@SaveFarris said in Programming baseball innings:

bool gameEnd = false;
int inningCnt = 0;

WHILE (gameEnd == false)
{
inningCnt++;
playRoadTeamBat();
if (inningCnt >= 9 && homeScr > roadScr)
{
gameEnd = true;
break;
}
if (rain == true && inningCnt > 5 && homeScr > roadScr)
{
gameEnd = true;
break;
}
playHomeTeamBat();
if (inningCnt >= 9 && homeScr > roadScr)
{
gameEnd = true;
break;
}
}

Thanks for the code - wasn't expecting somebody to go to the trouble but that's awesome you took the time to do that.

I'd go so far as to put the rainout check after each pitch rather than in between innings. I'd probably change it to a general 'if rainout then gameEnd' that calls a procedure that goes through the rainout rule decision process (the > or <= 5 inningCnt) separately.

What's making me overthink it is the home 9+ inning and checking for the win during the inning. I reckon I'll try changing the homeScr > roadScr check to a simple winner > 0 (possible values 0, 1, 2 for none, away, home) and have the score check after each pitch also (would do each PA except if a pitch is a balk, wild pitch, passed ball or steal then a plate appearance may not be over) so that it can break to the 'game' while loop if there is a winner.

In a couple more weeks I'll have some time to put together some procedures for a whole game. Might share some here.

SDS

Prob would also need a check for roadScr > homeScr somewhere, otherwise the while loop won't exit if the road team is winning.

SDS

I never knew I would find a discussion about baseball game development. Pretty cool.

I don't follow any structure so I by no means follow any best practices. I just keep dabbling at the code till it does what I want it do to. Here's something from a game I'm developing:

while($inning < 10 || ($inning > 9 && $homeExtraInningAtBat == false)) {

if($inning >= 9 && $hRuns > $vRuns && $inningHalf == 'bottom'){

	print "End game walk off<br>";	

	$endOfGame = true;

	...

}

if($inning > 8 && $hRuns > $vRuns){

	print "End game<br>";

	$endOfGame = true;
}

if($inning > 8 && $vRuns > $hRuns){

	print "End game<br>";

	$endOfGame = true;

	$homeExtraInningAtBat = true;

}

$inningHalf = 'top';
$outs = 0;
		
if($endOfGame == false){

	$inning = $inning + 1;

}

}

SDS

@je8675309 said in Programming baseball innings:

I never knew I would find a discussion about baseball game development. Pretty cool.

I don't follow any structure so I by no means follow any best practices. I just keep dabbling at the code till it does what I want it do to. Here's something from a game I'm developing:

while($inning < 10 || ($inning > 9 && $homeExtraInningAtBat == false)) {

if($inning >= 9 && $hRuns > $vRuns && $inningHalf == 'bottom'){

  print "End game walk off<br>";	

  $endOfGame = true;

  ...

}

if($inning > 8 && $hRuns > $vRuns){

  print "End game<br>";

  $endOfGame = true;

}

if($inning > 8 && $vRuns > $hRuns){

  print "End game<br>";

  $endOfGame = true;

  $homeExtraInningAtBat = true;

}

$inningHalf = 'top';
$outs = 0;

if($endOfGame == false){

  $inning = $inning + 1;

}

}

I like the game log generated as html idea that your code just gave me 🙂

I’m all for keeping this discussion going; it’s nice reading in amongst all the complaints. You never know, we could come up with something 😉

SDS

I'm not much into programming at all but what would this be for?

SDS

@Rabid55Wolverine said in Programming baseball innings:

I'm not much into programming at all but what would this be for?

Just messing around with ideas, keeping the brain active.

Log in to reply