/// <summary> /// Recursively goes through each character in a line and checks whether it is a <see cref="Mine"/> or a /// <see cref="Safe"/>. If it is a mine call the <see cref="IncrementNeighbours"/> method to increment the /// BorderingMines of the boxes within a 1 box range. If it is safe instantiate a new safe component. /// /// The recursion stops once the width value matches the BoardWidth value. /// </summary> /// <param name="line">The current line to be parsed</param> /// <param name="length">The length value of the char to be parsed</param> /// <param name="width">The width value of the char to be parsed</param> /// <exception cref="Exception">If a game specification does not match '.' or '*' throw an exception</exception> private void ParseLine(string line, int length, int width) { //Base Case when the width matches the Board Width return if (width == BoardWidth) { return; } //Iterative Case parse one character and recurse over method to the next character var coordinates = new Coordinates { CoordinateX = length, CoordinateY = width }; switch (line.ElementAt(width)) { case Mine: Box[length, width] = new MinesweeperBox(new Mine(coordinates)); IncrementNeighbours(coordinates, 1); break; case Safe: Box[length, width] = new MinesweeperBox (new Safe(IncrementComponent(GetComponent(coordinates)), coordinates)); break; default: throw new Exception("Incorrect Character Found"); } ParseLine(line, length, width + 1); }
/// <summary> /// This method builds the Board and returns itself as a finished board object. /// </summary> /// <param name="boardSettings">The length and width of the board</param> /// <param name="lines">The game specifications to be parsed</param> /// <returns>A finished board objected parsed from the given parameters</returns> public Board CreateBoard(List <int> boardSettings, List <string> lines) { //Create the board Validator.ValidateLength(lines.Count); BoardLength = boardSettings.ElementAt(0); BoardWidth = boardSettings.ElementAt(1); Box = new MinesweeperBox[BoardLength, BoardWidth]; //Fill the board ParseLines(lines, 0); //Return the board return(this); }