Example #1
0
        public SlitherFrame GetTestFrame()
        {
            var frame = new SlitherFrame();

            frame.Time        = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            frame.Kills       = 3;
            frame.SnakeLength = 2500;

            var foodString = File.ReadAllText("food.json");

            frame.Foods = JsonConvert.DeserializeObject <List <Food> >(foodString);

            var snakeString = File.ReadAllText("snake.json");

            frame.Snake = JsonConvert.DeserializeObject <Snake>(snakeString);

            var snakesString = File.ReadAllText("snakes.json");

            frame.Snakes = JsonConvert.DeserializeObject <List <Snake> >(snakesString);

            frame.WorldCenter = new Coordinates {
                X = 21600, Y = 21600
            };

            return(frame);
        }
        public CollisionSliceProcessorTests()
        {
            _collisionSliceProcessor = new CollisionSliceProcessor(new FoodSliceProcessor(new CollisionService()), new BadCollisionSliceProcessor(new CollisionService()), new SelfSliceProcessor(new CollisionService()));

            var testFrame = new TestFrame();

            _slitherFrame = testFrame.GetTestFrame();
        }
        public SelfSliceProcessorTests()
        {
            _selfSliceProcessor = new SelfSliceProcessor(new CollisionService());

            var testFrame = new TestFrame();

            _slitherFrame = testFrame.GetTestFrame();
        }
        public SlitherFrameNormalizerTests()
        {
            _slitherFrameNormalizer = new SlitherFrameNormalizer();

            var testFrame = new TestFrame();

            _slitherFrame = testFrame.GetTestFrame();
        }
Example #5
0
        public FoodSliceProcessorTests()
        {
            _foodSliceProcessor = new FoodSliceProcessor(new CollisionService());

            var testFrame = new TestFrame();

            _slitherFrame = testFrame.GetTestFrame();
        }
Example #6
0
 public GameDecision PlayGame(string id, int millisecondsToAction, [FromBody] SlitherFrame slitherFrame)
 {
     if (ActiveGameDatabase.ActiveGames.ContainsKey(id))
     {
         ActiveGameDatabase.ActiveGames[id].Frames.Add(slitherFrame);
     }
     return(SlitherPlayer.PlayGame(id, slitherFrame, millisecondsToAction));
 }
Example #7
0
        public CollisionMapProcessorTests()
        {
            _collisionMapProcessor = new CollisionMapProcessor(new CollisionMapResolutionProcessor(new CollisionSliceProcessor(new FoodSliceProcessor(new CollisionService()), new BadCollisionSliceProcessor(new CollisionService()), new SelfSliceProcessor(new CollisionService()))), new SlitherFrameNormalizer());

            var testFrame = new TestFrame();

            _slitherFrame = testFrame.GetTestFrame();
        }
