-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
71 lines (61 loc) · 1.62 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <optional>
#include <string>
#include "boardDefinitions.hpp"
#include "genOptions.hpp"
#include "readBoard.hpp"
void printBoard(Board& board) {
for (const auto& r : board) {
for (const auto& c : r) {
if (c==0){
std::cout << '_' << ", ";
}else{
std::cout << c << ", ";
}
}
std::cout << "\n";
}
}
std::optional<Loc> getEmptyCell(Board& board) {
for (size_t r{0}; r < 9; r++) {
for (size_t c{0}; c < 9; c++) {
if (board[r][c] == 0) {
return Loc{r, c};
}
}
}
return std::nullopt;
}
std::optional<Board> solveBoard(Board board) {
auto loc_ = getEmptyCell(board);
if (!loc_.has_value()) {
return board;
} // if no empty cells remaining return the board.
Loc loc{loc_.value()};
auto options = genOptions(board, loc);
if (options.size() == 0) {
return std::nullopt;
}
for (const auto& opt : options) {
board[loc.row][loc.col] = opt;
auto solveBoardCalc = solveBoard(board);
if (solveBoardCalc.has_value()) {
return solveBoardCalc.value();
}
}
return std::nullopt;
}
int main() {
std::string path{"./Board.txt"};
auto board{readBoard(path)};
std::cout << "\n\n\nUnsolved Board is:\n";
printBoard(board);
auto board_ = solveBoard(board);
if (board_.has_value()){
std::cout << "\n\n\nSolved Board is:\n";
printBoard(board_.value());
}else{
std::cout << "No Solution Exists" << std::endl;
}
return 0;
}