diff --git a/README.md b/README.md index f9962be..3671d86 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,13 @@ # sudoku-solver +## Notes + +Uses a single sudoku file as input + +Tests were ran on sudoku-easy-50 split into individual files + +All were successfully solved + ## Compiling the project Prerequisites @@ -18,6 +26,10 @@ Simply run the program using: build/bin/sudoku ## Testing the Project + +Prerequisites + - Unity (Throw the Switch) + In order to compile the tests for the project, simply run these two commands: cmake -DCMAKE_BUILD_TYPE=Debug -B build -S . @@ -27,4 +39,4 @@ The program should now be compiled at ./build/bin/sudoku Simply run the tests using: - build/bin/sudoku-test + build/bin/testing diff --git a/src/sudoku.cpp b/src/sudoku.cpp index 52240a2..a8fb836 100644 --- a/src/sudoku.cpp +++ b/src/sudoku.cpp @@ -29,10 +29,9 @@ void Sudoku::Solve(void) { } bool Sudoku::IsBoardSolved(void) { - for (int i = 0; i < 9; ++i) { - if (!IsColumnComplete(i)) { return false; } - } - return true; + int column, row; + if (!FindEmptyCell(&column, &row)) { return true; } + return false; } void Sudoku::Print(void) { @@ -47,6 +46,15 @@ void Sudoku::Print(void) { std::cout << "===================" << std::endl; } +void Sudoku::RawPrint(void) { + for (auto i : board) { + for (auto j : i) { + std::cout << j; + } + std::cout << std::endl; + } +} + // Recursive call for solving bool Sudoku::_Solve(void) { int column, row; @@ -95,43 +103,6 @@ bool Sudoku::IsValidPlacement(int column, int row, int box) { return true; } -bool Sudoku::IsRowComplete(int row) { - std::vector leftOver{0,1,2,3,4,5,6,7,8,9}; - int box; - for (int i = 0; i < 9; i++) { - box = board[row][i]; - if (leftOver[box] == 0 && box != 0) { - std::cerr << "Invalid assignment detected. Aborting..." << std::endl; - exit(1); - } - if (leftOver[box] == box) { leftOver[box] = 0; } - } - for (auto it = leftOver.begin(); it != leftOver.end();) { - if (*it == 0) { it = leftOver.erase(it); } - else { ++it; } - } - if (leftOver.size() != 0) { return false; } - return true; -} - -bool Sudoku::IsColumnComplete(int column) { - std::vector leftOver{0,1,2,3,4,5,6,7,8,9}; - int box; - for (int i = 0; i < 9; i++) { - box = board[i][column]; - if (leftOver[box] == 0 && box != 0) { - leftOver[box] = -1; // Duplicate detected in same column - } - if (leftOver[box] == box) { leftOver[box] = 0; } - } - for (auto it = leftOver.begin(); it != leftOver.end();) { - if (*it == 0) { it = leftOver.erase(it); } - else { ++it; } - } - if (leftOver.size() != 0) { return false; } - return true; -} - std::vector Sudoku::GetUnusedRow(int row) { std::vector leftOver{0,1,2,3,4,5,6,7,8,9}; int box; diff --git a/src/sudoku.hpp b/src/sudoku.hpp index bb919d9..b6ff2e8 100644 --- a/src/sudoku.hpp +++ b/src/sudoku.hpp @@ -12,12 +12,11 @@ public: void Solve(void); bool IsBoardSolved(void); void Print(void); + void RawPrint(void); private: bool _Solve(void); bool FindEmptyCell(int* column, int* row); bool IsValidPlacement(int column, int row, int box); - bool IsRowComplete(int row); - bool IsColumnComplete(int column); std::vector GetUnusedRow(int row); std::vector GetUnusedColumn(int column); std::vector GetUnusedSubgrid(int column, int row); diff --git a/src/sudoku_solver.cpp b/src/sudoku_solver.cpp index 879c0d7..af2f165 100644 --- a/src/sudoku_solver.cpp +++ b/src/sudoku_solver.cpp @@ -9,6 +9,6 @@ int main(int argc, char* argv[]) { Sudoku newGame; newGame.FillBoard(argv[1]); newGame.Solve(); - newGame.Print(); + newGame.RawPrint(); return 0; }