From 32d05000482cb476d85597af56c520986b489887 Mon Sep 17 00:00:00 2001 From: Gregory Date: Tue, 20 Dec 2016 05:47:48 +0200 Subject: [PATCH] 2 new functions: to_strct_array and build matrix --- .gitignore | 3 + Makefile | 3 +- inc/fillit.h | 37 ++++------ src/build_matrix.c | 106 +++++++++++++++++++++++++++ src/glue_figure.c | 2 +- src/main.c | 170 ++++++++++++++++++++----------------------- src/test!_check.c | 6 +- src/to_strct_array.c | 33 +++++++++ 8 files changed, 241 insertions(+), 119 deletions(-) create mode 100644 src/build_matrix.c create mode 100644 src/to_strct_array.c diff --git a/.gitignore b/.gitignore index e318427..b8cecc6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ # Vim temp *.swp + +# Sublime files +*.sublime* diff --git a/Makefile b/Makefile index 54405ef..c6da964 100644 --- a/Makefile +++ b/Makefile @@ -17,12 +17,13 @@ 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 \ test!_check.c \ test!_print_one_string.c \ test!_print_table.c diff --git a/inc/fillit.h b/inc/fillit.h index fc43bd4..33dd4f4 100644 --- a/inc/fillit.h +++ b/inc/fillit.h @@ -17,33 +17,18 @@ #include #include #include +#include #include // warning! # define BUF_S 8192 +# define HOW_MUCH 10000 -typedef struct s_tetrominoes +typedef struct s_ttrmn { - uint16_t line[16]; - int offset_y; - int height; -} t_tetrominoes; - -typedef struct s_map -{ - uint16_t line[16]; - unsigned int size; - t_tetrominoes *figure[26]; - unsigned int figure_amount; -} t_map; - -// obsolete, rewrite for unit16 -typedef struct s_tetrominoes_templates -{ - char figure[17]; -} t_tetrominoes_templates; - -void convert_tetramino(char *s, t_map *map, int index); -void map_initialization(char **tetramino_table, t_map *map); + char t[17]; + int x; + int y; +} t_ttrmn; // reads from file char *read_file(char *path); @@ -64,10 +49,16 @@ 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 +t_ttrmn **to_strct_array(char **ttr, t_ttrmn *templates); + +// build matrix for algorythm X +int **build_matrix(t_ttrmn **ttr); + #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/glue_figure.c b/src/glue_figure.c index f3719ad..099a012 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 e9610f4..885e851 100644 --- a/src/main.c +++ b/src/main.c @@ -13,102 +13,83 @@ #include "fillit.h" // bad global variable -t_tetrominoes_templates templates[19] = +t_ttrmn templates[19] = { - { "#..." - "#..." - "#..." - "#..." }, + { .t = "#...#...#...#...", + .x = 1, + .y = 4}, - { "####" - "...." - "...." - "...." }, + { .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) @@ -117,6 +98,8 @@ int main(int argc, char **argv) char **table; char **ttr; int i; + t_ttrmn **strct_ttr; + int **matrix; i = 0; if (argc != 2) @@ -124,31 +107,36 @@ int main(int argc, char **argv) ft_putstr("error\n"); return (1); } - string = read_file(argv[1]); if (!(string = read_file(argv[1]))) { ft_putstr("error\n"); return (1); } - printf("string:\n\n\n%s\n", string); + //printf("string:\n\n\n%s\n", string); table = to_table(&string); - printf("print_table:\n\n\n"); - print_table(table); + //printf("print_table:\n\n\n"); + //print_table(table); ttr = glue_figure(table); - printf("glued figures:\n\n\n"); - print_one_string(ttr); - + //printf("glued figures:\n\n\n"); + //print_one_string(ttr); move_up_left(ttr); - printf("move to up left:\n\n\n"); - 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); + //printf("test check:\n\n"); + //test_check(ttr, templates); - to_letters(ttr); - printf("to latters:\n\n"); - print_one_string(ttr); + strct_ttr = to_strct_array(ttr, templates); + + + matrix = build_matrix(strct_ttr); + //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 373200e..ec40143 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_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