Finished project #1

Merged
Trianta merged 10 commits from dev into master 2023-10-22 21:52:01 -05:00
4 changed files with 27 additions and 45 deletions
Showing only changes of commit e57dcce50c - Show all commits

View File

@ -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

View File

@ -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<int> 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<int> 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<int> Sudoku::GetUnusedRow(int row) {
std::vector<int> leftOver{0,1,2,3,4,5,6,7,8,9};
int box;

View File

@ -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<int> GetUnusedRow(int row);
std::vector<int> GetUnusedColumn(int column);
std::vector<int> GetUnusedSubgrid(int column, int row);

View File

@ -9,6 +9,6 @@ int main(int argc, char* argv[]) {
Sudoku newGame;
newGame.FillBoard(argv[1]);
newGame.Solve();
newGame.Print();
newGame.RawPrint();
return 0;
}