-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.cpp
More file actions
197 lines (190 loc) · 6.14 KB
/
project.cpp
File metadata and controls
197 lines (190 loc) · 6.14 KB
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
#include <bits/stdc++.h>
#include <cstdlib>
#include <cstring>
using namespace std;
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
const int N=9;
bool isSafe(int board[][N],int row,int col,int num){
//Check if num is already in row
for(int i=0;i<N;i++){
if(board[row][i]==num) return false;
}
//Check if num is already in column
for(int j=0;j<N;j++){
if(board[j][col]==num) return false;
}
//Check if num is in the same 3*3 box
int boxrowstart=row-row%3;
int boxcolumnstart=col-col%3;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(board[i+boxrowstart][j+boxcolumnstart]==num) return false;
}
}
return true;
}
void printboard(int grid[N][N]){
system("cls");
cout << "\t\t\t<================================================================================>" << endl;
cout << "\t\t\t| WELCOME TO SUDOKU Game! |" << endl;
cout << "\t\t\t| Fill in the missing numbers(represented by 0) to solve the puzzle. |" << endl;
cout << "\t\t\t<================================================================================>" << endl;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(j==3||j==6){
cout<<" | ";
}
cout<<grid[i][j]<<" ";
}
if(i==2||i==5){
cout<<endl;
for(int k=0;k<N;k++){
cout<<"---";
}
}
cout<<endl;
}
}
bool SudokuSolver(int board[N][N],int row,int col){
//if all cells are filled
if(row==N-1 && col==N) return true;
//if col are filled in a row change row
if(col==N){
row++;
col=0;
}
//Skipping cells already having a value
if(board[row][col]!=0)
return SudokuSolver(board,row,col+1);
//Filling current cells with all possible value
for(int num=1;num<=9;num++){
if(isSafe(board,row,col,num)){
board[row][col]=num;
if(SudokuSolver(board,row,col+1)){
return true;
}
board[row][col]=0;
}
}
return false;
}
bool isSolvedCompletely(int grid[N][N]){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(grid[i][j]==0) return false;
}
}
return true;
}
void playGame(int board[N][N]){
int ch;
int row,col,num;
while(true){
printboard(board);
cout<<endl<<endl;
cout << "Unable to solve? Enter -1 as row, col and num to view the solved sudoku."<<endl;
cout << "Enter row: ";
cin >> row;
cout << "Enter column: ";
cin >> col;
cout << "Enter number: ";
cin >> num;
if(row==-1||col==-1||num==-1){
SudokuSolver(board,0,0);
printboard(board);
cout<<endl;
cout<<"Better luck next time!"<<endl;
return;
}
if(isSolvedCompletely(board)){
break;
}
row--;
col--;
if(!isSafe(board,row,col,num)){
cout<<"Invalid Move! Try again!!"<<endl;
continue;
}
board[row][col]=num;
}
//Check if it has been solved completely
bool solved=true;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if(board[i][j]==0)
{
solved=false;
break;
}
if(solved)
{
cout<<"Congratulations! You have solved the puzzle"<<endl;
printboard(board);
}
else{
cout<<"Puzzle not solved.Better luck next time!"<<endl;
}
}
int main()
{
system("title Sudoku Game @copyassignment");
system("color B0");
int board[N][N] = {
{3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0}
};
cout << "\t\t\t<================================================================================>" << endl;
cout << "\t\t\t| WELCOME TO SUDOKU Game! |" << endl;
cout << "\t\t\t| Fill in the missing numbers(represented by 0) to solve the puzzle. |" << endl;
cout << "\t\t\t<================================================================================>" << endl;
while(true){
int choice;
cout<<endl<<endl;
cout << "\t\t[1] Solve the Sudoku" << endl;
cout << "\t\t[2] Unable to solve? View the solved Sudoku" << endl;
cout << "\t\t[3] Exit" << endl;
cout << "\t\tEnter your choice: ";
cin>>choice;
switch(choice){
case 1:
playGame(board);
break;
case 2:
if(SudokuSolver(board,0,0)){
cout<<"Completely solved Sudoku is: "<<endl;
cout<<endl<<endl;
for(int row=0;row<N;row++){
for(int column=0;column<N;column++){
if(column==3||column==6) cout<<" | ";
cout<<board[row][column]<<" ";
}
if(row==2||row==5){
cout<<endl;
for(int i=0;i<N;i++){
cout<<"---";
}
}
cout<<endl;
}
cout<<endl;
cout<<"Better luck next time!"<<endl;
}
else{
cout<<"No solution found!"<<endl;
}
break;
case 3:
exit(0);
default:
cout<<"Invalid choice"<<endl;
}
return 0;
}
}