diff --git a/f_bonus_print.c b/f_bonus_print.c index ccb310c..ace568c 100644 --- a/f_bonus_print.c +++ b/f_bonus_print.c @@ -16,7 +16,7 @@ ** function that print the given tetris if flag p is present */ -t_fillist *print_tetri(t_fillist *list) +t_fillist *print_tetri(t_fillist *list, int num) { unsigned int print; t_fillist *tmp; @@ -39,6 +39,7 @@ t_fillist *print_tetri(t_fillist *list) if (list->dope[1] && tmp->memory) ft_putendl("have a copy"); ft_putchar('\n'); + tmp->total = num; tmp = tmp->next; } } diff --git a/f_search_map.c b/f_search_map.c index 140fcea..2c00623 100644 --- a/f_search_map.c +++ b/f_search_map.c @@ -103,6 +103,50 @@ void add_remove(unsigned int *map, t_fillist *list, int size) } } +/* +** Function that try to place every tetri in the left places +** to know if it worth it to continue to check in this order +*/ + +int check_others(unsigned int *map, t_fillist *list, int size, int num) +{ + t_fillist *tmp; + int dots; + int total; + int i; + int j; + + dots = 0; + i = -1; + num *= 4; + total = size * size; + while (++i < total && dots <= total - num) + { + tmp = list->next; + j = 1; + while (1 << (i % 32) & map[i % 32]) + i++; + while (j && tmp) + { + if (tmp->width > (size - i % size) || (total - i) <= (tmp->height * size)) + tmp = tmp->next; + else if (!fit_in_place(map, list, size, i)) + tmp = tmp->next; + else + j = 0; + } + if (j) + dots++; + } + return (dots > total - num); +} + +/* +** Function that supress the memory of already placed tetri +** when a next one is placed before them +** because then they might fit where they already tried +*/ + void clean_memory(t_fillist *list, int pos, int mem) { t_fillist *tmp; @@ -139,8 +183,8 @@ int fill_map(unsigned int *map, t_fillist *list, int size) list->position = 0; while (find_place(map, list, size)) { -// if (list->position < pos) -// clean_memory(list, pos, pos); + if (list->position < pos) + clean_memory(list, pos, pos); add_remove(map, list, size); list->test = 1; if (list->dope[0]) @@ -150,8 +194,9 @@ int fill_map(unsigned int *map, t_fillist *list, int size) } if (list->dope[1]) if (check_tetri_memory(list, list->position)) - if (fill_map(map, list->next, size)) - return (1); + if (check_others(map, list, size, list->total)) + if (fill_map(map, list->next, size)) + return (1); if (!list->dope[1]) if (fill_map(map, list->next, size)) return (1); @@ -175,9 +220,14 @@ int search_map(t_fillist *list) int i; size = 2; - tmp = print_tetri(list); -// init_num_and_size(1, &size, tmp); - size = 10; + + num = 1; + tmp = list; + while ((tmp = tmp->next)) + num++; + + tmp = print_tetri(list, num); + init_num_and_size(1, &size, tmp); i = 0; while (!i) { diff --git a/fillit.h b/fillit.h index e293f40..fd6a989 100644 --- a/fillit.h +++ b/fillit.h @@ -63,6 +63,7 @@ typedef struct s_fillist int num; int total_num; int test; + int total; char letter; int *dope; unsigned int *memory; @@ -82,7 +83,7 @@ int check_same_tetri(t_fillist *list, int num); /* ** bonus_print.c */ -t_fillist *print_tetri(t_fillist *list); +t_fillist *print_tetri(t_fillist *list, int num); int print_binary_map(unsigned int *map, int size, int *dope); int print_flags_usage(void);