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)); }
public static IEnumerable <MapCoordinate> Neighbours(this MapCoordinate mapCoordinate) { return(mapCoordinate.NeighboursOfDistance(1)); }
public bool IsObstace(MapCoordinate coordinate) { return(ObstaclePositions.Contains(coordinate)); }
public bool IsFood(MapCoordinate coordinate) { return(FoodPositions.Contains(coordinate)); }
protected bool Equals(MapCoordinate other) { return(X == other.X && Y == other.Y); }
public bool IsSnake(MapCoordinate coordinate) { return(SnakeParts.Contains(coordinate)); }
public int GetManhattanDistanceTo(MapCoordinate other) { return(Math.Abs(X - other.X) + Math.Abs(Y - other.Y)); }
public double GetAreaDistanceTo(MapCoordinate other) { return((Math.Abs(X - other.X) + 1) * (Math.Abs(Y - other.Y) + 1)); }
public bool IsSafe(MapCoordinate mapCoordinate) => this[mapCoordinate].IsSafe();
public bool IsDanger(MapCoordinate mapCoordinate) => this[mapCoordinate].IsDanger();
public TileType this[MapCoordinate mapCoordinate] => TileGrid.ContainsKey(mapCoordinate) ? TileGrid[mapCoordinate] : TileType.OutOfBounds;
public bool IsFood(MapCoordinate coordinate) { return(this[coordinate] == TileType.Food); }
public bool IsObstacle(MapCoordinate coordinate) { return(this[coordinate] == TileType.Obstacle); }