/// <summary> /// Returns a string of the entire grid array. /// </summary> /// <returns>String containing the entire grid state.</returns> public override string ToString() { string grid = ""; string line; int additionalPad; for (int x = 0; x < GridArr.GetLength(0); x++) { additionalPad = 0; line = ""; for (int y = 0; y < GridArr.GetLength(1); y++) { if (y != 0 && y % SubgridRows == 0) { line += "| "; additionalPad += 2; } line += GridArr[x, y].Value; line = line.PadRight((y + 1) * (Alphabet.Length + 1) + additionalPad); } grid += line + "\n"; if ((x + 1) != GridArr.GetLength(0) && (x + 1) % SubgridRows == 0) { string lineSeparator = ""; for (int i = 0; i < line.Length; i++) { lineSeparator += "-"; } grid += lineSeparator + "\n"; } } return(grid); }
/// <summary> /// Checks if each cell in the grid has only one value. Does not check for correctness. /// </summary> /// <returns></returns> public bool IsSolved() { bool solved = true; for (int x = 0; x < GridArr.GetLength(0); x++) { for (int y = 0; y < GridArr.GetLength(1); y++) { if (GridArr[x, y].Value.Length > 1) { solved = false; } } } Console.WriteLine($"Puzzle solution reached: {solved}"); Console.WriteLine(ToString()); if (Program.solveLogFlag) { Program.solveLog.Write($"Puzzle solution reached: {solved}\n{ToString()}\n"); } return(solved); }
/// <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> /// Returns a column of cells as an array. /// </summary> /// <param name="colIndex"></param> /// <returns>Column of cells as a cell array</returns> public List <Cell> GetCol(int colIndex) { //Cell[] col = new Cell[GridArr.GetLength(0)]; List <Cell> col = new List <Cell>(); for (int i = 0; i < GridArr.GetLength(0); i++) { col.Add(GridArr[i, colIndex]); } return(col); }
/// <summary> /// Returns a row of cells as an array. /// </summary> /// <param name="rowIndex"></param> /// <returns>Row of cells as a cell array</returns> public List <Cell> GetRow(int rowIndex) { //Cell[] row = new Cell[GridArr.GetLength(1)]; List <Cell> row = new List <Cell>(); for (int i = 0; i < GridArr.GetLength(1); i++) { row.Add(GridArr[rowIndex, i]); } return(row); }
/// <summary> /// /// </summary> /// <param name="n">The equal set size to make an inference.</param> public bool NCellColSieve(int n) { bool success = false; for (int i = 0; i < GridArr.GetLength(1); i++) { if (SieveNCellGroup(GetCol(i), n) > 0) { success = true; } } return(success); }
/// <summary> /// /// </summary> /// <param name="n">The equal set size to make an inference.</param> public bool NCellRowSieve(int n) { bool success = false; //for each row for (int i = 0; i < GridArr.GetLength(0); i++) { // if (SieveNCellGroup(GetRow(i), n) > 0) { success = true; } } return(success); }