// public Cell RecursivebrowseGrid (CellsGrid grid, int line, int column) { // int hypothesisTest = 0; // for (int line = 0; line < grid.size; line++) { // for (int column = 0; line < grid.size; column++) { // if (grid [line] [column].valueIsNull) { // grid [line] [column].hypothesis [hypothesisTest]; // } // } // } // } public Dictionary <string, List <Cell> > counBlockCells(CellsGrid grid) { Dictionary <string, List <Cell> > listOccurenHypothesis = new Dictionary <String, List <Cell> >(); foreach (var cell in grid.grid) { if (cell.Value.Equals(".")) { if (cell.hypothesis.Count > 0) { String str = cell.hypothesis.Aggregate((stringa, stringb) => stringa + stringb); if (listOccurenHypothesis.ContainsKey(str)) { listOccurenHypothesis[str].Add(cell); } else { List <Cell> temp = new List <Cell>(); temp.Add(cell); listOccurenHypothesis.Add(str, temp); } } } } return(listOccurenHypothesis); }
private void testHypothesis(string Hypothesis, List <CellsGrid> testList, Cell cell) { testList.Add(new CellsGrid(this)); Console.Out.WriteLine(testList.Last()); Cell realCell = this[cell.PosX, cell.PosY]; this.cantResolve = false; Log(ModeText.Verbose, String.Format("test value {0} at ({1},{2})", Hypothesis, realCell.PosX, realCell.PosY)); realCell.Value = Hypothesis; realCell.diffuseInItsEnsemble(); //Console.Out.WriteLine(testList.Last()); this.resolveGrid(); if (this.cantResolve == true) { CellsGrid saveGrid = testList.Last(); testList.Remove(testList.Last()); foreach (Cell saveCell in saveGrid.grid) { this.grid[saveCell.PosX, saveCell.PosY].Value = saveCell.Value; this.grid[saveCell.PosX, saveCell.PosY].hypothesis.Clear(); this.grid[saveCell.PosX, saveCell.PosY].hypothesis.AddRange(saveCell.hypothesis); } testList.Last().cantResolve = true; Log(ModeText.Verbose, "RollBack"); } }
public CellsGrid(CellsGrid cellsgrid) : this(cellsgrid.observers) { this.listHypotheticSudoku = new List <CellsGrid>(cellsgrid.listHypotheticSudoku); this.cantResolve = cellsgrid.cantResolve; this.size = cellsgrid.size; this.required = cellsgrid.required; this.name = cellsgrid.name; this.isValid = cellsgrid.isValid; this.date = cellsgrid.date; this.error = cellsgrid.error; this.numberOfDots = cellsgrid.numberOfDots; this.GridRecursiveResolver = cellsgrid.GridRecursiveResolver; this.grid = new Cell[size, size]; List <Ensemble> MesEnsembleLine = new List <Ensemble>(); List <Ensemble> MesEnsembleColumn = new List <Ensemble>(); List <Ensemble> MesEnsembleSector = new List <Ensemble>(); double sqrtNumber = Math.Sqrt((Convert.ToDouble(size))); for (int i = 0; i < size; i++) { if (MesEnsembleLine.Count <= i) { MesEnsembleLine.Add(new Ensemble(this.observers)); } for (int j = 0; j < size; j++) { if (MesEnsembleColumn.Count <= j) { MesEnsembleColumn.Add(new Ensemble(this.observers)); } int indexSector = ((int)(Math.Floor(i / sqrtNumber) * sqrtNumber + Math.Floor(j / sqrtNumber))); if (MesEnsembleSector.Count <= indexSector) { MesEnsembleSector.Add(new Ensemble(this.observers)); } this.grid[i, j] = new Cell(MesEnsembleColumn[j], MesEnsembleLine[i], MesEnsembleSector[indexSector], cellsgrid.grid[i, j].Value, cellsgrid.grid[i, j].hypothesis, i, j, this.observers); this.grid[i, j].addItsEnsemble(); } } }
/** * Constructeur * @param (string) path = Nom du fichier .sud * @param (string) delimiter = Chaîne de caractères de séparation des sudokus (optionnel) * @param (int) mode = Mode du manager 0 = validation, 1 = résolution (optionnel) **/ public SudokuManager(string path, IObserver <SudokuObject> MainConsole, int mode = 0) : base() { this.Path = path; this.ModelList = new ObservableCollection <CellsGrid>(); this.Logs = new ObservableCollection <string>(); this.Mode = mode; Subscribe(MainConsole); if (!File.Exists(this.Path)) { Console.WriteLine("Erreur: " + this.Path + " n'existe pas."); return; } this.verifyIntegrityOfAllSudoku(); GridSelected = this.modelList.First(); }
public bool EqualsInCell(object obj) { if (obj is CellsGrid) { CellsGrid compareGrid = obj as CellsGrid; if (compareGrid.size != this.size) { return(false); } foreach (Cell c in this.grid) { if (!(compareGrid[c.PosX, c.PosY].EqualsInValueAndHypothesis(c))) { return(false); } } return(true); } return(false); }
public void RecursivebrowseGrid(CellsGrid grid, int line, int column) { for (int i = line; i < grid.size; i++) { for (int j = column; j < grid.size; j++) { if (grid[i, j].ValuesIsNull()) { Cell myCell = grid [i, j]; for (int h = 0; h < myCell.hypothesis.Count; h++) { int hypothesisTest = Convert.ToInt32(myCell.hypothesis[h]); if (myCell.ExistsInEnsemble(hypothesisTest) == false) { myCell.Value = hypothesisTest.ToString(); if (grid.isDone()) { Console.WriteLine("solution trouvée!"); grid.ToString(); } else { Console.Out.WriteLine("recursive solution"); Console.Out.WriteLine(grid); Console.ReadLine(); RecursivebrowseGrid(grid, i, j); } } else { myCell.Value = "."; } } } } } }
public void resolve(CellsGrid grid) { listHypotheticSudoku.Add(grid); this.index++; RecursivebrowseGrid(grid, 0, 0); }
public CellsGrid ResolveBlockCells(CellsGrid grid) { List <CellsGrid> testList = new List <CellsGrid>(); testList.Add(grid); testList.Add(new CellsGrid(grid)); Log(ModeText.Verbose, "Save last version"); Dictionary <string, List <Cell> > counter = this.counBlockCells(testList.Last()); List <KeyValuePair <string, List <Cell> > > sortedCells = this.sortedBlockCells(counter); List <Cell> blockCells = this.getBlockCells(sortedCells); Log(ModeText.Verbose, "cellule bloquante"); foreach (Cell c in blockCells) { Console.Out.WriteLine(c.hypothesis.Aggregate((stringa, stringb) => stringa + stringb)); Log(ModeText.Verbose, String.Format("({0},{1})", c.PosX, c.PosY)); // Console.ReadLine(); } if (blockCells.Count == 0) { if (listHypotheticSudoku.Any(Listgrid => grid.EqualsInCell(Listgrid))) { grid.cantResolve = true; return(grid); } listHypotheticSudoku.Add(grid); Log(ModeText.Verbose, "Pure brute force"); foreach (KeyValuePair <string, List <Cell> > keyPair in sortedCells) { foreach (Cell c in keyPair.Value) { List <String> temp = new List <String>(c.hypothesis); foreach (String hypothesis in temp) { testHypothesis(hypothesis, testList, c); if (testList.Last().isDone()) { profondeur--; return(testList.Last()); } } } } } else { for (int i = 0; i < blockCells.Count; i++) { var cell = blockCells[i]; List <String> temp = new List <String>(cell.hypothesis); testList.Last().cantResolve = false; // testList.Add(tempgrid); deleteAllHypothesisFromEnsembleInCell(blockCells, cell); testList.Add(new CellsGrid(testList.Last().resolveGrid(false))); Console.Out.WriteLine(testList.Last()); if (testList.Last().isDone()) { profondeur--; return(testList.Last()); } foreach (String Hypothesis in temp) { testHypothesis(Hypothesis, testList, cell); if (testList.Last().isDone()) { return(testList.Last()); } } testList.Last().cantResolve = true; testList.Remove(testList.Last()); } } grid.cantResolve = true; profondeur--; return(grid); }