adding a print_tetri function in print.c for testing

This commit is contained in:
Manzovince
2019-04-30 19:51:28 +02:00
parent 5dd491ed52
commit 7b26fc7feb
4 changed files with 32 additions and 286 deletions

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@@ -6,7 +6,7 @@
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}

29
print.c
View File

@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@@ -1,272 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_fillit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/22 16:05:59 by vmanzoni #+# #+# */
/* Updated: 2019/04/23 13:57:22 by vmanzoni ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <unistd.h> // for system call write
#include <fcntl.h> // for system call open
#include <string.h> // for memmove and strlen
#include <stdio.h> // for debug printf
#include <stdbool.h> // 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);
}