public void Fill(Cell cell, int value) { int maxValue = dimension * dimension; if (value > maxValue) throw new Exception(String.Format("Number should be less than {0}", maxValue)); cell.Number = value; }
public object Clone() { Matrix newMatrix = (Matrix)this.MemberwiseClone(); newMatrix.cells = new List<Cell>(); foreach (var cell in cells) { Cell newCell = new Cell(cell.X, cell.Y, newMatrix); if (cell.Number != 0) newCell.Number = cell.Number; newMatrix.cells.Add(newCell); } return newMatrix; }
private bool Backtracking(Cell cell) { if (sudokuFound) Thread.CurrentThread.Abort(); if (this.IsFilledSudoku) return true; if(cell == null) return false; Cell nextCell = null; int nextIndex = allCells.IndexOf(cell) + 1; if (nextIndex < allCells.Count) nextCell = allCells[allCells.IndexOf(cell) + (int)1]; /* if (cell.IsInitialized) { if (Backtracking(nextCell)) return true; }*/ for (int value = 1; value <= 9; value++) { if (!cell.Matrix.HaveTheSame(value) && !HaveTheSameH(cell.Matrix, cell, value) && !HaveTheSameV(cell.Matrix, cell, value)) { Fill(cell.Matrix.X, cell.Matrix.Y, cell.X, cell.Y, value); if (Backtracking(nextCell)) return true; } } cell.ClearCell(); return false; }
public bool HaveTheSameV(Matrix matrix, Cell cell, int value) { int matrixPosition = matrix.X; int position = cell.X; for (int y = 0; y < dimension; y++) { Matrix matrixToCheck = Matrixes.Where(m => m.X == matrixPosition && m.Y == y).First(); if (matrixToCheck.HaveTheSameV(position, value)) return true; } return false; }
public bool HaveTheSameH(Matrix matrix, Cell cell, int value) { int matrixPosition = matrix.Y; int position = cell.Y; for (int x = 0; x < dimension; x++) { Matrix matrixToCheck = Matrixes.Where(m => m.X == x && m.Y == matrixPosition).First(); if (matrixToCheck.HaveTheSameH(position, value)) return true; } return false; }
public bool IsInitialized(Cell cell) { if (cell.Number == 0) return false; return true; }