private void RunTestCase(int threadCount, Level level, Rocket expectedResult)
 {
     channel.AppendIfLastItemIsUnchanged(level.InitialRocket, null);
     if (threadCount == 0)
     {
         ComparePerformances(new My_Bot(level, channel, PerformanceMoves, PerformanceIterations, random, 1),
                             new My_Bot(level, channel, PerformanceMoves, PerformanceIterations, random, 2), level.InitialRocket);
     }
     else
     {
         var bot = new My_Bot(level, channel, CorrectnessMoves, CorrectnessIterations, random, threadCount);
         CheckAlmostAlwaysEquals(expectedResult, bot, level.InitialRocket);
     }
 }
        private void CheckAlmostAlwaysEquals(Rocket expectedResult, My_Bot bot, Rocket initialRocket, int times = 1000)
        {
            var successes = 0;

            for (var i = 0; i < times; ++i)
            {
                var actualResult = bot.GetNextMove(initialRocket);
                if (actualResult.Equals(expectedResult))
                {
                    ++successes;
                }
            }

            Assert.GreaterOrEqual(successes, 0.8 * times);
        }
示例#3
0
        private static void Main()
        {
            var channel = new Channel <Rocket>();
            var random  = new Random(223243);
            var level   = LevelsFactory.CreateLevel(random);

            channel.AppendIfLastItemIsUnchanged(level.InitialRocket, null);
            var bot    = new My_Bot(level.Clone(), channel, 45, 1000, random, 2);
            var thread = new Thread(() => bot.RunInfiniteLoop())
            {
                IsBackground = true
            };

            thread.Start();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new GameForm(level.Clone(), channel);

            Application.Run(form);
        }
        private void ComparePerformances(My_Bot singleThreadBot, My_Bot multiThreadBot, Rocket initialRocket, int times = 40)
        {
            var successes = 0;
            var stopWatch = new Stopwatch();

            for (var i = 0; i < times; ++i)
            {
                stopWatch.Restart();
                singleThreadBot.GetNextMove(initialRocket);
                stopWatch.Stop();
                var singleThreadBotTime = stopWatch.Elapsed;
                stopWatch.Restart();
                multiThreadBot.GetNextMove(initialRocket);
                stopWatch.Stop();
                var multiThreadBotTime = stopWatch.Elapsed;
                if (multiThreadBotTime < singleThreadBotTime)
                {
                    ++successes;
                }
            }

            Assert.GreaterOrEqual(successes, 0.8 * times,
                                  $"Ваше решение в два потока должно работать быстрее решения в один поток в {times * 0.8} случаев из {times}");
        }