diff --git a/.gitignore b/.gitignore index 29bf5b9..06b351e 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,11 @@ # Vim temp *.swp +<<<<<<< HEAD CMakeLists.txt .idea/ **/cmake-build-debug/* +======= +# Sublime files +*.sublime* +>>>>>>> github/develop diff --git a/Makefile b/Makefile index 8605c7b..c4e0bd1 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: gtertysh +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/12/14 16:54:48 by gtertysh #+# #+# # -# Updated: 2016/12/15 22:08:42 by ykolomie ### ########.fr # +# Updated: 2016/12/20 20:05:58 by ykolomie ### ########.fr # # # # **************************************************************************** # @@ -17,15 +17,18 @@ SRCDIR = ./src/ OBJDIR = ./obj/ SRC_FILES = main.c \ - fillit.c \ glue_figure.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 \ test!_check.c \ test!_print_one_string.c \ - test!_print_table.c + test!_print_table.c \ + check_raw_string.c OBJ_FILES = $(SRC_FILES:.c=.o) diff --git a/inc/fillit.h b/inc/fillit.h index fc43bd4..91297f0 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/14 16:42:06 by gtertysh #+# #+# */ -/* Updated: 2016/12/14 16:45:34 by gtertysh ### ########.fr */ +/* Updated: 2016/12/20 19:18:00 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,33 +17,44 @@ #include #include #include +#include #include // warning! # define BUF_S 8192 +# define HOW_MUCH 10000 -typedef struct s_tetrominoes +typedef struct s_coord { - uint16_t line[16]; - int offset_y; - int height; -} t_tetrominoes; + int x; + int y; +} t_coord; -typedef struct s_map +typedef struct s_ttrmn { - uint16_t line[16]; - unsigned int size; - t_tetrominoes *figure[26]; - unsigned int figure_amount; -} t_map; + char t[17]; + int x; + int y; + t_coord c[4]; +} t_ttrmn; -// obsolete, rewrite for unit16 -typedef struct s_tetrominoes_templates +typedef struct s_col { - char figure[17]; -} t_tetrominoes_templates; + struct s_col *left; + struct s_col *right; + void *up; + void *down; +} t_col; + +typedef struct s_node +{ + struct s_node *left; + struct s_node *right; + struct s_node *up; + struct s_node *down; + int row; + t_col *column; +} t_node; -void convert_tetramino(char *s, t_map *map, int index); -void map_initialization(char **tetramino_table, t_map *map); // reads from file char *read_file(char *path); @@ -64,10 +75,20 @@ void move_up_left(char **table); void print_one_string(char **glued); // compare 19 templates with each tetromino in *ttr table -void test_check(char **ttr, t_tetrominoes_templates *tamplates); +void test_check(char **ttr, t_ttrmn *tamplates); // change hashes ('#') with letters // obsolete, rewrite for structs array void to_letters(char **ttr); +// create array of ttr structures +int *to_strct_array(char **ttr, t_ttrmn *templates); + +t_col *add_column(t_col *root); +t_node *add_node(t_col *col, int row); +void cover(t_col *to_cover); +void uncover(t_col *to_uncover); + + + #endif diff --git a/src/build_matrix.c b/src/build_matrix.c new file mode 100644 index 0000000..1942d4c --- /dev/null +++ b/src/build_matrix.c @@ -0,0 +1,106 @@ +#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/check_raw_string.c b/src/check_raw_string.c new file mode 100644 index 0000000..9376456 --- /dev/null +++ b/src/check_raw_string.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_raw_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/15 18:32:54 by gtertysh #+# #+# */ +/* Updated: 2016/12/15 18:32:58 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int check_raw_string(char *raw_string) +{ + int n_count; + + n_count = 0; + if (*raw_string == 0 || *raw_string == '\n') + return (1); + while(*raw_string) + { + if (*raw_string == '\n') + n_count++; + if ((n_count + 1) % 4 == 0) + if (*(raw_string + 1) != '\0') + if (*(raw_string + 1) != '\n' && !(*(raw_string + 2) == '.' || *(raw_string + 2) == '#')) + return (1); + raw_string++; + + } + if (n_count < 3) + return (1); + return (0); +} \ No newline at end of file diff --git a/src/dancing_links.c b/src/dancing_links.c new file mode 100644 index 0000000..6d333e7 --- /dev/null +++ b/src/dancing_links.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dancing_links.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gtertysh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/12/20 19:21:42 by gtertysh #+# #+# */ +/* Updated: 2016/12/20 19:22:04 by gtertysh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +// t_col *add_column(t_col *root) +// { +// t_col *new_col; + +// new_col = (t_col*)malloc(sizeof(t_col)); +// new_col->down = (void*)new_col; +// new_col->up = (void*)new_col; +// new_col->left = root->left; +// new_col->right = root; +// return (new_col); +// } + +// t_node *add_node(t_col *col, int row) +// { +// t_node *new_node; + +// new_node = (t_node*)malloc(sizeof(t_node)); +// new_node->row = row; +// new_node->up = (t_node*)col->up; +// new_node->down = (t_node*)col; +// col->up->down = new_node; +// col->up = new_node; +// return (new_node); +// } + +// void cover(t_col *to_cover) +// { +// void *step_vert; +// t_node *step_horiz; + +// to_cover->left->right = to_cover->right; +// to_cover->right->left = to_cover->left; +// step_vert = to_cover->down; +// while (step_vert != (void*)to_cover) +// { +// step_horiz = (t_node*)step_vert->right; +// while (step_horiz != (t_node*)step_vert) { +// step_horiz->down->up = step_horiz->up; +// step_horiz->up->down = step_horiz->down; +// step_horiz = step_horiz->right; +// } +// step_vert = (t_node*)step_vert->down; +// } +// } + +// void uncover(t_col *to_uncover) +// { +// void *step_vert; +// t_node *step_horiz; + +// step_vert = to_uncover->up; +// while (step_vert != (void*)to_uncover) +// { +// step_horiz = (t_node*)step_vert->left; +// while (step_horiz != (t_node*)step_vert) +// { +// step_horiz->down->up = step_horiz; +// step_horiz->up->down = step_horiz; +// step_horiz = step_horiz->left; +// } +// step_vert = (t_node*)step_vert->up; +// } +// } + diff --git a/src/glue_figure.c b/src/glue_figure.c index 2cf8bdc..07e71b0 100644 --- a/src/glue_figure.c +++ b/src/glue_figure.c @@ -14,7 +14,7 @@ // This function creates string table in which one line represent one figure // by "glueing" evry 4 strings from table -char **glue_figure(char **table) +char **glue_figure(char **table) { int i; int j; diff --git a/src/main.c b/src/main.c index 7f21301..976341d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,120 +6,100 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/10 16:07:25 by gtertysh #+# #+# */ -/* Updated: 2016/12/14 16:45:31 by gtertysh ### ########.fr */ +/* Updated: 2016/12/20 20:05:29 by ykolomie ### ########.fr */ /* */ /* ************************************************************************** */ #include "../inc/fillit.h" // bad global variable -t_tetrominoes_templates templates[19] = +t_ttrmn g_templates[19] = { - { "#..." - "#..." - "#..." - "#..." }, + { + "#...#...#...#...", 1, 4, .c[0] = {0, 0}, .c[1] = {1, 0}, + .c[2] = {2, 0}, .c[3] = {3, 0} + }, - { "####" - "...." - "...." - "...." }, + { .t = "####............", + .x = 4, + .y = 1}, - { "#..." - "#..." - "##.." - "...." }, + { .t = "#...#...##......", + .x = 2, + .y = 3}, - { "..#." - "###." - "...." - "...." }, + { .t = "..#.###.........", + .x = 3, + .y = 2}, - { "##.." - ".#.." - ".#.." - "...." }, + { .t = "##...#...#......", + .x = 2, + .y = 3}, - { "###." - "#..." - "...." - "...." }, + { .t = "###.#...........", + .x = 3, + .y = 2}, - { ".#.." - ".#.." - "##.." - "...." }, + { .t = ".#...#..##......", + .x = 2, + .y = 3}, - { "###." - "..#." - "...." - "...." }, + { .t = "###...#.........", + .x = 3, + .y = 2}, - { "##.." - "#..." - "#..." - "...." }, + { .t = "##..#...#.......", + .x = 2, + .y = 3}, - { "#..." - "###." - "...." - "...." }, + { .t = "#...###.........", + .x = 3, + .y = 2}, - { "###." - ".#.." - "...." - "...." }, + { .t = "###..#..........", + .x = 3, + .y = 2}, - { "#..." - "##.." - "#..." - "...." }, + { .t = "#...##..#.......", + .x = 2, + .y = 3}, - { ".#.." - "###." - "...." - "...." }, + { .t = ".#..###.........", + .x = 3, + .y = 2}, - { ".#.." - "##.." - ".#.." - "...." }, + { .t = ".#..##...#......", + .x = 2, + .y = 3}, - { "##.." - "##.." - "...." - "...." }, + { .t = "##..##..........", + .x = 2, + .y = 2}, - { "##.." - ".##." - "...." - "...." }, + { .t = "##...##.........", + .x = 3, + .y = 2}, - { ".#.." - "##.." - "#..." - "...." }, + { .t = ".#..##..#.......", + .x = 2, + .y = 3}, - { ".##." - "##.." - "...." - "...." }, + { .t = ".##.##..........", + .x = 3, + .y = 2}, - { "#..." - "##.." - ".#.." - "...." } + { .t = "#...##...#......", + .x = 2, + .y = 3} }; int main(int argc, char **argv) { - char *string; - char **table; - char **ttr; - int i; - t_map map; - t_map copy; - unsigned int size_map; + char *string; + char **table; + char **ttr; + int i; + int *types; i = 0; if (argc != 2) @@ -131,22 +111,29 @@ int main(int argc, char **argv) { ft_putstr("error\n"); return (1); - } + } //printf("string:\n\n\n%s\n", string); table = to_table(&string); + //printf("print_table:\n\n\n"); + //print_table(table); + ttr = glue_figure(table); + //printf("glued figures:\n\n\n"); + //print_one_string(ttr); move_up_left(ttr); - print_one_string(ttr); + //printf("move to up left:\n\n\n"); + //print_one_string(ttr); + + //printf("test check:\n\n"); + //test_check(ttr, templates); + + types = to_strct_array(ttr, g_templates); - map_initialization(ttr, &map); - size_map = map.size; - copy = map; - while (!map.ok) - { - map = copy; - map.size = size_map++; - map = algorithm(map, 0); - } + + //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"); + //print_one_string(ttr); return (0); } diff --git a/src/test!_check.c b/src/test!_check.c index c339854..0723af5 100644 --- a/src/test!_check.c +++ b/src/test!_check.c @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/14 17:26:13 by gtertysh #+# #+# */ -/* Updated: 2016/12/14 18:47:16 by gtertysh ### ########.fr */ +/* Updated: 2016/12/20 19:02:34 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ // Test func. Compare figures from file // with templates -void test_check(char **ttr, t_tetrominoes_templates *tamplates) +void test_check(char **ttr, t_ttrmn *tamplates) { int i; int j; @@ -28,8 +28,8 @@ void test_check(char **ttr, t_tetrominoes_templates *tamplates) printf("\n\ninput:\n"); printf("%s\n", ttr[i]); printf("\n\ntemplate:\n"); - printf("%s\n", tamplates[j].figure); - if ((ft_memcmp(ttr[i], tamplates[j].figure, 17) == 0)) + printf("%s\n", tamplates[j].t); + if ((ft_memcmp(ttr[i], tamplates[j].t, 17) == 0)) printf(" match!\n"); j++; } diff --git a/src/to_strct_array.c b/src/to_strct_array.c new file mode 100644 index 0000000..11cebdb --- /dev/null +++ b/src/to_strct_array.c @@ -0,0 +1,33 @@ + +#include "fillit.h" + +t_ttrmn **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 (ret); +} \ No newline at end of file