From 5dd491ed529433b9b12fb6319442a889317e8d0d Mon Sep 17 00:00:00 2001 From: Manzovince Date: Tue, 30 Apr 2019 15:32:47 +0200 Subject: [PATCH 1/2] few little edits + show wrong tetri feature --- fillit.h | 6 +++--- parse_input.c | 30 ++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/fillit.h b/fillit.h index 5f65c3f..b43a3ee 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/30 14:19:39 by hulamy ### ########.fr */ +/* Updated: 2019/04/30 14:43:42 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,8 @@ #include // for system call open #include // for printf (DELETE BEFORE EVAL) -#include "../42-libft/includes/libft.h" -//# define BUFF_SIZE 1024 +#include "libft/includes/libft.h" +# define BUFF_SIZE 1024 /* ** STRUCTURE diff --git a/parse_input.c b/parse_input.c index 7a0c77c..5b3f33d 100644 --- a/parse_input.c +++ b/parse_input.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ -/* Updated: 2019/04/30 14:19:32 by hulamy ### ########.fr */ +/* Updated: 2019/04/30 15:30:53 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,13 +62,15 @@ 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; // transforme la ligne de . et # en un short de 0 et 1 list->tetribit = tab_to_bin(line); - // cree un mask avec des 1 sur la colonne de droite + ft_putstr("BEFORE: "); + print_bits(list->tetribit, 16); + // cree un mask avec des 1 sur la colonne de droite (#...#...#...#...) mask = (1 << 15) | (1 << 11) | (1 << 7) | (1 << 3); // utilise le mask pour trouver la largeur que prend le tetriminos i = 0; @@ -78,7 +80,8 @@ void fill_list(char line[], t_fillist *list) while (mask & list->tetribit && ++i < 4) mask >>= 1; list->width = i - list->width; - // deplace le tetriminos tout en haut a gauche (i - list->width = le nombre de colonne vide a gauche) + // deplace le tetriminos tout en haut a gauche + //(i - list->width = le nombre de colonne vide a gauche) list->tetribit <<= (i - list->width); while (!(list->tetribit & (~0u << 12))) list->tetribit <<= 4; @@ -91,11 +94,15 @@ 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'); + ft_putstr("AFTER: "); + print_bits(list->tetribit, 16); + ft_putstr("\n"); + tmp = list->tetribit; + tmp <<= 16; + ft_putstr("\n"); + print_map(&tmp, list->width, list->height); + tmp >>= 16; } /* @@ -143,7 +150,10 @@ void parse_input(char *input) tetri[j++] = input[i++]; tetri[19] = '\0'; if (check_tetri_errors(tetri)) - print_error("Error: Tetrimino not valid."); + { + ft_putstr(tetri); + print_error("\n\nerror: This tetrimino is not valid.\n"); + } add_to_list(tetri, &list); while (input[i] && input[i] != '.' && input[i] != '#') i++; From 7b26fc7feb97668b700b543558785e2c5761a04f Mon Sep 17 00:00:00 2001 From: Manzovince Date: Tue, 30 Apr 2019 19:51:28 +0200 Subject: [PATCH 2/2] adding a print_tetri function in print.c for testing --- fillit.h | 3 +- parse_input.c | 14 +-- print.c | 29 +++++- test_fillit.c | 272 -------------------------------------------------- 4 files changed, 32 insertions(+), 286 deletions(-) delete mode 100644 test_fillit.c diff --git a/fillit.h b/fillit.h index b43a3ee..97882da 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/30 14:43:42 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/30 19:23:23 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,6 +47,7 @@ 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 print_tetri(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); diff --git a/parse_input.c b/parse_input.c index 5b3f33d..33cc039 100644 --- a/parse_input.c +++ b/parse_input.c @@ -6,7 +6,7 @@ /* By: vmanzoni +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */ -/* Updated: 2019/04/30 15:30:53 by vmanzoni ### ########.fr */ +/* Updated: 2019/04/30 19:30:54 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,8 +68,6 @@ void fill_list(char line[], t_fillist *list) // transforme la ligne de . et # en un short de 0 et 1 list->tetribit = tab_to_bin(line); - ft_putstr("BEFORE: "); - print_bits(list->tetribit, 16); // cree un mask avec des 1 sur la colonne de droite (#...#...#...#...) mask = (1 << 15) | (1 << 11) | (1 << 7) | (1 << 3); // utilise le mask pour trouver la largeur que prend le tetriminos @@ -93,15 +91,11 @@ void fill_list(char line[], t_fillist *list) // fabrique la ligne pour le tetriminos de la bonne largeur list->tetribit = reduce_tetri(list->tetribit, list->width); - // imression pour tests - ft_putchar('\n'); - ft_putstr("AFTER: "); - print_bits(list->tetribit, 16); - ft_putstr("\n"); + // impression pour tests tmp = list->tetribit; tmp <<= 16; - ft_putstr("\n"); - print_map(&tmp, list->width, list->height); +// ft_putstr("\n"); +// print_map(&tmp, list->width, list->height); tmp >>= 16; } diff --git a/print.c b/print.c index 5e3256e..4e8ceae 100644 --- a/print.c +++ b/print.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* print.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: hulamy +#+ +:+ +#+ */ +/* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/04/30 13:24:28 by hulamy #+# #+# */ -/* Updated: 2019/04/30 13:25:18 by hulamy ### ########.fr */ +/* Updated: 2019/04/30 19:26:01 by vmanzoni ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,30 @@ void print_bits(unsigned int bits, int size) write(1, "\n", 1); } +/* +** DELETE BEFORE EVAL - TEST FUNCTION +** Prints a tetri from its bit form +*/ + +void print_tetri(unsigned int bits, int size) +{ + unsigned int mask; + short i; + + i = 0; + mask = 1 << (size - 1); + while (mask) + { + if (i % 4 == 0) + write(1, "\n", 1); + (bits & mask) ? write(1, "#", 1) : write(1, ".", 1); + write(1, " ", 1); + mask >>= 1; + i++; + } + write(1, "\n", 1); +} + /* ** DELETE BEFORE EVAL - TEST FUNCTION ** Print a map of height and width @@ -56,4 +80,3 @@ void print_map(unsigned int *tab, int width, int height) } write(1, "\n", 1); } - diff --git a/test_fillit.c b/test_fillit.c deleted file mode 100644 index 3704f2e..0000000 --- a/test_fillit.c +++ /dev/null @@ -1,272 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* test_fillit.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: vmanzoni +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2019/04/22 16:05:59 by vmanzoni #+# #+# */ -/* Updated: 2019/04/23 13:57:22 by vmanzoni ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include // for system call write -#include // for system call open -#include // for memmove and strlen -#include // for debug printf -#include // to use bool type - -#include "libft/includes/libft.h" - -/* -** STRUCTURE -*/ - -typedef struct s_fillist -{ - int tetribit; - int position; - struct s_fillist *next; -} t_fillist; - -/* -** Function that display error message *s on fd and exit program -*/ - -void print_error(char *s) -{ - write(2, s, strlen(s)); - exit(1); -} - -/* -** Function that read file -*/ - -char *read_file(char *file) -{ - char buf[BUFF_SIZE]; - int fd; - int rv; - int i; - char *result; - - if (((fd = open(file, O_RDONLY)) < 0) \ - || ((rv = read(fd, &buf, BUFF_SIZE)) < 0) \ - || !(result = malloc(sizeof(char) * rv))) - return (NULL); - buf[rv] = '\0'; - i = -1; - while (buf[++i]) - result[i] = buf[i]; - result[i] = '\0'; - close(fd); - return (result); -} - -/* -** Function to see if there if an error if the file -** - less than 4 lines -** - more than 104 (26 tetri) + 25 (\n) = 129 lines -** - two \n in a row -*/ - -int check_file_errors(char *file) -{ - int i; - int line_nbr; - - i = 0; - line_nbr = 0; - while (file[i]) - { - if (file[i] != '.' && file[i] != '#' && file[i] != '\n') - return (1); - if (file[i] == '\n') - line_nbr++; - if (file[i] == '\n' && file[i+1] != '\0' && \ - file[i+2] != '.' && file[i+2] != '#') - return (1); - i++; - } - if (line_nbr < 4 || line_nbr > 129) - return (1); - return (0); -} - -/* -** Function that check if tetrimino parts are linked -*/ - -int check_tetri_errors2(char *tetri) -{ - int i; - - i = 0; - while (tetri[i]) - { - if (tetri[i] == '.' || tetri[i] == '\n') - i++; - else if (tetri[i] == '#' && (tetri[i + 1] == '#' || tetri[i - 1] == '#' - || tetri[i + 5] == '#' || tetri[i - 5] == '#')) - i++; - else - return (1); - } - return (0); -} - -/* -** Function that check if tetrimino square contains: -** - 4 '#' -** - 12 '.' -*/ - -int check_tetri_errors(char *tetri) -{ - int i; - int htg; - int dot; - - i = 0; - htg = 0; - dot = 0; - while (tetri[i]) - { - if (tetri[i] == '#') - htg++; - else if (tetri[i] == '.') - dot++; - i++; - } - if (htg != 4 || dot != 12 || check_tetri_errors2(tetri)) - return (1); - return (0); -} - -/* -** Function that take char * tetri and convert to short and put it in a list -*/ - -int fill_list(char *line, t_fillist *list) -{ - short tmp; - short tet; - int i; - - i = -1; - tet = 0; - while (line[++i]) - { - tet <<= 1; - if (line[i] == '\n') - i++; - if (line[i] == '#') - tet |= 1; - } - return (0); -} - -/* -** this function create the linked list and add a new structure linked each time needed -*/ - -int add_to_list(char *line, t_fillist **list) -{ - t_fillist *tmp; - - if (!(tmp = (t_fillist*)malloc(sizeof(*tmp)))) - return (0); - if (!(*list)) - tmp->next = NULL; - else - tmp->next = *list; - *list = tmp; - fill_list(line, *list); - return (1); -} - -/* -** Function that parse a file and put each tetrimino in a linked list -*/ - -int parse_input(char *input, t_fillist list) -{ - //static t_fillist *list = NULL; - static int size; - char tetri[20]; - int i; - int j; - - i = 0; - size = 0; - while (input[i]) - { - j = 0; - while (j < 19) - tetri[j++] = input[i++]; - tetri[19] = '\0'; - if (check_tetri_errors(tetri)) - print_error("Error: Tetrimino not valid."); - add_to_list(tetri, &list); - while (input[i] == '\n') - i++; - size++; - } - printf("%i\n", size); - return (size); -} - -/* ***************************************************************************** -** Function that brute force with backtracking for smallest square with tetris -***************************************************************************** */ - -int get_smallest_square(t_fillist list, int size, unsigned int map[]) -{ - unsigned int mask; - int i; - int j; - - i = 0; - j = 0; - mask = map[size]; - while (list.next != NULL) - { - mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32))); - if (!(list.tetribit & mask)) - return (1); - i++; - } - return (0); -} - -/* ***************************************************************************** -** MAIN -***************************************************************************** */ - -int main(int argc, char **argv) -{ - char *input; - int map; - int size; - t_fillist list; - - if (argc == 2) - { - if (!(input = read_file(argv[1]))) - print_error("Error: Could not read file.\n"); - if (check_file_errors(input)) - print_error("Error: Invalid file.\n"); - size = parse_input(input, list); - get_smallest_square(list, size, map); - /* - Backtracking for smallest square - Transform tetriminos with letters - Print result - */ - } - else - print_error("Usage: Please submit a file.\n"); - return (0); -}