Example #8
0
        public BadCollisionSliceProcessorTests()
        {
            _badCollisionSliceProcessor = new BadCollisionSliceProcessor(new CollisionService());

            var testFrame = new TestFrame();

            _slitherFrame = testFrame.GetTestFrame();
        }
 public bool UpdateGame(string id, [FromBody] SlitherFrame slitherFrame)
 {
     if (ActiveGameDatabase.ActiveGames.ContainsKey(id))
     {
         ActiveGameDatabase.ActiveGames[id].Frames.Add(slitherFrame);
         return(true);
     }
     return(false);
 }
        public CollisionSlice ProcessSlice(SlitherFrame slitherFrame, double angleStart, double angleEnd, int distanceStep)
        {
            var slice = new CollisionSlice();

            slice.BadCollisions  = _badCollisionSliceProcessor.ProcessSlice(slitherFrame, angleStart, angleEnd, distanceStep);
            slice.FoodCollisions = _foodSliceProcessor.ProcessSlice(slitherFrame, angleStart, angleEnd, distanceStep);
            slice.SelfCollisions = _selfSliceProcessor.ProcessSlice(slitherFrame, angleStart, angleEnd, distanceStep);
            return(slice);
        }
 private OutcomeScore GetScore(SlitherFrame sourceFrame, SlitherFrame outcomeFrame)
 {
     return(new OutcomeScore
     {
         Alive = true,
         Growth = outcomeFrame.SnakeLength - sourceFrame.SnakeLength,
         Kills = outcomeFrame.Kills - sourceFrame.Kills
     });
 }
        public ProcessedFrame ProcessSingleFrame(SlitherFrame slitherFrame)
        {
            var processedFrame = new ProcessedFrame();

            processedFrame.CollisionMap = _collisionMapProcessor.ProcessCollision(slitherFrame);
            processedFrame.SnakeLength  = slitherFrame.SnakeLength;
            processedFrame.Time         = slitherFrame.Time;
            processedFrame.SnakeAngle   = slitherFrame.Snake.Ang;

            return(processedFrame);
        }
        void SingleFrameCollisionMap()
        {
            var snake = new Snake {
                Ang = 0
            };
            var now   = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var frame = new SlitherFrame {
                SnakeLength = 2, Time = now + 2000, Snake = snake
            };
            var processedFrame = _frameProcessor.ProcessSingleFrame(frame);

            _collisionMapProcessor.Verify(m => m.ProcessCollision(frame), Times.Once(), "Collision processed once per frame");
        }
        void SingleFrameSnakeAngle()
        {
            var snake = new Snake {
                Ang = 0
            };
            var now   = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            var frame = new SlitherFrame {
                SnakeLength = 2, Time = now + 2000, Snake = snake
            };
            var processedFrame = _frameProcessor.ProcessSingleFrame(frame);

            Assert.Equal(snake.Ang, processedFrame.SnakeAngle);
        }
        public CollisionMap ProcessCollision(SlitherFrame slitherFrame)
        {
            var normalizedFrame = _slitherFrameNormalizer.NormalizeFrame(slitherFrame);

            return(new CollisionMap
            {
                LowestResolution = _collisionMapResolutionProcessor.ProcessCollisionMap(normalizedFrame, 8, 1000),
                LowResolution = _collisionMapResolutionProcessor.ProcessCollisionMap(normalizedFrame, 16, 100),
                MediumResolution = _collisionMapResolutionProcessor.ProcessCollisionMap(normalizedFrame, 32, 10),
                HighResolution = _collisionMapResolutionProcessor.ProcessCollisionMap(normalizedFrame, 128, 1),
                HighestResolution = _collisionMapResolutionProcessor.ProcessCollisionMap(normalizedFrame, 512, 1)
            });
        }
        public List<Collision> ProcessSlice(SlitherFrame slitherFrame, double angleStart, double angleEnd, int distanceStep)
        {
            var collision = new List<Collision>();

            var points = slitherFrame.Snake.Pts.Where(p => p.Dying == false);
            for (int index = 0; index < points.Count(); index++)
            {
                var distance = _collisionService.GetDistance(slitherFrame.Snake.Pts[index].Xx, slitherFrame.Snake.Pts[index].Yy, 0, angleStart, angleEnd, distanceStep);
                if (distance != null)
                {
                    collision.Add(new SnakeBody { Distance = distance.Value, Size = slitherFrame.Snake.Sc * 14.5, StartIndex = index, EndIndex = points.Count() - index });
                }
            }

            return collision.OrderBy(c => c.Distance).ToList();
        }
        public GameDecision PlayGame(string id, SlitherFrame slitherFrame, int millisecondsToAction)
        {
            var normalizedFrame = SlitherFrameNormalizer.NormalizeFrame(slitherFrame);
            var processedFrame  = FrameProcessor.ProcessSingleFrame(normalizedFrame);

            var decision = new GameDecision {
                MatchConfidence = 0
            };
            var killDecision = new GameDecision {
                MatchConfidence = 0
            };

            foreach (var game in GameDatabase.Games)
            {
                Parallel.ForEach(game.Frames, (frame, pls, frameIndex) =>
                {
                    if (frame.Outcome.ShortTerm.Alive)
                    {
                        ActionResult actionResult = null;
                        var confidence            = ProcessedFrameMatchAnalyzer.GetMatchConfidence(processedFrame, frame);
                        if (confidence > decision.MatchConfidence)
                        {
                            actionResult = GetActionResult(millisecondsToAction, game, (int)frameIndex);

                            decision.MatchConfidence  = confidence;
                            decision.PredictedOutcome = frame.Outcome;
                            decision.TargetAngle      = actionResult.Angle;
                            decision.Sprint           = actionResult.Sprinting;
                        }
                        if (confidence > killDecision.MatchConfidence && frame.Outcome.ShortTerm.Kills > 0)
                        {
                            if (actionResult == null)
                            {
                                actionResult = GetActionResult(millisecondsToAction, game, (int)frameIndex);
                            }

                            killDecision.MatchConfidence  = confidence;
                            killDecision.PredictedOutcome = frame.Outcome;
                            killDecision.TargetAngle      = actionResult.Angle;
                            killDecision.Sprint           = actionResult.Sprinting;
                        }
                    }
                });
            }

            return(decision);
        }
