-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku-validator.cpp
336 lines (261 loc) · 8.99 KB
/
sudoku-validator.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
*========================
* Sudoku Validator
*========================
*
* Author: Sayan Mukherjee
* Objective: To verify whether a completed Sudoku puzzle is valid or not.
*/
#include<iostream>
#include<fstream>
using namespace std;
class SudokuFrame{
int sudokuFrame[9][9];
public:SudokuFrame(){
menu();
}
/**
* @desc This function generates a menu for the user at the starting of the
* program execution. This lets the user select the input method for entering
* the values for the Sudoku Puzzle into the SudokuFrame object.
*
* @param none
* @return none
*/
private:void menu(){
cout<<"\n======================\n";
cout<<" Sudoku Validator\n";
cout<<"======================\n\n";
cout<<"Welcome to Sudoku Validator!\n";
cout<<"Before we start, you will have to input the puzzle into this program.\n\n";
cout<<"You can either:-\n";
cout<<"\t1. Input the puzzle by entering the values manually. (Enter 1)\n";
cout<<"\t2. Input the puzzle by reading a file with values in it. (Enter 2)\n";
cout<<"\t The file must not have a name > 30 characters.\n";
cout<<"\t The file must be in the same directory as this C++ file.\n";
cout<<"\t The file must have all 81 values seperated with spaces.\n";
cout<<"\t Blank cells must be filled in as 0 (eg; 1 2 9 8 1 9 ...).\n";
cout<<"\t --> ";
int option;
cin>>option;
if(option==1) readFrameValues();
else if(option==2) readFrameValuesFile();
else{
while(true){
cout<<"\nYou seem to have entered an invalid option. Try again.\n";
cout<<"\t --> ";
cin>>option;
if(option==1) readFrameValues();
else if(option==2) readFrameValuesFile();
else continue;
break;
}
}
}
/**
* @desc Reads the values for the Sudoku Frame cell-by-cell.
* @param none
* @return none
*/
private:void readFrameValues(){
cout<<"\nEnter the specified value when prompted.\n";
cout<<"Enter 0 if cell is empty.\n\n";
int rowIter, colIter;
for(rowIter=0; rowIter<9; rowIter++){ //Iterating over cells to read vals.
for(colIter=0; colIter<9; colIter++){
int enteredValue;
cout<<"Enter value for cell["<<rowIter+1<<"]["<<colIter+1<<"] --> ";
cin>>enteredValue;
if(!(enteredValue>=1 && enteredValue<=9)){ //Checking for bounds in input.
while(true){ //We loop until valid input is read from user.
cout<<"Oops! You seem to have entered a wrong value! Try again.\n";
cout<<"Enter value for cell ["<<rowIter+1<<"]["<<colIter+1<<"] --> ";
cin>>enteredValue;
if(enteredValue>=1 && enteredValue<=9) break;
}
}
sudokuFrame[rowIter][colIter]=enteredValue;
}
cout<<"-------\n"; //Display a break after every row for convenience.
}
}
/**
* @desc This function reads values from a specified file.
* @param none
* @return none
*/
private:void readFrameValuesFile(){
int rowIter, colIter;
char filename[30];
ifstream file;
cout<<"\nEnter the name of the file which contains the puzzle --> ";
cin>>filename;
file.open(filename,ios::in);
for(rowIter=0; rowIter<9; rowIter++){
for(colIter=0; colIter<9; colIter++){
int readValue;
file>>readValue;
sudokuFrame[rowIter][colIter]=readValue;
if(file.eof()) break;
}
}
//If there aren't 81 values in the file, then it is invalid.
if((colIter*rowIter)!=81){
cout<<"Oops! The file doesn't have the required amount of values.\n";
cout<<"Try again!\n";
exit(EXIT_FAILURE);
}
file.close();
}
/**
* @desc Returns the value of a cell of a specified col and row.
* @param row (int) The specified row.
* @param col (int) The specified column.
* @return The value in the specified cell.
*/
public:int getCellValue(int row, int col){
return sudokuFrame[row][col];
}
};
class SudokuValidator{
SudokuFrame frame;
/**
* @desc Calls the function which validates the values in the Sudoku frame.
* @param none
*/
public:SudokuValidator(){
validateFrame();
}
/**
* @desc Calls functions which validates the rows, columns and 3x3 grids.
* @param none
* @return none
*/
private:void validateFrame(){
if(valuesAreValid()){
if(rowsAreValid()){
if(columnsAreValid()){
if(ThreeByThreeSquaresAreValid()){
cout<<"\nYour puzzle is valid!\n\n";
}
else cout<<"Your puzzle in invalid!\n";
}
else cout<<"Your puzzle is invalid!\n";
}
else cout<<"Your puzzle is invalid!\n";
}
else cout<<"Your puzzle is invalid!\n";
}
/**
* @desc Checks if all values in the frame are between the ranges of 1-9.
* @param none
* @return (bool) Whether all the values are valid or not.
*/
private:bool valuesAreValid(){
int rowIter, colIter;
for(rowIter=0; rowIter<9; rowIter++){
for(colIter=0; colIter<9; colIter++){
int cellValue=frame.getCellValue(rowIter,colIter);
if(!(cellValue>=0 && cellValue<=9)) return false;
}
}
return true;
}
/**
* @desc Checks if the rows are valid in the frame.
* @param none
* @return (bool) Whether the rows are valid or not.
*/
private:bool rowsAreValid(){
int rowIter, colIter, valIter;
for(rowIter=0; rowIter<9; rowIter++){ //Iterating over rows
for(valIter=1; valIter<=9; valIter++){ //Iter over 1-9
if(rowContains(rowIter,valIter)==false)
return false;
} //Value iteration loop ends here
} //Row iteration loop ends here
return true;
}
/**
* @desc Checks if the columns in the frame are valid.
* @param none
* @return (bool) Whether the columns are valid or not.
*/
private:bool columnsAreValid(){
int rowIter, colIter, valIter;
for(colIter=0; colIter<9; colIter++){ //Iterating over cols
for(valIter=1; valIter<=9; valIter++){ //Iter over 1-9
if(columnContains(colIter,valIter)==false)
return false;
} //Value iteration loops ends
} //Col iteration loop ends here
return true;
}
/**
* @desc Checks if the 3x3 grids in the frame are valid.
* @param none
* @return (bool) Whether the 3x3 grids are valid or not.
*/
private:bool ThreeByThreeSquaresAreValid(){
int squareIter, valIter;
for(squareIter=0; squareIter<9; squareIter++){ //Iter over squares
for(valIter=1; valIter<=9; valIter++){ //Iter over 1-9
if(squareContains(squareIter,valIter)==false)
return false;
} //Value iteration loop ends here
} //Square iteration loop ends here
return true;
}
/**
* @desc Checks whether a given value is present in a specified row.
* @param row (int) The specified row.
* @param value (int) The value to be checked for.
* @return (bool) Whether the value is present in the row or not.
*/
private:bool rowContains(int row, int value){
int colIter;
for(colIter=0; colIter<9; colIter++){
if(frame.getCellValue(row,colIter)==value)
return true;
}
return false;
}
/**
* @desc Checks whether a given value is present in the specified column.
* @param col (int) The specified column.
* @param value (int) The value to be checked for.
* @return (bool) Whether the value is present in the col or not.
*/
private:bool columnContains(int col, int value){
int rowIter=0;
for(rowIter=0; rowIter<9; rowIter++){
if(frame.getCellValue(rowIter,col)==value)
return true;
}
return false;
}
/**
* @desc Checks whether a given value is present in the specified 3x3 grid.
* @param squareNumber (int) The 3x3 grid specified. The available grids are 0-8.
* @param value (int) The value to be checked for.
* @return (bool) Whether the value is present or not.
*/
private:bool squareContains(int squareNumber, int value){
int rowStart=(squareNumber/3)*3;
int rowEnd=rowStart+2;
int colStart=(squareNumber%3)*3;
int colEnd=colStart+2;
int rowIter, colIter;
for(rowIter=rowStart; rowIter<=rowEnd; rowIter++){
for(colIter=colStart; colIter<=colEnd; colIter++){
if(frame.getCellValue(rowIter,colIter)==value)
return true;
}
}
return false;
}
};
int main(){
SudokuValidator s;
return 0;
}