-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
370 lines (302 loc) · 7.9 KB
/
game.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
361
362
363
364
365
366
367
368
369
370
#include <iostream>
#include <string.h>
#include<random>
#include<fstream>
using namespace std;
void Board();
void Input();
bool over();
void PlayAgain();
void SaveBoard(string);
void ReadBoard(string);
void InitializeBoard();
bool recursiveSolve(int,int);
bool isValidBox(int,int, int);
bool isBuiltinBox(int,int);
bool isPresentInRow(int,int);
bool isPresentInCol(int,int);
bool isPresentInBlock(int,int, int);
int rows = 9, cols = 9;
int board[9][9] = {0};
int solution[9][9] = {0};
int builtin[9][9] = {0};
bool quit = false;
char playAgain = 'Y'; //The Default PlayAgain Option
random_device Seed; //RandomValue Generator
uniform_int_distribution<int> randomNumber(1,9);
int main()
{
cout << endl;
cout << "Welcome to the Sudoku-Board-Game. A Logical-Puzzle Game. \n\n";
do {
InitializeBoard();
Board();
//Main Game
while(true)
{
cout << "Select the Box (Row Col) on the Board: ";
Input();
Board();
if(quit)
break;
if(over()) {
cout << "\n\n Congratulations! You Won. The Whole Board is Completed. \n\n";
break;
}
}
PlayAgain();
}
while(playAgain == 'Y');
cout << endl << endl;
cout << "Thanks for Playing. Hope You Enjoyed the Game! \n";
cout << "It would be very Kind of you, if you give us an Honest Feedback. \n\n";
cout << "You can Find me in GitHub https://github.com/Haseebullah9012. \n";
getchar();
return 0;
}
void Board()
{
cout << endl;
for(int r=0; r<rows; r++)
{
if(r%3 == 0)
cout << "\t ------------------------- \n";
cout << "\t ";
for(int c=0; c<cols; c++) {
if(c%3 == 0)
cout << "| ";
if(board[r][c] == 0)
cout << board[r][c] << " ";
else if(board[r][c] == builtin[r][c])
cout << "\033[4;1;32m" << board[r][c] << "\033[0m ";
else
cout << "\033[36m" << board[r][c] << "\033[0m ";
}
cout << "| \n";
}
cout << "\t ------------------------- \n";
cout << endl;
}
void Input()
{
int a,b;
cin >> a >> b;
if(a==-9 && b==-9) {
quit = true;
cout << "\n\n You are Quiting the Game. \n\n";
return;
}
else if(a==-8 && b==-8) {
SaveBoard("Game");
cout << "\n\n Your Sudoku Game is Saved. \n\n";
return;
}
if(a<1 || a>rows || b<1 || b>cols) {
cout << "Oops! Its Outside the Box. There are only 9 Rows and Columns. \n\n";
return;
}
if(isBuiltinBox(a-1,b-1)) {
cout << "Oops! Its a Builtin Box, with Predefined Value. \n\n";
return;
}
int value;
cout << "Enter the Number: ";
cin >> value;
if(value<0 || value>9) {
cout << "Oops! Its not Legal. Number should be only 1 to 9. 0 for Empty. \n\n";
return;
}
if(value == board[a-1][b-1]) {
cout << value << " is already Present Here. \n\n";
return;
}
if(value!=0 && !isValidBox(a-1,b-1, value))
{
cout << "Oops! " << value << " is already Present in the ";
if(isPresentInRow(a-1, value))
cout << "Row ";
if(isPresentInCol(b-1, value))
cout << "Column ";
if(isPresentInBlock(a-1,b-1, value))
cout << "Box ";
cout << endl << endl;
return;
}
board[a-1][b-1] = value;
}
bool over()
{
for(int r=0; r<rows; r++)
for(int c=0; c<cols; c++)
if(board[r][c] == 0)
return false;
return true;
}
void InitializeBoard()
{
char importBoard;
cout << "Do You Want to Import Saved Sudoku Board (Y/N): ";
cin >> importBoard;
importBoard = toupper(importBoard);
cin.ignore(255, '\n');
if(importBoard == 'Y')
ReadBoard
("Solution");
else
recursiveSolve(0,0); //Generate New Board
//Copy the Filled Board as Solution
for(int r=0; r<rows; r++)
for(int c=0; c<cols; c++)
solution[r][c] = board[r][c];
SaveBoard("Solution");
if(importBoard == 'Y')
ReadBoard("Puzzle");
else {
for(int i=0, r,c; i<80; i++) {
r = randomNumber(Seed) -1;
c = randomNumber(Seed) -1;
board[r][c] = 0; //Remove Values from Random Boxes (Max60-Avg42, Max80-Avg52)
}
}
//Make The Builtin the Same as Board
for(int r=0; r<rows; r++)
for(int c=0; c<cols; c++)
builtin[r][c] = board[r][c];
SaveBoard("Puzzle");
if(importBoard == 'Y')
ReadBoard("Game");
}
bool recursiveSolve(int row,int col)
{
//Next Row
if(col>=cols) {
col=0;
row++;
if(row>=rows)
return true; //All Rows Filled
}
if(board[row][col] != 0)
return recursiveSolve(row,col+1); //Skip Default Numbers
int num = randomNumber(Seed);
for(int i=0; i<10; i++,num++)
{
if(isValidBox(row,col, num)) {
board[row][col] = num;
if(recursiveSolve(row,col+1))
return true;
else
board[row][col] = 0;
}
if(num>=9)
num=0;
}
return false;
}
bool isValidBox(int row, int col, int x)
{
if(!isPresentInRow(row, x) && !isPresentInCol(col, x) && !isPresentInBlock(row,col, x))
return true;
return false;
}
bool isBuiltinBox(int row, int col)
{
if(board[row][col]!=0 && board[row][col]==builtin[row][col])
return true;
return false;
}
bool isPresentInRow(int row, int x)
{
for(int c=0; c<cols; c++)
if(board[row][c] == x)
return true;
return false;
}
bool isPresentInCol(int col, int x)
{
for(int r=0; r<rows; r++)
if(board[r][col] == x)
return true;
return false;
}
bool isPresentInBlock(int row, int col, int x)
{
int i = row - row%3;
int j = col - col%3;
for(int r=i; r<i+3; r++)
for(int c=j; c<j+3; c++)
if(board[r][c] == x)
return true;
return false;
}
void PlayAgain()
{
cout << "Do You Want to Play Again (Y/N): ";
cin >> playAgain;
playAgain = toupper(playAgain);
cin.ignore(255, '\n');
if(playAgain != 'Y' && playAgain != 'N') {
cout << " Oops! Its not a legal Response. \n\n";
cout << "Again, ";
PlayAgain();
}
}
void SaveBoard(string name)
{
ofstream file("Sudoku.txt", ios::out | ios::app);
if(name=="Solution")
file << "\n\n" << "\t -------- Solution ------- \n";
else if(name=="Puzzle")
file << "\t --------- Puzzle -------- \n";
else if(name=="Game")
file << "\t ---------- Game --------- \n";
for(int r=0; r<rows; r++)
{
if(r%3 == 0)
file << "\t ------------------------- \n";
file << "\t ";
for(int c=0; c<cols; c++)
{
if(c%3 == 0)
file << "| ";
file << board[r][c] << " ";
}
file << "| \n";
}
file << "\t ------------------------- \n";
file.close();
}
void ReadBoard(string name)
{
ifstream file("Sudoku.txt");
if(!file.is_open())
return;
string line;
int skip = 1;
if(name=="Puzzle")
skip += 14;
else if(name=="Game")
skip += 2*14;
for(int i=0; i<skip; i++)
getline(file, line); //Skip to the Desired Board
int r=0, c=0, num;
while(!file.eof())
{
getline(file, line);
for(int i=4; i<line.length(); i++)
{
num = line[i]-'0';
if(num>=0 && num<=9)
{
board[r][c] = num;
c++;
if(c==cols) {
c=0;
r++;
}
}
}
if(r>=rows)
break;
}
file.close();
}