/// <summary> /// Print the grid. /// </summary> /// <param name="minesweeperGrid">Grid to print.</param> public static StringWriter PrintGrid(MinesweeperGrid minesweeperGrid) { StringWriter stringWriter = new StringWriter(); for (int row = 0; row < minesweeperGrid.Height; ++row) { stringWriter.Write(GenerateSeparatorLine(minesweeperGrid)); stringWriter.Write("|"); for (int column = 0; column < minesweeperGrid.Width; ++column) { var cell = minesweeperGrid[row, column]; if (cell.HasMine) { stringWriter.Write(" * "); } else { stringWriter.Write(" {0} ", cell.NumberOfNearbyMines); } stringWriter.Write("|"); } // Write a line between rows. stringWriter.Write("\n"); } stringWriter.Write(GenerateSeparatorLine(minesweeperGrid)); return(stringWriter); }
/// <summary> /// Generate a line that separates the rows of a MinesweeperGrid /// instance being printed. /// </summary> /// <param name="minesweeperGrid">Grid to use to determine the length of the separator line.</param> /// <returns>A string containing the separator line.</returns> private static string GenerateSeparatorLine(MinesweeperGrid minesweeperGrid) { StringBuilder stringBuilder = new StringBuilder("+"); for (int column = 0; column < minesweeperGrid.Width; ++column) { stringBuilder.Append("---+"); } stringBuilder.Append("\n"); return(stringBuilder.ToString()); }
static void Main(string[] args) { // File path could be read from a command line arg. var fileReader = new StreamReader("../../../test.txt"); // Create the grid specification by reading the file. var gridSpecification = GenerateGridSpecification(fileReader); // Create the grid from the specification. var minesweeperGrid = new MinesweeperGrid(gridSpecification); // Print the contents of the grid. var gridText = PrintGrid(minesweeperGrid); Console.WriteLine(gridText); }