list s'imprime dans main

This commit is contained in:
Hugo LAMY
2019-05-17 18:43:33 +02:00
6 changed files with 121 additions and 151 deletions

View File

@@ -6,7 +6,7 @@
# By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/03/01 13:24:35 by vmanzoni #+# #+# #
# Updated: 2019/05/06 16:37:52 by hulamy ### ########.fr #
# Updated: 2019/05/17 17:11:53 by hulamy ### ########.fr #
# #
# **************************************************************************** #
@@ -25,7 +25,7 @@ LDLIBS = -lft
SRCS = $(shell find . -depth 1 -type f -not -name '.*' -not -name 'test*' -name '*.c')
TRASH = $(shell find . -depth 1 -type f -name '*.dSYM')
TRASH = $(shell find . -depth 1 -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')

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */
/* Updated: 2019/05/13 17:56:19 by vmanzoni ### ########.fr */
/* Updated: 2019/05/17 18:37:47 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -53,6 +53,10 @@ typedef struct s_fillist
int width;
int height;
int position;
int place;
int rank;
int num;
int test;
char letter;
uint64_t memory;
struct s_fillist *same;
@@ -66,7 +70,7 @@ typedef struct s_fillist
char *read_file(char *file);
void print_error(char *s);
void print_error_extended(int error);
void parse_input(char *input);
int parse_input(char *input, t_fillist **list);
int check_file_errors(char *file);
int check_tetri_errors(char *tetri);
int check_tetri_errors_proxy(char *tetri);
@@ -74,9 +78,9 @@ int add_to_list(char *square, t_fillist **list, char letter);
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);
int search_map(t_fillist *list);
void print_map(unsigned int *tab, int width, int height, char letter);
void print_final_map(t_fillist *list, int size);
void print_final_map(t_fillist *list, int size, int flag);
void ft_put_tetri_color(char c);
#endif

12
main.c
View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */
/* Updated: 2019/05/09 11:45:36 by vmanzoni ### ########.fr */
/* Updated: 2019/05/17 18:40:34 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,8 +14,11 @@
int main(int argc, char **argv)
{
char *input;
t_fillist *list;
char *input;
int size;
list = NULL;
if (argc == 2)
{
if (!(input = read_file(argv[1])))
@@ -25,7 +28,10 @@ int main(int argc, char **argv)
print_error("error\n");
// print_error("error: Invalid file.\n");
// print_error_extended(check_file_errors(input));
parse_input(input);
size = parse_input(input, &list);
ft_putnbrendl(size);
print_final_map(list, size, 1); // DEBUG
print_final_map(list, size, 0); // DEBUG
}
else
print_error("usage: Please submit a file.\n> ./fillit file.fillit\n");

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */
/* Updated: 2019/05/13 17:13:08 by vmanzoni ### ########.fr */
/* Updated: 2019/05/17 18:40:30 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -90,7 +90,8 @@ void fill_list(char line[], t_fillist *list)
list->height = i;
// fabrique la ligne pour le tetriminos de la bonne largeur
list->tetribit = reduce_tetri(list->tetribit, list->width);
list->position = -1; // DEBUG pour que print_final_map puisse imprimer correctement au fur et a mesure
list->position = 0;
list->test = 0; // DEBUG pour que print_final_map puisse imprimer correctement au fur et a mesure
}
/*
@@ -124,9 +125,8 @@ int add_to_list(char *line, t_fillist **list, char letter)
** Function that parse a file and put each tetrimino in a linked list
*/
void parse_input(char *input)
int parse_input(char *input, t_fillist **list)
{
static t_fillist *list = NULL;
char tetri[20];
int i;
int j;
@@ -144,9 +144,10 @@ void parse_input(char *input)
print_error("error\n");
// print_error("error: Wrong tetrimino.\n");
// print_error_extended(check_tetri_errors(tetri));
add_to_list(tetri, &list, letter++);
add_to_list(tetri, list, letter++);
while (input[i] && input[i] != '.' && input[i] != '#')
i++;
}
search_map(list);
int size = search_map(*list);
return (size);
}

