/// <summary> /// The Main() method creates an instances of the AdjacencyMatrix class and the Engine class. Then it asigns all the input from the /// static variables to the matrix and runs the Engine.Start() method passing the target element data for receiving the result /// and printing it to the console as required. /// </summary> public static void Main() { try { ReadInput(); //Declaring the matrix and completing it with the data from the input which has been already checked var matrix = new AdjacencyMatrix(width, height); if (matrix == null) { throw new NullReferenceException(nullMatrixMessage); } for (int row = 0; row < height; row++) { var inputRow = inputMatrix[row]; for (int col = 0; col < width; col++) { //Checks value from input (0 or 1) and creates an object with the relevant type {RedNode() or GreenNode()} //according to the task requirement if (inputRow[col] == '1') { matrix.Nodes[row, col] = new GreenNode(); } if (inputRow[col] == '0') { matrix.Nodes[row, col] = new RedNode(); } } } //1. Initializing the Engine class and injecting a reference of the completed matrix. //2. Invoking the Engine.Start(...) method for extracting the result var engine = new Engine(matrix); var result = engine.Start(targetRow, targetCol, generationsCount); //Printing the result on the console Console.WriteLine(result); Console.ReadKey(); } catch (InputException inputException) { Console.WriteLine(inputErrorMessage + inputException.Message); //throw; } catch (Exception exception) { Console.WriteLine(exception.Message); } }
public Engine(AdjacencyMatrix matrix) { if (matrix != null) { this._matrix = matrix; } else { throw new NullReferenceException("Empty matrix"); } }