diff options
-rw-r--r-- | src/solverwindow.cc | 8 | ||||
-rw-r--r-- | src/sudoku.cc | 39 | ||||
-rw-r--r-- | src/sudoku.h | 9 |
3 files changed, 43 insertions, 13 deletions
diff --git a/src/solverwindow.cc b/src/solverwindow.cc index a959a91..7f88b5f 100644 --- a/src/solverwindow.cc +++ b/src/solverwindow.cc @@ -157,16 +157,16 @@ void SolverWindow::step_constraints() { Sudoku sudoku; solverwindow_sudokuwidget->get_values(sudoku); - sudoku.solve_constraints(); + int solved = sudoku.solve_constraints(); solverwindow_sudokuwidget->set_values(sudoku); - //qDebug() << cells_solved << " cells solved"; + qDebug() << solved << " cells solved"; } void SolverWindow::step_coverage() { Sudoku sudoku; solverwindow_sudokuwidget->get_values(sudoku); - sudoku.solve_coverage(); + int solved = sudoku.solve_coverage(); solverwindow_sudokuwidget->set_values(sudoku); - + qDebug() << solved << " cells solved"; }
\ No newline at end of file diff --git a/src/sudoku.cc b/src/sudoku.cc index 3deabf9..6c241d9 100644 --- a/src/sudoku.cc +++ b/src/sudoku.cc @@ -19,7 +19,21 @@ void Sudoku::assign(const Sudoku & other) } } } - + +int Sudoku::compare_and_assign(const Sudoku & other) +{ + int d = 0; + for (int row = 0; row < 9; row++) { + for (int column = 0; column < 9; column++) { + if (sudoku_cell[row][column].value() != other.sudoku_cell[row][column].value()) { + d++; + } + sudoku_cell[row][column].assign(other.sudoku_cell[row][column]); + } + } + return d; +} + void Sudoku::set_value(int row, int column, int cell_value) { sudoku_cell[row][column].set_value(cell_value); @@ -85,7 +99,7 @@ void Sudoku::validate_cell(int pos_row, int pos_column) * value from 1-9 has to appear exactly once in each row, column and subgrid * Cells with a unique solution are solved */ -void Sudoku::solve_coverage() +int Sudoku::solve_coverage() { // calculate cell.possibilities validate(); @@ -99,6 +113,8 @@ void Sudoku::solve_coverage() for (int row = 0; row < 9; row++) { int available_column = 0; int covered = 0; + + // verify if there's exactly one possibility for v in this row for (int column = 0; column < 9 ; column++) { if (!value(row, column) && sudoku_cell[row][column].possibility(v - 1)) { // value is still possible for this cell @@ -117,6 +133,8 @@ void Sudoku::solve_coverage() for (int column = 0; column < 9; column++) { int available_row = 0; int covered = 0; + + // verify if there's exactly one possibility for v in this column for (int row = 0; row < 9; row++) { if (!value(row, column) && sudoku_cell[row][column].possibility(v - 1)) { // value is still possible a single cell @@ -132,8 +150,18 @@ void Sudoku::solve_coverage() } // verify coverage for each subgrid + for (int subgrid = 0; subgrid < 9; subgrid++) { + + // TODO the subgrid and subgrididx indices need te be delinearized + // and translated into cell coordinates + + // verify if there's exactly one possibility for v in this subgrid + for (int subgrididx = 0; subgrididx < 9; subgrididx++) { + } + } } - assign(solution); + + return compare_and_assign(solution); } /* @@ -141,7 +169,7 @@ void Sudoku::solve_coverage() * can appear only once in each column, row and subgrid * Cells with a unique solution are solved */ -void Sudoku::solve_constraints() +int Sudoku::solve_constraints() { Sudoku solution; for (int row = 0; row < 9; row++) { @@ -149,7 +177,8 @@ void Sudoku::solve_constraints() solution.set_value(row, column, solve_constraints(row, column)); } } - assign(solution); + + return compare_and_assign(solution); } int Sudoku::solve_constraints(int pos_row, int pos_column) diff --git a/src/sudoku.h b/src/sudoku.h index 3b9af8e..dd0ca08 100644 --- a/src/sudoku.h +++ b/src/sudoku.h @@ -13,12 +13,12 @@ public: void assign(const Sudoku & other); - // inspectors + int compare_and_assign(const Sudoku & other); + inline int value(int row, int column) const { return sudoku_cell[row][column].value(); } - // mutators void validate(); void set_value(int row, int column, int cell_value); @@ -27,9 +27,10 @@ public: int solve_constraints(int pos_row, int pos_column); - void solve_constraints(); + int solve_constraints(); + + int solve_coverage(); - void solve_coverage(); private: Cell sudoku_cell[9][9]; }; |