/// <summary> /// Plays the game. /// </summary> /// <returns>The game results.</returns> public GameResults PlayBall() { logger.Debug("PlayBall Start"); // Initialization this.scoreboard = new ScoreBoard(); this.random = new Random(); this.bases = new Bases(); this.gameResults.HomeTeamIdentifier = this.parameters.Teams[Game.HomeTeam].Identifier; this.gameResults.VisitorTeamIdentifier = this.parameters.Teams[Game.VisitorTeam].Identifier; // Managers determine starting pitchers and lineup this.ManagersPreGame(); this.AnnouncerOutput("Play ball!"); // Play the game bool gameOver = false; while (!gameOver) { // Play an inning this.PlayInning(); this.AnnouncerOutput(string.Format("End of inning #{0}.", this.scoreboard.Inning)); this.AnnouncerOutput(string.Format("{0}: {1} {2}: {3}.", this.parameters.Teams[Game.VisitorTeam].Name, this.scoreboard.VisitorScore, this.parameters.Teams[Game.HomeTeam].Name, this.scoreboard.HomeScore)); this.AnnouncerOutput("======================================================================"); this.scoreboard.Inning++; // Is the game over? if (this.scoreboard.Inning >= 10) { // Make sure we're not tied if (this.scoreboard.VisitorScore != this.scoreboard.HomeScore) { gameOver = true; } } } this.gameResults.ScoreBoard = this.scoreboard; this.gameResults.HandleEndOfGame(); logger.Debug("PlayBall End"); return(this.gameResults); }
/// <summary> /// Pinches the hitter. /// </summary> /// <param name="batter">The batter.</param> /// <param name="scoreboard">The scoreboard.</param> /// <param name="bases">The bases.</param> /// <param name="isHome">if set to <c>true</c> [is home].</param> /// <returns>The pinch hitter.</returns> public Player PinchHitter(Player batter, ScoreBoard scoreboard, Bases bases, Boolean isHome) { Player pinchHitter = null; Int32 ourScore = 0; Int32 theirScore = 0; if (isHome) { ourScore = scoreboard.HomeScore; theirScore = scoreboard.VisitorScore; } else { ourScore = scoreboard.VisitorScore; theirScore = scoreboard.HomeScore; } Int32 runsBehind = 0; if (theirScore > ourScore) { runsBehind = theirScore - ourScore; } Int32 numberOfRunners = 0; for (Int32 baseNumber = 1; baseNumber < 4; baseNumber++) { if (bases.Occupied[baseNumber] != null) { numberOfRunners++; } } Int32 number = runsBehind + scoreboard.Inning + numberOfRunners - scoreboard.Outs; if (number > 7) { // Is the pitcher batting? if (batter.Equals(this.lineup.Pitcher)) { pinchHitter = this.SelectPinchHitter(); for (Int32 slot = 0; slot < 9; slot++) { if (this.lineup.BattingOrder[slot].Equals(batter)) { this.lineup.BattingOrder[slot] = pinchHitter; break; } } this.lineup.Pitcher = null; } } if (pinchHitter != null) { this.bench.Remove(pinchHitter); } return(pinchHitter); }