/// <summary> /// <see cref="ISearchAgent"/> ProcessVariables event handler. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected override void OnProcessVariables(object sender, ProcessVariablesEventArgs e) { var candidate = new SudokuPuzzle(); ISudokuPuzzle local = candidate; for (var row = 0; row < 9; row++) { for (var col = 0; col < 9; col++) { local[row, col] = (int)_cells[row, col].Value(); } } /* If we're here processing variables, it should be because we are processing the next * solution. However, in the event we still do not have a solution, then simply return. */ if (!local.IsSolved) { return; } Solution = local; // False is the default, so only mark whether ShouldBreak when we have one. e.ShouldBreak = true; base.OnProcessVariables(sender, e); }
/// <summary> /// Copy Constructor /// </summary> /// <param name="other"></param> public SudokuPuzzle(SudokuPuzzle other) { _grid = new Dictionary <Address, int>(); foreach (var item in other._grid) { _grid.Add(new Address(item.Key), item.Value); } }
/// <summary> /// Returns a Sudoku SudokuPuzzle based on the <paramref name="values"/>. Values are /// treated in row major manner. Each value is validated first and foremost. /// </summary> /// <param name="values"></param> /// <returns></returns> public static SudokuPuzzle ToSudokuPuzzle(this int[] values) { foreach (var value in values) { value.VerifyValue(); } var result = new SudokuPuzzle(); for (var row = 0; row < 9; row++) { for (var column = 0; column < 9; column++) { result[new Address(row, column)] = values[row * 9 + column]; } } return(result); }
/// <summary> /// <see cref="ISearchAgent"/> ProcessVariables event handler. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected override void OnProcessVariables(object sender, ProcessVariablesEventArgs e) { var candidate = new SudokuPuzzle(); ISudokuPuzzle local = candidate; // In this case we know that there is a Single Aspect. var aspect = Aspects.SingleOrDefault(); Assert.That(aspect, Is.Not.Null); const int size = SudokuProblemSolverAspect.Size; for (var row = 0; row < size; row++) { for (var col = 0; col < size; col++) { // ReSharper disable once PossibleNullReferenceException local[row, col] = (int)aspect.Cells[row, col].Value(); } } /* If we're here processing variables, it should be because we are processing the next * solution. However, in the event we still do not have a solution, then simply return. */ // TODO: TBD: we really shoul never land here I don't think... if (!local.IsSolved) { return; } Solution = local; // False is the default, so only mark whether ShouldBreak when we have one. e.ShouldBreak = true; base.OnProcessVariables(sender, e); }