/// <summary> /// Experimental: http://www.informit.com/articles/article.aspx?p=2438407&seqNum=11 /// Unfinished. /// </summary> //private class CommandLineClass //{ // public string Action { get; set; } // public string AfterAction { get; set; } // public CommandLineClass(string[] arguments) // { // for (int argCounter = 0; argCounter < arguments.Length; argCounter++) // { // switch (argCounter) // { // case 0: // Action = arguments[0].ToLower(); // break; // default: // AfterAction += arguments[argCounter]; // break; // } // } // } //} /// <summary> /// Lists the initial values of the puzzle file. /// </summary> /// <param name="xmlPuzzleFile">Test files: @"../../puzzle1.xml", @"../../puzzle2.xml", @"../../MxNpuzzle1.xml", </param> private static void TestPrintXmlInitialValues(string xmlPuzzleFile) { TextReader puzzleStream = OpenPuzzleFile(xmlPuzzleFile); XmlPuzzle puzzle = DeserializeXmlPuzzle(puzzleStream); Console.WriteLine("\nInitial values: "); for (int i = 0; i < puzzle.InitialValues.Cells.Count; i++) { Console.WriteLine("Row: " + puzzle.InitialValues.Cells[i].Row.ToString() + " Col: " + puzzle.InitialValues.Cells[i].Col.ToString() + " Value: " + puzzle.InitialValues.Cells[i].Value); } }
/// <summary> /// Creates a sudoku grid according to the puzzle file params /// </summary> /// <param name="puzzle"></param> public Grid(XmlPuzzle puzzle) { //TODO error catch for puzzle obj import on grid creation if (!(puzzle.Dimensions == null)) { Alphabet = puzzle.Alphabet.Replace("\"", ""); int MxN = puzzle.Dimensions.Columns * puzzle.Dimensions.Rows; GridArr = new Cell[MxN, MxN]; SubgridRows = puzzle.Dimensions.Rows; SubgridCols = puzzle.Dimensions.Columns; AlphabetArr = Alphabet.ToArray(); } else { Alphabet = "123456789"; GridArr = new Cell[9, 9]; SubgridRows = 3; SubgridCols = 3; AlphabetArr = Alphabet.ToArray(); } PuzzleSize = SubgridRows * SubgridCols; //Write the alphabet to every cell of the grid. for (int row = 0; row < GridArr.GetLength(0); row++) { for (int col = 0; col < GridArr.GetLength(1); col++) { GridArr[row, col] = new Cell(row, col, Alphabet); } } //TODO error catch for puzzle obj var initialValues = puzzle.InitialValues.Cells; //Overwrite default grid with initial values for (int i = 0; i < initialValues.Count; i++) { GridArr[initialValues[i].Row, initialValues[i].Col] = new Cell(initialValues[i]); } //Create a list of subgrids Subgrids = new List <List <Cell> >(GridArr.GetLength(0)); for (int colIndex = 0; colIndex < SubgridRows; colIndex++) { for (int rowIndex = 0; rowIndex < SubgridCols; rowIndex++) { Subgrids.Add(GetSubgrid(rowIndex, colIndex)); } } }
/// <summary> /// TODO /// </summary> /// <param name="reader"></param> /// <returns></returns> private static XmlPuzzle DeserializeXmlPuzzle(TextReader reader) { XmlSerializer deserializer = new XmlSerializer(typeof(XmlPuzzle)); XmlPuzzle xmlData = null; try { var puzzle = deserializer.Deserialize(reader); xmlData = (XmlPuzzle)puzzle; } catch (InvalidOperationException e) { Console.WriteLine(e.Message); Console.WriteLine("Exit code: 3"); if (errorLogFlag) { using (errorLog) { errorLog.Write($"{e.Message}\nExit code: 3"); } } Console.ReadLine(); Environment.Exit(3); } catch (Exception e) { Console.WriteLine(e.Message); if (errorLogFlag) { using (errorLog) { errorLog.Write(e.Message); } } Console.ReadLine(); throw e; } finally { reader.Close(); } return xmlData; }