public void Print(string s, int x, int y) { if (s.Equals("bu")) { Console.WriteLine("(" + x + ";" + y + ") " + isPushed); } else if (s.Equals("buttonConnectives")) { foreach (Switchable switchable in traps) { FallTrap ft = (FallTrap)switchable; Console.WriteLine("(" + x + ";" + y + ")-(" + ft.GetMasterField().coordX + ";" + ft.GetMasterField().coordY + ")"); } } }
public void LoadMap(string filename) { // Read the file and display it line by line. using (StreamReader file = new StreamReader(filename + @".txt")) { Dictionary <int, Dictionary <int, Field> > fieldsMap = new Dictionary <int, Dictionary <int, Field> >(); Dictionary <int, Dictionary <int, Box> > boxesMap = new Dictionary <int, Dictionary <int, Box> >(); Dictionary <int, Dictionary <int, BoxContainer> > boxContainerMap = new Dictionary <int, Dictionary <int, BoxContainer> >(); Dictionary <int, Dictionary <int, FallTrap> > fallTrapMap = new Dictionary <int, Dictionary <int, FallTrap> >(); Dictionary <int, Dictionary <int, Button> > buttonMap = new Dictionary <int, Dictionary <int, Button> >(); Dictionary <int, Dictionary <int, Hole> > holeMap = new Dictionary <int, Dictionary <int, Hole> >(); const short x = 0; const short y = 1; int[] coordinates; string inputLine; string state = "none"; string[] coordinateParts; string[] paramList; while ((inputLine = file.ReadLine()) != null) { if (inputLine.EndsWith(":")) { // inputLine is the next state state = inputLine; } else { switch (state) { case "fields:": coordinates = CoordSplit(inputLine); if (!fieldsMap.ContainsKey(coordinates[x])) { fieldsMap.Add(coordinates[x], new Dictionary <int, Field>()); } Field f = new Field(coordinates[x], coordinates[y]); f.SetFriction(1); fieldsMap[coordinates[x]].Add(coordinates[y], f); break; case "box:": coordinates = CoordSplit(inputLine); if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("NOPE"); } if (!boxesMap.ContainsKey(coordinates[x])) { boxesMap.Add(coordinates[x], new Dictionary <int, Box>()); } boxesMap[coordinates[x]].Add(coordinates[y], new Box()); fieldsMap[coordinates[x]][coordinates[y]].AddMoveable(boxesMap[coordinates[x]][coordinates[y]]); break; case "boxcontainers:": coordinates = CoordSplit(inputLine); if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("NOPE"); } if (!boxContainerMap.ContainsKey(coordinates[x])) { boxContainerMap.Add(coordinates[x], new Dictionary <int, BoxContainer>()); } boxContainerMap[coordinates[x]].Add(coordinates[y], new BoxContainer()); fieldsMap[coordinates[x]][coordinates[y]].AddFeature(boxContainerMap[coordinates[x]][coordinates[y]]); break; case "button:": coordinates = CoordSplit(inputLine); if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("NOPE"); } if (!buttonMap.ContainsKey(coordinates[x])) { buttonMap.Add(coordinates[x], new Dictionary <int, Button>()); } buttonMap[coordinates[x]].Add(coordinates[y], new Button()); fieldsMap[coordinates[x]][coordinates[y]].AddFeature(buttonMap[coordinates[x]][coordinates[y]]); break; case "falltrap:": coordinates = CoordSplit(inputLine); if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine); } if (!fallTrapMap.ContainsKey(coordinates[x])) { fallTrapMap.Add(coordinates[x], new Dictionary <int, FallTrap>()); } fallTrapMap[coordinates[x]].Add(coordinates[y], new FallTrap(fieldsMap[coordinates[x]][coordinates[y]])); fieldsMap[coordinates[x]][coordinates[y]].AddFeature(fallTrapMap[coordinates[x]][coordinates[y]]); break; case "hole:": coordinates = CoordSplit(inputLine); if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine); } if (!holeMap.ContainsKey(coordinates[x])) { holeMap.Add(coordinates[x], new Dictionary <int, Hole>()); } holeMap[coordinates[x]].Add(coordinates[y], new Hole()); fieldsMap[coordinates[x]][coordinates[y]].AddFeature(holeMap[coordinates[x]][coordinates[y]]); break; case "neighbourhood:": paramList = inputLine.Split(' '); coordinateParts = paramList[0].Split('-'); coordinates = CoordSplit(coordinateParts[0]); // parse first part if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine); } Field first = fieldsMap[coordinates[x]][coordinates[y]]; coordinates = CoordSplit(coordinateParts[1]); // parse first part if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine); } Field second = fieldsMap[coordinates[x]][coordinates[y]]; first.AddNeighbour(second, stringToDirection(paramList[2])); second.AddNeighbour(first, stringToDirection(paramList[1])); break; case "wires:": coordinateParts = inputLine.Split('-'); coordinates = CoordSplit(coordinateParts[0]); // parse first part if (!(buttonMap.ContainsKey(coordinates[x]) && buttonMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine + ". In state:" + state); } Button buttontmp = buttonMap[coordinates[x]][coordinates[y]]; coordinates = CoordSplit(coordinateParts[1]); // parse second part if (!(fallTrapMap.ContainsKey(coordinates[x]) && fallTrapMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine + ". In state:" + state); } FallTrap fallTraptmp = fallTrapMap[coordinates[x]][coordinates[y]]; buttontmp.AddSwitchable(fallTraptmp); break; case "compatibility:": coordinateParts = inputLine.Split('-'); coordinates = CoordSplit(coordinateParts[0]); // parse first part if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine); } if (!(boxContainerMap.ContainsKey(coordinates[x]) && boxContainerMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("NOPE"); } BoxContainer boxContainertmp = boxContainerMap[coordinates[x]][coordinates[y]]; coordinates = CoordSplit(coordinateParts[1]); // parse second part if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine); } if (!(boxesMap.ContainsKey(coordinates[x]) && boxesMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine + ". In state:" + state); } Box boxtmp = boxesMap[coordinates[x]][coordinates[y]]; boxContainertmp.AddBox(boxtmp); break; case "spawnplaces:": paramList = inputLine.Split(' '); coordinates = CoordSplit(paramList[0]); if (!(fieldsMap.ContainsKey(coordinates[x]) && fieldsMap[coordinates[x]].ContainsKey(coordinates[y]))) { throw new WrongCoordException("Wrong Coords Added at line: " + inputLine); } double[] dtmp = { (double)coordinates[x], (double)coordinates[y], Double.Parse(paramList[1]) }; spawnPlaces.Add(dtmp); break; default: throw new WrongLineFormat("Wrong Status header before line:" + inputLine); } } } foreach (int xc in fieldsMap.Keys) { foreach (int yc in fieldsMap[xc].Keys) { fields.Add(fieldsMap[xc][yc]); } } foreach (int xc in boxesMap.Keys) { foreach (int yc in boxesMap[xc].Keys) { boxes.Add(boxesMap[xc][yc]); } } } }