示例#1
0
        public static Map FromJson(JObject json, string playerId)
        {
            int width  = (int)json["width"];
            int height = (int)json["height"];
            int tick   = (int)json["worldTick"];

            var snakes = json["snakeInfos"].Select(token =>
            {
                string name   = (string)token["name"];
                string id     = (string)token["id"];
                int points    = (int)token["points"];
                var positions = token["positions"].Select(i => MapCoordinate.FromIndex((int)i, width));
                return(new SnakePlayer(id, name, points, positions));
            }).ToList();

            var mySnake = snakes.FirstOrDefault(s => s.Id.Equals(playerId));

            var foods     = json["foodPositions"].Select(i => MapCoordinate.FromIndex((int)i, width));
            var obstacles = json["obstaclePositions"].Select(i => MapCoordinate.FromIndex((int)i, width));

            return(new Map(width, height, tick, mySnake, snakes, foods, obstacles));
        }
示例#2
0
 public static IEnumerable <MapCoordinate> Neighbours(this MapCoordinate mapCoordinate)
 {
     return(mapCoordinate.NeighboursOfDistance(1));
 }
示例#3
0
 public bool IsObstace(MapCoordinate coordinate)
 {
     return(ObstaclePositions.Contains(coordinate));
 }
示例#4
0
 public bool IsFood(MapCoordinate coordinate)
 {
     return(FoodPositions.Contains(coordinate));
 }
示例#5
0
 protected bool Equals(MapCoordinate other)
 {
     return(X == other.X && Y == other.Y);
 }
示例#6
0
 public bool IsSnake(MapCoordinate coordinate)
 {
     return(SnakeParts.Contains(coordinate));
 }
示例#7
0
 public int GetManhattanDistanceTo(MapCoordinate other)
 {
     return(Math.Abs(X - other.X) + Math.Abs(Y - other.Y));
 }
示例#8
0
 public double GetAreaDistanceTo(MapCoordinate other)
 {
     return((Math.Abs(X - other.X) + 1) * (Math.Abs(Y - other.Y) + 1));
 }
示例#9
0
 public bool IsSafe(MapCoordinate mapCoordinate) => this[mapCoordinate].IsSafe();
示例#10
0
 public bool IsDanger(MapCoordinate mapCoordinate) => this[mapCoordinate].IsDanger();
示例#11
0
 public TileType this[MapCoordinate mapCoordinate] => TileGrid.ContainsKey(mapCoordinate) ? TileGrid[mapCoordinate] : TileType.OutOfBounds;
示例#12
0
 public bool IsFood(MapCoordinate coordinate)
 {
     return(this[coordinate] == TileType.Food);
 }
示例#13
0
 public bool IsObstacle(MapCoordinate coordinate)
 {
     return(this[coordinate] == TileType.Obstacle);
 }