示例#1
0
        public MoveDirection GetMove(Map map, PlayerType playerToMove, State state, TimeSpan timeout, int turns, int maxruns = int.MaxValue)
        {
            this.Sw = new Stopwatch();
            this.Sw.Start();
            this.PlayerToMove = playerToMove;

            var hero   = state.GetHero(playerToMove);
            var source = map[hero];

            options.Clear();
            foreach (var dir in source.Directions.Where(d => d != MoveDirection.x))
            {
                options[dir] = new Dictionary <PlayerType, long>()
                {
                    { PlayerType.Hero1, 0 },
                    { PlayerType.Hero2, 0 },
                    { PlayerType.Hero3, 0 },
                    { PlayerType.Hero4, 0 },
                };
            }
            runs = 0;

            while (runs < maxruns && this.Sw.Elapsed < timeout)
            {
                if (RunParallel)
                {
                    Parallel.ForEach(options.Keys, (dir) =>
                    {
                        var rnd    = Rnds[dir];
                        var target = source[dir];
                        GetScore(map, state, hero, playerToMove, source, target, dir, rnd, turns);
                    });
                }
                else
                {
                    foreach (var dir in options.Keys)
                    {
                        var target = source[dir];
                        GetScore(map, state, hero, playerToMove, source, target, dir, this.Rnds[MoveDirection.N], turns);
                    }
                }
            }
            this.Sw.Stop();

            this.Decision = AverageScoreDecision.Create(options, runs, this.PlayerToMove);
            return(this.Decision.Move);
        }
        public void Create_3OptionsIn17Runs_West()
        {
            var scores = new Dictionary <MoveDirection, Dictionary <PlayerType, long> >()
            {
                { MoveDirection.E, new Dictionary <PlayerType, long>()
                  {
                      { PlayerType.Hero1, 12345 },
                      { PlayerType.Hero2, 80345 },
                      { PlayerType.Hero3, 45345 },
                      { PlayerType.Hero4, 45345 },
                  } },
                { MoveDirection.W, new Dictionary <PlayerType, long>()
                  {
                      { PlayerType.Hero1, 12345 },
                      { PlayerType.Hero2, 80345 },
                      { PlayerType.Hero3, 45345 },
                      { PlayerType.Hero4, 71234 },
                  } },
                { MoveDirection.S, new Dictionary <PlayerType, long>()
                  {
                      { PlayerType.Hero1, 1200 },
                      { PlayerType.Hero2, 1200 },
                      { PlayerType.Hero3, 1200 },
                      { PlayerType.Hero4, 1201 },
                  } }
            };

            var act = AverageScoreDecision.Create(scores, 17 * 3, PlayerType.Hero4);

            foreach (var score in act.Scores)
            {
                Console.WriteLine(score.DebuggerDisplay);
            }

            var expMove  = MoveDirection.W;
            var expScore = "h1: 726.17, h2: 4,726.17, h3: 2,667.35, h4: 4,190.23";

            Assert.AreEqual(expMove, act.Move);
            Assert.AreEqual(expScore, act.Score.DebuggerDisplay);
        }