public PitchResults HandlePitch() { PitchResults toReturn = PitchResults.Strike; Random r = new Random(Guid.NewGuid().GetHashCode()); double randVal = r.NextDouble(); if (randVal < _ballStruckPct) { double struckOutcomeRnd = r.NextDouble(); if (struckOutcomeRnd < _hitPct) { toReturn = PitchResults.Single; } else if (struckOutcomeRnd < _foulPct) { toReturn = PitchResults.FoulBall; } else { toReturn = PitchResults.GroundOut; } } else if (randVal < _ballStruckPct + _ballPct) { toReturn = PitchResults.Ball; } return(toReturn); }
/// <summary> /// Execute the at bat. /// </summary> public void Execute() { int roll = Dice.Roll2d10(); GamePlayResult res = null; try { if (roll <= this.pitcher.PitchingStats.CurrentControl) { PitchResults theResult = this.pitcher.PitchingStats.PitchResults[roll - 1]; res = GamePlayResultFactory.GetResult(this, theResult); } else { BattingResults theResult = this.batter.BattingStats.BattingResults[roll - 1]; res = GamePlayResultFactory.GetResult(this, theResult); } this.inning.Game.Announcer.AnnounceToConsole(String.Format($"And {res.ControllingPlayer} is in control of the at bat.")); this.pitcher.PitchingStatistics.BattersFaced++; this.result = res; this.batter.IsBatting = false; OnGamePlayResult(res); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Checks if result is a hit. /// </summary> /// <returns><c>true</c>, if hit was ised, <c>false</c> otherwise.</returns> /// <param name="result">bool</param> public static bool IsHit(PitchResults result) { switch (result) { case (Base.PitchResults.Single): return(true); case (Base.PitchResults.Double): return(true); case (Base.PitchResults.Triple): return(true); case (Base.PitchResults.HR): return(true); default: return(false); } }
/// <summary> /// Gets the result. /// </summary> /// <returns>GamePlayResult</returns> /// <param name="currentAtBat">AtBat</param> /// <param name="result">GamePlayResult</param> public static GamePlayResult GetResult(AtBat currentAtBat, PitchResults result) { switch (result) { case PitchResults.Balk: return(new Balk(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.HBP: return(new HitByPitch(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.BB: return(new Walk(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.K: return(new Strikeout(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.GO: return(new GroundOut(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.FO: int roll = Dice.Roll2d10(); if (roll <= currentAtBat.Pitcher.PitchingStats.Control) { return(new PopFlyOut(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); //currentAtBat.Pitcher in control will induce infield popout } return(new Flyout(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.Single: return(new Single(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.Double: return(new Double(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.Triple: return(new Triple(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); case PitchResults.HR: return(new HomeRun(currentAtBat.Pitcher, currentAtBat.Batter, currentAtBat.Batter)); } return(null); }
public void TestPitcherGame() { Player ryan = new Player("34", "Ryan", "Nolan", "34", Positions.PositionNames[0], Race.White, Handedness.Right, Handedness.Right, new Height(74), new Weight(190), new Birthday(1947, 1, 31)); ryan.PitchingStats = ps; Dictionary <PitchResults, int> frequency = new Dictionary <PitchResults, int>(); for (int i = 0; i < 27; i++) { int roll = Dice.Roll(1, 100); PitchResults key = ryan.PitchingStats.PitchResults[roll - 1]; if (!frequency.ContainsKey(key)) { frequency.Add(key, 1); } else { frequency[key] += 1; } } Assert.IsTrue(true); }
public AtBatResult SimulateAtBat(I_Batter batter) { while (_strikes < 3 && _balls < 4) { PitchResults res = batter.HandlePitch(); if (res == PitchResults.Ball) { _balls++; } if (res == PitchResults.FoulBall) { if (_strikes < 2) { _strikes++; } } if (res == PitchResults.Strike) { _strikes++; } if (res == PitchResults.Single) { return(AtBatResult.Hit); } } if (_strikes == 3) { return(AtBatResult.Out); } //if (_balls == 4) //{ return(AtBatResult.Walk); //} }
public void TestPitcherVersusBatterOutcomes() { ryan.PitchingStats = ps; p.BattingStats = bs; ryan.PitchingStats.CurrentControl -= p.BattingStats.ControlModifier; Assert.IsTrue(ryan.PitchingStats.Control == 75); Assert.IsTrue(ryan.PitchingStats.CurrentControl == 65); Dictionary <PitchResults, int> frequencyPitcherControl = new Dictionary <PitchResults, int>(); Dictionary <BattingResults, int> frequencyBatterControl = new Dictionary <BattingResults, int>(); int pitcherControl = 0; int hits = 0; for (int i = 0; i < AT_BATS; i++) { int roll = Dice.Roll2d10(); if (roll <= ryan.PitchingStats.Control) { pitcherControl++; roll = Dice.Roll2d10(); PitchResults key = ryan.PitchingStats.PitchResults[roll - 1]; if (!frequencyPitcherControl.ContainsKey(key)) { frequencyPitcherControl.Add(key, 1); } else { frequencyPitcherControl[key] += 1; } if (BattingStats.IsHit(key) == true) { hits++; } } else { roll = Dice.Roll2d10(); BattingResults key2 = p.BattingStats.BattingResults[roll - 1]; if (!frequencyBatterControl.ContainsKey(key2)) { frequencyBatterControl.Add(key2, 1); } else { frequencyBatterControl[key2] += 1; } if (BattingStats.IsHit(key2) == true) { hits++; } } } Dictionary <PitchResults, int> .Enumerator cursor = frequencyPitcherControl.GetEnumerator(); Console.WriteLine("The pitcher remained control {0} out of {1} at bats [{2:P}]", pitcherControl, AT_BATS, ((double)pitcherControl / (double)AT_BATS)); while (cursor.MoveNext()) { Console.WriteLine(cursor.Current.ToString()); } Console.WriteLine("Results when the batter retained control"); Dictionary <BattingResults, int> .Enumerator cursor2 = frequencyBatterControl.GetEnumerator(); while (cursor2.MoveNext()) { Console.WriteLine(cursor2.Current.ToString()); } Console.WriteLine("The batter had a batting average of {0:.000}", ((double)hits / (double)AT_BATS)); }