diff --git a/Makefile b/Makefile index b243b37..e6b8ac7 100644 --- a/Makefile +++ b/Makefile @@ -6,49 +6,50 @@ # By: vmanzoni +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2019/03/01 13:24:35 by vmanzoni #+# #+# # -# Updated: 2019/04/27 15:12:16 by vmanzoni ### ########.fr # +# Updated: 2019/04/30 14:20:52 by hulamy ### ########.fr # # # # **************************************************************************** # -NAME = fillit +# - - - - - - - - - - - - - - - # +# VARIABLES # +# - - - - - - - - - - - - - - - # -OBJ_DIR = ./objs -HEADER = fillit.h +NAME = fillit +CC = gcc -SRCS = main.c \ - read_file.c \ - handle_errors.c \ - parse_input.c \ -# add_to_list.c \ #DELETE BEFORE EVAL - NOT USED ANYMORE -# get_smallest_square.c \ - print_fillit.c +CFLAGS = -I. +CFLAGS += -Wall -Wextra -Werror +LDFLAGS = -L./libft/ +LDLIBS = -lft -OBJS = $(SRCS:.c=.o) -LIB = ../42-libft/ +SRCS = $(shell find . -depth 1 -type f -not -name '.*' -not -name 'test*' -name '*.c') -CC = gcc -CFLAGS = -Wall -Werror -Wextra +TRASH = $(shell find . -depth 1 -type f -name '*.dSYM') +TRASH += $(shell find . -depth 1 -type f -name '*.o') +TRASH += $(shell find . -depth 1 -type f -name '*.swp') +TRASH += $(shell find . -depth 1 -type f -name '*.swo') +TRASH += $(shell find . -depth 1 -type f -name '*.swn') -RM = rm -rf +# - - - - - - - - - - - - - - - # +# RULES # +# - - - - - - - - - - - - - - - # -all: $(NAME) +all: $(NAME) -$(NAME): - make -C $(LIB) - $(CC) $(CFLAGS) -I$(HEADER) -c $(SRCS) - $(CC) -o $(NAME) $(OBJS) -L $(LIB) -lft - mkdir $(OBJ_DIR) - mv $(OBJS) $(OBJ_DIR) +$(NAME): $(SRCS) + $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(SRCS) -o $(NAME) + +debug: $(SRCS) + $(CC) -g $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(SRCS) -o $(NAME) + +lib: + make -C ./libft/ clean: - make -C $(LIB) clean - $(RM) $(OBJ_DIR) + /bin/rm -rf $(TRASH) -fclean: clean - make -C $(LIB) fclean - $(RM) $(NAME) +fclean: clean + /bin/rm -rf $(NAME) -re: fclean all - -.PHONY: all clean fclean re +re: clean all diff --git a/fillit.h b/fillit.h index 1943b4b..5f65c3f 100644 --- a/fillit.h +++ b/fillit.h @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */ -/* Updated: 2019/04/27 15:11:06 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/30 14:19:39 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,7 +46,8 @@ int check_tetri_errors(char *tetri); int check_tetri_errors_proxy(char *tetri); int add_to_list(char *square, t_fillist **list); void fill_list(char line[], t_fillist *list); - void print_bits(unsigned int bits, int size); //TO DELETE BEFORE EVAL +void search_map(t_fillist *list); +void print_map(unsigned int *tab, int width, int height); #endif diff --git a/parse_input.c b/parse_input.c index 99a4095..7a0c77c 100644 --- a/parse_input.c +++ b/parse_input.c @@ -6,59 +6,14 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ -/* Updated: 2019/04/27 15:15:53 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/30 14:19:32 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ #include "fillit.h" /* -** DELETE BEFORE EVAL - TEST FUNCTION -** Prints a ligne of size bits -*/ - -void print_bits(unsigned int bits, int size) -{ - unsigned int mask; - - mask = 1 << (size - 1); - while (mask) - { - (bits & mask) ? write(1, "#", 1) : write(1, ".", 1); - write(1, " ", 1); - mask >>= 1; - } - write(1, "\n", 1); -} - -/* -** DELETE BEFORE EVAL - TEST FUNCTION -** Print a map of height and width -*/ - -void print_map(unsigned int *tab, int width, int height) -{ - int i; - unsigned int mask; - - i = 0; - mask = 0; - while (i++ < width) - mask = (mask >> 1) | ((mask | 1) << 31); - i = 0; - while (i < width * height) - { - if (i && !(i % width)) - ft_putchar('\n'); - tab[i / 32] & (1 << (31 - i % 32)) ? ft_putchar('#') : ft_putchar('.'); - ft_putchar(' '); - i++; - } - write(1, "\n", 1); -} - -/* -** Function that transform a tab of . and # into a binary tab of int +** function that transform a tab of . and # into a binary tab of int */ unsigned short tab_to_bin(char line[]) @@ -107,7 +62,7 @@ unsigned short reduce_tetri(unsigned short tetri, int width) void fill_list(char line[], t_fillist *list) { - unsigned int tmp; +// unsigned int tmp; unsigned int mask; int i; @@ -136,11 +91,11 @@ void fill_list(char line[], t_fillist *list) list->tetribit = reduce_tetri(list->tetribit, list->width); // imression pour tests - ft_putchar('\n'); - tmp = list->tetribit; - tmp <<= 16; - print_map(&tmp, list->width, list->height); - tmp >>= 16; +// ft_putchar('\n'); +// tmp = list->tetribit; +// tmp <<= 16; +// print_map(&tmp, list->width, list->height); +// tmp >>= 16; } /* @@ -151,15 +106,21 @@ void fill_list(char line[], t_fillist *list) int add_to_list(char *line, t_fillist **list) { t_fillist *tmp; + t_fillist *test; if (!(tmp = (t_fillist*)malloc(sizeof(*tmp)))) return (0); - if (!(*list)) - tmp->next = NULL; + tmp->next = NULL; + test = *list; + if (!test) + *list = tmp; else - tmp->next = *list; - *list = tmp; - fill_list(line, *list); + { + while(test->next) + test = test->next; + test->next = tmp; + } + fill_list(line, tmp); return (1); } @@ -187,4 +148,5 @@ void parse_input(char *input) while (input[i] && input[i] != '.' && input[i] != '#') i++; } + search_map(list); } diff --git a/print.c b/print.c new file mode 100644 index 0000000..5e3256e --- /dev/null +++ b/print.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/04/30 13:24:28 by hulamy #+# #+# */ +/* Updated: 2019/04/30 13:25:18 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +/* +** DELETE BEFORE EVAL - TEST FUNCTION +** Prints a ligne of size bits +*/ + +void print_bits(unsigned int bits, int size) +{ + unsigned int mask; + + mask = 1 << (size - 1); + while (mask) + { + (bits & mask) ? write(1, "#", 1) : write(1, ".", 1); + write(1, " ", 1); + mask >>= 1; + } + write(1, "\n", 1); +} + +/* +** DELETE BEFORE EVAL - TEST FUNCTION +** Print a map of height and width +*/ + +void print_map(unsigned int *tab, int width, int height) +{ + int i; + unsigned int mask; + + i = 0; + mask = 0; + while (i++ < width) + mask = (mask >> 1) | ((mask | 1) << 31); + i = 0; + while (i < width * height) + { + if (i && !(i % width)) + ft_putchar('\n'); + tab[i / 32] & (1 << (31 - i % 32)) ? ft_putchar('#') : ft_putchar('.'); + ft_putchar(' '); + i++; + } + write(1, "\n", 1); +} + diff --git a/search_map.c b/search_map.c new file mode 100644 index 0000000..0effc76 --- /dev/null +++ b/search_map.c @@ -0,0 +1,178 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* search_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: hulamy +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */ +/* Updated: 2019/04/30 14:07:47 by hulamy ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "fillit.h" + +/* +** function that look for the first place in the map for a tetri +*/ + +int find_place(unsigned int *tab, t_fillist *list, int size) +{ + int i; + int j; + unsigned int mask; + unsigned int tmp; + + // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) + mask = ~0u << (32 - list->width); + tmp = mask; + i = 0; + // boucle jusqu'a la dernier place pour le tetri dans la map ou qu'il fit dans un trou + while (i < (size - list->height + 1) * size) + { + tmp = 0; + j = list->height * size + i; + // construit un tmp qui est une photo de la map de la taille du tetri a un emplacement donne + while (j >= i) + { + tmp >>= list->width; + tmp |= (mask & (tab[j / 32] << j)); + tmp |= (mask & (tab[(j + size) / 32] >> (32 - j))); // cette deuxieme ligne est la au cas ou on serait a cheval sur deux int du tab + j -= size; + } +// print_map(&tmp, list->width, list->height); // test pour imprimer la photo en map +// write(1, "\n", 1); +// print_bits(tmp >> 16, 32); // test pour imprimer la photo en bits + if (!((tmp >> 16) & list->tetribit)) + return ((list->position = i + 1)); + if (i % size == size - list->width) + i += list->width - 1; + i++; + } + return (0); +} + +/* +** function that add or remove a tetri on the map +*/ + +void add_remove(unsigned int *map, t_fillist *list, int size, int pos) +{ + unsigned int mask; + unsigned int tmp; + int j; + + mask = ~0u << (32 - list->width); + tmp = 0; + j = list->height * size + pos; +// (void)map; + // construit un tmp qui est une photo de la map de la taille du tetri a un emplacement donne +// ft_putnbrendl(pos); + while (j >= pos) + { + map[j / 32] |= ((mask >> (32 - j)) ^ map[j / 32]); + map[(j + size) / 32] |= ((mask << j) ^ map[(j + size) / 32]); // cette deuxieme ligne est la au cas ou on serait a cheval sur deux int du tab + j -= size; + } +} + +/* +** function that recursively try to fill the map with the tetris +*/ + +int fill_map(unsigned int *map, t_fillist *list, int size) +{ + if (!list) + return (1); + while (find_place(map, list, size)) + { + add_remove(map, list, size, list->position); + print_map(map, size, size); + if (fill_map(map, list->next, size)) + return (1); + add_remove(map, list, size, list->position); + list->position++; + } + return (0); +} + +/* +** function that init the map to the right size fill with int equal to zeros +*/ + +unsigned int *init_map(int i) +{ + unsigned int *new; + int size; + + size = (i * i) / 32 + 1; + new = (unsigned int *)malloc(sizeof(*new) * size); + while (size) + new[size--] = 0; + return (new); +} + +/* +** function that send to "fill_map" a map of a certain size and increment its size untill it's solved +*/ + +void search_map(t_fillist *list) +{ + t_fillist *tmp; + + //////////////////////////// TEST //////////////////////////// + // ce tableau permet de monter jusqu'a une map de 16*16 + unsigned int print; + unsigned int tab[8]; + tab[0] = 2656554334; + tab[1] = 1394456818; + tab[2] = 1494256918; + tab[3] = 2656554334; + tab[4] = 1592453883; + tab[5] = 1444352908; + tab[6] = 2154339230; + tab[7] = 1576493154; + print_map(tab, 10, 10); + tmp = list; + while (tmp) + { + // imression pour tests + print = tmp->tetribit; + print <<= 16; +// print_map(&print, tmp->width, tmp->height); // test, imprime le tetri +// ft_putchar('\n'); + ft_putnbrendl(find_place(tab, tmp, 10)); + ft_putchar('\n'); + tmp = tmp->next; + } + //////////////////////////// TEST //////////////////////////// + + ////////////////////////// en cours ////////////////////////// + unsigned int *map; + int size; + int i; + + size = 2; + i = 1; + tmp = list; + // trouve le nombre de tetri en parcourant la liste chainee + while ((tmp = tmp->next)) + i++; + // trouve la taille minimale de la map + while (size * size < i * 4) + size++; + map = init_map(size); + + // en cours de test : + // quand add_remove_marchera la ligne d'apres imprimera la map avec les pixels du tetri rajoutes a la bonne place + add_remove(tab, list, 10, find_place(tab, list, 10)); + print_map(tab, 10, 10); + + // lance la recursive fill_map en augmentant la taille de la map tant qu'il n'y a pas de solution +// ft_putstr("test "); +// ft_putnbrendl(size); +// while (!fill_map(map, list, size)) +// map = init_map(size++); + ////////////////////////// en cours ////////////////////////// +} + diff --git a/test_add_to_list.c b/test_add_to_list.c index 178aaf4..ab16e12 100644 --- a/test_add_to_list.c +++ b/test_add_to_list.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */ -/* Updated: 2019/04/27 14:54:11 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/30 14:14:32 by hulamy ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ void print_bits(short line) /* ** Function that transforme a tetrminos char* into a short of 16 bites -** then it fills it and its reverse into the list +** and then fills it and its reversed into the list */ void fill_list(char line[], t_fillist *list) diff --git a/test_big_tetri.c b/test_big_tetri.c index 6ec28da..e2b5198 100644 --- a/test_big_tetri.c +++ b/test_big_tetri.c @@ -14,21 +14,17 @@ void print_bits(unsigned int bits, int size) write(1, "\n", 1); } -void print_map(unsigned int *tab, int size) +void print_map(unsigned int *tab, int width, int height) { int i; unsigned int mask; - unsigned int tmp; i = 0; - mask = 0; // creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000) - while (i++ < size) - mask = (mask >> 1) | ((mask | 1) << 31); - i = 0; - while (i < size * size) + mask = ~0u << (32 - width); + while (i < width * height) { - if (!(i % size)) + if (!(i % width)) ft_putchar('\n'); tab[i / 32] & (1 << (31 - i % 32)) ? ft_putchar('#') : ft_putchar('.'); ft_putchar(' '); @@ -44,13 +40,14 @@ void find_place(unsigned int *tab, char *line, int size) int j; unsigned int mask; unsigned int tmp; - int large; + int width; + int height; -// ft_putendl("# . . # # # # T T # . # . # # # # # . . # # . # T # . # # # # . . # . # . . # # T . . # # # . # # . # # # . . . # # # # . . # . . # . # # . . # . . . # . . . . # . . . # # . # . . . # . # # ."); /////////////// create tetri /////////////// i = 0; tmp = 0; - large = 3; + width = 3; + height = 2; while (line[i]) { tetri <<= 1; @@ -63,31 +60,31 @@ void find_place(unsigned int *tab, char *line, int size) tetri <<= 1; print_bits(tetri, 16); tmp = (tetri | tmp) << 16; - print_map(&tmp, large); + print_map(&tmp, width, height); /////////////// create tetri /////////////// mask = ~0; - mask <<= 32 - large; + mask <<= 32 - width; ft_putendl("mask: "); print_bits(mask, 32); i = 0; - while (i < 20) + while (i < (size - height + 1) * size) { tmp = 0; - j = 3 * size + i; + j = height * size + i; while (j >= i) { - tmp >>= large; + tmp >>= width; tmp |= (mask & (tab[j / 32] << j)); tmp |= (mask & (tab[(j + size) / 32] >> (32 - j))); j -= size; } - print_map(&tmp, large); + print_map(&tmp, width, height); print_bits(tmp >> 16, 32); print_bits(tetri, 32); print_bits((tmp >> 16) & tetri, 32); - if (i % size == size - large) - i += large - 1; + if (i % size == size - width) + i += width - 1; i++; } } @@ -119,11 +116,10 @@ int main(int ac, char **av) // mettre l'option "b" pour afficher la troisieme argument en binaire if (av[0][0] == 'b' && ac > 2) ft_putendl(ft_convertbase(av[1], "01", "0123456789")); - print_map(tab, 10); + print_map(tab, 10, 10); write(1, "\n", 1); if (av[0][0] == 't' && ac > 2) find_place(tab, av[1], 10); } return (0); } -