-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoard.cpp
264 lines (239 loc) · 5.96 KB
/
Board.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <Windows.h>
#include <iostream>
#include "Board.h"
#include "checkML.h"
using namespace std;
void init(tBoard board) { //We have to also do all values in interval [1,,,9]
for (int row = 0; row < NUMBERS; row++) {
for (int col = 0; col < NUMBERS; col++) {
initCell(board[row][col]);
}
}
}
bool load(const string &filename, tBoard board) { //Check if file.get works!
bool canBeLoaded = true;
ifstream file;
char num, discard;
file.open(filename);
if (!file.is_open()) {
canBeLoaded = false;
}
else {
for (int row = 0; row < NUMBERS; row++) {
for (int col = 0; col < NUMBERS; col++) {
file.get(num);
if (place(board, row, col, integer(num))) { //trivially true
fillCell(board[row][col], num, num != ' ');
}
}
file.get(discard);
}
}
return canBeLoaded;
}
void draw(const tBoard board) {
HANDLE hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
cout << " ";
putchar(179);
for (int i = 1; i < 10; i++) {
SetConsoleTextAttribute(hConsoleHandle, 224);
cout << " " << i << " ";
SetConsoleTextAttribute(hConsoleHandle, 15);
putchar(179);
}
cout << endl;
//nums
putchar(196);
putchar(196);
putchar(196);
cout << " ";
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
putchar(218);
putchar(196);
putchar(196);
putchar(196);
for (int i = 0; i < 8; i++) {
putchar(194);
putchar(196);
putchar(196);
putchar(196);
}
putchar(191);
cout << endl;
//toprow
SetConsoleTextAttribute(hConsoleHandle, 224);
cout << " 1 ";
SetConsoleTextAttribute(hConsoleHandle, 15);
cout << " ";
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
putchar(179);
for (int j = 1; j < 10; j++) {
cout << " ";
if (board[0][j - 1].state != Empty) {
drawCell(board[0][j - 1]);
}
else {
cout << " ";
}
cout << " "; //board[i][j];
if (j % 3 == 0) {
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
}
else {
SetConsoleTextAttribute(hConsoleHandle, 15);
}
putchar(179);
}
cout << endl;
SetConsoleTextAttribute(hConsoleHandle, 15); //starting row
for (int i = 2; i < 10; i++) { //starts at 2 for cout purposes
putchar(196);
putchar(196);
putchar(196);
cout << " ";
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
putchar(195);
if ((i - 1) % 3 == 0) {
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
}
else {
SetConsoleTextAttribute(hConsoleHandle, 15);
}
for (int k = 1; k < 9; k++) {
putchar(196);
putchar(196);
putchar(196);
if (k % 3 == 0 || k == 9) {
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
}
putchar(197);
if ((i - 1) % 3 == 0) {
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
}
else {
SetConsoleTextAttribute(hConsoleHandle, 15);
}
}
putchar(196);
putchar(196);
putchar(196);
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
putchar(180);
cout << endl;
SetConsoleTextAttribute(hConsoleHandle, 224);
cout << " " << i << " ";
SetConsoleTextAttribute(hConsoleHandle, 15);
cout << " ";
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
putchar(179);
for (int j = 1; j < 10; j++) {
cout << " ";
if (board[i - 1][j - 1].state != Empty) {
drawCell(board[i - 1][j - 1]);
}
else {
cout << " ";
}
cout << " ";//board[i][j];
if (j % 3 == 0) {
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
}
else {
SetConsoleTextAttribute(hConsoleHandle, 15);
}
putchar(179);
SetConsoleTextAttribute(hConsoleHandle, 15);
}
cout << endl;
} //body of sudoku
putchar(196);
putchar(196);
putchar(196);
SetConsoleTextAttribute(hConsoleHandle, FOREGROUND_BLUE);
cout << " ";
putchar(192);
putchar(196);
putchar(196);
putchar(196);
for (int i = 0; i < 8; i++) {
putchar(193);
putchar(196);
putchar(196);
putchar(196);
}
putchar(217);
//botrow
cout << endl;
SetConsoleTextAttribute(hConsoleHandle, 15);
}
bool place(tBoard board, int row, int col, int c) {
bool aux = board[row][col].state == Empty && (c < 10 && c > 0);
if (aux) {
char disp = tochar(c);
board[row][col].number = disp;
for (int i = 0; i < 9; i++) {
erase(board[row][i].set, c);
erase(board[i][col].set, c);
} //row n col
for (int i = (row / 3) * 3; i < row / 3 * 3 + 3; i++) {
for (int j = (col / 3) * 3; j < col / 3 * 3 + 3; j++) {
erase(board[i][j].set, c);
}
} //region
board[row][col].state = Full;
}
return aux;
}
bool erase(tBoard board, int row, int col) {
bool aux = board[row][col].state == Full && board[row][col].state != Fixed;
if (aux) {
board[row][col].state = Empty;
for (int i = 0; i < 9; i++) {
add(board[row][i].set, integer(board[row][col].number));
add(board[i][col].set, integer(board[row][col].number));
} //row n col
for (int i = (row / 3) * 3; i < row / 3 * 3 + 3; i++) {
for (int j = (col / 3) * 3; j < col / 3 * 3 + 3; j++) {
add(board[i][j].set, integer(board[row][col].number));
}
} //region
}
return aux;
}
bool full(const tBoard board) {
bool aux = true;
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (size(board[row][col].set) != 0) {
aux = false;
}
}
}
return aux;
}
void possible(const tBoard board, int row, int col) {
HANDLE hConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsoleHandle, 63);
cout << "All possible values for cell at " << col + 1 << " , " << row + 1 << " are:" << endl;
SetConsoleTextAttribute(hConsoleHandle, 9);
display(board[row][col].set);
SetConsoleTextAttribute(hConsoleHandle, 15);
}
void fillSimple(tBoard board) {
int n;
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (isSimple(board[row][col], n)) {
place(board, row, col, n);
}
}
}
}
int integer(char num) {
int aux = num - '0';
return aux;
}
int tochar(int value)
{
return '0' + value;
}