private void RaceButton_Click(object sender, EventArgs e)
        {
            DataClasses.HorseDataClass winner = new DataClasses.HorseDataClass();
            var horses = getHorses();

            RaceCore.HorseRacing.Race(horses, ref winner);

            string message = String.Format(" Horse {0} WON. {3} He had a {1}% chance of winning, with the Odds of {2} ", winner.Name, winner.Odds, Math.Round(winner.PercentageChance, 1), Environment.NewLine);

            MessageBox.Show(message, "Winner", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        public static void Race(List <DataClasses.HorseDataClass> horses, ref DataClasses.HorseDataClass winner)
        {
            if (horses.Count.Equals(0))
            {
                return;//No horses
            }
            decimal margin = HorseRacingMargin.CalculateMargin(horses);

            if (margin < 110 && margin > 140)
            {
                return;//Margin is not corret
            }
            calChance(margin, ref horses);
            pickWinnerPercent(horses, ref winner);
        }
        private static void setStats(DataClasses.HorseDataClass winner, ref List <DataClasses.UnitTestingWinnerStatsDataClass> horseWonStats)
        {
            var current = horseWonStats.FirstOrDefault(x => x.Percent.Equals(winner.PercentageChance));

            if (current != null)
            {
                current.Total++;
            }
            else
            {
                horseWonStats.Add(new DataClasses.UnitTestingWinnerStatsDataClass()
                {
                    Total = 1, Percent = winner.PercentageChance
                });
            }
        }
 private void AddHorseButton_Click(object sender, EventArgs e)
 {
     DataClasses.HorseDataClass horse = new DataClasses.HorseDataClass();
     if (RaceCore.HorseRacing.CreateHorse(NameText.Text, numerator, denominator, ref horse))
     {
         setHorse(horse);
         clearFields();
         ClearAllButton.Enabled = true;
         enableRace();
         NameText.Focus();
     }
     else
     {
         MessageBox.Show("Horse creation failed", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
        public static bool CreateHorse(string name, int numerator, int denominator, ref DataClasses.HorseDataClass horse)
        {
            horse = new DataClasses.HorseDataClass();
            if (!HorseRacingHorseValidation.ValidateName(name))
            {
                return(false);
            }

            if (!HorseRacingHorseValidation.ValidateOdd(numerator, denominator))
            {
                return(false);
            }


            horse.Name        = name;
            horse.Denominator = denominator;
            horse.Numerator   = numerator;
            return(true);
        }
Example #6
0
        /// <summary>
        /// This will do a simple test to show that this application can return example answer on question sheet
        /// </summary>
        public static void TestRace()
        {
            Console.WriteLine("Creating horses");
            List <DataClasses.HorseDataClass> horses = new List <DataClasses.HorseDataClass>();

            createHorses(ref horses);
            Console.WriteLine("Horses created");

            Console.WriteLine("");
            Console.WriteLine("Calulate margine");
            decimal margin = RaceCore.HorseRacingMargin.CalculateMargin(horses);

            Console.WriteLine(String.Format(" Margine: {0}", margin));
            Console.WriteLine("");
            Console.WriteLine("Race");
            DataClasses.HorseDataClass winner = new DataClasses.HorseDataClass();
            RaceCore.HorseRacing.Race(horses, ref winner);
            Console.WriteLine(String.Format(" Horse {0} has a {1}% chance of winning has had the Odds of {2} has WON", winner.Name, winner.Odds, Math.Round(winner.PercentageChance, 1)));
            Console.ReadKey();
        }
        private static void createHorses(ref List <DataClasses.HorseDataClass> horses)
        {
            horses = new List <DataClasses.HorseDataClass>();

            DataClasses.HorseDataClass horse = new DataClasses.HorseDataClass();


            if (RaceCore.HorseRacing.CreateHorse("One over two", 1, 2, ref horse))
            {
                horses.Add(horse);
            }

            if (RaceCore.HorseRacing.CreateHorse("Two over one", 2, 1, ref horse))
            {
                horses.Add(horse);
            }

            if (RaceCore.HorseRacing.CreateHorse("Three over one", 3, 1, ref horse))
            {
                horses.Add(horse);
            }

            if (RaceCore.HorseRacing.CreateHorse("Eight over one", 8, 1, ref horse))
            {
                horses.Add(horse);
            }


            Console.WriteLine("Horses created details");
            foreach (var item in horses)
            {
                Console.WriteLine(String.Format("  Name {0}, numerator {1}, denominator {2}, Odd {3}, Implied probability {4}", item.Name, item.Numerator, item.Denominator, item.Odds, item.ImpliedProbability.ToString("#.#")));
            }

            Console.WriteLine("All look good? (Nothing will come from your answer, press key to continue)");
            Console.ReadKey();
            Console.WriteLine("");
        }
        /// <summary>
        /// This will do a simple test to show that this application can return example answer on question sheet
        /// </summary>
        public static void TestRace()
        {
            Console.WriteLine("Creating horses");
            List <DataClasses.HorseDataClass> horses = new List <DataClasses.HorseDataClass>();

            createHorses(ref horses);
            decimal margin = RaceCore.HorseRacingMargin.CalculateMargin(horses);

            DataClasses.HorseDataClass winner = new DataClasses.HorseDataClass();
            int loop = 0;
            List <DataClasses.UnitTestingWinnerStatsDataClass> horseWonStats = new List <DataClasses.UnitTestingWinnerStatsDataClass>();

            while (loop < TOTALRACES)
            {
                RaceCore.HorseRacing.Race(horses, ref winner);
                Console.WriteLine(String.Format(" {3}/{4} Horse {0} has a {1}% chance of winning has had the Odds of {2} has WON", winner.Name, winner.Odds, Math.Round(winner.PercentageChance, 1), loop, TOTALRACES));
                loop++;
                setStats(winner, ref horseWonStats);
            }
            Console.WriteLine("");
            Console.WriteLine(String.Format("{0} races have now completed.  Press key to see stats", TOTALRACES));
            showStats(horseWonStats);
            Console.ReadKey();
        }
 private void setHorse(DataClasses.HorseDataClass horse)
 {
     HorsesList.Items.Add(horse);
     HorsesList.DisplayMember = "Name";
 }
        private static void pickWinnerPercent(List <DataClasses.HorseDataClass> horses, ref DataClasses.HorseDataClass winner)
        {
            var  rnd   = new Random();
            bool found = false;

            while (!found)
            {
                int percentValue = rnd.Next(0, 100);
                var result       = horses.FirstOrDefault(x => x.PercentageChanceFull.Equals(percentValue));
                if (result != null)
                {
                    winner = result;
                    found  = true;
                    break;
                }
            }
        }
        private static void pickWinnerRandom(List <DataClasses.HorseDataClass> horses, ref DataClasses.HorseDataClass winner)
        {
            var rnd    = new Random();
            var result = horses.OrderBy(item => rnd.Next()).First();

            winner = result;
        }