Beispiel #1
0
 bool OperateOn(int col, int row, SudokuModel model)
 {
     if (!model.IsSolved(col, row)) {
         // obviously can't eliminate anything in a solved cell.
         // try each int i that is a possibility for the cell, and see if an error results
         foreach (var i in model.GetPossibilitySetCell(col, row).HighBitPositions()) {
             var clone = new SudokuModel(model);
             clone.SetValue(col, row, i);
             _elimStrategy.Run(clone);
             if (clone.IsConsistent()) {
                 continue;
             }
             // If so, we can eliminate that from the original model.
             model.Eliminate(col, row, i);
             return true;
             // return immediately after eliminating something, since
             // this is a slow algorithm, and we want our faster
             // algorithms to see if they make some more eliminations first.
         }
     }
     return false;
 }
Beispiel #2
0
        void OpenFile(string name)
        {
            _name = name;
            // Open and read the file
            try {
                TextReader reader = new FileInfo(name).OpenText();
                var text = reader.ReadToEnd();
                reader.Dispose();
                var lines = text.Split(new[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);

                // Create a new model and solver, and add event listeners
                if (_model != null) {
                    _model.ModelChanged -= HandleModelModelChanged;
                    _solver.ScanFinished -= HandleSolverScanFinished;
                    _solver.Finished -= HandleSolverFinished;
                }
                _model = new SudokuModel(lines.Length);
                _solver = new Solver(_model);
                _model.ModelChanged += HandleModelModelChanged;
                _solver.ScanFinished += HandleSolverScanFinished;
                _solver.Finished += HandleSolverFinished;

                // Update the screen
                _grid.Model = _model;
                _txtProgress.Clear();

                // Load data into the model
                for (var row = 0; row < lines.Length; ++row) {
                    var cells = lines[row].Split(',');
                    for (var col = 0; col < lines.Length; ++col) {
                        if (!string.IsNullOrEmpty(cells[col])) {
                            _model.SetValue(col, row, cells[col][0] - 'A');
                        }
                    }
                }
            } catch (FileNotFoundException) {}
        }