From 68da3e6ab18544d43069d8d25d50abadf7267b30 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 15 Jun 2012 18:52:40 +0000 Subject: Integrated Cell class, minor refactoring. --- src/Makefile.am | 1 + src/solverwindow.cc | 17 +++++++++-------- src/sudoku.cc | 32 ++++++++++++++++---------------- src/sudoku.h | 10 +++++----- src/sudokuwidget.cc | 9 +++++++-- src/sudokuwidget.h | 2 ++ 6 files changed, 40 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 943ed52..2e1bae4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,6 +3,7 @@ bin_PROGRAMS = sudokusolver # You have two .cpp files you wrote, editor.cpp and another.cpp # Remember to include the name of the resource file with the .cpp extension. sudokusolver_SOURCES = \ + cell.cc \ main.cc \ mainwindow.cc \ solverwindow.cc \ diff --git a/src/solverwindow.cc b/src/solverwindow.cc index 06de5fb..23157f4 100644 --- a/src/solverwindow.cc +++ b/src/solverwindow.cc @@ -85,7 +85,7 @@ void SolverWindow::load() for (int column = 0; column < 9 ; column++) { int i; textstream >> i; - sudoku.value(row, column) = i; + sudoku.set_value(row, column, i); } } solverwindow_sudokuwidget->set_values(sudoku); @@ -150,20 +150,21 @@ void SolverWindow::clear() void SolverWindow::step() { - int nbSolutions = 0; + int cells_solved = 0; Sudoku sudoku; solverwindow_sudokuwidget->get_values(sudoku); Sudoku solution; for (int row = 0; row < 9; row++) { for (int column = 0; column < 9; column++) { - int i = sudoku.solve_step(row, column); - if ((i > 0) && (sudoku.value(row, column) == 0)) { - nbSolutions++; + solution.set_value(row, column, sudoku.solve_step(row, column)); + + if ((sudoku.value(row, column) == 0) && (solution.value(row, column) > 0)) { + cells_solved++; } - solution.value(row, column) = i; + } } solverwindow_sudokuwidget->set_values(solution); - qDebug() << nbSolutions << " cells solved"; -} \ No newline at end of file + qDebug() << cells_solved << " cells solved"; +} diff --git a/src/sudoku.cc b/src/sudoku.cc index 13369fe..2725b02 100644 --- a/src/sudoku.cc +++ b/src/sudoku.cc @@ -3,18 +3,18 @@ Sudoku::Sudoku() { - for (int row = 0; row < 9; row++) { - for (int column = 0; column < 9; column++) { - sudoku_value[row][column] = 0; - } - } +} + +void Sudoku::set_value(int row, int column, int value) +{ + sudoku_cell[row][column].set_value(value); } int Sudoku::solve_step(int pos_row, int pos_column) { // verify if the cell has already been solved - if ((sudoku_value[pos_row][pos_column] > 0 ) && (sudoku_value[pos_row][pos_column] <= 9)) { - return sudoku_value[pos_row][pos_column]; + if ((value(pos_row,pos_column) > 0 ) && (value(pos_row,pos_column) <= 9)) { + return value(pos_row,pos_column); } bool possible_solution[9]; @@ -26,18 +26,18 @@ int Sudoku::solve_step(int pos_row, int pos_column) // eliminate row for (int column = 0; column < 9; column++) { if (column != pos_column) { - int value = sudoku_value[pos_row][column]; - if ((value > 0) && (value <= 9)) { - possible_solution[value - 1] = false; + const int v = value(pos_row,column); + if ((v > 0) && (v <= 9)) { + possible_solution[v - 1] = false; } } } // eliminate column for (int row = 0; row < 9; row++) { if (row != pos_row) { - int value = sudoku_value[row][pos_column]; - if ((value > 0) && (value <= 9)) { - possible_solution[value - 1] = false; + const int v = value(row,pos_column); + if ((v > 0) && (v <= 9)) { + possible_solution[v - 1] = false; } } @@ -49,9 +49,9 @@ int Sudoku::solve_step(int pos_row, int pos_column) for (int row = grid_row; row < grid_row + 3; row++) { for (int column = grid_column; column < grid_column + 3; column ++) { if ((column != pos_column) && (row != pos_row)) { - int value = sudoku_value[row][column]; - if ((value > 0) && (value <= 9)) { - possible_solution[value - 1] = false; + const int v = value(row, column); + if ((v > 0) && (v <= 9)) { + possible_solution[v - 1] = false; } } diff --git a/src/sudoku.h b/src/sudoku.h index afb9f16..a6bf821 100644 --- a/src/sudoku.h +++ b/src/sudoku.h @@ -2,6 +2,8 @@ #ifndef __INCLUDED_SUDOKUSOLVER_SUDOKU__ #define __INCLUDED_SUDOKUSOLVER_SUDOKU__ +#include "cell.h" + class Sudoku { public: @@ -9,18 +11,16 @@ public: // inspector inline int value(int row, int column) const { - return sudoku_value[row][column]; + return sudoku_cell[row][column].value(); } // mutator - inline int & value(int row, int column) { - return sudoku_value[row][column]; - } + void set_value(int row, int column, int value); int solve_step(int pos_row, int pos_column); private: - int sudoku_value[9][9]; + Cell sudoku_cell[9][9]; }; #endif // __INCLUDED_SUDOKUSOLVER_SUDOKU__ diff --git a/src/sudokuwidget.cc b/src/sudokuwidget.cc index 742798f..7d756ae 100644 --- a/src/sudokuwidget.cc +++ b/src/sudokuwidget.cc @@ -23,6 +23,11 @@ SudokuWidget::SudokuWidget() setLayout(gridlayout); } +QSize SudokuWidget::sizeHint () const +{ + return QSize(512, 512); +} + void SudokuWidget::set_values(const Sudoku & values) { for (int row = 0; row < 9; row++) { @@ -46,9 +51,9 @@ void SudokuWidget::get_values(Sudoku & values) QString str(sudokuwidget_value[row][column]->text()); int i = str.toInt(&ok); if (ok && (i > 0) && (i <= 9)) { - values.value(row, column) = i; + values.set_value(row, column, i); } else { - values.value(row, column) = 0; + values.set_value(row, column, 0); } } } diff --git a/src/sudokuwidget.h b/src/sudokuwidget.h index 06d1b6f..c96df7d 100644 --- a/src/sudokuwidget.h +++ b/src/sudokuwidget.h @@ -25,6 +25,8 @@ public: * @brief set sudoku values from widget values * */ void get_values(Sudoku & values); + + virtual QSize sizeHint() const; private: QLineEdit * sudokuwidget_value[9][9]; -- cgit v1.2.3