-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku3.c
189 lines (172 loc) · 5.33 KB
/
sudoku3.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
#include "rlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct field_t {
unsigned int value;
unsigned int selected;
unsigned int initial;
unsigned int column;
unsigned int row;
unsigned int index;
unsigned char box;
} field_t;
field_t * new_field(){
field_t * field = (field_t *)calloc(1,sizeof(field_t));
return field;
}
typedef struct row_t {
unsigned int index;
field_t * columns;
unsigned int column_count;
} row_t;
row_t * new_row(unsigned int size) {
row_t * row = (row_t *)calloc(1, sizeof(row));
row->columns = calloc(size,sizeof(field_t));
row->index = 0;
return row;
}
typedef struct grid_t {
unsigned int size;
unsigned int row_count;
unsigned int field_count;
row_t * rows;
field_t * fields;
field_t * (*get)(struct grid_t * grid, unsigned int row, unsigned int col);
field_t * (*get_empty_field)(struct grid_t * grid);
} grid_t;
field_t * grid_get(grid_t * grid, unsigned int row, unsigned int col){
return &grid->rows[row].columns[col];
}
field_t * grid_get_empty_field(grid_t * grid){
for(int i = 0; i < grid->size * grid->size; i++){
printf("AT:%d\n",i);
printf("V:%d\n",grid->fields[i].value);
printf("V2:%d\n",grid->rows[i].columns[i].value);
printf("V2:%d\n",grid->get(grid,i,i)->value);
if(grid->fields[i].value == 0)
return &grid->fields[i];
}
return NULL;
}
char get_box_letter(grid_t * grid, unsigned int row, unsigned int col){
//char box_letter = 65 + (row)*grid->size / grid->size / 3 + row; // (row*grid->size+col)+(grid->size / 3);// + grid->size % (grid->size / 3));
char box_letter = 65 + ((row % (grid->size / 3))) + grid->size % (grid->size / 3);// (row-1)*grid->size+(col-1)+(row-1)+(col-1);
//(65 + row * (grid->size / 3) % (grid->size / 3)+col); /// (grid->size / 3));
return box_letter;
}
grid_t * new_grid(unsigned int size){
grid_t * grid = (grid_t *)calloc(1,sizeof(grid_t));
grid->get = grid_get;
grid->get_empty_field = grid_get_empty_field;
grid->size = size;
grid->row_count = 0;
grid->rows = (row_t *)calloc(size, sizeof(row_t));
grid->fields = (field_t *)calloc(size*size, sizeof(field_t));
for(unsigned int irow = 0; irow < size; irow++){
row_t * row = new_row(size);
row->index = grid->row_count;
row->column_count = size;
grid->row_count++;
grid->rows[row->index] = *row;
for(unsigned int icol = 0; icol < size; icol++){
unsigned int field_index = irow * size + icol;
field_t * field = new_field();
field->column = icol;
field->row = irow;
field->index = field_index;
field->box = get_box_letter(grid, irow, icol);
row->columns[icol] = *field;
grid->fields[field_index] = *field;
grid->field_count++;
}
}
return grid;
}
char * field_to_string_human(field_t * field){
static char result[4] = {0};
result[0] = 0;
sprintf(result,"%d, ",field->value);
return sbuf(result);
}
char * field_to_string(field_t * field){
static char result[4] = {0};
result[0] = 0;
sprintf(result,"%d",field->value);
return sbuf(result);
}
char * row_to_string(row_t * row,char * field_string_fn(field_t * field)){
static char result[100] = {0};
result[0] = 0;
for(unsigned int i = 0; i < row->column_count; i++){
strcat(result, field_string_fn(&row->columns[i]));
}
return sbuf(result);
}
char * row_to_string_human(row_t * row,char * field_string_fn(field_t * field)){
static char result[100] = {0};
result[0] = 0;
for(unsigned int i = 0; i < row->column_count; i++){
strcat(result, field_string_fn(&row->columns[i]));
}
strcat(result, "\n");
return sbuf(result);
}
char * row_to_string_c(row_t * row){
static char result[1000] = {0};
result[0] = 0;
strcat(result,"{");
for(int i = 0; i < row->column_count; i++){
strcat(result, field_to_string_human(&row->columns[i]));
}
result[strlen(result)-2] = 0;
strcat(result,"},\n");
return sbuf(result);
}
char * grid_to_string(grid_t * grid, char * row_to_string_fn(row_t * row,char * (field_t *)), char * field_string_fn(field_t * field)){
static char result[1000] = {0};
result[0] = 0;
for(unsigned int irow = 0; irow < grid->size; irow++){
row_t row = grid->rows[irow];
row.column_count++;
strcat(result,row_to_string_fn(&row,field_string_fn));
}
result[strlen(result)-1] = 0;
return sbuf(result);
}
char * grid_to_string_c(grid_t * grid){
char result[1000] = {0};
result[0] = 0;
strcat(result,"{\n");
for(int i = 0; i < grid->size; i++){
strcat(result," ");
strcat(result,row_to_string_c(&grid->rows[i]));
}
result[strlen(result)-2] = 0;
strcat(result,"\n}");
return sbuf(result);
}
char * grid_to_string_human(grid_t * grid){
return sbuf(grid_to_string(grid, row_to_string_human, field_to_string_human));
}
int main(){
grid_t * grid = new_grid(9);
printf("%d \n",grid->rows[3].index);
printf("%d \n",grid->fields[30].index);
printf("%d \n",grid->rows[6].columns[6].index);
field_t * f = grid->get(grid, 0,3);
printf("%d \n", f->index);
//for(int i = 0; i < grid->size*grid->size;i++){
// printf("%c",grid->fields[i].box);
// }
grid->get(grid,5,5)->value = 3;
grid->get(grid,0,0)->value = 1;
printf("**%d**\n",grid->get(grid,5,5)->index);
printf("^^%d^^\n",grid->get(grid,5,5)->value);
printf("$$%d$$\n", grid->fields[0].index);
printf("!!%d!!\n", grid->get_empty_field(grid)->index);
printf("<%s>\n",grid_to_string(grid,row_to_string,field_to_string));
printf("<%s>\n",grid_to_string_human(grid));
printf("<%s>\n",grid_to_string_c(grid));
return 0;
}