Compare commits
116 Commits
add_to_lis
...
clean
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94c0e5d57f | ||
|
|
e5aea30023 | ||
|
|
6705bf9133 | ||
|
|
2e53b04a9a | ||
|
|
af527c6316 | ||
|
|
aa8ec6fe66 | ||
|
|
213eff3255 | ||
|
|
67cb5dea38 | ||
|
|
2738a5e24b | ||
|
|
1ff7abafd6 | ||
|
|
d057ef941b | ||
|
|
23c9e19ea6 | ||
|
|
74ef38e23a | ||
|
|
60d1d87b76 | ||
|
|
e784b96efe | ||
|
|
b85454473d | ||
|
|
6558b77ef2 | ||
|
|
7872fac59e | ||
|
|
c1850f3fd6 | ||
|
|
fdb4c53787 | ||
|
|
1cf791746c | ||
|
|
8d9b314781 | ||
|
|
7d0dab1e1e | ||
|
|
60b970e901 | ||
|
|
671585098f | ||
|
|
bd38cd52ed | ||
|
|
192c2ba0ae | ||
|
|
8a49caad94 | ||
|
|
05e265b3b1 | ||
|
|
929e070fb0 | ||
|
|
f035f37607 | ||
|
|
c8570a26f1 | ||
|
|
37b565f3da | ||
|
|
2291152158 | ||
|
|
527b621526 | ||
|
|
0e6e4a3caa | ||
|
|
c118eac450 | ||
|
|
646d611381 | ||
|
|
c40da94786 | ||
|
|
c50f971f50 | ||
|
|
7b26fc7feb | ||
|
|
5dd491ed52 | ||
|
|
9639bd80e3 | ||
|
|
4d4ec6d8e0 | ||
|
|
7e2ef9e878 | ||
|
|
ef78667474 | ||
|
|
2363e42167 | ||
|
|
ed1cde6e22 | ||
|
|
c7f7ac205c | ||
|
|
0eef1b4825 | ||
|
|
8967fcb464 | ||
|
|
817528c943 | ||
|
|
656973ae52 | ||
|
|
1dd8a9e8c8 | ||
|
|
003d2b3cbb | ||
|
|
4f212d598a | ||
|
|
12fff81bde | ||
|
|
faae791431 | ||
|
|
3e20319691 | ||
|
|
fa281b58ea | ||
|
|
16f98e1a1d | ||
|
|
fc0530a257 | ||
|
|
48576180a2 | ||
|
|
b5ad1209f4 | ||
|
|
213cf40eb4 | ||
|
|
c83ac7599d | ||
|
|
e1f4ebee2a | ||
|
|
4007f0d707 | ||
|
|
51205f5c56 | ||
|
|
ac29587a10 | ||
|
|
23ec23f8bc | ||
|
|
33f74f31fc | ||
|
|
c70803b01b | ||
|
|
2364b6322a | ||
|
|
dabdd7dbef | ||
|
|
2bac429596 | ||
|
|
8da1c5bec6 | ||
|
|
8d9afa579d | ||
|
|
ef84335727 | ||
|
|
c2101d3af9 | ||
|
|
7bf76e5d41 | ||
|
|
39a59058ba | ||
|
|
2c9340c645 | ||
|
|
0aa3026f65 | ||
|
|
f4492e2735 | ||
|
|
21c10a5787 | ||
|
|
acf5aed90e | ||
|
|
9a463f3358 | ||
|
|
952cc5bf22 | ||
|
|
d0de4dcb47 | ||
|
|
b2d291603c | ||
|
|
6d093e66cc | ||
|
|
02159dc587 | ||
|
|
704a47684d | ||
|
|
3f6f6f249a | ||
|
|
b5f8e37699 | ||
|
|
88b126d3f1 | ||
|
|
64464522f0 | ||
|
|
fc69b0242c | ||
|
|
41f5d6b202 | ||
|
|
bcfe54e614 | ||
|
|
1422c8e046 | ||
|
|
61cab1af7f | ||
|
|
730964a817 | ||
|
|
cfa772677c | ||
|
|
729853cea0 | ||
|
|
15bbf591e8 | ||
|
|
851622dc93 | ||
|
|
041aa6c9b5 | ||
|
|
4462517caf | ||
|
|
de86161fcd | ||
|
|
7dfe29b224 | ||
|
|
67ec57fd3d | ||
|
|
f592c3cede | ||
|
|
e9d29e6dc1 | ||
|
|
301d2ba5fb |
Binary file not shown.
8
.gitignore
vendored
8
.gitignore
vendored
@@ -5,3 +5,11 @@ objs/
|
||||
a\.out\.dSYM/
|
||||
|
||||
a\.out
|
||||
|
||||
*.swo
|
||||
|
||||
*.swp
|
||||
|
||||
fillit
|
||||
|
||||
test_fillit\.c
|
||||
|
||||
Binary file not shown.
BIN
.test_mask.c.swp
BIN
.test_mask.c.swp
Binary file not shown.
61
Makefile
61
Makefile
@@ -6,43 +6,50 @@
|
||||
# By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2019/03/01 13:24:35 by vmanzoni #+# #+# #
|
||||
# Updated: 2019/04/16 14:05:39 by hulamy ### ########.fr #
|
||||
# Updated: 2019/05/17 17:11:53 by hulamy ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
NAME = fillit
|
||||
# - - - - - - - - - - - - - - - #
|
||||
# VARIABLES #
|
||||
# - - - - - - - - - - - - - - - #
|
||||
|
||||
OBJ_DIR = ./
|
||||
HEADER = ./
|
||||
NAME = fillit
|
||||
CC = gcc
|
||||
|
||||
SRCS = *.c
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
LIB = fillit.h
|
||||
CFLAGS = -I.
|
||||
CFLAGS += -Wall -Wextra -Werror
|
||||
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Werror -Wextra
|
||||
LDFLAGS = -L./libft/
|
||||
LDLIBS = -lft
|
||||
|
||||
RM = rm -rf
|
||||
SRCS = $(shell find . -depth 1 -type f -not -name '.*' -not -name 'test*' -name '*.c')
|
||||
|
||||
all: $(NAME)
|
||||
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')
|
||||
TRASH += $(shell find . -depth 1 -type f -name '*.swn')
|
||||
|
||||
$(NAME):
|
||||
make -C libft/
|
||||
$(CC) $(CFLAGS) -I$(HEADER) -c $(SRCS)
|
||||
$(CC) -o $(NAME) $(OBJS) -L libft/ -lft
|
||||
#$(CC) $(CFLAGS) -I$(HEADER) -c $(addprefix $(SRC_DIR), $(SRCS))
|
||||
#$(CC) $(OBJS) -o $(NAME)
|
||||
mkdir $(OBJ_DIR)
|
||||
mv $(OBJS) $(OBJ_DIR)
|
||||
# - - - - - - - - - - - - - - - #
|
||||
# RULES #
|
||||
# - - - - - - - - - - - - - - - #
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(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 libft/ clean
|
||||
$(RM) $(OBJ_DIR)
|
||||
/bin/rm -rf $(TRASH)
|
||||
|
||||
fclean: clean
|
||||
make -C libft/ fclean
|
||||
$(RM) $(NAME)
|
||||
fclean: clean
|
||||
/bin/rm -rf $(NAME)
|
||||
|
||||
re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
re: fclean all
|
||||
|
||||
18
README.md
18
README.md
@@ -1,20 +1,22 @@
|
||||
# Fillit
|
||||
|
||||
**hulamy** and **vmanzoni**
|
||||
Team: **hulamy** and **vmanzoni**
|
||||
|
||||
Le but de ce projet est d’agencer les Tetriminos entre eux pour former le plus petit carré possible, sachant que ce carré peut présenter des trous quand les pièces données ne s’emboîtent pas parfaitement.
|
||||
|
||||
## To do
|
||||
|
||||
- [x] Check if we have a file
|
||||
- [x] Read file
|
||||
- [x] Check if there are errors in file
|
||||
- At least 1 tetrimino or less than 26
|
||||
- [x] Check if every tetrimino is valid
|
||||
- 4 char * 4 lines
|
||||
- 4 blocks in 1 tetrimino
|
||||
- No solo block
|
||||
- [x] Transform file into tetriminos
|
||||
- [ ] Backtracking for smallest square
|
||||
- [ ] Transform tetriminos to letters
|
||||
- [ ] Print result
|
||||
- [x] Backtracking for smallest square
|
||||
- [x] Transform tetriminos to letters
|
||||
- [x] Print result (Can be done with above step)
|
||||
- [ ] Free everything (NO LEAKS)
|
||||
|
||||
## BONUS
|
||||
- [x] Best error handler (more details on why there is an error.)
|
||||
- [ ] Optimisation
|
||||
- [ ] Add colors to tetri when printing result map
|
||||
|
||||
152
add_to_list.c
152
add_to_list.c
@@ -1,152 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* add_to_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */
|
||||
/* Updated: 2019/04/18 01:33:00 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fillit.h"
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
** Function that fills the char **tetraminos section of the structure
|
||||
** with the most little rectangle that fit the tetraminos
|
||||
*/
|
||||
|
||||
char **fill_tetraminos(char **square, int *tab)
|
||||
{
|
||||
char **result;
|
||||
int height;
|
||||
int length;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
height = tab[2] - tab[0] + 1;
|
||||
length = tab[3] - tab[1] + 1;
|
||||
if (!(result = (char**)malloc(sizeof(*result) * (height + 1))))
|
||||
return (NULL);
|
||||
while (i < height)
|
||||
{
|
||||
if (!(result[i] = (char*)malloc(sizeof(**result) * (length + 1))))
|
||||
return (NULL);
|
||||
result[i][length] = '\0';
|
||||
i++;
|
||||
}
|
||||
i = -1;
|
||||
while (++i < height && (j = -1))
|
||||
while (++j < length)
|
||||
result[i][j] = square[tab[0] + i][tab[1] + j];
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
** This function calculate the line and columns where the tetraminos
|
||||
** start and end, by skipping the empty lines
|
||||
**
|
||||
** ! it has a little bug so far, i need to fix it
|
||||
*/
|
||||
|
||||
void find_start_and_end(char **square, int **x)
|
||||
{
|
||||
int i;
|
||||
|
||||
x[0][0] = -1;
|
||||
x[0][1] = -1;
|
||||
x[0][2] = 4;
|
||||
x[0][3] = 4;
|
||||
i = 4;
|
||||
while (x[0][0] < 4 && i == 4 && !(i = 0) && square[++(x[0][0])][0] != '#')
|
||||
while (i < 4 && square[*(x[0])][i] != '#')
|
||||
i++;
|
||||
i = 4;
|
||||
while (x[0][1] < 4 && i == 4 && !(i = 0) && square[0][++(x[0][1])] != '#')
|
||||
while (i < 4 && square[i][x[0][1]] != '#')
|
||||
i++;
|
||||
i = -1;
|
||||
while (x[0][2] >= 0 && i == -1 && (i = 3) && square[--(x[0][2])][3] != '#')
|
||||
while (i >= 0 && square[x[0][2]][i] != '#')
|
||||
i--;
|
||||
i = -1;
|
||||
while (x[0][3] >= 0 && i == -1 && (i = 3) && square[3][--(x[0][3])] != '#')
|
||||
while (i >= 0 && square[i][x[0][3]] != '#')
|
||||
i--;
|
||||
}
|
||||
|
||||
/*
|
||||
** this function first call find_start_and_end to find the coordinates
|
||||
** of start en end of the most little rectangle that fit the tetraminos
|
||||
**
|
||||
** it allows it to fill the structure with the size information
|
||||
** (for instance :
|
||||
** "##" ".#" ".#"
|
||||
** is a tatraminos of 2 by 3)
|
||||
** then it fills also the area information (2 * 3 = 6)
|
||||
**
|
||||
** and finally it calls fill_tetraminos to fill the char **tetraminos
|
||||
*/
|
||||
|
||||
int fill_list(char **square, t_fillist *list)
|
||||
{
|
||||
int *tab;
|
||||
|
||||
tab = (int*)malloc(sizeof(int) * 4);
|
||||
find_start_and_end(square, &tab);
|
||||
list->size[0] = tab[3] - tab[1] + 1;
|
||||
list->size[1] = tab[2] - tab[0] + 1;
|
||||
list->area = list->size[0] * list->size[1];
|
||||
list->tetraminos = fill_tetraminos(square, tab);
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
** this function first checks if the structure has been created
|
||||
** if not, it creates the first element, else it adds an element
|
||||
** and modifies the initial pointer to link to the new first element
|
||||
**
|
||||
** then it calls fill_list to fill the data of the structure
|
||||
*/
|
||||
|
||||
int add_to_list(char **square, 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(square, *list);
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
**int main(int ac, char **av)
|
||||
**{
|
||||
** static t_fillist *list = NULL; // avant d'appeller add_to_list il faut declarer un pointeur static vers la structure
|
||||
** int i;
|
||||
**
|
||||
** if (ac > 1)
|
||||
** {
|
||||
** add_to_list(++av, &list); // l'appel de la fonction se fait avec un carre valide de 4*4 et l'adresse du pointeur vers la liste
|
||||
** if (ac == 9)
|
||||
** add_to_list(av += 4, &list);
|
||||
** while (list && (i = -1))
|
||||
** {
|
||||
** while (++i < list->size[1])
|
||||
** printf("%s\n", list->tetraminos[i]);
|
||||
** printf("\n");
|
||||
** list = list->next;
|
||||
** }
|
||||
** }
|
||||
**
|
||||
** return (0);
|
||||
**}
|
||||
*/
|
||||
49
fillit.h
49
fillit.h
@@ -6,7 +6,7 @@
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/01 13:34:46 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/04/15 20:53:57 by vmanzoni ### ########.fr */
|
||||
/* Updated: 2019/05/17 17:03:35 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -16,23 +16,39 @@
|
||||
#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 <stdio.h> // for printf (DELETE BEFORE EVAL)
|
||||
|
||||
#include "libft/includes/libft.h"
|
||||
|
||||
/*
|
||||
** DEFINE
|
||||
*/
|
||||
|
||||
# define BUFF_SIZE 1024
|
||||
|
||||
#define RED "\x1B[31m"
|
||||
#define GRN "\x1B[32m"
|
||||
#define YEL "\x1B[33m"
|
||||
#define BLU "\x1B[34m"
|
||||
#define MAG "\x1B[35m"
|
||||
#define CYN "\x1B[36m"
|
||||
#define RESET "\x1B[0m"
|
||||
|
||||
/*
|
||||
** STRUCTURE
|
||||
*/
|
||||
|
||||
typedef struct s_fillist
|
||||
{
|
||||
int id;
|
||||
char **tetraminos;
|
||||
int position[2];
|
||||
int size[2];
|
||||
int area;
|
||||
unsigned short tetribit;
|
||||
int width;
|
||||
int height;
|
||||
int position;
|
||||
int place;
|
||||
int rank;
|
||||
int num;
|
||||
int test;
|
||||
char letter;
|
||||
struct s_fillist *next;
|
||||
} t_fillist;
|
||||
|
||||
@@ -40,13 +56,20 @@ typedef struct s_fillist
|
||||
** FUNCTIONS
|
||||
*/
|
||||
|
||||
void print_test(char *test); //DELETE BEFORE EVALUATION
|
||||
char *read_file(char *file);
|
||||
void print_error(char *s);
|
||||
void parse_input(char *input);
|
||||
void print_error_extended(int error);
|
||||
void parse_input(char *input);
|
||||
int check_file_errors(char *file);
|
||||
int check_tetri_errors(char *tetri);
|
||||
int check_tetri_errors2(char *tetri);
|
||||
int add_to_list(char **square, t_fillist **list);
|
||||
int check_tetri_errors_proxy(char *tetri);
|
||||
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
|
||||
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 ft_put_tetri_color(char c);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/01 13:29:05 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/04/15 14:41:19 by vmanzoni ### ########.fr */
|
||||
/* Updated: 2019/05/03 19:11:02 by vmanzoni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -22,6 +22,27 @@ void print_error(char *s)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that display error message *s on fd
|
||||
** with more informations
|
||||
** and exit program
|
||||
*/
|
||||
|
||||
void print_error_extended(int error)
|
||||
{
|
||||
if (error == 1)
|
||||
ft_putstr("error: File contains char other than . # \\n found.\n");
|
||||
if (error == 2)
|
||||
ft_putstr("error: File contains more than 2 \\n in a row.\n");
|
||||
if (error == 3)
|
||||
ft_putstr("error: File contains less than 1 tetrimino or more than 26.\n");
|
||||
if (error == 4)
|
||||
ft_putstr("\n\nerror: This tetrimino has more or less than 4 #.\n");
|
||||
if (error == 5)
|
||||
ft_putstr("\n\nerror: This tetrimino # are not well connected.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function to see if there if an error if the file
|
||||
** - less than 4 lines
|
||||
@@ -44,18 +65,18 @@ int check_file_errors(char *file)
|
||||
line_nbr++;
|
||||
if (file[i] == '\n' && file[i+1] != '\0' && \
|
||||
file[i+2] != '.' && file[i+2] != '#')
|
||||
return (1);
|
||||
return (2);
|
||||
i++;
|
||||
}
|
||||
if (line_nbr < 4 || line_nbr > 129)
|
||||
return (1);
|
||||
return (3);
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that check if tetrimino square contains:
|
||||
** - 4 '#'
|
||||
** - 12 '.'
|
||||
** - 4 x #
|
||||
** - 12 x .
|
||||
*/
|
||||
|
||||
int check_tetri_errors(char *tetri)
|
||||
@@ -75,16 +96,16 @@ int check_tetri_errors(char *tetri)
|
||||
dot++;
|
||||
i++;
|
||||
}
|
||||
if (htg != 4 || dot != 12 || check_tetri_errors2(tetri))
|
||||
return (1);
|
||||
if (htg != 4 || dot != 12 || check_tetri_errors_proxy(tetri))
|
||||
return (4 + check_tetri_errors_proxy(tetri));
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that check if tetrimino parts are linked
|
||||
** Function that check if 4 # (tetrimino parts) are linked
|
||||
*/
|
||||
|
||||
int check_tetri_errors2(char *tetri)
|
||||
int check_tetri_errors_proxy(char *tetri)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
||||
1
libft
1
libft
Submodule libft deleted from dad20d5d96
16
main.c
16
main.c
@@ -6,7 +6,7 @@
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/12 13:20:48 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/04/15 20:54:24 by vmanzoni ### ########.fr */
|
||||
/* Updated: 2019/05/03 19:12:47 by vmanzoni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,24 +14,22 @@
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *input;
|
||||
char *input;
|
||||
|
||||
if (argc == 2)
|
||||
{
|
||||
if (!(input = read_file(argv[1])))
|
||||
print_error("Error: Could not read file.\n");
|
||||
print_error("error: Could not read file.\n");
|
||||
if (check_file_errors(input))
|
||||
print_error("Error: Invalid file.\n");
|
||||
print_error_extended(check_file_errors(input));
|
||||
// print_error("error: Invalid file.\n");
|
||||
parse_input(input);
|
||||
/*
|
||||
Transform input to tetriminos
|
||||
Backtracking for smallest square
|
||||
Transform tetriminos with letters
|
||||
Print result
|
||||
Transform tetriminos with letters & Print result
|
||||
*/
|
||||
//print_test(input);
|
||||
}
|
||||
else
|
||||
print_error("Usage: Please submit a file.\n");
|
||||
print_error("usage: Please submit a file.\n> ./fillit file.fillit\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
149
parse_input.c
149
parse_input.c
@@ -6,72 +6,149 @@
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/15 14:48:14 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/04/16 15:28:11 by hulamy ### ########.fr */
|
||||
/* Updated: 2019/05/16 15:30:57 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fillit.h"
|
||||
|
||||
/*
|
||||
** Function that parse a file and put each tetrimino in a linked list
|
||||
** function that transform a tab of . and # into a binary tab of int
|
||||
*/
|
||||
|
||||
char **create_square(char *tetri)
|
||||
unsigned short tab_to_bin(char line[])
|
||||
{
|
||||
char **square;
|
||||
int i;
|
||||
int k;
|
||||
unsigned short tmp;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!(square = (char**)malloc(sizeof(*square) * 4)))
|
||||
return (NULL);
|
||||
while (*tetri && (k = -1))
|
||||
tmp = 0;
|
||||
while (line[i])
|
||||
{
|
||||
if (!(square[i] = (char*)malloc(sizeof(**square) * (4 + 1))))
|
||||
return (NULL);
|
||||
square[i][4] = '\0';
|
||||
while (++k < 4)
|
||||
square[i][k] = *(tetri++);
|
||||
while (*tetri == '\n')
|
||||
tetri++;
|
||||
// printf("%s\n", square[i]);
|
||||
i++;
|
||||
tmp <<= 1;
|
||||
if (line[i] == '\n')
|
||||
i++;
|
||||
if (line[i++] == '#')
|
||||
tmp |= 1;
|
||||
}
|
||||
return (square);
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that take a tetrimino of 4*4
|
||||
** and reduce it to its right size, in binary
|
||||
*/
|
||||
|
||||
unsigned short reduce_tetri(unsigned short tetri, int width)
|
||||
{
|
||||
unsigned int mask;
|
||||
unsigned int tmp;
|
||||
|
||||
// cree un mask avec des 1 a gauche sur la largeur du tetriminos
|
||||
mask = ~0u << (32 - width) >> 16;
|
||||
// fabrique la ligne pour le tetriminos de la bonne largeur
|
||||
tmp = tetri;
|
||||
tmp = (mask & tetri);
|
||||
tmp |= ((mask & tetri << 4) >> width);
|
||||
tmp |= ((mask & tetri << 8) >> (2 * width));
|
||||
tmp |= ((mask & tetri << 12) >> (3 * width));
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that transforme a tetriminos char* into a short of 16 bites
|
||||
** and then fills it and its reversed into the list
|
||||
*/
|
||||
|
||||
void fill_list(char line[], t_fillist *list)
|
||||
{
|
||||
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 (#...#...#...#...)
|
||||
mask = (1 << 15) | (1 << 11) | (1 << 7) | (1 << 3);
|
||||
// utilise le mask pour trouver la largeur que prend le tetriminos
|
||||
i = 0;
|
||||
while (!(mask & list->tetribit) && i++ < 4)
|
||||
mask >>= 1;
|
||||
list->width = i;
|
||||
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)
|
||||
list->tetribit <<= (i - list->width);
|
||||
while (!(list->tetribit & (~0u << 12)))
|
||||
list->tetribit <<= 4;
|
||||
// trouve la hauteur du tetri
|
||||
i = 0;
|
||||
while (i < 4 && list->tetribit & (~0u << 28 >> (i * 4 + 16)))
|
||||
i++;
|
||||
list->height = i;
|
||||
// fabrique la ligne pour le tetriminos de la bonne largeur
|
||||
list->tetribit = reduce_tetri(list->tetribit, list->width);
|
||||
list->position = 0;
|
||||
list->test = 0; // DEBUG pour que print_final_map puisse imprimer correctement au fur et a mesure
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that creates the linked list and add a new structure
|
||||
** linked each time needed
|
||||
*/
|
||||
|
||||
int add_to_list(char *line, t_fillist **list, char letter)
|
||||
{
|
||||
t_fillist *tmp;
|
||||
t_fillist *test;
|
||||
|
||||
if (!(tmp = (t_fillist*)malloc(sizeof(*tmp))))
|
||||
return (0);
|
||||
tmp->next = NULL;
|
||||
test = *list;
|
||||
if (!test)
|
||||
*list = tmp;
|
||||
else
|
||||
{
|
||||
while(test->next)
|
||||
test = test->next;
|
||||
test->next = tmp;
|
||||
}
|
||||
fill_list(line, tmp);
|
||||
tmp->letter = letter;
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that parse a file and put each tetrimino in a linked list
|
||||
*/
|
||||
|
||||
void parse_input(char *input)
|
||||
{
|
||||
static t_fillist *list = NULL;
|
||||
char tetri[20];
|
||||
int i;
|
||||
int j;
|
||||
char **test;
|
||||
int letter;
|
||||
|
||||
i = 0;
|
||||
letter = 'A';
|
||||
while (input[i])
|
||||
{
|
||||
j = 0;
|
||||
while (j < 19)
|
||||
tetri[j++] = input[i++];
|
||||
tetri[19] = '\0';
|
||||
//printf("PRINT:\n%s\n", tetri);
|
||||
if (check_tetri_errors(tetri))
|
||||
print_error("Error: Tetrimino not valid.");
|
||||
test = create_square(tetri);
|
||||
add_to_list(test, &list);
|
||||
while (input[i] == '\n')
|
||||
{
|
||||
ft_putstr(tetri);
|
||||
print_error_extended(check_tetri_errors(tetri));
|
||||
//print_error("\n\nerror: This tetrimino is not valid.\n");
|
||||
}
|
||||
add_to_list(tetri, &list, letter++);
|
||||
while (input[i] && input[i] != '.' && input[i] != '#')
|
||||
i++;
|
||||
}
|
||||
/*
|
||||
** ce qui suit sert juste a afficher le contenu de tous les tetraminos de la liste chainee
|
||||
** pour debug, a effacer au rendu
|
||||
*/
|
||||
while (list && (i = -1))
|
||||
{
|
||||
while (++i < list->size[1])
|
||||
printf("%s\n", list->tetraminos[i]);
|
||||
printf("\n");
|
||||
list = list->next;
|
||||
}
|
||||
search_map(list);
|
||||
}
|
||||
|
||||
124
print.c
Normal file
124
print.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/30 13:24:28 by hulamy #+# #+# */
|
||||
/* Updated: 2019/05/16 15:52:52 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
|
||||
** 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
|
||||
*/
|
||||
|
||||
void print_map(unsigned int *tab, int width, int height, char letter)
|
||||
{
|
||||
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_put_tetri_color(letter) : ft_putchar('.');
|
||||
ft_putchar(' ');
|
||||
i++;
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
/*
|
||||
** Print the final map with the letters
|
||||
*/
|
||||
|
||||
void print_final_map(t_fillist *list, int size)
|
||||
{
|
||||
// unsigned int print; // DEBUG
|
||||
t_fillist *tmp;
|
||||
char *map;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
map = (char *)malloc(sizeof(*map) * (size * size + 1));
|
||||
map[size*size] = '\0';
|
||||
i = -1;
|
||||
while (++i < size * size)
|
||||
map[i] = '.';
|
||||
tmp = list;
|
||||
while (tmp)
|
||||
{
|
||||
j = 0;
|
||||
i = -1;
|
||||
while (++i < tmp->width * tmp->height)
|
||||
{
|
||||
if (i && i % tmp->width == 0)
|
||||
j += size - tmp->width;
|
||||
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;
|
||||
}
|
||||
i = -1;
|
||||
while (++i < size * size)
|
||||
{
|
||||
if (i && i % size == 0)
|
||||
ft_putchar('\n');
|
||||
ft_put_tetri_color(map[i]);
|
||||
ft_putchar(' ');
|
||||
}
|
||||
ft_putchar('\n');
|
||||
}
|
||||
78
print_map_with_colors.c
Normal file
78
print_map_with_colors.c
Normal file
@@ -0,0 +1,78 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* print_map_with_colors.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/03 20:27:22 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/05/04 19:15:35 by vmanzoni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fillit.h"
|
||||
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
void ft_putchar_red(char c)
|
||||
{
|
||||
write(1, RED, 5);
|
||||
ft_putchar(c);
|
||||
write(1, RESET, 5);
|
||||
}
|
||||
|
||||
void ft_putchar_blue(char c)
|
||||
{
|
||||
write(1, BLU, 5);
|
||||
ft_putchar(c);
|
||||
write(1, RESET, 5);
|
||||
}
|
||||
|
||||
void ft_putchar_green(char c)
|
||||
{
|
||||
write(1, GRN, 5);
|
||||
ft_putchar(c);
|
||||
write(1, RESET, 5);
|
||||
}
|
||||
|
||||
void ft_putchar_yellow(char c)
|
||||
{
|
||||
write(1, YEL, 5);
|
||||
ft_putchar(c);
|
||||
write(1, RESET, 5);
|
||||
}
|
||||
|
||||
void ft_putchar_magenta(char c)
|
||||
{
|
||||
write(1, MAG, 5);
|
||||
ft_putchar(c);
|
||||
write(1, RESET, 5);
|
||||
}
|
||||
|
||||
void ft_putchar_cyan(char c)
|
||||
{
|
||||
write(1, CYN, 5);
|
||||
ft_putchar(c);
|
||||
write(1, RESET, 5);
|
||||
}
|
||||
|
||||
void ft_put_tetri_color(char c)
|
||||
{
|
||||
if (c == 'A' || c == 'G' || c == 'M' || c == 'S')
|
||||
ft_putchar_red(c);
|
||||
else if (c == 'B' || c == 'H' || c == 'N' || c == 'T' || c == 'Y')
|
||||
ft_putchar_blue(c);
|
||||
else if (c == 'C' || c == 'I' || c == 'O' || c == 'U' || c == 'Z')
|
||||
ft_putchar_green(c);
|
||||
else if (c == 'D' || c == 'J' || c == 'P' || c == 'V')
|
||||
ft_putchar_yellow(c);
|
||||
else if (c == 'E' || c == 'K' || c == 'Q' || c == 'W')
|
||||
ft_putchar_magenta(c);
|
||||
else if (c == 'F' || c == 'L' || c == 'R' || c == 'X')
|
||||
ft_putchar_cyan(c);
|
||||
else
|
||||
ft_putchar(c);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/13 12:09:46 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/04/15 14:48:36 by vmanzoni ### ########.fr */
|
||||
/* Updated: 2019/04/22 15:16:18 by vmanzoni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -29,12 +29,9 @@ char *read_file(char *file)
|
||||
|| !(result = malloc(sizeof(char) * rv)))
|
||||
return (NULL);
|
||||
buf[rv] = '\0';
|
||||
i = 0;
|
||||
while (buf[i])
|
||||
{
|
||||
i = -1;
|
||||
while (buf[++i])
|
||||
result[i] = buf[i];
|
||||
i++;
|
||||
}
|
||||
result[i] = '\0';
|
||||
close(fd);
|
||||
return (result);
|
||||
|
||||
4
samples/1full
Normal file
4
samples/1full
Normal file
@@ -0,0 +1,4 @@
|
||||
####
|
||||
####
|
||||
####
|
||||
####
|
||||
@@ -1,4 +1,4 @@
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
#...
|
||||
#...
|
||||
#...
|
||||
#...
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
###.
|
||||
.#..
|
||||
....
|
||||
....
|
||||
....
|
||||
####
|
||||
4
samples/1tetri_wrong
Normal file
4
samples/1tetri_wrong
Normal file
@@ -0,0 +1,4 @@
|
||||
#...
|
||||
.#..
|
||||
#...
|
||||
#...
|
||||
9
samples/2tetri
Normal file
9
samples/2tetri
Normal file
@@ -0,0 +1,9 @@
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
|
||||
....
|
||||
##..
|
||||
.##.
|
||||
....
|
||||
14
samples/3tetri
Normal file
14
samples/3tetri
Normal file
@@ -0,0 +1,14 @@
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
|
||||
....
|
||||
##..
|
||||
.##.
|
||||
....
|
||||
|
||||
....
|
||||
###.
|
||||
.#..
|
||||
....
|
||||
14
samples/3tetribis
Normal file
14
samples/3tetribis
Normal file
@@ -0,0 +1,14 @@
|
||||
....
|
||||
##..
|
||||
.##.
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
|
||||
....
|
||||
###.
|
||||
..#.
|
||||
....
|
||||
19
samples/4tetri
Normal file
19
samples/4tetri
Normal file
@@ -0,0 +1,19 @@
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
19
samples/4tetribis
Normal file
19
samples/4tetribis
Normal file
@@ -0,0 +1,19 @@
|
||||
....
|
||||
##..
|
||||
.##.
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
...#
|
||||
|
||||
....
|
||||
###.
|
||||
..#.
|
||||
....
|
||||
|
||||
....
|
||||
.##.
|
||||
.##.
|
||||
....
|
||||
@@ -111,4 +111,4 @@
|
||||
....
|
||||
....
|
||||
####
|
||||
....iskjdslkfsljklfdjk
|
||||
....i
|
||||
|
||||
99
samples/map_hard
Normal file
99
samples/map_hard
Normal file
@@ -0,0 +1,99 @@
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
4
samples/test_bug
Normal file
4
samples/test_bug
Normal file
@@ -0,0 +1,4 @@
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
99
samples/test_ultra_lent
Normal file
99
samples/test_ultra_lent
Normal file
@@ -0,0 +1,99 @@
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
|
||||
#...
|
||||
###.
|
||||
....
|
||||
....
|
||||
|
||||
.#..
|
||||
.#..
|
||||
##..
|
||||
....
|
||||
|
||||
...#
|
||||
...#
|
||||
..##
|
||||
....
|
||||
124
samples/tetri_wrong
Normal file
124
samples/tetri_wrong
Normal file
@@ -0,0 +1,124 @@
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
.##.
|
||||
..#.
|
||||
..#.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
###.
|
||||
.#..
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
.###
|
||||
..#.
|
||||
....
|
||||
|
||||
#...
|
||||
.#..
|
||||
#...
|
||||
#...
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
.##.
|
||||
..#.
|
||||
..#.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
|
||||
....
|
||||
....
|
||||
###.
|
||||
.#..
|
||||
|
||||
....
|
||||
....
|
||||
..##
|
||||
.##.
|
||||
169
search_map.c
Normal file
169
search_map.c
Normal file
@@ -0,0 +1,169 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* search_map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/27 20:47:22 by hulamy #+# #+# */
|
||||
/* Updated: 2019/05/17 17:06:41 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 *map, t_fillist *list, int size)
|
||||
{
|
||||
int limit;
|
||||
int pos;
|
||||
|
||||
pos = list->position;
|
||||
list->place = pos % size;
|
||||
list->rank = pos % 32;
|
||||
list->num = pos / 32;
|
||||
limit = (size - list->height + 1) * size;
|
||||
while (pos < limit)
|
||||
{
|
||||
if (list->rank >= 32 && ++list->num)
|
||||
list->rank -= 32;
|
||||
if (list->place > size - list->width)
|
||||
{
|
||||
list->place = -1;
|
||||
pos += list->width - 2;
|
||||
list->rank += list->width - 2;
|
||||
}
|
||||
else if (fit_in_place(map, list, size, 0))
|
||||
return ((list->position = pos + 1));
|
||||
pos++;
|
||||
list->place++;
|
||||
list->rank++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** function that add or remove a tetri on the map
|
||||
*/
|
||||
|
||||
void add_remove(unsigned int *map, t_fillist *list, int size)
|
||||
{
|
||||
unsigned int mask;
|
||||
unsigned short tetri;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
tetri = list->tetribit;
|
||||
mask = ~0u << (32 - list->width);
|
||||
i = (list->height - 1) * list->width;
|
||||
j = (list->height - 1) * size + list->position;
|
||||
// change les bits du tetri sur la map a la position donnee
|
||||
while (j >= list->position)
|
||||
{
|
||||
map[(j - 1) / 32] ^= (mask & tetri << (16 + i)) >> (j - 1);
|
||||
map[(j - 1) / 32 + 1] ^= (mask & tetri << (16 + i)) << (32 - j) << 1;
|
||||
j -= size;
|
||||
i -= list->width;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** 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);
|
||||
list->position = 0;
|
||||
while (find_place(map, list, size))
|
||||
{
|
||||
add_remove(map, list, size);
|
||||
if (fill_map(map, list->next, size))
|
||||
return (1);
|
||||
add_remove(map, list, size);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
/*
|
||||
** 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)
|
||||
{
|
||||
t_fillist *tmp;
|
||||
unsigned int *map;
|
||||
int size;
|
||||
int num;
|
||||
int i;
|
||||
|
||||
/////////////////////////////////////////////////// TEST
|
||||
unsigned int print;
|
||||
tmp = list;
|
||||
while (tmp)
|
||||
{
|
||||
// imression pour tests
|
||||
print = tmp->tetribit;
|
||||
print <<= 16;
|
||||
print_map(&print, tmp->width, tmp->height, tmp->letter);
|
||||
ft_putchar('\n');
|
||||
tmp = tmp->next;
|
||||
}
|
||||
/////////////////////////////////////////////////// TEST
|
||||
|
||||
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 - 1); // DEBUG
|
||||
print_map(map, size - 1, size - 1, '#'); // DEBUG
|
||||
return (--size);
|
||||
}
|
||||
95
test_add_to_list.c
Normal file
95
test_add_to_list.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* add_to_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/14 15:20:53 by hulamy #+# #+# */
|
||||
/* Updated: 2019/04/30 14:14:32 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fillit.h"
|
||||
//#include <stdio.h>
|
||||
|
||||
/*
|
||||
** DELETE BEFORE EVAL - TEST FUNCTION
|
||||
** Function that prints a 16 bites short
|
||||
*/
|
||||
/*
|
||||
void print_bits(short line)
|
||||
{
|
||||
int mask;
|
||||
|
||||
mask = 1 << 16;
|
||||
while (mask >>= 1)
|
||||
(line & mask) ? write(1, "1", 1) : write(1, "0", 1);
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
** Function that transforme a tetrminos char* into a short of 16 bites
|
||||
** and then fills it and its reversed into the list
|
||||
*/
|
||||
|
||||
void fill_list(char line[], t_fillist *list)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (line[i])
|
||||
{
|
||||
list->tetribit <<= 1;
|
||||
if (line[i] == '\n')
|
||||
i++;
|
||||
if (line[i++] == '#')
|
||||
list->tetribit |= 1;
|
||||
print_bits(list->tetribit, 16);
|
||||
}
|
||||
// print_bits(list->tetribit);
|
||||
}
|
||||
|
||||
/*
|
||||
** Function that 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);
|
||||
}
|
||||
|
||||
/*
|
||||
** DELETE BEFORE EVAL - MAIN FOR TEST
|
||||
*/
|
||||
|
||||
//int main(int ac, char **av)
|
||||
//{
|
||||
// static t_fillist *list = NULL; // avant d'appeller add_to_list il faut declarer un pointeur static vers la structure
|
||||
// int i;
|
||||
//
|
||||
// if (ac > 1)
|
||||
// {
|
||||
// add_to_list(*(++av), &list);
|
||||
// while (list && (i = -1))
|
||||
// {
|
||||
// printf("%d\n", list->tetribit);
|
||||
// print_bits(list->tetribit);
|
||||
// print_bits(list->tibirtet);
|
||||
// list = list->next;
|
||||
// }
|
||||
// }
|
||||
// return (0);
|
||||
//}
|
||||
105
test_big_map.c
Normal file
105
test_big_map.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void print_bits(int octet)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
i = 1 << 31;
|
||||
while (i)
|
||||
{
|
||||
(octet & i) ? printf("1") : printf("0");
|
||||
i >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
void print_short(short octet)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
i = 1 << 15;
|
||||
while (i)
|
||||
{
|
||||
(octet & i) ? printf("1") : printf("0");
|
||||
i >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** cette fonction affiche un mask qui se decale le long d'un tableau d'int
|
||||
** lancer avec gcc test_big_map.c puis ./a.out pour voir
|
||||
*/
|
||||
|
||||
void test(unsigned int map[])
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int k;
|
||||
int l;
|
||||
unsigned int mask;
|
||||
short tetri;
|
||||
|
||||
tetri = (1 << 4) | (1 << 5) | (1 << 6);
|
||||
mask = map[9];
|
||||
i = -1;
|
||||
j = 9;
|
||||
while (j >= 0)
|
||||
{
|
||||
// a chaque fois que mask s'est decale jusqu'au int precedant du tableau, le if suivant reaffiche le tableau en une ligne
|
||||
if (!(++i % 32) && j-- && (k = -1))
|
||||
{
|
||||
while (++k < 10)
|
||||
print_bits(map[k]);
|
||||
printf("\n");
|
||||
}
|
||||
// imprime le bon nombre d'espaces pour que le mask s'imprime pile sous le tableau d'int a la position ou il en est
|
||||
l = 0;
|
||||
while (l++ < 320 - 32 - i)
|
||||
printf(" ");
|
||||
// imprime le mask
|
||||
print_bits(mask);
|
||||
printf("\n");
|
||||
// compare le mask (int) avec le short et l'imprime s'ils sont compatibles
|
||||
if (!(tetri & mask))
|
||||
{
|
||||
l = 0;
|
||||
printf(">>>>>");
|
||||
while (l++ < 320 - 32 + 16 - i - 5)
|
||||
printf(" ");
|
||||
print_short(tetri);
|
||||
printf("\n");
|
||||
}
|
||||
/*
|
||||
else
|
||||
{
|
||||
l = 0;
|
||||
while (l++ < 320 - 32 + 16 - i)
|
||||
printf(" ");
|
||||
print_short(tetri);
|
||||
printf("\n");
|
||||
}
|
||||
l = 0;
|
||||
while (l++ < 320 - 32 + 16 - i)
|
||||
printf(" ");
|
||||
print_short(tetri & mask);
|
||||
printf("\n");
|
||||
*/
|
||||
// fait tourner le mask le long du tableau d'int
|
||||
// (mask >> 1) decale le mask vers la droite (perd le bit le plus a droite et insere un bit a gauche)
|
||||
// (1 << (i % 32)) creer un mask avec un 1 a la premiere place a droite, puis a la deuxieme, puis la troisieme, etc.. jusqu'a 32, et recommence
|
||||
// (0000000000000010000000 & map[j]) recupere la valeur du bit de map[j] (qui est la map precedante sur le tableau d'int) a la position du 1
|
||||
// (0000000000000010000000 << (31 - (i % 32))) = 100000000000000000000
|
||||
// (mask >> 1) | 1000000000000000000000 = insere le 32eme bit dans mask
|
||||
mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32)));
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
unsigned int map[10] = {1568713153, 817645681, 654186132, 538171355, 1718453135, 551286515, 1631843343, 3413834313, 1155555555, 999999999};
|
||||
int i;
|
||||
|
||||
//while (i < 10)
|
||||
// printf("%d\n", map[i++]);
|
||||
test(map);
|
||||
return (0);
|
||||
}
|
||||
125
test_big_tetri.c
Normal file
125
test_big_tetri.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include "./libft/includes/libft.h"
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void print_map(unsigned int *tab, int width, int height)
|
||||
{
|
||||
int i;
|
||||
unsigned int mask;
|
||||
|
||||
i = 0;
|
||||
// creer un mask avec les size bits de gauche a 1 (ex: 11111110000000000000000000000000)
|
||||
mask = ~0u << (32 - width);
|
||||
while (i < width * height)
|
||||
{
|
||||
if (!(i % width))
|
||||
ft_putchar('\n');
|
||||
tab[i / 32] & (1 << (31 - i % 32)) ? ft_putchar('#') : ft_putchar('.');
|
||||
ft_putchar(' ');
|
||||
i++;
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
void find_place(unsigned int *tab, char *line, int size)
|
||||
{
|
||||
unsigned short tetri;
|
||||
int i;
|
||||
int j;
|
||||
unsigned int mask;
|
||||
unsigned int tmp;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
/////////////// create tetri ///////////////
|
||||
i = 0;
|
||||
tmp = 0;
|
||||
width = 3;
|
||||
height = 2;
|
||||
while (line[i])
|
||||
{
|
||||
tetri <<= 1;
|
||||
if (line[i] == '\n')
|
||||
i++;
|
||||
if (line[i++] == '#')
|
||||
tetri |= 1;
|
||||
}
|
||||
while (!(tetri & (1 << 15)))
|
||||
tetri <<= 1;
|
||||
print_bits(tetri, 16);
|
||||
tmp = (tetri | tmp) << 16;
|
||||
print_map(&tmp, width, height);
|
||||
/////////////// create tetri ///////////////
|
||||
|
||||
mask = ~0;
|
||||
mask <<= 32 - width;
|
||||
ft_putendl("mask: ");
|
||||
print_bits(mask, 32);
|
||||
i = 0;
|
||||
while (i < (size - height + 1) * size)
|
||||
{
|
||||
tmp = 0;
|
||||
j = height * size + i;
|
||||
while (j >= i)
|
||||
{
|
||||
tmp >>= width;
|
||||
tmp |= (mask & (tab[j / 32] << j));
|
||||
tmp |= (mask & (tab[(j + size) / 32] >> (32 - j)));
|
||||
j -= size;
|
||||
}
|
||||
print_map(&tmp, width, height);
|
||||
print_bits(tmp >> 16, 32);
|
||||
print_bits(tetri, 32);
|
||||
print_bits((tmp >> 16) & tetri, 32);
|
||||
if (i % size == size - width)
|
||||
i += width - 1;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
unsigned int tab[8];
|
||||
|
||||
// ce tableau permet de monter jusqu'a une map de 16*16
|
||||
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_bits(tab[0], 32);
|
||||
print_bits(tab[1], 32);
|
||||
print_bits(tab[2], 32);
|
||||
print_bits(tab[3], 32);
|
||||
print_bits(tab[4], 32);
|
||||
print_bits(tab[5], 32);
|
||||
print_bits(tab[6], 32);
|
||||
print_bits(tab[7], 32);
|
||||
if (ac > 1)
|
||||
{
|
||||
++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, 10);
|
||||
write(1, "\n", 1);
|
||||
if (av[0][0] == 't' && ac > 2)
|
||||
find_place(tab, av[1], 10);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
@@ -6,15 +6,30 @@
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/12 22:29:45 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/04/14 21:37:19 by vmanzoni ### ########.fr */
|
||||
/* Updated: 2019/04/27 15:04:46 by vmanzoni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fillit.h"
|
||||
|
||||
/*
|
||||
char *get_smallest_square()
|
||||
{
|
||||
|
||||
}
|
||||
** Function to bruteforce with backtracking for smallest square
|
||||
*/
|
||||
|
||||
int *get_smallest_square(t_fillist list, int size, unsigned int map[])
|
||||
{
|
||||
unsigned int mask;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
mask = map[size];
|
||||
while (list.tetribit != NULL)
|
||||
{
|
||||
mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32)));
|
||||
if (!(tetri & mask))
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
50
test_mask.c
50
test_mask.c
@@ -40,16 +40,16 @@ void compare(int initial, int compare)
|
||||
!(initial & compare) ? ft_putendl("&: fit") : ft_putendl("&: not fit");
|
||||
|
||||
/*
|
||||
// j = 1 << 12;
|
||||
// while (j >>= 1)
|
||||
// (initial | compare) & j ? ft_putnbr(1) : ft_putnbr(0);
|
||||
// ft_putchar('\n');
|
||||
// (initial | compare) ? ft_putendl("|: fit") : ft_putendl("|: not fit");
|
||||
// j = 1 << 12;
|
||||
// while (j >>= 1)
|
||||
// (initial ^ compare) & j ? ft_putnbr(1) : ft_putnbr(0);
|
||||
// ft_putchar('\n');
|
||||
// (initial ^ compare) ? ft_putendl("^: fit") : ft_putendl("^: not fit");
|
||||
j = 1 << 12;
|
||||
while (j >>= 1)
|
||||
(initial | compare) & j ? ft_putnbr(1) : ft_putnbr(0);
|
||||
ft_putchar('\n');
|
||||
(initial | compare) ? ft_putendl("|: fit") : ft_putendl("|: not fit");
|
||||
j = 1 << 12;
|
||||
while (j >>= 1)
|
||||
(initial ^ compare) & j ? ft_putnbr(1) : ft_putnbr(0);
|
||||
ft_putchar('\n');
|
||||
(initial ^ compare) ? ft_putendl("^: fit") : ft_putendl("^: not fit");
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -74,19 +74,19 @@ int main(int ac, char **av)
|
||||
}
|
||||
|
||||
/*
|
||||
**int main(int ac, char **av)
|
||||
**{
|
||||
** int init;
|
||||
** int mask;
|
||||
**
|
||||
** mask = (1 << 4);
|
||||
** if (ac == 4)
|
||||
** {
|
||||
** ft_putendl(ft_convertbase(av[1], av[2], av[3]));
|
||||
** ft_putnbrendl(init);
|
||||
** init |= mask;
|
||||
** ft_putnbrendl(init);
|
||||
** }
|
||||
** return (0);
|
||||
**}
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
int init;
|
||||
int mask;
|
||||
|
||||
mask = (1 << 4);
|
||||
if (ac == 4)
|
||||
{
|
||||
ft_putendl(ft_convertbase(av[1], av[2], av[3]));
|
||||
ft_putnbrendl(init);
|
||||
init |= mask;
|
||||
ft_putnbrendl(init);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
*/
|
||||
|
||||
266
test_othermethod_fillit.c
Normal file
266
test_othermethod_fillit.c
Normal file
@@ -0,0 +1,266 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test_othermethod_fillit.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/01 15:25:46 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/05/02 17:08:26 by vmanzoni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/includes/libft.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct s_fillist
|
||||
{
|
||||
unsigned int tetribit;
|
||||
int position;
|
||||
struct s_fillist *next;
|
||||
} t_fillist;
|
||||
|
||||
/* *****************************************************************************
|
||||
** TEST FUNCTION
|
||||
***************************************************************************** */
|
||||
|
||||
void print_bits(char *str, unsigned int bits, int size)
|
||||
{
|
||||
unsigned int mask;
|
||||
|
||||
ft_putstr(str);
|
||||
mask = 1 << (size - 1);
|
||||
while (mask)
|
||||
{
|
||||
(bits & mask) ? write(1, "1", 1) : write(1, "0", 1);
|
||||
mask >>= 1;
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
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);
|
||||
mask >>= 1;
|
||||
i++;
|
||||
}
|
||||
write(1, "\n", 1);
|
||||
}
|
||||
|
||||
/* *****************************************************************************
|
||||
** PROGRAM
|
||||
***************************************************************************** */
|
||||
|
||||
void error(char *str)
|
||||
{
|
||||
ft_putstr(str);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
char *read_file(char *file)
|
||||
{
|
||||
char buf[1000];
|
||||
int fd;
|
||||
int rv;
|
||||
int i;
|
||||
char *result;
|
||||
|
||||
if (((fd = open(file, O_RDONLY)) < 0) \
|
||||
|| ((rv = read(fd, &buf, 130)) < 0) \
|
||||
|| !(result = malloc(sizeof(char) * rv)))
|
||||
return (NULL);
|
||||
buf[rv] = '\0';
|
||||
i = -1;
|
||||
while (buf[++i])
|
||||
result[i] = buf[i];
|
||||
close(fd);
|
||||
return (result);
|
||||
}
|
||||
|
||||
unsigned int tetribit_resize(unsigned int tetribit,
|
||||
unsigned int from_size,
|
||||
unsigned int to_size)
|
||||
{
|
||||
unsigned int mask;
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
unsigned int ret;
|
||||
unsigned int new_tet;
|
||||
|
||||
i = from_size;
|
||||
mask = 0;
|
||||
while (i--)
|
||||
mask |= (1 << i) + 1; //Cree un mask de from_size en bit
|
||||
// print_bits("tet: ", tetribit, 32);
|
||||
// print_bits("mak: ", mask, 32);
|
||||
new_tet = tetribit & mask; //initialise new_tet avec la ligne du bas de tetribit
|
||||
// print_bits("new: ", new_tet, 32);
|
||||
// ft_putstr("while\n");
|
||||
j = 0;
|
||||
while (j < from_size - 1) //pour chaque ligne du tetribit
|
||||
{
|
||||
i = 0;
|
||||
mask = 0;
|
||||
while (i != from_size) //on fait un mask sur chaque ligne
|
||||
mask |= 1 << (from_size + (from_size * j) + i++);
|
||||
// print_bits("tet: ", tetribit, 32);
|
||||
// print_bits("mak: ", mask, 32);
|
||||
ret = (tetribit & mask);
|
||||
// print_bits("rbf: ",ret, 32);
|
||||
ret = ret << (to_size - from_size + (j * (to_size - from_size)));
|
||||
// print_bits("raf: ",ret, 32);
|
||||
new_tet |= ret;
|
||||
// print_bits("new: ",new_tet, 32);
|
||||
j++;
|
||||
}
|
||||
return (new_tet);
|
||||
}
|
||||
|
||||
unsigned int tetri_to_binary(char line[])
|
||||
{
|
||||
unsigned int tmp;
|
||||
int i;
|
||||
// unsigned int mask;
|
||||
|
||||
i = 0;
|
||||
tmp = 0;
|
||||
while (line[i])
|
||||
{
|
||||
tmp <<= 1;
|
||||
if (line[i] == '\n')
|
||||
i++;
|
||||
if (line[i++] == '#')
|
||||
tmp |= 1;
|
||||
}
|
||||
// mask = (1 << 3 | 1 << 2 | 1 << 1) + 1;
|
||||
// while ((tmp & mask) < 8) //place le tetri le plus en bas a droite possible sans le casser
|
||||
// tmp /= 2;
|
||||
// print_bits("before\n", tmp, 32);
|
||||
// print_bits("after\n", tetribit_resize(tmp, 4, 5), 32);
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
int add_to_list(char *line, t_fillist **list)
|
||||
{
|
||||
t_fillist *tmp;
|
||||
t_fillist *test;
|
||||
|
||||
if (!(tmp = (t_fillist*)malloc(sizeof(*tmp))))
|
||||
return (0);
|
||||
tmp->next = NULL;
|
||||
test = *list;
|
||||
if (!test)
|
||||
*list = tmp;
|
||||
else
|
||||
{
|
||||
while(test->next)
|
||||
test = test->next;
|
||||
test->next = tmp;
|
||||
}
|
||||
tmp->tetribit = tetri_to_binary(line);
|
||||
return (1);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int find_place(unsigned int *map, t_fillist *list)
|
||||
{
|
||||
ft_putstr(">>>\n");
|
||||
print_bits("map: ", *map, 32);
|
||||
print_bits("lst: ", list->tetribit, 32);
|
||||
print_bits("m&l: ", (*map & (short)(list->tetribit)), 32);
|
||||
while ((*map & (int)(list->tetribit)) == 0
|
||||
&& ((int)(list->tetribit) & (1 << 31)))
|
||||
{
|
||||
list->tetribit = list->tetribit << 1;
|
||||
print_bits("lst: ", list->tetribit, 32);
|
||||
ft_putnbr(list->tetribit);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int backtracking(t_fillist *list)
|
||||
{
|
||||
unsigned int *map;
|
||||
|
||||
map = init_map(1);
|
||||
if (!list)
|
||||
return (0);
|
||||
// while (list)
|
||||
// {
|
||||
// print_bits("test: ", list->tetribit, 16);
|
||||
// list = list->next;
|
||||
// }
|
||||
find_place(map, list);
|
||||
// list = list->next;
|
||||
// find_place(map, list);
|
||||
/*
|
||||
while (find_place(map, list))
|
||||
{
|
||||
add_remove(map, list, size, list->position);
|
||||
if (fill_map(map, list->next))
|
||||
return (1);
|
||||
add_remove(map, list, size, list->position);
|
||||
list->position++;
|
||||
}
|
||||
*/
|
||||
return (0);
|
||||
// mask = (mask >> 1) | (((1 << (i % 32)) & map[j]) << (31 - (i % 32)));
|
||||
}
|
||||
|
||||
void parse_file(char *file, t_fillist *list)
|
||||
{
|
||||
char tetri[20];
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (file[i])
|
||||
{
|
||||
if (file[i] != '.' && file[i] != '#' && file[i] != '\n')
|
||||
error("error.\n");
|
||||
j = 0;
|
||||
while (j < 19)
|
||||
tetri[j++] = file[i++];
|
||||
tetri[19] = '\0';
|
||||
add_to_list(tetri, &list);
|
||||
while (file[i] && file[i] != '.' && file[i] != '#')
|
||||
i++;
|
||||
}
|
||||
backtracking(list);
|
||||
}
|
||||
|
||||
int solve(char *file)
|
||||
{
|
||||
static t_fillist *list = NULL;
|
||||
|
||||
parse_file(read_file(file), list);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
if (ac == 2)
|
||||
solve(av[1]);
|
||||
else
|
||||
error("usage: ./fillit [file]\n");
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: vmanzoni <vmanzoni@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/01 13:35:48 by vmanzoni #+# #+# */
|
||||
/* Updated: 2019/04/15 00:11:22 by hulamy ### ########.fr */
|
||||
/* Updated: 2019/04/16 16:41:46 by vmanzoni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -18,8 +18,3 @@ void ft_print_fillit()
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
void print_test(char *test)
|
||||
{
|
||||
write(1, test, strlen(test));
|
||||
}
|
||||
Reference in New Issue
Block a user