From 7eb11a8333d80a02ea7e21f9b5d1dd8c98f29d8d Mon Sep 17 00:00:00 2001 From: Gregory Date: Thu, 22 Dec 2016 05:34:24 +0200 Subject: [PATCH] add output routines --- .gitignore | 1 + CMakeLists.txt | 1 - inc/fillit.h | 18 +++---- src/dancing_links.c | 58 +++++++++++++++++---- src/main.c | 8 +-- tests/color | Bin 0 -> 8936 bytes tests/sample2.fillit | 34 +++++++++++++ tests/sample3.fillit | 19 +++++++ tests/sample4.fillit | 39 ++++++++++++++ tests/sample5.fillit | 4 ++ tests/sample6.fillit | 119 +++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 278 insertions(+), 23 deletions(-) create mode 100755 tests/color create mode 100644 tests/sample2.fillit create mode 100644 tests/sample3.fillit create mode 100644 tests/sample4.fillit create mode 100644 tests/sample5.fillit create mode 100644 tests/sample6.fillit diff --git a/.gitignore b/.gitignore index ed17cff..d0ca43c 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ # Clion files .idea +cmake-build-debug diff --git a/CMakeLists.txt b/CMakeLists.txt index 41f9715..b52e1ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,6 @@ include_directories(inc) include_directories(libft) set(SOURCE_FILES - inc/fillit.h libft/ft_atoi.c libft/ft_bzero.c libft/ft_isalnum.c diff --git a/inc/fillit.h b/inc/fillit.h index 0436fa2..f26a3f0 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -21,7 +21,6 @@ #include // warning! # define BUF_S 8192 -# define HOW_MUCH 10000 typedef struct s_coord { @@ -45,7 +44,9 @@ typedef struct s_node struct s_node *down; struct s_node *column; t_coord coord; -} t_node; + int type; + int size; +} t_node; extern t_ttrmn g_templates[19]; @@ -74,10 +75,7 @@ void test_check(char **ttr, t_ttrmn *tamplates); // 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_node *init_root(void); +t_node *init_root(int size); t_node *add_column(t_node *root); @@ -85,17 +83,17 @@ t_node **add_cols(t_node *root, int number); t_node *add_node(t_node *col); -t_node *add_node_with_coords(t_node *col, t_coord row); +t_node *add_node_with_coord(t_node *col, t_coord row, int type); t_node **add_cols(t_node *root, int number); -void print_solution(t_list *sol); +void print_solution(t_list *sol, t_node *root, int amount); void add_rows(int *types, int amount, int size, t_node **cols_arr); void link_row(int *col_nums, t_node **cols_arr); -void add_row(int *col_numbers, t_node **cols_arr, t_coord coord); +void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type); void search(t_node* root, t_list *solution, int k, int amount); @@ -109,4 +107,6 @@ int get_amount(char **ttr); unsigned int ft_sqrt_ceil(unsigned int num); +void fill_map(char *map, int size, t_node *ttr, int letter); + #endif diff --git a/src/dancing_links.c b/src/dancing_links.c index 2b0b8ec..27271bb 100644 --- a/src/dancing_links.c +++ b/src/dancing_links.c @@ -13,13 +13,14 @@ #include "../inc/fillit.h" #include -t_node *init_root(void) +t_node *init_root(int size) { t_node *new; new = (t_node*)malloc(sizeof(t_node)); new->left = new->right = new->down = new->up = new; new->column = NULL; + new->size = size; return (new); } @@ -38,12 +39,13 @@ t_node *add_column(t_node *root) return (new_col); } -t_node *add_node_with_coord(t_node *col, t_coord coord) +t_node *add_node_with_coord(t_node *col, t_coord coord, int type) { t_node *new_node; new_node = (t_node*)malloc(sizeof(t_node)); new_node->coord = coord; + new_node->type = type; new_node->column = col; new_node->up = col->up; new_node->down = col; @@ -91,9 +93,9 @@ int *get_coords(t_coord coord, int amount, int type, int size) { } return (result); } -void add_row(int *col_numbers, t_node **cols_arr, t_coord coord) +void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type) { - add_node_with_coord(cols_arr[col_numbers[0]], coord); + add_node_with_coord(cols_arr[col_numbers[0]], coord, type); add_node(cols_arr[col_numbers[1]]); add_node(cols_arr[col_numbers[2]]); add_node(cols_arr[col_numbers[3]]); @@ -134,7 +136,7 @@ void add_rows(int *types, int amount, int size, t_node **cols_arr) coord.y = l; col_num = get_coords(coord, amount, types[i], size); col_num[0] = i; - add_row(col_num, cols_arr, coord); + add_row(col_num, cols_arr, coord, types[i]); link_row(col_num, cols_arr); k++; } @@ -185,10 +187,48 @@ void uncover(t_node *to_uncover) to_uncover->right->left = to_uncover; } -void print_solution(t_list *sol) +void fill_map(char *map, int size, t_node *ttr, int letter) { - if (sol) - printf("as\n"); + int i; + char a; + + a = 'A' + letter - 1; + + i = 0; + while (i < 4) + { + ft_memcpy((map + (ttr->coord.y + g_templates[ttr->type].c[i].y) * size + + ttr->coord.x + g_templates[ttr->type].c[i].x), &a, 1); + i++; + } +} + +void print_solution(t_list *sol, t_node *root, int amount) +{ + char *map; + int i; + int map_size; + + map_size = root->size * root->size + 1; + map = malloc(sizeof(char) * map_size); + map[map_size] = 0; + while (sol) + { + fill_map(map, root->size, (*(t_node **)sol->content), amount); + sol = sol->next; + amount--; + } + i = 0; + while (i < root->size * root->size) + { + if (map[i] >= 'A' && map[i] <= 'Z') + ft_putchar(map[i]); + else + ft_putchar('.'); + if ((i + 1) % root->size == 0) + ft_putchar('\n'); + i++; + } exit(0); } @@ -199,7 +239,7 @@ void search(t_node* root, t_list *solution, int k, int amount) t_node *j; if (k == amount) - print_solution(solution); + print_solution(solution, root, amount); if (root->right == root) return ; current_col = root->right; diff --git a/src/main.c b/src/main.c index dd8cf1f..76e9024 100644 --- a/src/main.c +++ b/src/main.c @@ -35,7 +35,7 @@ t_ttrmn g_templates[19] = {"##..##..........", 2, 2, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}}, {"##...##.........", 3, 2, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}}, {".#..##..#.......", 2, 3, {{0, 1}, {1, 0}, {1, 1}, {2, 0}}}, - {".##.##..........", 3, 2, {{0, 1}, {0, 2}, {1, 0}, {1, 2}}}, + {".##.##..........", 3, 2, {{0, 1}, {0, 2}, {1, 0}, {1, 1}}}, {"#...##...#......", 2, 3, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}} }; @@ -76,8 +76,8 @@ 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, g_templates); + //printf("test check:\n\n"); + //test_check(ttr, g_templates); @@ -85,7 +85,7 @@ int main(int argc, char **argv) amount = get_amount(ttr); size = ft_sqrt_ceil(amount * 4); while (1) { - r = init_root(); + r = init_root(size); cols_arr = add_cols(r, (size * size + amount)); add_rows(types, amount, size, cols_arr); search(r, ans, 0, amount); diff --git a/tests/color b/tests/color new file mode 100755 index 0000000000000000000000000000000000000000..4ea5ef2ce9f1b4cb12340f7335d8e47da2c951e5 GIT binary patch literal 8936 zcmeHMeQaCR6~DIgMO$o_@zD>oK6T9&isCvY34yZloR9XUN(;0J6R_~&*iK^bM`S;< z6tTjT7Rf4wO53FTF(jt3iD?_^q-}KjqfJ*ti!^O5;%j1)Is_!ql$E^&p> z#RhS`XaG`=qRSTIc8&R>a?QL+>G{CxP`bP}(B;)-h57Y1j2>*_5fZ2VDx|Bb%`d19 zm$1YU-G&8owz~^F(G)BzxjGYLIBmSEEo4P<#G|kNn_d)BY8& zJpZG^2lpR%;)QdAuaYenRKGA$zFbv9d*w3tre*LOm%+o!;N8GAlqG~X#V9PD!Zh<% zH1?nwn|RP1izZWIvS7s~qB${{OJ=MwkxNA5P$C1;%vokSn#=$hPiHdwI-Gc_l^sE`=4=C9fcx`$!qI7#NY9a>$uSYl1dT z=}7*VGzYFyTsE~haJs%sVF%7ROKn{a+<9Lc)Bk8P4%H8>)P!+x)~cV&L)n;qvFQ|h zw0Wtc8!DydUi=3(=%|rAL5=h0OQ7akNv5ICpOx}fl4;2Evr^tnG7WY9sFXL7OhcT1 zM#`_O(8Smg$dcMV>@S|X-zdIqOuw^m=V1To*=KNv8mCW&B|H7q23^d(0K*GofepPv z9D4&395PzZf*I3`exvyAL+g)`E1K5o;SS;oWPJCZ(!t3~ zJ~%Ln3&zR0+YQf4#<@$@HDzxr*;`4sK{?3939QZk~z;F9L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6K$gch#XgONsaVQ|17Mp#BX?rtz_Xu;xz#ZL@0rQUS+xPYjnuC$A)hE87t>5BTreHY=an1# z^myk(<{p(8J*j9uFS7!Z<%>a4;LNFtfe7XIrLE{FxRtZ{1lMq0oybj!P$p|7LXqzN zRx3KLr14B4G+IdF!$2}FLL^N@^AjQz-o_PQ>AaCEL`_Puqg)55%B`ZldXA3}eVzklCr7qWre+8uxh3$FW$={bWv^j~N zgN(JI;_PxL_VvoZ8&vvp4a0|D$rOtO5*t}uTUt8*pP_-q&)x7o<*2g%Z}Iz; z%bvgI@^{{L?pSdZ)-p<;SDgKq_dP057s{G;#T~zw0aK2vbS>|X_`9!L-z~oe+knHK z*Uf9xjpNpL%YTP0ji3AH{fhPl?cakffA3tRNr713 zmMSviAECiHe!=TU)q&22inAT_MVCFVdv#@h#AUC!$Imsnf=dMGsi5!ETQy1>bsBb~x`OveBi0;o? U{U22aa}V&1DYfmns;XrF0j`150ssI2 literal 0 HcmV?d00001 diff --git a/tests/sample2.fillit b/tests/sample2.fillit new file mode 100644 index 0000000..fa06f41 --- /dev/null +++ b/tests/sample2.fillit @@ -0,0 +1,34 @@ +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... + +#### +.... +.... +.... diff --git a/tests/sample3.fillit b/tests/sample3.fillit new file mode 100644 index 0000000..032b064 --- /dev/null +++ b/tests/sample3.fillit @@ -0,0 +1,19 @@ +.... +##.. +.#.. +.#.. + +.... +#### +.... +.... + +#... +###. +.... +.... + +.... +##.. +.##. +.... \ No newline at end of file diff --git a/tests/sample4.fillit b/tests/sample4.fillit new file mode 100644 index 0000000..d275539 --- /dev/null +++ b/tests/sample4.fillit @@ -0,0 +1,39 @@ +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... \ No newline at end of file diff --git a/tests/sample5.fillit b/tests/sample5.fillit new file mode 100644 index 0000000..d24b25d --- /dev/null +++ b/tests/sample5.fillit @@ -0,0 +1,4 @@ +.... +..## +.##. +.... \ No newline at end of file diff --git a/tests/sample6.fillit b/tests/sample6.fillit new file mode 100644 index 0000000..d7cc980 --- /dev/null +++ b/tests/sample6.fillit @@ -0,0 +1,119 @@ +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... + +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... + +...# +...# +...# +...# + +.... +.... +.... +#### + +.### +...# +.... +.... + +.... +..## +.##. +.... + +.... +.##. +.##. +.... + +.... +.... +##.. +.##. + +##.. +.#.. +.#.. +.... + +.... +###. +.#.. +.... \ No newline at end of file