Example #18
0
        public List <CollisionSlice> ProcessCollisionMap(SlitherFrame slitherFrame, int sliceCount, int distanceStep)
        {
            var fullCircle = Math.PI * 2;
            var sliceSize  = fullCircle / sliceCount;

            var slices = new List <CollisionSlice>();

            var angle = 0.0;

            while (angle + (sliceSize / 2) < fullCircle)
            {
                angle += sliceSize;
                slices.Add(_collisionSliceProcessor.ProcessSlice(slitherFrame, angle, angle + sliceSize, distanceStep));
            }

            return(slices);
        }
        public List <FoodCollision> ProcessSlice(SlitherFrame slitherFrame, double angleStart, double angleEnd, int distanceStep)
        {
            var foods = new List <FoodCollision>();

            foreach (var food in slitherFrame.Foods)
            {
                if (food != null)
                {
                    var distance = _collisionService.GetDistance(food.Xx, food.Yy, 1, angleStart, angleEnd, distanceStep);
                    if (distance != null)
                    {
                        foods.Add(new FoodCollision {
                            Distance = distance.Value, Size = 1
                        });
                    }
                }
            }

            return(foods.OrderBy(c => c.Distance).ToList());
        }
Example #20
0
        public List <Collision> ProcessSlice(SlitherFrame slitherFrame, double angleStart, double angleEnd, int distanceStep)
        {
            var collision  = new List <Collision>();
            var selfRadius = GetSnakeRadius(slitherFrame.Snake);

            foreach (var snake in slitherFrame.Snakes)
            {
                if (snake.DeadAmt == 0)
                {
                    var snakeRadius = GetSnakeRadius(snake);
                    var radius      = selfRadius + snakeRadius;
                    var distance    = _collisionService.GetDistance(snake.Xx, snake.Yy, radius, angleStart, angleEnd, distanceStep);
                    if (distance != null)
                    {
                        collision.Add(new SnakeHead {
                            Distance = distance.Value, RelativeAngle = snake.Ang, Size = snake.Pts.Count
                        });
                    }

                    var points = snake.Pts.Where(p => p.Dying == false);
                    for (int index = 0; index < points.Count(); index++)
                    {
                        distance = _collisionService.GetDistance(snake.Pts[index].Xx, snake.Pts[index].Yy, radius, angleStart, angleEnd, distanceStep);
                        if (distance != null)
                        {
                            collision.Add(new SnakeBody {
                                Distance = distance.Value, Size = snakeRadius, StartIndex = index, EndIndex = points.Count() - index
                            });
                        }
                    }
                }
            }

            collision.Add(new Boundry {
                Distance = GetBoundryDistance(slitherFrame.WorldCenter, selfRadius, angleStart, angleEnd, distanceStep)
            });

            return(collision.OrderBy(c => c.Distance).ToList());
        }
        /// <summary>
        /// normalize the map so the snake angle is 0 and the snake point is 0,0
        /// </summary>
        /// <param name="slitherFrame"></param>
        /// <returns></returns>
        public SlitherFrame NormalizeFrame(SlitherFrame slitherFrame)
        {
            var angleChange        = -slitherFrame.Snake.Ang;
            var centerShiftedPoint = new Coordinates {
                X = worldRadius - slitherFrame.Snake.Xx, Y = worldRadius - slitherFrame.Snake.Yy
            };
            var pointChange = new Coordinates {
                X = worldRadius - centerShiftedPoint.X, Y = worldRadius - centerShiftedPoint.Y
            };

            var normalizedFrame = new SlitherFrame();

            normalizedFrame.Snake       = GetNormalizedSnake(slitherFrame.Snake, pointChange, angleChange);
            normalizedFrame.Snakes      = GetNormalizedSnakes(slitherFrame.Snakes, pointChange, angleChange);
            normalizedFrame.Foods       = GetNormalizedFoods(slitherFrame.Foods, pointChange, angleChange);
            normalizedFrame.WorldCenter = RotateCoordinates(centerShiftedPoint, angleChange);

            normalizedFrame.Kills       = slitherFrame.Kills;
            normalizedFrame.SnakeLength = slitherFrame.SnakeLength;
            normalizedFrame.Time        = slitherFrame.Time;

            return(normalizedFrame);
        }