-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.cpp
206 lines (161 loc) · 5.66 KB
/
puzzle.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "puzzle.h"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <stdexcept>
namespace Sudoku {
PuzzleRow_t Puzzle::GetRow(const int row) const { return board_[row]; }
PuzzleCol_t Puzzle::GetColumn(const int column) const {
PuzzleCol_t col{};
for (auto &row : board_) {
col.push_back(row[column]);
}
return col;
}
bool Puzzle::IsValid() const {
if (board_.size() != kBoardSize) {
return false;
}
for (auto &row : board_) {
if (row.size() != kBoardSize) {
return false;
}
for (auto &elem : row) {
if (elem >= kBoardSize) {
return false;
}
}
}
if (!IsLegal()) {
return false;
}
return Size() == kTotalBoardSize;
}
bool Puzzle::IsLegal() const {
for (int row = 0; row < kBoardSize; ++row) {
for (int column = 0; column < kBoardSize; ++column) {
if (!IsValidAssignment({row, column}, GetRow(row)[column])) {
return false;
}
}
}
return true;
}
bool Puzzle::IsValidAssignment(const PuzzleCoord_t &coordinate, int value) const {
int row = coordinate.first;
int column = coordinate.second;
if (value == kUnassigned || GetRow(row)[column] == kUnassigned) {
return true;
}
return IsValidRowAssignment(row, column, value) &&
IsValidColumnAssignment(row, column, value) && IsValidBoxAssignment(row, column, value);
}
bool Puzzle::IsValidRowAssignment(const int row, const int column, const int value) const {
PuzzleRow_t puzzle_row = GetRow(row);
// Since it has the given value, remove this cell before the check.
puzzle_row[column] = kUnassigned;
// check row for value
return std::find(puzzle_row.begin(), puzzle_row.end(), value) == puzzle_row.end();
}
bool Puzzle::IsValidColumnAssignment(const int row, const int column, const int value) const {
PuzzleCol_t puzzle_column = GetColumn(column);
// Since it has the given value, remove this cell before the check.
puzzle_column[row] = kUnassigned;
// check column for value
return std::find(puzzle_column.begin(), puzzle_column.end(), value) == puzzle_column.end();
}
bool Puzzle::IsValidBoxAssignment(const int row, const int column, const int value) const {
int box_row_offset = (row / kBoardSquareSize) * kBoardSquareSize;
int box_column_offset = (column / kBoardSquareSize) * kBoardSquareSize;
for (int i = box_row_offset; i < kBoardSquareSize + box_row_offset; ++i) {
for (int j = box_column_offset; j < kBoardSquareSize + box_column_offset; ++j) {
if ((i == row) && (j == column)) {
continue;
}
if (GetRow(i)[j] == value) {
return false;
}
}
}
return true;
}
PuzzleCoord_t Puzzle::FindUnassignedPosition() const {
int row, col;
for (row = 0; row < 10; ++row) {
for (col = 0; col < 10; ++col) {
if (GetRow(row)[col] == 0) {
return PuzzleCoord_t{row, col};
}
}
}
// could not find position
return PuzzleCoord_t{-1, 1};
}
int Puzzle::Size() const {
int count = 0;
for (auto &row : board_) {
count += row.size();
}
return count;
}
std::string Puzzle::ToString() const {
std::string output;
for (auto &row : board_) {
for (auto &elem : row) {
output += (elem == kUnassigned) ? "_" : std::to_string(elem);
}
}
return output;
}
std::ostream &operator<<(std::ostream &out, const Puzzle &puzzle) {
for (int row = 0; row < kBoardSize; ++row) {
for (int col = 0; col < kBoardSize; ++col) {
out << ((puzzle.board_[row][col] == kUnassigned)
? " "
: std::to_string(puzzle.board_[row][col]));
if (((col % kBoardSquareSize) == kBoardSquareSize) && (col < kBoardSize - 1)) {
// print vertical column
out << "|";
}
}
out << std::endl;
if (((row % kBoardSquareSize) == kBoardSquareSize - 1) && (row < kBoardSize - 1)) {
// print horizontal row
out << std::string(kBoardSize + kBoardSquareSize, '-') << std::endl;
}
}
return out;
}
std::istream &operator>>(std::istream &in, Puzzle &puzzle) {
std::string input;
std::getline(in, input);
puzzle.board_ = Puzzle::BuildBoardVector(input);
return in;
}
PuzzleRow_t &Puzzle::operator[](const int row) { return board_[row]; }
PuzzleBoard_t Puzzle::BuildBoardVector(const std::string board_string) {
if (board_string.length() != kTotalBoardSize) {
throw std::invalid_argument(
"Board string must contain " + std::to_string(kTotalBoardSize) +
" characters. Inputted string's size: " + std::to_string(board_string.length()));
}
PuzzleBoard_t board_vector(kBoardSize);
for (int row = 0; row < kBoardSize; ++row) {
for (int col = 0; col < kBoardSize; ++col) {
int string_index = row * kBoardSize + col;
char board_char = board_string[string_index];
if (board_char == kUnassignedChar) {
board_vector[row].emplace_back(kUnassigned);
} else if (board_char >= '0' && board_char <= '9') {
// convert char to number
board_vector[row].emplace_back(board_char + '0');
} else {
throw std::invalid_argument(
"Board string must only contain 1-9 and _. Invalid character: " +
std::to_string(board_char));
}
}
}
return board_vector;
}
} // namespace Sudoku