generated from Trianta/cpp-unity-template
Trimmed project before turn in
This commit is contained in:
parent
e12cb72dcf
commit
e57dcce50c
14
README.md
14
README.md
@ -1,5 +1,13 @@
|
|||||||
# sudoku-solver
|
# 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
|
## Compiling the project
|
||||||
|
|
||||||
Prerequisites
|
Prerequisites
|
||||||
@ -18,6 +26,10 @@ Simply run the program using:
|
|||||||
build/bin/sudoku
|
build/bin/sudoku
|
||||||
|
|
||||||
## Testing the Project
|
## Testing the Project
|
||||||
|
|
||||||
|
Prerequisites
|
||||||
|
- Unity (Throw the Switch)
|
||||||
|
|
||||||
In order to compile the tests for the project, simply run these two commands:
|
In order to compile the tests for the project, simply run these two commands:
|
||||||
|
|
||||||
cmake -DCMAKE_BUILD_TYPE=Debug -B build -S .
|
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:
|
Simply run the tests using:
|
||||||
|
|
||||||
build/bin/sudoku-test
|
build/bin/testing
|
||||||
|
@ -29,10 +29,9 @@ void Sudoku::Solve(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Sudoku::IsBoardSolved(void) {
|
bool Sudoku::IsBoardSolved(void) {
|
||||||
for (int i = 0; i < 9; ++i) {
|
int column, row;
|
||||||
if (!IsColumnComplete(i)) { return false; }
|
if (!FindEmptyCell(&column, &row)) { return true; }
|
||||||
}
|
return false;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sudoku::Print(void) {
|
void Sudoku::Print(void) {
|
||||||
@ -47,6 +46,15 @@ void Sudoku::Print(void) {
|
|||||||
std::cout << "===================" << std::endl;
|
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
|
// Recursive call for solving
|
||||||
bool Sudoku::_Solve(void) {
|
bool Sudoku::_Solve(void) {
|
||||||
int column, row;
|
int column, row;
|
||||||
@ -95,43 +103,6 @@ bool Sudoku::IsValidPlacement(int column, int row, int box) {
|
|||||||
return true;
|
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> Sudoku::GetUnusedRow(int row) {
|
||||||
std::vector<int> leftOver{0,1,2,3,4,5,6,7,8,9};
|
std::vector<int> leftOver{0,1,2,3,4,5,6,7,8,9};
|
||||||
int box;
|
int box;
|
||||||
|
@ -12,12 +12,11 @@ public:
|
|||||||
void Solve(void);
|
void Solve(void);
|
||||||
bool IsBoardSolved(void);
|
bool IsBoardSolved(void);
|
||||||
void Print(void);
|
void Print(void);
|
||||||
|
void RawPrint(void);
|
||||||
private:
|
private:
|
||||||
bool _Solve(void);
|
bool _Solve(void);
|
||||||
bool FindEmptyCell(int* column, int* row);
|
bool FindEmptyCell(int* column, int* row);
|
||||||
bool IsValidPlacement(int column, int row, int box);
|
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> GetUnusedRow(int row);
|
||||||
std::vector<int> GetUnusedColumn(int column);
|
std::vector<int> GetUnusedColumn(int column);
|
||||||
std::vector<int> GetUnusedSubgrid(int column, int row);
|
std::vector<int> GetUnusedSubgrid(int column, int row);
|
||||||
|
@ -9,6 +9,6 @@ int main(int argc, char* argv[]) {
|
|||||||
Sudoku newGame;
|
Sudoku newGame;
|
||||||
newGame.FillBoard(argv[1]);
|
newGame.FillBoard(argv[1]);
|
||||||
newGame.Solve();
|
newGame.Solve();
|
||||||
newGame.Print();
|
newGame.RawPrint();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user