private static ITile TransposeTile(ITile tile) { if (tile is Pit) return new Pit(); var originalFloorTile = (Floor) tile; Floor floorTile; if (tile is ExpressConveyer) floorTile = new ExpressConveyer(); else if (tile is Conveyer) floorTile = new Conveyer(); else if (tile is WrenchHammer) floorTile = new WrenchHammer(); else if (tile is Wrench) floorTile = new Wrench(); else if (tile is Gear) floorTile = new Gear((tile as Gear).Direction); else floorTile = new Floor(); foreach (var edge in originalFloorTile.Edges) { IEdge newEdge; if (edge.Item2 is WallLaserEdge) newEdge = new WallLaserEdge((edge.Item2 as WallLaserEdge).Lasers); else if (edge.Item2 is WallPusherEdge) newEdge = new WallPusherEdge((edge.Item2 as WallPusherEdge).Registers); else newEdge = new WallEdge(); floorTile.PutEdge(Utilities.GetOppositeOrientation(edge.Item1), newEdge); } var originalConveyer = originalFloorTile as Conveyer; if (originalConveyer != null) { var entrances = originalConveyer.Entrances.Select(Utilities.GetOppositeOrientation).ToList(); var newConveyer = (Conveyer) floorTile; newConveyer.Entrances = entrances; newConveyer.Exit = Utilities.GetOppositeOrientation(originalConveyer.Exit); } return floorTile; }
public static IEnumerable<IEnumerable<ITile>> JsonToMap(string json) { var reader = new JsonTextReader(new StringReader(json)); var deserializedMap = new JsonSerializer().Deserialize<SerializedMap>(reader); var map = new ITile[deserializedMap.Height,deserializedMap.Width]; for (int j = 0; j < deserializedMap.Height; j++) { for (int i = 0; i < deserializedMap.Width; i++) map[j,i] = new Floor(); } foreach (var conveyor in deserializedMap.Conveyors) { if (conveyor.Type == "express") map[conveyor.Row, conveyor.Column] = new ExpressConveyer(conveyor.In.Select(o => ParseOrientation(o)), ParseOrientation(conveyor.Out)); else map[conveyor.Row, conveyor.Column] = new Conveyer(conveyor.In.Select(o => ParseOrientation(o)), ParseOrientation(conveyor.Out)); } foreach (var gear in deserializedMap.Gears) { map[gear.Row, gear.Column] = new Gear(ParseRotation(gear.Type)); } foreach (var wrench in deserializedMap.Wrenches) { if (wrench.Type == "option") map[wrench.Row, wrench.Column] = new WrenchHammer(); else map[wrench.Row, wrench.Column] = new Wrench(); } foreach (var pit in deserializedMap.Pits) { map[pit.Row, pit.Column] = new Pit(); } // Flags foreach (var wall in deserializedMap.Walls) { (map[wall.Row, wall.Column] as Floor).Edges.AddRange(wall.Edges.Select(o => new Tuple<Orientation, IEdge>(ParseOrientation(o), new WallEdge()))); } foreach (var laser in deserializedMap.Lasers) { Orientation laserWallEdge = LaserWallEdgeOrientation(laser); (map[laser.Row, laser.Column] as Floor).Edges.RemoveAll(tup => tup.Item1 == laserWallEdge); (map[laser.Row, laser.Column] as Floor).Edges.Add(new Tuple<Orientation, IEdge>(laserWallEdge, new WallLaserEdge(laser.Damage))); } foreach (var pusher in deserializedMap.Pushers) { Orientation pusherWallEdge = ParseOrientation(pusher.Wall); (map[pusher.Row, pusher.Column] as Floor).Edges.RemoveAll(tup => tup.Item1 == pusherWallEdge); (map[pusher.Row, pusher.Column] as Floor).Edges.Add(new Tuple<Orientation, IEdge>(pusherWallEdge, new WallPusherEdge(pusher.Registers))); } var jaggedArray = new List<List<ITile>>(deserializedMap.Height); for (int j = 0; j < deserializedMap.Height; j++) { jaggedArray.Add(new List<ITile>(deserializedMap.Width)); for (int i = 0; i < deserializedMap.Width; i++) { jaggedArray[j].Add(map[j, i]); } } return jaggedArray; }