41
print.c
View File

@@ -6,7 +6,7 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/30 13:24:28 by hulamy #+# #+# */
/* Updated: 2019/05/08 19:46:03 by vmanzoni ### ########.fr */
/* Updated: 2019/05/17 17:24:54 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,30 +31,6 @@ 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
@@ -85,9 +61,8 @@ void print_map(unsigned int *tab, int width, int height, char letter)
** Print the final map with the letters
*/
void print_final_map(t_fillist *list, int size)
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
{
// unsigned int print; // DEBUG
t_fillist *tmp;
char *map;
int i;
@@ -107,7 +82,7 @@ void print_final_map(t_fillist *list, int size)
{
if (i && i % tmp->width == 0)
j += size - tmp->width;
if (1 << (15 - i) & tmp->tetribit && tmp->position != -1) // DEBUG "&& tmp->position != -1" pour imprimer les bonnes lettres au coours du debug
if (1 << (15 - i) & tmp->tetribit/* && tmp->test == 1*/) // DEBUG "&& tmp->position != -1" pour imprimer les bonnes lettres au coours du debug
map[tmp->position + i + j - 1] = tmp->letter;
}
tmp = tmp->next;
@@ -117,9 +92,13 @@ void print_final_map(t_fillist *list, int size)
{
if (i && i % size == 0)
ft_putchar('\n');
ft_putchar(map[i]);
// ft_put_tetri_color(map[i]);
// ft_putchar(' ');
if (flag == 0) // DEBUG imprim comme rendu
ft_putchar(map[i]);
else // DEBUG imprim avec couleurs
{
ft_put_tetri_color(map[i]);
ft_putchar(' ');
}
}
ft_putchar('\n');
}

View File

