flags organises
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/05/24 14:42:46 by hulamy #+# #+# */
|
/* Created: 2019/05/24 14:42:46 by hulamy #+# #+# */
|
||||||
/* Updated: 2019/05/24 14:46:08 by hulamy ### ########.fr */
|
/* Updated: 2019/05/27 19:31:53 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -30,31 +30,12 @@ int check_tetri_memory(t_fillist *list, int pos)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// if (!(tetri->memory[pos / 32] & mask))
|
if (!(tetri->memory[pos / 32] & mask))
|
||||||
return (tetri->memory[pos / 32] |= mask);
|
return (tetri->memory[pos / 32] |= mask);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove_tetri_memory(t_fillist *list, int pos)
|
|
||||||
{
|
|
||||||
t_fillist *tetri;
|
|
||||||
unsigned int mask;
|
|
||||||
|
|
||||||
tetri = list;
|
|
||||||
mask = 1 << ((pos % 32) - 1);
|
|
||||||
if (tetri->same != NULL)
|
|
||||||
{
|
|
||||||
if ((tetri->same->memory[pos / 32] & mask))
|
|
||||||
tetri->same->memory[pos / 32] ^= mask;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if ((tetri->memory[pos / 32] & mask))
|
|
||||||
tetri->memory[pos / 32] ^= mask;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Test optimisation for not testing wrong maps when tetri are identical
|
** Test optimisation for not testing wrong maps when tetri are identical
|
||||||
*/
|
*/
|
||||||
@@ -66,7 +47,7 @@ int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b)
|
|||||||
if (tetri_a->width != tetri_b->width)
|
if (tetri_a->width != tetri_b->width)
|
||||||
return (0);
|
return (0);
|
||||||
if (tetri_a->height != tetri_b->height)
|
if (tetri_a->height != tetri_b->height)
|
||||||
return (0);
|
return (0);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +65,8 @@ int check_same_tetri(t_fillist *list, int num)
|
|||||||
while (curr_tetri != NULL)
|
while (curr_tetri != NULL)
|
||||||
{
|
{
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!(curr_tetri->memory = (unsigned int *)malloc(sizeof(*curr_tetri->memory) * num)))
|
if (!(curr_tetri->memory =
|
||||||
|
(unsigned int *)malloc(sizeof(*curr_tetri->memory) * num)))
|
||||||
return (0);
|
return (0);
|
||||||
while (i < num)
|
while (i < num)
|
||||||
curr_tetri->memory[i++] = 0;
|
curr_tetri->memory[i++] = 0;
|
||||||
@@ -100,4 +82,3 @@ int check_same_tetri(t_fillist *list, int num)
|
|||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
77
f_bonus_print.c
Normal file
77
f_bonus_print.c
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* f_bonus_print.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/05/27 13:46:29 by hulamy #+# #+# */
|
||||||
|
/* Updated: 2019/05/27 19:33:09 by hulamy ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "fillit.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
** function that print the given tetris if flag p is present
|
||||||
|
*/
|
||||||
|
|
||||||
|
t_fillist *print_tetri(t_fillist *list)
|
||||||
|
{
|
||||||
|
unsigned int print;
|
||||||
|
t_fillist *tmp;
|
||||||
|
|
||||||
|
tmp = list;
|
||||||
|
if (list->dope[2])
|
||||||
|
{
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
check_same_tetri(list, 1);
|
||||||
|
print = tmp->tetribit;
|
||||||
|
print <<= 16;
|
||||||
|
print_sized_map(&print, tmp->width, tmp->height, tmp->letter);
|
||||||
|
if (tmp->same)
|
||||||
|
{
|
||||||
|
print = tmp->same->tetribit;
|
||||||
|
print <<= 16;
|
||||||
|
ft_putstr("same --> ");
|
||||||
|
ft_putchar(tmp->same->letter);
|
||||||
|
ft_putchar('\n');
|
||||||
|
}
|
||||||
|
ft_putchar('\n');
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (list);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** function that print the map in binary if flag p is present
|
||||||
|
** it returns anyway the size of the map for main to print it
|
||||||
|
*/
|
||||||
|
|
||||||
|
int print_binary_map(unsigned int *map, int size, int *dope)
|
||||||
|
{
|
||||||
|
size--;
|
||||||
|
if (dope[2])
|
||||||
|
{
|
||||||
|
ft_putendl("result in binary :");
|
||||||
|
print_sized_map(map, size, size, '#');
|
||||||
|
ft_putchar('\n');
|
||||||
|
}
|
||||||
|
return (size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** function that print the flags usage
|
||||||
|
*/
|
||||||
|
|
||||||
|
int print_flags_usage(void)
|
||||||
|
{
|
||||||
|
ft_putendl("flags usage :");
|
||||||
|
ft_putendl("d : debug print (print the map during the backtracking)");
|
||||||
|
ft_putendl("o : optimisation ultra fast but with some errors still");
|
||||||
|
ft_putendl("p : print the tetri and the map in different formats");
|
||||||
|
ft_putendl("e : error msgs more precise AND no error for too much tetri\n");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */
|
/* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */
|
||||||
/* Updated: 2019/05/24 18:04:46 by hulamy ### ########.fr */
|
/* Updated: 2019/05/27 19:43:06 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -44,10 +44,7 @@ unsigned short reduce_tetri(unsigned short tetri, int width)
|
|||||||
unsigned int mask;
|
unsigned int mask;
|
||||||
unsigned int tmp;
|
unsigned int tmp;
|
||||||
|
|
||||||
// cree un mask avec des 1 a gauche sur la largeur du tetriminos
|
|
||||||
mask = ~0u << (32 - width) >> 16;
|
mask = ~0u << (32 - width) >> 16;
|
||||||
// fabrique la ligne pour le tetriminos de la bonne largeur
|
|
||||||
tmp = tetri;
|
|
||||||
tmp = (mask & tetri);
|
tmp = (mask & tetri);
|
||||||
tmp |= ((mask & tetri << 4) >> width);
|
tmp |= ((mask & tetri << 4) >> width);
|
||||||
tmp |= ((mask & tetri << 8) >> (2 * width));
|
tmp |= ((mask & tetri << 8) >> (2 * width));
|
||||||
@@ -66,9 +63,12 @@ unsigned short reduce_tetri(unsigned short tetri, int width)
|
|||||||
** (i - list->width = le nombre de colonne vide a gauche)
|
** (i - list->width = le nombre de colonne vide a gauche)
|
||||||
** 5) trouve la hauteur du tetri
|
** 5) trouve la hauteur du tetri
|
||||||
** 6) fabrique la ligne pour le tetriminos de la bonne largeur
|
** 6) fabrique la ligne pour le tetriminos de la bonne largeur
|
||||||
|
**
|
||||||
|
** list->test is used to debug the backtracking, allowing to print the
|
||||||
|
** map each time without the previous tries
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void fill_list(char line[], t_fillist *list)
|
void fill_list(char line[], t_fillist *list)
|
||||||
{
|
{
|
||||||
unsigned int mask;
|
unsigned int mask;
|
||||||
int i;
|
int i;
|
||||||
@@ -92,7 +92,7 @@ void fill_list(char line[], t_fillist *list)
|
|||||||
list->height = i;
|
list->height = i;
|
||||||
list->tetribit = reduce_tetri(list->tetribit, list->width);
|
list->tetribit = reduce_tetri(list->tetribit, list->width);
|
||||||
list->same = NULL;
|
list->same = NULL;
|
||||||
list->test = 0; // DEBUG pour que print_final_map puisse imprimer correctement au fur et a mesure
|
list->test = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -100,7 +100,7 @@ void fill_list(char line[], t_fillist *list)
|
|||||||
** linked each time needed
|
** linked each time needed
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int add_to_list(char *line, t_fillist **list, char letter)
|
int add_to_list(char *line, t_fillist **lst, char letter, int *dope)
|
||||||
{
|
{
|
||||||
t_fillist *tmp;
|
t_fillist *tmp;
|
||||||
t_fillist *test;
|
t_fillist *test;
|
||||||
@@ -108,17 +108,19 @@ int add_to_list(char *line, t_fillist **list, char letter)
|
|||||||
if (!(tmp = (t_fillist*)malloc(sizeof(*tmp))))
|
if (!(tmp = (t_fillist*)malloc(sizeof(*tmp))))
|
||||||
return (0);
|
return (0);
|
||||||
tmp->next = NULL;
|
tmp->next = NULL;
|
||||||
test = *list;
|
test = *lst;
|
||||||
if (!test)
|
if (!test)
|
||||||
*list = tmp;
|
*lst = tmp;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
while(test->next)
|
while (test->next)
|
||||||
test = test->next;
|
test = test->next;
|
||||||
test->next = tmp;
|
test->next = tmp;
|
||||||
}
|
}
|
||||||
fill_list(line, tmp);
|
fill_list(line, tmp);
|
||||||
tmp->letter = letter;
|
tmp->letter = letter;
|
||||||
|
tmp->dope = dope;
|
||||||
|
tmp->start = *lst;
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,12 +128,13 @@ int add_to_list(char *line, t_fillist **list, char letter)
|
|||||||
** Function that parse a file and put each tetrimino in a linked list
|
** Function that parse a file and put each tetrimino in a linked list
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int parse_input(char *input, t_fillist **list, int *dope)
|
int parse_input(char *input, t_fillist **list, int *dope)
|
||||||
{
|
{
|
||||||
char tetri[20];
|
char tetri[20];
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
int letter;
|
int letter;
|
||||||
|
int size;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
letter = 'A';
|
letter = 'A';
|
||||||
@@ -145,10 +148,10 @@ int parse_input(char *input, t_fillist **list, int *dope)
|
|||||||
print_error("error\n");
|
print_error("error\n");
|
||||||
// print_error("error: Wrong tetrimino.\n");
|
// print_error("error: Wrong tetrimino.\n");
|
||||||
// print_error_extended(check_tetri_errors(tetri));
|
// print_error_extended(check_tetri_errors(tetri));
|
||||||
add_to_list(tetri, list, letter++);
|
add_to_list(tetri, list, letter++, dope);
|
||||||
while (input[i] && input[i] != '.' && input[i] != '#')
|
while (input[i] && input[i] != '.' && input[i] != '#')
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
int size = search_map(*list);
|
size = search_map(*list);
|
||||||
return (size);
|
return (size);
|
||||||
}
|
}
|
||||||
|
|||||||
90
f_print.c
90
f_print.c
@@ -6,37 +6,18 @@
|
|||||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/04/30 13:24:28 by hulamy #+# #+# */
|
/* Created: 2019/04/30 13:24:28 by hulamy #+# #+# */
|
||||||
/* Updated: 2019/05/22 14:34:45 by hulamy ### ########.fr */
|
/* Updated: 2019/05/27 19:47:11 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "fillit.h"
|
#include "fillit.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** DELETE BEFORE EVAL - TEST FUNCTION
|
** function that print a map of height and width
|
||||||
** Prints a ligne of size bits
|
** usefull to print tetris
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void print_bits(unsigned int bits, int size)
|
void print_sized_map(unsigned int *tab, int width, int height, char letter)
|
||||||
{
|
|
||||||
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, char letter)
|
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
unsigned int mask;
|
unsigned int mask;
|
||||||
@@ -50,7 +31,10 @@ void print_map(unsigned int *tab, int width, int height, char letter)
|
|||||||
{
|
{
|
||||||
if (i && !(i % width))
|
if (i && !(i % width))
|
||||||
ft_putchar('\n');
|
ft_putchar('\n');
|
||||||
tab[i / 32] & (1 << (31 - i % 32)) ? ft_put_tetri_color(letter) : ft_putchar('.');
|
if (tab[i / 32] & (1 << (31 - i % 32)))
|
||||||
|
ft_put_tetri_color(letter);
|
||||||
|
else
|
||||||
|
ft_putchar('.');
|
||||||
ft_putchar(' ');
|
ft_putchar(' ');
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@@ -59,42 +43,56 @@ void print_map(unsigned int *tab, int width, int height, char letter)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Print the final map with the letters
|
** Print the final map with the letters
|
||||||
|
** if flag value is 0 -> print moulinette version
|
||||||
|
** if flag value is 0 -> print in color
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void print_final_map(t_fillist *list, int size, int flag) // DEBUG flag vaut 0 pour imprimer comme rendu et 1 pour imprimer en couleur
|
char *init_print_map(t_fillist *list, int size)
|
||||||
{
|
{
|
||||||
t_fillist *tmp;
|
|
||||||
char *map;
|
char *map;
|
||||||
int i;
|
int i;
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
map = (char *)malloc(sizeof(*map) * (size * size + 1));
|
map = (char *)malloc(sizeof(*map) * (size * size + 1));
|
||||||
map[size*size] = '\0';
|
map[size * size] = '\0';
|
||||||
i = -1;
|
i = -1;
|
||||||
while (++i < size * size)
|
while (++i < size * size)
|
||||||
map[i] = '.';
|
map[i] = '.';
|
||||||
tmp = list;
|
while (list)
|
||||||
while (tmp)
|
|
||||||
{
|
{
|
||||||
j = 0;
|
j = 0;
|
||||||
i = -1;
|
i = -1;
|
||||||
while (++i < tmp->width * tmp->height)
|
while (++i < list->width * list->height)
|
||||||
{
|
{
|
||||||
if (i && i % tmp->width == 0)
|
if (i && i % list->width == 0)
|
||||||
j += size - tmp->width;
|
j += size - list->width;
|
||||||
if (1 << (15 - i) & tmp->tetribit && tmp->test == 1) // DEBUG "&& tmp->test == 1" pour imprimer les bonnes lettres au coours du debug
|
if (1 << (15 - i) & list->tetribit && list->test == 1)
|
||||||
map[tmp->position + i + j - 1] = tmp->letter;
|
map[list->position + i + j - 1] = list->letter;
|
||||||
}
|
}
|
||||||
tmp = tmp->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
|
return (map);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Function that print the map with color if flag = 1
|
||||||
|
** or for moulinette if flag = 0;
|
||||||
|
*/
|
||||||
|
|
||||||
|
void print_letter_map(t_fillist *list, int size, int flag)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *map;
|
||||||
|
|
||||||
|
map = init_print_map(list, size);
|
||||||
i = -1;
|
i = -1;
|
||||||
while (++i < size * size)
|
while (++i < size * size)
|
||||||
{
|
{
|
||||||
if (i && i % size == 0)
|
if (i && i % size == 0)
|
||||||
ft_putchar('\n');
|
ft_putchar('\n');
|
||||||
if (flag == 0) // DEBUG imprim comme rendu
|
if (flag == 0)
|
||||||
ft_putchar(map[i]);
|
ft_putchar(map[i]);
|
||||||
else // DEBUG imprim avec couleurs
|
else
|
||||||
{
|
{
|
||||||
ft_put_tetri_color(map[i]);
|
ft_put_tetri_color(map[i]);
|
||||||
ft_putchar(' ');
|
ft_putchar(' ');
|
||||||
@@ -102,3 +100,21 @@ void print_final_map(t_fillist *list, int size, int flag) // DEBUG flag vaut 0 p
|
|||||||
}
|
}
|
||||||
ft_putchar('\n');
|
ft_putchar('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Function that print the map
|
||||||
|
*/
|
||||||
|
|
||||||
|
void print_final_map(t_fillist *list, int size)
|
||||||
|
{
|
||||||
|
if (list->dope[2])
|
||||||
|
{
|
||||||
|
ft_putendl("result for humans :");
|
||||||
|
print_letter_map(list, size, 1);
|
||||||
|
ft_putchar('\n');
|
||||||
|
ft_putendl("result for moulinette :");
|
||||||
|
}
|
||||||
|
print_letter_map(list, size, 0);
|
||||||
|
if (list->dope[2])
|
||||||
|
ft_putchar('\n');
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */
|
/* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */
|
||||||
/* Updated: 2019/04/22 15:16:18 by vmanzoni ### ########.fr */
|
/* Updated: 2019/05/27 19:46:54 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ char *read_file(char *file)
|
|||||||
|
|
||||||
if (((fd = open(file, O_RDONLY)) < 0) \
|
if (((fd = open(file, O_RDONLY)) < 0) \
|
||||||
|| ((rv = read(fd, &buf, BUFF_SIZE)) < 0) \
|
|| ((rv = read(fd, &buf, BUFF_SIZE)) < 0) \
|
||||||
|| !(result = malloc(sizeof(char) * rv)))
|
|| !(result = malloc(sizeof(char) * rv)))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
buf[rv] = '\0';
|
buf[rv] = '\0';
|
||||||
i = -1;
|
i = -1;
|
||||||
|
|||||||
173
f_search_map.c
173
f_search_map.c
@@ -6,116 +6,12 @@
|
|||||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */
|
/* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */
|
||||||
/* Updated: 2019/05/24 14:48:33 by hulamy ### ########.fr */
|
/* Updated: 2019/05/27 19:30:37 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "fillit.h"
|
#include "fillit.h"
|
||||||
|
|
||||||
/*
|
|
||||||
** OLD VERSION of find_place and fit_in_place all in one but with a lot of divisions and modulo
|
|
||||||
**
|
|
||||||
** int find_place(unsigned int *tab, t_fillist *list, int size, int pos)
|
|
||||||
** {
|
|
||||||
** int i;
|
|
||||||
** int j;
|
|
||||||
** unsigned int mask;
|
|
||||||
** unsigned int tmp;
|
|
||||||
**
|
|
||||||
** mask = ~0u << (32 - list->width);
|
|
||||||
** tmp = mask;
|
|
||||||
** i = pos;
|
|
||||||
** while (i < (size - list->height + 1) * size)
|
|
||||||
** {
|
|
||||||
** if (i % size == size - list->width + 1)
|
|
||||||
** i += list->width - 1;
|
|
||||||
** else
|
|
||||||
** {
|
|
||||||
** tmp = 0;
|
|
||||||
** j = (list->height - 1) * size + i;
|
|
||||||
** while (j >= i)
|
|
||||||
** {
|
|
||||||
** tmp >>= list->width;
|
|
||||||
** tmp |= (mask & (tab[j / 32] << j));
|
|
||||||
** tmp |= (mask & (tab[(j + list->width) / 32] >> (32 - j)));
|
|
||||||
** j -= size;
|
|
||||||
** }
|
|
||||||
** if (!((tmp >> 16) & list->tetribit))
|
|
||||||
** return (i + 1);
|
|
||||||
** i++;
|
|
||||||
** }
|
|
||||||
** }
|
|
||||||
** return (0);
|
|
||||||
** }
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
** function that try to optimize the speed
|
|
||||||
** SECOND ATTEMPT
|
|
||||||
** by finding the isolated dots whille fillling the map
|
|
||||||
** untill they get more numerous than the max amount available
|
|
||||||
** TOTAL FAILURE :p
|
|
||||||
**
|
|
||||||
** 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;
|
|
||||||
** // saute les position pas vides
|
|
||||||
** while (1 << (i % 32) & map[i % 32])
|
|
||||||
** i++;
|
|
||||||
** // pour chaque position vide regarde si chaque tetri non encode places peuvent y rentrer
|
|
||||||
** while (j && tmp)
|
|
||||||
** {
|
|
||||||
** // si le tetri est trop a droite ou trop en bas il ne rentre pas ce n'est pas la peine de chercher donc on passe au tetri suivant
|
|
||||||
** if (tmp->width > (size - i % size) || (total - i) <= (tmp->height * size))
|
|
||||||
** tmp = tmp->next;
|
|
||||||
** // sinon verifier si on peut le mettre a cette position et si on ne peut pas passer au tetri suivant
|
|
||||||
** else if (!fit_in_place(map, list, size, i))
|
|
||||||
** tmp = tmp->next;
|
|
||||||
** // si le tetri peut rentrer on arrete la boucle en mettant j = 0
|
|
||||||
** else
|
|
||||||
** j = 0;
|
|
||||||
** }
|
|
||||||
** // si j existe c que le tetri ne pouvait pas etre place donc on rajoute 1 au compteur de point isoles
|
|
||||||
** if (j)
|
|
||||||
** dots++;
|
|
||||||
** }
|
|
||||||
** return (dots > total - num);
|
|
||||||
** }
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
** function that try to optimize the speed
|
|
||||||
** FIRST ATTEMPT
|
|
||||||
** by verifying if the place left is enough to place at least
|
|
||||||
** one of each tetri left
|
|
||||||
** TOTAL FAILURE :p
|
|
||||||
**
|
|
||||||
** int check_others(unsigned int *map, t_fillist *list, int size, int num)
|
|
||||||
** {
|
|
||||||
** t_fillist *tmp;
|
|
||||||
**
|
|
||||||
** // verifie que les tetri restant puissent un par un se placer sur la map
|
|
||||||
** // ca n'optimise qu'en fin de map donc ca ralentit les grosses map en fait
|
|
||||||
** while ((tmp = tmp->next))
|
|
||||||
** if (!find_place(map, tmp, size, 0))
|
|
||||||
** return (0);
|
|
||||||
** return (1);
|
|
||||||
** }
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** function that look if a tretri fit in a place
|
** function that look if a tretri fit in a place
|
||||||
*/
|
*/
|
||||||
@@ -151,7 +47,7 @@ unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int size, int i)
|
|||||||
** function that look for the first place in the map for a tetri
|
** function that look for the first place in the map for a tetri
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int find_place(unsigned int *map, t_fillist *list, int size)
|
int find_place(unsigned int *map, t_fillist *list, int size)
|
||||||
{
|
{
|
||||||
int limit;
|
int limit;
|
||||||
int pos;
|
int pos;
|
||||||
@@ -184,7 +80,7 @@ int find_place(unsigned int *map, t_fillist *list, int size)
|
|||||||
** function that add or remove a tetri on the map
|
** function that add or remove a tetri on the map
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void add_remove(unsigned int *map, t_fillist *list, int size)
|
void add_remove(unsigned int *map, t_fillist *list, int size)
|
||||||
{
|
{
|
||||||
unsigned int mask;
|
unsigned int mask;
|
||||||
unsigned short tetri;
|
unsigned short tetri;
|
||||||
@@ -208,7 +104,7 @@ void add_remove(unsigned int *map, t_fillist *list, int size)
|
|||||||
** Function that recursively try to fill the map with the tetris
|
** Function that recursively try to fill the map with the tetris
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link)
|
int fill_map(unsigned int *map, t_fillist *list, int size)
|
||||||
{
|
{
|
||||||
if (!list)
|
if (!list)
|
||||||
return (1);
|
return (1);
|
||||||
@@ -216,28 +112,31 @@ int fill_map(unsigned int *map, t_fillist *list, int size, t_fillist *link)
|
|||||||
while (find_place(map, list, size))
|
while (find_place(map, list, size))
|
||||||
{
|
{
|
||||||
add_remove(map, list, size);
|
add_remove(map, list, size);
|
||||||
|
list->test = 1;
|
||||||
list->test = 1; // DEBUG
|
if (list->dope[0])
|
||||||
// print_final_map(link, size, 1); // DEBUG
|
{
|
||||||
// ft_putchar('\n'); // DEBUG
|
print_letter_map(list->start, size, 1);
|
||||||
|
ft_putchar('\n');
|
||||||
if (check_tetri_memory(list, list->position))
|
}
|
||||||
if (fill_map(map, list->next, size, link))
|
if (list->dope[1])
|
||||||
|
if (check_tetri_memory(list, list->position))
|
||||||
|
if (fill_map(map, list->next, size))
|
||||||
|
return (1);
|
||||||
|
if (!list->dope[1])
|
||||||
|
if (fill_map(map, list->next, size))
|
||||||
return (1);
|
return (1);
|
||||||
add_remove(map, list, size);
|
add_remove(map, list, size);
|
||||||
// remove_tetri_memory(list, list->position);
|
list->test = 0;
|
||||||
|
|
||||||
list->test = 0; // DEBUG
|
|
||||||
|
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** function that send to "fill_map" a map of a certain size and increment its size untill it's solved
|
** function that send to "fill_map" a map of a certain size
|
||||||
|
** and increment its size untill it's solved
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int search_map(t_fillist *list)
|
int search_map(t_fillist *list)
|
||||||
{
|
{
|
||||||
t_fillist *tmp;
|
t_fillist *tmp;
|
||||||
unsigned int *map;
|
unsigned int *map;
|
||||||
@@ -245,32 +144,9 @@ int search_map(t_fillist *list)
|
|||||||
int num;
|
int num;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/////////////////////////////////////////////////// TEST
|
|
||||||
unsigned int print;
|
|
||||||
tmp = list;
|
|
||||||
while (tmp)
|
|
||||||
{
|
|
||||||
// imression pour tests
|
|
||||||
check_same_tetri(list, 1);
|
|
||||||
print = tmp->tetribit;
|
|
||||||
print <<= 16;
|
|
||||||
print_map(&print, tmp->width, tmp->height, tmp->letter);
|
|
||||||
if (tmp->same)
|
|
||||||
{
|
|
||||||
print = tmp->same->tetribit;
|
|
||||||
print <<= 16;
|
|
||||||
ft_putstr("same --> ");
|
|
||||||
ft_putchar(tmp->same->letter);
|
|
||||||
ft_putchar('\n');
|
|
||||||
}
|
|
||||||
ft_putchar('\n');
|
|
||||||
tmp = tmp->next;
|
|
||||||
}
|
|
||||||
/////////////////////////////////////////////////// TEST
|
|
||||||
|
|
||||||
size = 2;
|
size = 2;
|
||||||
num = 1;
|
num = 1;
|
||||||
tmp = list;
|
tmp = print_tetri(list);
|
||||||
while ((tmp = tmp->next))
|
while ((tmp = tmp->next))
|
||||||
num++;
|
num++;
|
||||||
while (size * size < num * 4)
|
while (size * size < num * 4)
|
||||||
@@ -278,16 +154,13 @@ int search_map(t_fillist *list)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (!i)
|
while (!i)
|
||||||
{
|
{
|
||||||
ft_putnbrendl(size);
|
|
||||||
num = (size * size) / 32 + 1;
|
num = (size * size) / 32 + 1;
|
||||||
if (!(map = (unsigned int *)malloc(sizeof(*map) * num)))
|
if (!(map = (unsigned int *)malloc(sizeof(*map) * num)))
|
||||||
return (0);
|
return (0);
|
||||||
check_same_tetri(list, num);
|
check_same_tetri(list, num);
|
||||||
while (num)
|
while (num)
|
||||||
map[num--] = 0;
|
map[num--] = 0;
|
||||||
i = fill_map(map, list, size++, list);
|
i = fill_map(map, list, size++);
|
||||||
}
|
}
|
||||||
ft_putendl("result in binary :"); // DEBUG (pas dans le main car besoin de map)
|
return (print_binary_map(map, size, list->dope));
|
||||||
print_map(map, size - 1, size - 1, '#'); // DEBUG
|
|
||||||
return (--size);
|
|
||||||
}
|
}
|
||||||
|
|||||||
70
fillit.h
70
fillit.h
@@ -6,7 +6,7 @@
|
|||||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */
|
/* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */
|
||||||
/* Updated: 2019/05/24 18:04:17 by hulamy ### ########.fr */
|
/* Updated: 2019/05/27 19:26:04 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -57,50 +57,74 @@ typedef struct s_fillist
|
|||||||
int num;
|
int num;
|
||||||
int test;
|
int test;
|
||||||
char letter;
|
char letter;
|
||||||
|
int *dope;
|
||||||
unsigned int *memory;
|
unsigned int *memory;
|
||||||
struct s_fillist *same;
|
struct s_fillist *same;
|
||||||
struct s_fillist *next;
|
struct s_fillist *next;
|
||||||
|
struct s_fillist *start;
|
||||||
} t_fillist;
|
} t_fillist;
|
||||||
|
|
||||||
|
/*
|
||||||
|
** bonus_print.c
|
||||||
|
*/
|
||||||
|
t_fillist *print_tetri(t_fillist *list);
|
||||||
|
int print_binary_map(unsigned int *map, int size, int *dope);
|
||||||
|
int print_flags_usage(void);
|
||||||
|
|
||||||
|
/*
|
||||||
|
** bonus_opti.c
|
||||||
|
*/
|
||||||
|
int check_same_tetri(t_fillist *list, int num);
|
||||||
|
int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b);
|
||||||
|
int check_tetri_memory(t_fillist *list, int pos);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** main.c
|
** main.c
|
||||||
*/
|
*/
|
||||||
int main(int argc, char **argv);
|
int main(int argc, char **argv);
|
||||||
int *create_dope(char *av);
|
int *create_dope(char *av, int mdp);
|
||||||
int is_mdp(int ac, char **av);
|
int is_mdp(int ac, char **av);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** read_file.c
|
** read_file.c
|
||||||
*/
|
*/
|
||||||
char *read_file(char *file);
|
char *read_file(char *file);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** handle_errors.c
|
** handle_errors.c
|
||||||
*/
|
*/
|
||||||
void print_error(char *s);
|
void print_error(char *s);
|
||||||
void print_error_extended(int error, int *dope);
|
void print_error_extended(int error, int *dope);
|
||||||
int check_file_errors(char *file, int *dope);
|
void check_file_errors(char *file, int *dope);
|
||||||
int check_tetri_errors(char *tetri);
|
int check_tetri_errors(char *tetri);
|
||||||
int check_tetri_errors_proxy(char *tetri);
|
int check_tetri_errors_proxy(char *tetri);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** handle_errors.c
|
** parse_input.c
|
||||||
*/
|
*/
|
||||||
int parse_input(char *input, t_fillist **list, int *dope);
|
int parse_input(char *input, t_fillist **list, int *dope);
|
||||||
int add_to_list(char *square, t_fillist **list, char letter);
|
int add_to_list(char *sqr, t_fillist **lst, char lett, int *dope);
|
||||||
void fill_list(char line[], t_fillist *list);
|
void fill_list(char line[], t_fillist *list);
|
||||||
unsigned short reduce_tetri(unsigned short tetri, int width);
|
unsigned short reduce_tetri(unsigned short tetri, int width);
|
||||||
unsigned short tab_to_bin(char line[]);
|
unsigned short tab_to_bin(char line[]);
|
||||||
|
|
||||||
void print_bits(unsigned int bits, int size); //TO DELETE BEFORE EVAL
|
/*
|
||||||
void print_tetri(unsigned int bits, int size); //TO DELETE BEFORE EVAL
|
** search_map.c
|
||||||
int search_map(t_fillist *list);
|
*/
|
||||||
void print_map(unsigned int *tab, int width, int height, char letter);
|
unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int siz, int i);
|
||||||
void print_final_map(t_fillist *list, int size, int flag);
|
int find_place(unsigned int *map, t_fillist *list, int size);
|
||||||
|
void add_remove(unsigned int *map, t_fillist *list, int size);
|
||||||
|
int fill_map(unsigned int *map, t_fillist *list, int size);
|
||||||
|
int search_map(t_fillist *list);
|
||||||
|
|
||||||
|
/*
|
||||||
|
** print.c
|
||||||
|
*/
|
||||||
|
void print_sized_map(unsigned int *tab, int width, int height, char letter);
|
||||||
|
char *init_print_map(t_fillist *list, int size);
|
||||||
|
void print_letter_map(t_fillist *list, int size, int flag);
|
||||||
|
void print_final_map(t_fillist *list, int size);
|
||||||
|
|
||||||
void ft_put_tetri_color(char c);
|
void ft_put_tetri_color(char c);
|
||||||
int check_tetri_memory(t_fillist *list, int pos);
|
|
||||||
void remove_tetri_memory(t_fillist *list, int pos);
|
|
||||||
int check_same_tetri(t_fillist *list, int num);
|
|
||||||
int compare_tetri(t_fillist *tetri_a, t_fillist *tetri_b);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
30
main.c
30
main.c
@@ -6,7 +6,7 @@
|
|||||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */
|
/* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */
|
||||||
/* Updated: 2019/05/24 18:02:29 by hulamy ### ########.fr */
|
/* Updated: 2019/05/27 18:21:58 by hulamy ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
** e : error extended (error message more precise AND no error for too much tetri)
|
** e : error extended (error message more precise AND no error for too much tetri)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int *create_dope(char *av)
|
int *create_dope(char *av, int mdp)
|
||||||
{
|
{
|
||||||
char *comp;
|
char *comp;
|
||||||
int *dope;
|
int *dope;
|
||||||
@@ -34,17 +34,13 @@ int *create_dope(char *av)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (i < 4)
|
while (i < 4)
|
||||||
dope[i++] = 0;
|
dope[i++] = 0;
|
||||||
|
if (!mdp)
|
||||||
|
return (dope);
|
||||||
i = -1;
|
i = -1;
|
||||||
while (++i < 4 && (j = -1))
|
while (++i < 4 && (j = -1))
|
||||||
while (av[++j])
|
while (av[++j])
|
||||||
if (comp[i] == av[j])
|
if (comp[i] == av[j])
|
||||||
dope[i] = 1;
|
dope[i] = 1;
|
||||||
|
|
||||||
ft_putendl(av);
|
|
||||||
i = -1;
|
|
||||||
while (++i < 4)
|
|
||||||
ft_putnbr(dope[i]);
|
|
||||||
ft_putchar('\n');
|
|
||||||
return (dope);
|
return (dope);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +53,7 @@ int is_mdp(int ac, char **av)
|
|||||||
char *mdp;
|
char *mdp;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (ac != 4)
|
if (ac < 3 || ac > 4)
|
||||||
return (0);
|
return (0);
|
||||||
mdp = "trompette";
|
mdp = "trompette";
|
||||||
i = 0;
|
i = 0;
|
||||||
@@ -65,6 +61,8 @@ int is_mdp(int ac, char **av)
|
|||||||
i++;
|
i++;
|
||||||
if (av[2][i] || mdp[i])
|
if (av[2][i] || mdp[i])
|
||||||
return (0);
|
return (0);
|
||||||
|
if (ac == 3)
|
||||||
|
return (print_flags_usage());
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,11 +76,12 @@ int main(int ac, char **av)
|
|||||||
char *input;
|
char *input;
|
||||||
int *dope;
|
int *dope;
|
||||||
int size;
|
int size;
|
||||||
|
int mdp;
|
||||||
|
|
||||||
list = NULL;
|
list = NULL;
|
||||||
if (ac == 2 || is_mdp(ac, av))
|
dope = create_dope(av[3], (mdp = is_mdp(ac, av)));
|
||||||
|
if (ac == 2 || mdp)
|
||||||
{
|
{
|
||||||
dope = create_dope(av[3]);
|
|
||||||
if (!(input = read_file(av[1])))
|
if (!(input = read_file(av[1])))
|
||||||
{
|
{
|
||||||
if (dope[3])
|
if (dope[3])
|
||||||
@@ -92,14 +91,7 @@ int main(int ac, char **av)
|
|||||||
}
|
}
|
||||||
check_file_errors(input, dope);
|
check_file_errors(input, dope);
|
||||||
size = parse_input(input, &list, dope);
|
size = parse_input(input, &list, dope);
|
||||||
|
print_final_map(list, size);
|
||||||
ft_putchar('\n'); // DEBUG
|
|
||||||
ft_putendl("result for humans :"); // DEBUG
|
|
||||||
print_final_map(list, size, 1); // DEBUG
|
|
||||||
ft_putchar('\n'); // DEBUG
|
|
||||||
ft_putendl("result for moulinette :"); // DEBUG
|
|
||||||
|
|
||||||
print_final_map(list, size, 0);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
print_error("usage: Please submit a file.\n> ./fillit file.fillit\n");
|
print_error("usage: Please submit a file.\n> ./fillit file.fillit\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user