Esempio n. 1
0
        public double simulateOBP(double ballPct, double ballStruckPct, int nmSims)
        {
            int nmWalks = 0;
            int nmHits  = 0;
            int nmOuts  = 0;

            for (int i = 0; i < nmSims; i++)
            {
                AtBat bt = new AtBat();

                //SimpleBatter SimpleBatter = new SimpleBatter(0.35, 0.35);
                SimpleBatter SimpleBatter = new SimpleBatter(ballPct, ballStruckPct);

                AtBat.AtBatResult res = bt.SimulateAtBat(SimpleBatter);
                if (res == AtBat.AtBatResult.Hit)
                {
                    nmHits++;
                }
                if (res == AtBat.AtBatResult.Walk)
                {
                    nmWalks++;
                }
                if (res == AtBat.AtBatResult.Out)
                {
                    nmOuts++;
                }
            }

            double obp = ((double)(nmHits + nmWalks)) / ((double)nmSims);

            return(obp);
        }
Esempio n. 2
0
        public void PlayHalfInning()

        {
            Team CurrentTeam;
            int  Outs = 0;

            //check if it is top or the bottom of the inning
            //for simplicity sake batting order will be done in the sequential order

            if (TodayGame.CurrentInning.HalfInning == Team.TeamType.Away)
            {
                CurrentTeam = AwayTeam;
                TodayGame.CurrentInning.HalfInning = Team.TeamType.Home;
            }
            else
            {
                CurrentTeam = HomeTeam;
                TodayGame.CurrentInning.HalfInning = Team.TeamType.Away;
            }

            foreach (Player Batter in CurrentTeam.TeamPlayers)
            {
                //get truly random value
                Random       rnd           = new Random(Guid.NewGuid().GetHashCode());
                double       ballpct       = rnd.NextDouble();
                double       ballStruckPct = rnd.NextDouble();
                SimpleBatter SB            = new SimpleBatter(ballpct, ballStruckPct);

                AtBat AtBatNow = new AtBat();

                AtBat.AtBatResult AtBatResultNow = AtBatNow.SimulateAtBat(SB);
                Batter.AtBat++;

                if (AtBatResultNow == AtBat.AtBatResult.Hit)
                {
                    Batter.Hits++;
                }
                if (AtBatResultNow == AtBat.AtBatResult.Out)
                {
                    Outs++;
                    if (Outs == 3)
                    {
                        break;
                    }
                    // three times and you're team is out of the Inning
                }
                if (AtBatResultNow == AtBat.AtBatResult.Walk)
                {
                    Batter.Walks++;
                }
                Batter.BattingAVG = (double)Batter.Hits / (double)Batter.AtBat;
                Batter.OBP        = ((double)Batter.Hits + (double)Batter.Walks) / ((double)Batter.AtBat + (double)Batter.Walks);
            }
            //calculate running Batting Average

            TodayGame.CurrentInning.InningNumber = TodayGame.CurrentInning.InningNumber + 0.5;
        }