-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.cpp
361 lines (323 loc) · 12.6 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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// thibault Joseph Twahirwa
// CSE 20312, Lab 4
//
// Puzzle class implementation
#include <iostream>
#include <fstream>
#include "Puzzle.h"
using namespace std;
Puzzle::Puzzle( string puzzleFile ) // constructor
{
inFile.open(puzzleFile.c_str(), ios::in); // opens the puzzlefile for input
int tempVar; // temporary variable used to store puzzle values
vector<int> temp; // temporary vector stores each row one at a time
for(int i=0; i<9; i++) {
temp.clear(); // clear the previous row out
for(int j=0; j<9; j++) {
inFile >> tempVar; // read in row i one variable at a time
temp.push_back(tempVar); // add the row variables to tempVect
}
board.push_back(temp); //construct board
}
inFile.close();
}
void Puzzle::solve()
{
checkPosVals(); // gets the initial possibilities established
checkHoriz();
checkVert();
checkminigrid();
cout << endl << "Original given board: " << endl << endl;
printBoard();
while( solveInit() || solveHoriz() || solveVert() || solveminigrid() ) // while the solving functions can still add solutions
{
checkPosVals();
checkHoriz();
checkVert();
checkminigrid();
}
cout << "Final solved board: " << endl << endl;
printBoard();
}
void Puzzle::checkPosVals() // fills the 3dimensional vector with possibilities 1-9
{
vector<int> temp1;
vector< vector<int> > temp2;
possibleVals.clear(); // clear the possibilities
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
if( board[row][col] == 0 ) // only need to list possibilities for zero spaces
{
for( int i = 1; i <= 9; i++ ) // values of 1 through 9
{
temp1.push_back(i);
}
}
temp2.push_back( temp1 ); // populating 2D vector with 1D vectors
temp1.clear(); // clear this vector
}
possibleVals.push_back( temp2 ); // populating the possibilities 3D vector with 2D vectors
temp2.clear(); // clear this vector
}
}
void Puzzle::checkHoriz() // at every position, check possibilities based on horizontal members
{
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
for( int i = 0; i < 9; i++ ) // checking every column (horizontal)
{
if( col == i ) continue; // skip checking
else if( board[row][i] == 0 ) continue;
else
{
for( int j = 0; j < possibleVals[row][col].size(); j++ ) // for every possibility remaining
{
if( board[row][i] == possibleVals[row][col][j] )
{
possibleVals[row][col].erase( possibleVals[row][col].begin() + j ); // remove that possibility if found in row
}
}
}
}
}
}
}
void Puzzle::checkVert() // at every position, check possibilities based on vertical members
{
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
for( int i = 0; i < 9; i++ ) // checking every row (vertical)
{
if( row == i ) continue;
else if( board[i][col] == 0 ) continue;
else
{
for( int j = 0; j < possibleVals[row][col].size(); j++ ) // for every possibility remaining
{
if( board[i][col] == possibleVals[row][col][j] )
{
possibleVals[row][col].erase( possibleVals[row][col].begin() + j ); // remove that possibility if found in row
}
}
}
}
}
}
}
void Puzzle::checkminigrid() // at every position, check possibilities based on containing minigrid
{
int gridSize = 3;
int colStart; // find the beginning column index
int rowStart; // find the beginning row index
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
if( row < gridSize ) rowStart = 0; // determine the row and col to begin the search
else if( row < 2*gridSize ) rowStart = gridSize;
else rowStart = 2*gridSize;
if( col < gridSize ) colStart = 0;
else if( col < 2*gridSize ) colStart = gridSize;
else colStart = 2*gridSize;
for( int r = rowStart; r < (rowStart + 3); r++ ) // row within minigrid
{
for( int c = colStart; c < (colStart + 3); c++ ) // column within minigrid
{
if( row == r && col == c ) continue; // skip checking against itself
else if( board[r][c] == 0 ) continue; // zero spaces cannot help us
else
{
for( int j = 0; j < possibleVals[row][col].size(); j++ ) // for every possibility remaining
{
if( board[r][c] == possibleVals[row][col][j] )
{
possibleVals[row][col].erase( possibleVals[row][col].begin() + j ); // remove that possibility if found in row
}
}
}
}
}
}
}
}
bool Puzzle::solveInit() // enter solutions if there are single possibilities
{
bool boolReturn = false;
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
if( possibleVals[row][col].size() == 1 ) // singleton that can be chosen right away
{
board[row][col] = possibleVals[row][col][0];
boolReturn = true;
}
}
}
return boolReturn;
}
bool Puzzle::solveHoriz() // fill in the board based on singletons in each column
{
bool unique = false; // if unique possibility (singeton) found
bool boolReturn = false; // boolean to return true if sol added
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
for( int currPoss = 0; currPoss < possibleVals[row][col].size(); currPoss++ ) // for every possibility in the current index
{
for( int i = 0; i < 9; i++ ) // checking every column (horizontal)
{
if( col == i ) continue; // skip checking against itselfi
else if( possibleVals[row][i].size() == 0 ) continue; // skip empty
else
{
for( int checkPoss = 0; checkPoss < possibleVals[row][i].size(); checkPoss++ ) // for every possibility being checked
{
if( possibleVals[row][col][currPoss] == possibleVals[row][i][checkPoss] )
{
unique = false;
break;
}
else unique = true;
}
if( !unique ) break;
}
}
if( unique ) // we have a unique possibility in the row
{
board[row][col] = possibleVals[row][col][currPoss];
boolReturn = true;
unique = false;
}
}
}
}
return boolReturn;
}
bool Puzzle::solveVert() // fill in the board based on singletons in each row
{
bool unique = false; // if unique possibility (singleton) found
bool boolReturn = false;
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
for( int currPoss = 0; currPoss < possibleVals[row][col].size(); currPoss++ ) // for every possibility in the current index
{
for( int i = 0; i < 9; i++ ) // checking every row (vertical)
{
if( row == i ) continue; // skip checking against itself
else if( possibleVals[i][col].size() == 0 ) continue; // skip empty
else
{
for( int checkPoss = 0; checkPoss < possibleVals[i][col].size(); checkPoss++ ) // for every possibility being checked
{
if( possibleVals[row][col][currPoss] == possibleVals[i][col][checkPoss] )
{
unique = false;
break;
}
else unique = true;
}
if( !unique ) break;
}
}
if( unique ) // we have a unique possibility in the row
{
board[row][col] = possibleVals[row][col][currPoss];
boolReturn = true;
unique = false;
}
}
}
}
return boolReturn;
}
bool Puzzle::solveminigrid() // at every position, isolate possibilities based on containing minigrid
{
int gridSize = 3;
int colStart;
int rowStart;
bool unique = false;
bool boolReturn = false;
for( int row = 0; row < board.size(); row++ )
{
for( int col = 0; col < board[row].size(); col++ )
{
if( row < gridSize ) rowStart = 0; // determine the row and col to begin the search
else if( row < 2*gridSize ) rowStart = gridSize;
else rowStart = 2*gridSize;
if( col < gridSize ) colStart = 0;
else if( col < 2*gridSize ) colStart = gridSize;
else colStart = 2*gridSize;
for( int currPoss = 0; currPoss < possibleVals[row][col].size(); currPoss++ ) // for every possibility in the current position
{
for( int r = rowStart; r < (rowStart + 3); r++ ) // row within minigrid
{
for( int c = colStart; c < (colStart + 3); c++ ) // column within minigrid
{
if( row == r && col == c ) continue; // skip checking against itself
else if( possibleVals[r][c].size() == 0 ) continue; // skip empty
else
{
for( int checkPoss = 0; checkPoss < possibleVals[r][c].size(); checkPoss++ ) // for every possibility being checked
{
if( possibleVals[row][col][currPoss] == possibleVals[r][c][checkPoss] )
{
unique = false;
break;
}
else unique = true;
}
if( !unique ) break;
}
}
if( !unique ) break;
}
if( unique )
{
board[row][col] = possibleVals[row][col][currPoss];
boolReturn = true;
unique = false;
}
}
}
}
return boolReturn;
}
void Puzzle::printBoard() // prints the 2D board to the screen
{
int RowCounter = 0;
int colCounter = 0;
cout << " -------------------------" << endl;
cout << " |";
for( int i = 0; i < board.size(); i++ )
{
if( RowCounter == 3 )
{
cout << " -------------------------" << endl;
RowCounter = 0;
}
for( int j = 0; j < board[i].size(); j++ )
{
if( colCounter == 3 )
{
cout << " |";
colCounter = 0;
}
cout << " " << board[i][j];
colCounter++;
}
cout << " |";
RowCounter++;
cout << endl;
}
cout << " -------------------------" << endl << endl;
}