-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilesAux.c
326 lines (293 loc) · 9.31 KB
/
FilesAux.c
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
#include "MainAux.h"
#include <stdlib.h>
void free_mem_board() {
int i;
for(i=0; i<curr_board->len; i++) {
free(curr_board->board[i]);
}
free(curr_board->board);
free(curr_board);
}
int solve_mode_to_file(char* file_name){
int i,j;
FILE *in_file = fopen(file_name, "w");
if (! in_file ){
printf("Error: file can't be read\n");
return 0;
}
fprintf(in_file, "%d %d\n", curr_board->block_width, curr_board->block_height);
for (i = 0; i < curr_board->len; i++) {
for (j = 0; j < curr_board->len; j++) {
fprintf(in_file, "%d", curr_board->board[i][j].value);
if (curr_board->board[i][j].is_fixed!=0 && curr_board->board[i][j].value!=0) {
fprintf(in_file, ".");
}
fprintf(in_file, " ");
}
fprintf(in_file, "\n");
}
fclose(in_file);
return 1;
}
int edit_mode_to_file(char* file_name) {
int i,j;
FILE *in_file = fopen(file_name, "w");
if (! in_file ){
printf("Error: file can't be read\n");
return 0;
}
fprintf(in_file, "%d %d\n", curr_board->block_width, curr_board->block_height);
for (i = 0; i < curr_board->len; i++) {
for (j = 0; j < curr_board->len; j++) {
fprintf(in_file, "%d", curr_board->board[i][j].value);
if (curr_board->board[i][j].value != 0) {
fprintf(in_file, ". ");
} else {
fprintf(in_file, " ");
}
}
fprintf(in_file, "\n");
}
fclose(in_file);
return 1;
}
int trans_board_to_file(char* file_name){
if(state == Solve) {
return solve_mode_to_file(file_name);
} else if(state == Edit && check_erroneous_board()){
printf("Error: Erroneous board cannot be saved in Edit mode!\n");
return 0;
}
else if(!check_validate() && state == Edit){
printf("Error: Board without a solution cannot be saved in Edit mode!\n");
return 0;
}
else {
if(state == Edit) {
return edit_mode_to_file(file_name);
}
}
return 0;
}
void create_board_from_file(int len, int width, int height) {
int i;
curr_board = (struct curr_board *) calloc(len, sizeof(struct cell));
if (curr_board == NULL) {
memory_error("calloc");
}
curr_board->block_width = width;
curr_board->block_height = height;
curr_board->len = len ;
curr_board->board = (struct cell **) calloc(len, sizeof(struct cell *));
if (curr_board->board == NULL) {
memory_error("calloc");
}
for (i = 0; i < len; ++i) {
curr_board->board[i] = (struct cell *) calloc(len, sizeof(struct cell));
if (curr_board->board[i] == NULL) {
memory_error("calloc");
}
}
}
void create_temporary_board_from_file(int len, int width, int height) {
int i;
temporary_board = (struct curr_board *) calloc(len, sizeof(struct cell));
if (temporary_board == NULL) {
memory_error("calloc");
}
temporary_board->block_width = width;
temporary_board->block_height = height;
temporary_board->len = len ;
temporary_board->board = (struct cell **) calloc(len, sizeof(struct cell *));
if (temporary_board->board == NULL) {
memory_error("calloc");
}
for (i = 0; i < len; ++i) {
temporary_board->board[i] = (struct cell *) calloc(len, sizeof(struct cell));
if (temporary_board->board[i] == NULL) {
memory_error("calloc");
}
}
}
void copy_from_temporary_to_curr_board(){
int i,j;
create_board_from_file(temporary_board->len, temporary_board->block_width, temporary_board->block_height);
for (i=0;i<temporary_board->len;i++){
for (j=0;j<temporary_board->len;j++){
curr_board->board[i][j].is_fixed = temporary_board->board[i][j].is_fixed;
curr_board->board[i][j].value = temporary_board->board[i][j].value;
curr_board->board[i][j].is_erroneous = temporary_board->board[i][j].is_erroneous;
curr_board->board[i][j].list_poss_values = temporary_board->board[i][j].list_poss_values;
curr_board->board[i][j].list_poss_values_len = temporary_board->board[i][j].list_poss_values_len;
}
}
}
void free_temporary_board(){
int i;
for (i = 0; i < temporary_board->len; ++i)
free(temporary_board->board[i]);
free(temporary_board->board);
free(temporary_board);
}
int scan_size_from_file(FILE* in_file){
int height, width, len;
if(fscanf(in_file, "%d %d", &height, &width) != 2 )
{
printf("Error: error in scanning size\n");
return 0;
}
if( height < 1 || width<1){
printf("Error: size smaller than 1\n");
return 0;
}
else{
len = (height)*(width);
create_temporary_board_from_file(len, width, height);
}
return 1;
}
int scan_rows_from_file(FILE *in_file, enum state State) {
int i,j;
int c;
for(i=0; i<temporary_board->len; i++) {
for (j = 0; j < temporary_board->len; j++) {
if (fscanf(in_file, "%d", &temporary_board->board[i][j].value) != 1) {
return 0;
} else{
if (temporary_board->board[i][j].value < 0 ||
temporary_board->board[i][j].value > temporary_board->len){
printf("Error: a number out of range (1,%d) in file!\n",temporary_board->len);
}
if (temporary_board->board[i][j].value != 0) {
temporary_board->board[i][j].is_fixed = 0;
}
}
if ((fgetc(in_file))=='.' && State == Solve && temporary_board->board[i][j].value != 0){
temporary_board->board[i][j].is_fixed = 1;
}
}
}
/*return 0 if there is anything else written if the file*/
while ((c=fgetc(in_file))!=EOF){
if(c>=33&&c<=126) {
return 0;
}
}
return 1;
}
/*
* Return false if there's a cell in the same row
* with the same value, otherwise, returns true
* */
int in_row_fixed(int row, int num) {
int j;
for (j = 0; j < temporary_board->len; ++j) {
if (temporary_board->board[row][j].value == num && temporary_board->board[row][j].is_fixed) {
if(num!=0) {
return 1;
}
else{
return 0;
}
}
}
return 0;
}
/*
* Return false if there's a cell in the same column
* with the same value, otherwise, returns true
* */
int in_col_fixed(int col, int num) {
int j;
for (j = 0; j < temporary_board->len; ++j) {
if (temporary_board->board[j][col].value == num && temporary_board->board[j][col].is_fixed) {
if (num != 0) {
return 1;
} else {
return 0;
}
}
}
return 0;
}
/*
* Return false if there's a cell in the same block
* with the same value, otherwise, returns true
* */
int in_block_fixed(int x, int y, int num) {
int i, j;
for (i = 0; i < temporary_board->block_height; ++i) {
for (j = 0; j < temporary_board->block_width; ++j) {
if (temporary_board->board[i+x][j+y].value == num && temporary_board->board[i+x][j+y].is_fixed) {
if(num!=0) {
return 1;
}
else{
return 0;
}
}
}
}
return 0;
}
int is_valid_fixed_set(int i, int j, int num){
int block_x = (i / (temporary_board->block_height))*(temporary_board->block_height);
int block_y = (j / (temporary_board->block_width))*(temporary_board->block_width);
return (!in_row_fixed(i,num) && !in_col_fixed(j,num) &&
!in_block_fixed(block_x,block_y,num));
}
int check_erroneous_fixed_cells_temp(){
int i,j,num;
for (i = 0; i < temporary_board->len ; ++i) {
for (j = 0; j < temporary_board->len; ++j) {
if (temporary_board->board[i][j].is_fixed){
num = temporary_board->board[i][j].value;
temporary_board->board[i][j].value = 0;
if (!is_valid_fixed_set(i,j,num)){
temporary_board->board[i][j].value = num;
return 0;
}else{
temporary_board->board[i][j].value = num;
}
}
}
}
return 1;
}
int trans_file_to_board(char* file_name, enum state State){
FILE* in_file = fopen(file_name, "r");
if (! in_file )
{
printf("Error: file can't be read\n");
return 0;
}
if(! scan_size_from_file(in_file) ){
fclose(in_file);
return 0;
}
if(! scan_rows_from_file(in_file, State)){
printf("Error: File's board is not in a correct format\n");
free_temporary_board();
fclose(in_file);
return 0;
}
if(! check_erroneous_fixed_cells_temp()){
printf("Error: File's board is not legal - erroneous fixed cells\n");
free_temporary_board();
fclose(in_file);
return 0;
}
if(state!=Init){
free_mem_board();
clear_list(end_list);
end_list = NULL;
start_list = NULL;
}
state = State;
copy_from_temporary_to_curr_board();
free_temporary_board();
update_erroneous_cells();
fclose(in_file);
init_start_list();
return 1;
}