diff --git a/.gitignore b/.gitignore index 06b351e..b8cecc6 100644 --- a/.gitignore +++ b/.gitignore @@ -35,11 +35,5 @@ # Vim temp *.swp -<<<<<<< HEAD -CMakeLists.txt -.idea/ -**/cmake-build-debug/* -======= # Sublime files *.sublime* ->>>>>>> github/develop diff --git a/Makefile b/Makefile index c4e0bd1..577c3c7 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gtertysh +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/12/14 16:54:48 by gtertysh #+# #+# # -# Updated: 2016/12/20 20:05:58 by ykolomie ### ########.fr # +# Updated: 2016/12/20 20:37:58 by gtertysh ### ########.fr # # # # **************************************************************************** # @@ -21,10 +21,9 @@ SRC_FILES = main.c \ move_up_left.c \ read_file.c \ string_to_table.c \ - to_strct_array.c \ to_letters.c \ - build_matrix.c \ dancing_links.c \ + get_types.c \ test!_check.c \ test!_print_one_string.c \ test!_print_table.c \ @@ -46,14 +45,12 @@ LIBFOLDER = ./libft/ FLAGS = -Werror -Wextra -Wall -STACK = -Wl,-stack_size -Wl,0x10000000000 - CC = clang all: $(NAME) $(NAME): $(OBJ) $(LIBFOLDER)$(LIB) - $(CC) $(FLAGS) $(STACK) $(OBJ) $(LIBFLAGS) -o $(NAME) + $(CC) $(FLAGS) $(OBJ) $(LIBFLAGS) -o $(NAME) $(OBJDIR)%.o : $(SRCDIR)%.c $(CC) $(FLAGS) $(INC) -c $< -o $@ diff --git a/inc/fillit.h b/inc/fillit.h index 9b2c403..d8767f0 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -45,8 +45,10 @@ typedef struct s_node struct s_node *down; int row; struct s_node *column; + t_coord coord; } t_node; +extern t_ttrmn g_templates[19]; // reads from file char *read_file(char *path); @@ -77,10 +79,13 @@ void to_letters(char **ttr); int *to_strct_array(char **ttr, t_ttrmn *templates); t_node *add_column(t_node *root); + t_node *add_node(t_node *col, int row); + void cover(t_node *to_cover); + void uncover(t_node *to_uncover); - +int *get_types(char **ttr); #endif diff --git a/src/build_matrix.c b/src/build_matrix.c deleted file mode 100644 index 1942d4c..0000000 --- a/src/build_matrix.c +++ /dev/null @@ -1,106 +0,0 @@ -#include "fillit.h" - -unsigned int ft_sqrt_ceil(unsigned int num) -{ - unsigned int i; - - i = 1; - while (i * i < num) - i++; - return (i); -} - -void put_figure(char **char_matrix, t_ttrmn t, int y, int x) -{ - int i; - int j; - int t_x; - int runner; - - t_x = x; - i = 0; - runner = 0; - while (i < t.y) - { - x = t_x; - j = 0; - while (j < t.x) - { - char_matrix[y][x] = t.t[runner]; - j++; - x++; - runner++; - } - } -} - -void print_char_matrix(char **matrix, int size) -{ - int y; - - y = 0; - while (y < size) - { - printf("%s\n", matrix[y]); - y++; - } -} - -int **build_matrix(t_ttrmn **ttr) -{ - int **matrix; - char **char_matrix; - int i; - int j; - int amount; - int size; - int k; - int curr_line; - - amount = 0; - i = 0; - matrix = malloc(sizeof(int*) * HOW_MUCH); - while (i < HOW_MUCH) - { - j = 0; - matrix[i] = malloc(sizeof(int) * HOW_MUCH); - while (j < HOW_MUCH) - matrix[i][j++] = -1; - i++; - } - while(ttr[amount]) - amount++; - size = ft_sqrt_ceil(amount * 4); - if (size < 4) - size = 4; - char_matrix = malloc(sizeof(char *) * size + 1); - i = 0; - while (i < size) - { - char_matrix[i] = malloc(sizeof(char) * size + 1); - char_matrix[i][size] = 0; - i++; - } - char_matrix[size] = 0; - i = 0; - curr_line = 0; - while (i < amount) - { - k = 0; - while (k < size - ttr[i]->y) - { - j = 0; - while (j < size - ttr[i]->x) - { - put_figure(char_matrix, *ttr[i], k, j); - //print_char_matrix(char_matrix, size); - //count_cover(matrix, char_matrix, curr_line++); - //clear_char_matrix(&char_matrix, size); - j++; - } - k++; - } - i++; - } - return (matrix); -} \ No newline at end of file diff --git a/src/dancing_links.c b/src/dancing_links.c index 6600625..2f76f5d 100644 --- a/src/dancing_links.c +++ b/src/dancing_links.c @@ -41,7 +41,7 @@ t_node *add_node(t_node *col, int row) void cover(t_node *to_cover) { - void *step_vert; + t_node *step_vert; t_node *step_horiz; to_cover->left->right = to_cover->right; @@ -59,9 +59,9 @@ void cover(t_node *to_cover) } } -void uncover(t_col *to_uncover) +void uncover(t_node *to_uncover) { - void *step_vert; + t_node *step_vert; t_node *step_horiz; step_vert = to_uncover->up; diff --git a/src/fillit.c b/src/fillit.c index 01a3ca2..cea1c54 100644 --- a/src/fillit.c +++ b/src/fillit.c @@ -1,4 +1,4 @@ -#include "../inc/fillit.h" +#include "fillit.h" unsigned int ft_sqrt_ceil(unsigned int num) { @@ -8,164 +8,4 @@ unsigned int ft_sqrt_ceil(unsigned int num) while (i * i < num) i++; return (i); -} - -void clear_map(t_map *map) -{ - int i; - - i = 0; - while (i < 16) - map->line[i++] = 0; -} - -void convert_tetramino(char *s, t_map *map, int index) -{ - uint16_t temp; - int i; - int y; - int sharp_count; - - map->figure[index].offset_y = 0; - sharp_count = 0; - i = 0; - y = 1; - while (i < 16 && sharp_count < 4) - { - temp = 0; - while (i < y * 4) - { - temp = temp << 1; - if (s[i++] == '#' && ++sharp_count) - temp = temp | 0x01; - - } - temp = temp << 12; - map->figure[index].line[y - 1] = temp; - y++; - } - map->figure[index].height = y - 1; -} - -void map_initialization(char **tetramino_table, t_map *map) -{ - unsigned int i; - - i = 0; - while (*tetramino_table) - convert_tetramino(*tetramino_table++, map, i++); - map->figure_amount = i; - map->size = ft_sqrt_ceil(i * 4); - map->mask = 1; - map->mask <<= 16 - map->size; - map->ok = 0; - clear_map(map); -} - -void insert_tetramino(t_map *map, int index) -{ - int i; - int k; - t_tetrominoes *temp; - - temp = &map->figure[index]; - i = temp->offset_y; - k = i + temp->height; - while (i < k) - { - map->line[i] |= temp->line[i]; - i++; - } -} - - -void erase_tetramino(t_map *map, int index) -{ - int i; - int k; - t_tetrominoes *temp; - - temp = &map->figure[index]; - i = temp->offset_y; - k = 0; - while (k < temp->height) - { - map->line[i + k] ^= temp->line[i + k]; - k++; - } -} - -int can_insert(t_map *map, int index) -{ - int i; - int k; - t_tetrominoes *temp; - - temp = &map->figure[index]; - i = temp->offset_y; - k = i + temp->height; - while (i < k) - { - if (map->line[i] & temp->line[i]) - return (0); - i++; - } - return (1); -} - -int shift_tetramino(t_map *map, int index) -{ - int right_shift; - unsigned int i; - unsigned int k; - t_tetrominoes *temp; - - temp = &map->figure[index]; - i = temp->offset_y; - k = i + temp->height; - right_shift = 1; - while (i < k) - if (temp->line[i++] & map->mask) - right_shift = 0; - i = temp->offset_y; - if (right_shift && ++(temp->offset_x)) - while (i < k) - temp->line[i++] >>= 1; - else if (k < map->size && ++(temp->offset_y)) { - while (k-- > i) { - temp->line[k] <<= temp->offset_x; - temp->line[k + 1] = temp->line[k]; - } - temp->offset_x = 0; - } - else - return (0); - return (1); -} - - -t_map algorithm(t_map map, unsigned int index) -{ - int ins = 0; - t_map copy; - - if (index == map.figure_amount) { - map.ok = 1; - return map; - } - while (!(ins = can_insert(&map, index))) - if (!shift_tetramino(&map, index)) - break; - if (ins) - { - copy = map; - insert_tetramino(&map, index); - map = algorithm(map, index + 1); - if (!map.ok) - { - shift_tetramino(©, index); - map = algorithm(copy, index); - } - } - return map; } \ No newline at end of file diff --git a/src/get_types.c b/src/get_types.c new file mode 100644 index 0000000..157649a --- /dev/null +++ b/src/get_types.c @@ -0,0 +1,30 @@ +#include "fillit.h" + +int *get_types(char **ttr) +{ + int *types; + int i; + int j; + int size; + + i = 0; + size = 0; + while (ttr[size]) + size++; + types = malloc(sizeof(int) * size); + while (i < size) + { + j = 0; + while (j < 19) + { + if (ft_strcmp(ttr[i], g_templates[j].t) == 0) + { + types[i] = j; + break; + } + j++; + } + i++; + } + return (types); +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 01b9ab0..22a95e0 100644 --- a/src/main.c +++ b/src/main.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../inc/fillit.h" +#include "fillit.h" /* ** bad global variable @@ -45,7 +45,11 @@ int main(int argc, char **argv) char **table; char **ttr; int i; - //int *types; + int *types; + int amount; + t_node *r; + t_node **cols_arr; + t_list *ans; i = 0; if (argc != 2) @@ -70,14 +74,19 @@ int main(int argc, char **argv) //printf("move to up left:\n\n\n"); //print_one_string(ttr); - //printf("test check:\n\n"); - //test_check(ttr, templates); + printf("test check:\n\n"); + test_check(ttr, g_templates); - while (i < 19) - { - print_one_string(g_templates[i]); - i++; - } + + + types = get_types(ttr); + amount = get_amount(ttr); + size = ft_sqrt_ceil(amount * 4); + r = init_root(); + cols_arr = add_cols(r, (size * size + amount)); + add_rows(r, types, amount, cols_arr); + ans = dlx(r); + print_ans(ans); //printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y); //to_letters(ttr); //printf("to latters:\n\n"); diff --git a/src/test!_check.c b/src/test!_check.c index 0723af5..6f89662 100644 --- a/src/test!_check.c +++ b/src/test!_check.c @@ -14,7 +14,7 @@ // Test func. Compare figures from file // with templates -void test_check(char **ttr, t_ttrmn *tamplates) +void test_check(char **ttr, t_ttrmn *templates) { int i; int j; @@ -25,12 +25,12 @@ void test_check(char **ttr, t_ttrmn *tamplates) j = 0; while(j < 19) { - printf("\n\ninput:\n"); - printf("%s\n", ttr[i]); - printf("\n\ntemplate:\n"); - printf("%s\n", tamplates[j].t); - if ((ft_memcmp(ttr[i], tamplates[j].t, 17) == 0)) + if ((ft_memcmp(ttr[i], templates[j].t, 17) == 0)) + { printf(" match!\n"); + print_one_string(templates[j]); + break; + } j++; } i++; diff --git a/src/to_strct_array.c b/src/to_strct_array.c deleted file mode 100644 index 68ef8d2..0000000 --- a/src/to_strct_array.c +++ /dev/null @@ -1,33 +0,0 @@ - -#include "fillit.h" - -int *to_strct_array(char **ttr, t_ttrmn *templates) -{ - t_ttrmn **ret; - int amount; - int i; - int j; - int k; - - amount = 0; - i = 0; - k = 0; - while (ttr[amount]) - amount++; - ret = malloc(sizeof(ret) * (amount + 1)); - while (k < amount) - ret[k++] = malloc(sizeof(t_ttrmn)); - ret[k] = 0; - while (i < amount) - { - j = 0; - while(ft_strcmp(templates[j].t, ttr[i]) != 0) - j++; - //printf("%d\n", j); - ft_strcpy(ret[i]->t, ttr[i]); - ret[i]->x = templates[j].x; - ret[i]->y = templates[j].y; - i++; - } - return (0); -} \ No newline at end of file