-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.c
More file actions
231 lines (202 loc) · 5.79 KB
/
functions.c
File metadata and controls
231 lines (202 loc) · 5.79 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
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
// functions.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include<stdbool.h>
void printSudoku(char sudoku[9][9]) {
printf("-------------------------------\n");
for (int i = 0; i < 9; i++) {
printf("|");
for (int j = 0; j < 9; j++) {
printf(" %c ", sudoku[i][j]);
if((j+1) % 3 == 0){
printf("|");
}
}
printf("\n");
if((i+1) % 3 == 0){
printf("-------------------------------\n");
}
}
}
void read_sudoku(char a[9][9])
{
for (int i = 0; i < 9; i++) {
printf("Enter row %d: ", i + 1);
for (int j = 0; j < 9; j++) {
scanf(" %c", &a[i][j]);
}
}
}
int isValid(char sudoku[9][9], int row, int col, char num) {
// Check if num is present in current row or column
for (int i = 0; i < 9; i++) {
if (sudoku[row][i] == num || sudoku[i][col] == num) {
return 0;
}
}
// Check if num is present in current 3x3 subgrid
int startRow = row - row % 3;
int startCol = col - col % 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (sudoku[i + startRow][j + startCol] == num) {
return 0;
}
}
}
return 1;
}
int solveSudoku(char sudoku[9][9]) {
int row, col;
// Find an empty cell
int foundEmptyCell = 0;
for (row = 0; row < 9; row++) {
for (col = 0; col < 9; col++) {
if (sudoku[row][col] == '.') {
foundEmptyCell = 1;
break;
}
}
if (foundEmptyCell) {
break;
}
}
// If no empty cell found, Sudoku is solved
if (!foundEmptyCell) {
return 1;
}
// Generate random numbers for filling the empty cell
// srand(time(NULL));
char numbers[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
for (int i = 0; i < 9; i++) {
int j = rand() % (9 - i) + i;
char temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
// Try numbers from 1 to 9
for (int x = 0; x < 9; x++) {
if (isValid(sudoku, row, col, numbers[x])) {
sudoku[row][col] = numbers[x];
// Recur to solve remaining Sudoku
if (solveSudoku(sudoku)) {
return 1;
}
// If recursion fails, backtrack
sudoku[row][col] = '.';
}
}
// No solution found
return 0;
}
void fprintemptysudoku(FILE* fp){
fprintf(fp,"-------------------------------\n");
for (int i = 0; i < 9; i++) {
fprintf(fp,"|");
for (int j = 0; j < 9; j++) {
fprintf(fp, ".");
if((j+1) % 3 == 0){
fprintf(fp,"|");
}
}
fprintf(fp,"\n");
if((i+1) % 3 == 0){
fprintf(fp,"-------------------------------\n");
}
}
fclose(fp);
}
void fprintSudoku(FILE* fp,char sudoku[9][9]){
fprintf(fp,"-------------------------------\n");
for (int i = 0; i < 9; i++) {
fprintf(fp,"|");
for (int j = 0; j < 9; j++) {
fprintf(fp, "%c", sudoku[i][j]);
if((j+1) % 3 == 0){
fprintf(fp,"|");
}
}
fprintf(fp,"\n");
if((i+1) % 3 == 0){
fprintf(fp,"-------------------------------\n");
}
}
}
void read_file_into_matrix(FILE* file,char matrix[9][9]) {
char character;
int row = 0, col = 0;
if (file == NULL) {
printf("Error opening file.\n");
return;
}
while ((character = fgetc(file)) != EOF && row < 9) {
if ((character >= '0' && character <= '9') || character == '.') {
matrix[row][col] = character;
col++;
if (col == 9) {
col = 0;
row++;
}
}
}
fclose(file);
}
void generateSudoku(char sudoku[9][9], int difficulty,char r[9][9]) {
// Initialize Sudoku grid with empty cells
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
sudoku[i][j] = '.';
}
}
// Solve the empty Sudoku grid
solveSudoku(sudoku);
//copied the solution to the next arry
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
r[i][j]=sudoku[i][j];
}
}
// Remove cells based on difficulty level
int removeCount = 0;
switch (difficulty) {
case 1:
removeCount = 20; // Easy: Remove 20 cells
break;
case 2:
removeCount = 40; // Medium: Remove 40 cells
break;
case 3:
removeCount = 60; // Hard: Remove 60 cells
break;
default:
removeCount = 40; // Medium by default
}
while (removeCount > 0) {
int row = rand() % 9;
int col = rand() % 9;
if (sudoku[row][col] != '.') {
sudoku[row][col] = '.';
removeCount--;
}
}
// Print the generated Sudoku
printf("The . denotes the space to be filled\n");
printSudoku(sudoku);
FILE* fp=fopen("rhythm.txt","w");
FILE* c=fopen("solution.txt","w");
fprintSudoku(fp,sudoku);
fprintSudoku(c,sudoku);
fclose(fp);
fclose(c);
}
bool areArraysEqual(char array1[9][9], char array2[9][9]) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (array1[i][j] != array2[i][j]) {
return false; // If any element is different, arrays are not equal
}
}
}
return true; // All elements are equal
}