@@ -6,50 +6,72 @@
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */
/* Updated: 2019/05/13 21:46:32 by vmanzoni ### ########.fr */
/* Updated: 2019/05/17 18:31:44 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
/*
** function that look if a tretri fit in a place
*/
unsigned int fit_in_place(unsigned int *map, t_fillist *lst, int size, int i)
{
unsigned int tmp;
unsigned int mask;
unsigned int tetri;
int n;
int r;
n = lst->num;
r = lst->rank;
i = lst->height;
tetri = lst->tetribit << 16 >> lst->width;
tmp = 0;
mask = ~0u << (32 - lst->width);
while (i--)
{
if (tmp & tetri)
return (0);
if (r >= 32 && ++n)
r -= 32;
tmp = (mask & (map[n] << r)) | (mask & (map[n + 1] >> (32 - r)));
tetri <<= lst->width;
r += size;
}
return (!(tmp & tetri));
}
/*
** 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 pos)
int find_place(unsigned int *map, t_fillist *list, int size)
{
int i;
int j;
unsigned int mask;
unsigned int tmp;
int limit;
int pos;
// creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000)
mask = ~0u << (32 - list->width);
tmp = mask;
i = pos;
// 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)
pos = list->position;
list->place = pos % size;
list->rank = pos % 32;
list->num = pos / 32;
limit = (size - list->height + 1) * size;
while (pos < limit)
{
// pour ne pas deborder a droite de la map
if (i % size == size - list->width + 1)
i += list->width - 1;
else
if (list->rank >= 32 && ++list->num)
list->rank -= 32;
if (list->place > size - list->width)
{
tmp = 0;
// construit un tmp qui est une photo de la map de la taille du tetri a un emplacement donne
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++;
list->place = -1;
pos += list->width - 2;
list->rank += list->width - 2;
}
// print_final_map(list, size);
else if (fit_in_place(map, list, size, 0))
return ((list->position = pos + 1));
pos++;
list->place++;
list->rank++;
}
return (0);
}
@@ -58,7 +80,7 @@ int find_place(unsigned int *tab, t_fillist *list, int size, int pos)
** function that add or remove a tetri on the map
*/
void add_remove(unsigned int *map, t_fillist *list, int size, int pos)
void add_remove(unsigned int *map, t_fillist *list, int size)
{
unsigned int mask;
unsigned short tetri;
@@ -68,13 +90,12 @@ void add_remove(unsigned int *map, t_fillist *list, int size, int pos)
tetri = list->tetribit;
mask = ~0u << (32 - list->width);
i = (list->height - 1) * list->width;
j = (list->height - 1) * size + pos;
j = (list->height - 1) * size + list->position;
// change les bits du tetri sur la map a la position donnee
while (j >= pos)
while (j >= list->position)
{
map[(j - 1) / 32] ^= (mask & tetri << (16 + i)) >> (j - 1);
if (j % 32 != 1)
map[(j + size) / 32] ^= (mask & tetri << (16 + i)) << (32 - j + 1);
map[(j - 1) / 32 + 1] ^= (mask & tetri << (16 + i)) << (32 - j) << 1;
j -= size;
i -= list->width;
}
@@ -151,80 +172,34 @@ int check_tetri_memory(t_fillist *list, int pos)
** 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) // DEBUG "link"
int fill_map(unsigned int *map, t_fillist *list, int size)
{
int pos;
pos = 0;
if (!list)
return (1);
// unsigned int tmp = list->tetribit << 16; print_map(&tmp, list->width, list->height, list->letter); // DEBUG
while ((pos = find_place(map, list, size, pos)))
list->position = 0;
while (find_place(map, list, size))
{
list->memory = (list->memory << 1) + 1;
add_remove(map, list, size, pos);
list->position = pos;
if (check_tetri_memory(list, pos))
pos = check_tetri_memory(list, pos);
print_final_map(link, size); // DEBUG
// ft_putnbrendl(pos);
// print_map(map, size, size, '#'); // DEBUG
ft_putchar('\n'); // DEBUG
// if (list->same != NULL && check_tetri_memory)
// {
// list = list->next;
// if (fill_map(map, list->next, size, link))
// return (1);
// }
if (fill_map(map, list->next, size, link))
add_remove(map, list, size);
if (fill_map(map, list->next, size))
return (1);
add_remove(map, list, size, pos);
list->position = -1; // DEBUG
add_remove(map, list, size);
}
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)
int search_map(t_fillist *list)
{
t_fillist *tmp;
unsigned int *map;
int size;
int num;
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);
/////////////////////////////////////////////////// TEST
unsigned int print;
tmp = list;
while (tmp)
@@ -232,24 +207,29 @@ void search_map(t_fillist *list)
// imression pour tests
print = tmp->tetribit;
print <<= 16;
print_map(&print, tmp->width, tmp->height, tmp->letter); // test, imprime le tetri
print_map(&print, tmp->width, tmp->height, tmp->letter);
ft_putchar('\n');
tmp = tmp->next;
}
check_same_tetri(list);
// lance la recursive fill_map en augmentant la taille de la map tant qu'il n'y a pas de solution
while (!fill_map(map, list, size, list))
{
t_fillist *tmp;
/////////////////////////////////////////////////// TEST
tmp = list;
map = init_map(size++);
while (tmp != NULL)
{
tmp->memory = 0;
tmp = tmp->next;
}
size = 2;
num = 1;
tmp = list;
while ((tmp = tmp->next))
num++;
while (size * size < num * 4)
size++;
i = 0;
while (!i)
{
num = (size * size) / 32 + 1;
if (!(map = (unsigned int *)malloc(sizeof(*map) * num)))
return (0);
while (num)
map[num--] = 0;
i = fill_map(map, list, size++);
}
print_final_map(list, size); // DEBUG
// print_map(map, size, size, '#'); // DEBUG
print_map(map, size - 1, size - 1, '#'); // DEBUG
return (--size);
}