/// <summary> /// Initializes a new instance of the <see cref="State"/> class. /// </summary> /// <param name="northTileStatus">North tile status.</param> /// <param name="northEastTileStatus">North east tile status.</param> /// <param name="eastTileStatus">East tile status.</param> /// <param name="southEastStatus">South east status.</param> /// <param name="southTileStatus">South tile status.</param> /// <param name="southWestTileStatus">South west tile status.</param> /// <param name="westTileStatus">West tile status.</param> /// <param name="northWestTileStatus">North west tile status.</param> /// <param name="position">Current position.</param> public State(Tile.MowStatus northTileStatus, Tile.MowStatus northEastTileStatus, Tile.MowStatus eastTileStatus, Tile.MowStatus southEastStatus, Tile.MowStatus southTileStatus, Tile.MowStatus southWestTileStatus, Tile.MowStatus westTileStatus, Tile.MowStatus northWestTileStatus, Garden.GridPosition position ) { this.NorthTileStatus = northTileStatus; this.NorthEastTileStatus = northEastTileStatus; this.EastTileStatus = eastTileStatus; this.SouthEastTileStatus = southEastStatus; this.SouthTileStatus = southTileStatus; this.SouthWestTileStatus = southWestTileStatus; this.WestTileStatus = westTileStatus; this.NorthWestTileStatus = northWestTileStatus; this.Position = position; }
/// <summary> /// Sets the status of the tile at the given position in the garden. /// </summary> /// <param name="position">The Ppsition of the tile.</param> /// <param name="status">The status to set.</param> public void SetTileStatus(GridPosition position, Tile.MowStatus status) { SetTileStatus((uint)position.X, (uint)position.Y, status); }
/// <summary> /// Creates a Garden object using a xml-based representation. /// </summary> /// <returns>The garden from the xml file.</returns> /// <param name="filename">The filename of the xml-file defining the garden.</param> /// <param name="paramSet">The set of parameters for this project.</param> public static Garden CreateGardenFromFile(string filename, ParamSet paramSet) { // Init the xml document XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filename); // Init containers for the extracted information uint gardenWidth, gardenHeight; Garden.GridPosition mowerStartPosition; List <Tile> tiles; List <MovingObstacle> movingObstacles; List <StaticObstacle> staticObstacles = new List <StaticObstacle>(); bool parseResult = true; XmlNode gardenNode = xmlDoc.SelectSingleNode("/" + gardenNodeName); // Parse the garden if (gardenNode == null) { throw new InvalidGardenXMLException(MissingNodeErrorMessage(gardenNodeName)); } XmlNode widthAttribute = gardenNode.SelectSingleNode("@" + widthAttributeName); if (widthAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(gardenNodeName, widthAttributeName)); } parseResult = uint.TryParse(widthAttribute.InnerText, out gardenWidth); XmlNode heightAttribute = gardenNode.SelectSingleNode("@" + heightAttributeName); if (heightAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(gardenNodeName, heightAttributeName)); } parseResult = uint.TryParse(heightAttribute.InnerText, out gardenHeight); // Parse the mower starting position XmlNode startPosNode = gardenNode.SelectSingleNode(startPosNodeName); if (startPosNode == null) { throw new InvalidGardenXMLException(MissingNodeErrorMessage(startPosNodeName)); } XmlNode xAttribute = startPosNode.SelectSingleNode("@" + xPosAttributeName); if (xAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(startPosNodeName, xPosAttributeName)); } uint x, y; parseResult = uint.TryParse(xAttribute.InnerText, out x); XmlNode yAttribute = startPosNode.SelectSingleNode("@" + yPosAttributeName); if (yAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(startPosNodeName, yPosAttributeName)); } parseResult = uint.TryParse(yAttribute.InnerText, out y); mowerStartPosition = new Garden.GridPosition((int)x, (int)y); // Parse the tiles XmlNodeList tileNodes = gardenNode.SelectNodes(tilesNodeName + "/" + tileNodeName); if (tileNodes.Count != (gardenWidth * gardenHeight)) { throw new InvalidGardenXMLException(string.Format("Only found {0} <" + tileNodeName + "> nodes. Expecting {1}.", tileNodes.Count, gardenWidth * gardenHeight)); } tiles = new List <Tile>(tileNodes.Count); foreach (XmlNode tileNode in tileNodes) { xAttribute = tileNode.SelectSingleNode("@" + xPosAttributeName); if (xAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(tileNodeName, xPosAttributeName)); } parseResult = uint.TryParse(xAttribute.InnerText, out x); yAttribute = tileNode.SelectSingleNode("@" + yPosAttributeName); if (yAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(tileNodeName, yPosAttributeName)); } parseResult = uint.TryParse(yAttribute.InnerText, out y); XmlNode typeAttribute = tileNode.SelectSingleNode("@" + typeAttributeName); if (typeAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(tileNodeName, typeAttributeName)); } Garden.GridPosition position = new Garden.GridPosition((int)x, (int)y); Tile.MowStatus tileStatus = Tile.MowStatus.LongGrass; switch (typeAttribute.InnerText) { case tileTypeLongGrass: tileStatus = Tile.MowStatus.LongGrass; break; case tileTypeShortGrass: tileStatus = Tile.MowStatus.ShortGrass; break; case tileTypeRock: tileStatus = Tile.MowStatus.Obstacle; staticObstacles.Add(new StaticObstacle(position, StaticObstacle.StaticObstacleType.Rock)); break; case tileTypeWater: tileStatus = Tile.MowStatus.Obstacle; staticObstacles.Add(new StaticObstacle(position, StaticObstacle.StaticObstacleType.Water)); break; case tileTypeChargingStation: tileStatus = Tile.MowStatus.ChargingStation; break; default: throw new InvalidGardenXMLException( InvalidAttributeValueInNodeErrorMessage(tileNodeName, new KeyValuePair <string, string>(typeAttributeName, typeAttribute.InnerText)) ); } bool occupiedByMower = position.Equals(mowerStartPosition); tiles.Add(new Tile(position, tileStatus, occupiedByMower)); } // Parse the moving obstacles XmlNodeList obstacleNodes = gardenNode.SelectNodes(movingObstaclesNodeName + "/" + movingObstacleNodeName); movingObstacles = new List <MovingObstacle>(obstacleNodes.Count); foreach (XmlNode obstacleNode in obstacleNodes) { xAttribute = obstacleNode.SelectSingleNode("@" + xPosAttributeName); if (xAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(movingObstacleNodeName, xPosAttributeName)); } parseResult = uint.TryParse(xAttribute.InnerText, out x); yAttribute = obstacleNode.SelectSingleNode("@" + yPosAttributeName); if (yAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(movingObstacleNodeName, yPosAttributeName)); } parseResult = uint.TryParse(yAttribute.InnerText, out y); XmlNode typeAttribute = obstacleNode.SelectSingleNode("@" + typeAttributeName); if (typeAttribute == null) { throw new InvalidGardenXMLException(MissingAttributeInNodeErrorMessage(movingObstacleNodeName, typeAttributeName)); } Garden.GridPosition position = new Garden.GridPosition((int)x, (int)y); MovingObstacle.MovingObstacleType obstacleType = MovingObstacle.MovingObstacleType.Animal; switch (typeAttribute.InnerText) { case movingObstacleTypeAnimal: obstacleType = MovingObstacle.MovingObstacleType.Animal; break; case movingObstacleTypePerson: obstacleType = MovingObstacle.MovingObstacleType.Person; break; default: throw new InvalidGardenXMLException( InvalidAttributeValueInNodeErrorMessage(movingObstacleNodeName, new KeyValuePair <string, string>(typeAttributeName, typeAttribute.InnerText)) ); } MovingObstacle obstacle = new MovingObstacle(position, obstacleType); // Register the tile as occupied foreach (var tile in tiles) { if (tile.Position.Equals(position)) { if (tile.Occupied == true) { throw new InvalidGardenXMLException(string.Format("Multiple objects positioned at {0}.", position)); } else { tile.Occupied = true; } break; } } movingObstacles.Add(obstacle); // Save the initial state of all tiles foreach (var tile in tiles) { tile.SetCurrentStateAsInitialState(); } } // Create the garden (finally...) return(new Garden(gardenWidth, gardenHeight, tiles, mowerStartPosition, movingObstacles, staticObstacles, paramSet)); }