Compare commits

..

No commits in common. "fb658728480a7fbaf1bb1e3af3bf6de0c72874e1" and "7cc54100016a0ed468f167164cc459a1db14a8c9" have entirely different histories.

23 changed files with 462 additions and 661 deletions

10
.gitignore vendored
View file

@ -35,9 +35,11 @@
# Vim temp
*.swp
<<<<<<< HEAD
CMakeLists.txt
.idea/
**/cmake-build-debug/*
=======
# Sublime files
*.sublime*
# Clion files
.idea
cmake-build-debug
>>>>>>> github/develop

View file

@ -1,92 +0,0 @@
cmake_minimum_required(VERSION 3.5.2)
project(fillit)
set(CMAKE_CXX_STANDARD 11)
include_directories(inc)
include_directories(libft)
set(SOURCE_FILES
libft/ft_atoi.c
libft/ft_bzero.c
libft/ft_isalnum.c
libft/ft_isalpha.c
libft/ft_isascii.c
libft/ft_isdigit.c
libft/ft_isprint.c
libft/ft_itoa.c
libft/ft_lst_at.c
libft/ft_lstadd.c
libft/ft_lstadd_back.c
libft/ft_lstdel.c
libft/ft_lstdelone.c
libft/ft_lstfind.c
libft/ft_lstiter.c
libft/ft_lstmap.c
libft/ft_lstnew.c
libft/ft_lststrsplit.c
libft/ft_memalloc.c
libft/ft_memccpy.c
libft/ft_memchr.c
libft/ft_memcmp.c
libft/ft_memcpy.c
libft/ft_memdel.c
libft/ft_memmove.c
libft/ft_memset.c
libft/ft_putchar.c
libft/ft_putchar_fd.c
libft/ft_putendl.c
libft/ft_putendl_fd.c
libft/ft_putnbr.c
libft/ft_putnbr_fd.c
libft/ft_putstr.c
libft/ft_putstr_fd.c
libft/ft_realloc.c
libft/ft_strcat.c
libft/ft_strchr.c
libft/ft_strclr.c
libft/ft_strcmp.c
libft/ft_strcpy.c
libft/ft_strdel.c
libft/ft_strdup.c
libft/ft_strequ.c
libft/ft_striter.c
libft/ft_striteri.c
libft/ft_strjoin.c
libft/ft_strlcat.c
libft/ft_strlen.c
libft/ft_strmap.c
libft/ft_strmapi.c
libft/ft_strncat.c
libft/ft_strncmp.c
libft/ft_strncpy.c
libft/ft_strnequ.c
libft/ft_strnew.c
libft/ft_strnstr.c
libft/ft_strrchr.c
libft/ft_strsplit.c
libft/ft_strstr.c
libft/ft_strsub.c
libft/ft_strtrim.c
libft/ft_tolower.c
libft/ft_toupper.c
libft/ft_lstpop.c
libft/libft.h
src/check_raw_string.c
src/dancing_links.c
src/fillit.c
src/get_types.c
src/glue_figure.c
src/main.c
src/move_up_left.c
src/read_file.c
src/string_to_table.c
src/test!_check.c
src/test!_print_one_string.c
src/test!_print_table.c
src/to_letters.c
src/get_amount.c
tests/sample.fillit
tests/sample1.fillit)
add_executable(fillit ${SOURCE_FILES})

View file

@ -6,7 +6,7 @@
# By: gtertysh <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/12/14 16:54:48 by gtertysh #+# #+# #
# Updated: 2016/12/22 18:18:36 by gtertysh ### ########.fr #
# Updated: 2016/12/20 20:05:58 by ykolomie ### ########.fr #
# #
# **************************************************************************** #
@ -17,15 +17,14 @@ SRCDIR = ./src/
OBJDIR = ./obj/
SRC_FILES = main.c \
fillit.c \
glue_figure.c \
move_up_left.c \
read_file.c \
string_to_table.c \
to_strct_array.c \
to_letters.c \
build_matrix.c \
dancing_links.c \
get_types.c \
get_amount.c \
test!_check.c \
test!_print_one_string.c \
test!_print_table.c \
@ -37,7 +36,7 @@ SRC = $(addprefix $(SRCDIR), $(SRC_FILES))
OBJ = $(addprefix $(OBJDIR), $(OBJ_FILES))
INC = -I ./inc -I $(LIBFOLDER)
INC = -I ./inc
LIB = libft.a
@ -45,14 +44,16 @@ LIBFLAGS = -lft -L $(LIBFOLDER)
LIBFOLDER = ./libft/
FLAGS = -Werror -Wextra -Wall -O2
FLAGS = -Werror -Wextra -Wall
STACK = -Wl,-stack_size -Wl,0x10000000000
CC = clang
all: $(NAME)
$(NAME): $(OBJ) $(LIBFOLDER)$(LIB)
$(CC) $(FLAGS) $(OBJ) $(LIBFLAGS) -o $(NAME)
$(CC) $(FLAGS) $(STACK) $(OBJ) $(LIBFLAGS) -o $(NAME)
$(OBJDIR)%.o : $(SRCDIR)%.c
$(CC) $(FLAGS) $(INC) -c $< -o $@

View file

@ -21,18 +21,19 @@
#include <stdio.h> // warning!
# define BUF_S 8192
# define HOW_MUCH 10000
typedef struct s_coord
{
int y;
int x;
int y;
} t_coord;
typedef struct s_ttrmn
{
char t[17];
int width;
int height;
int x;
int y;
t_coord c[4];
} t_ttrmn;
@ -42,13 +43,10 @@ typedef struct s_node
struct s_node *right;
struct s_node *up;
struct s_node *down;
int row;
struct s_node *column;
t_coord coord;
int type;
int size;
} t_node;
} t_node;
extern t_ttrmn g_templates[19];
// reads from file
char *read_file(char *path);
@ -75,40 +73,14 @@ void test_check(char **ttr, t_ttrmn *tamplates);
// obsolete, rewrite for structs array
void to_letters(char **ttr);
t_node *init_root(int size);
// create array of ttr structures
int *to_strct_array(char **ttr, t_ttrmn *templates);
t_node *add_column(t_node *root);
t_node *add_node(t_node *col, int row);
void cover(t_node *to_cover);
void uncover(t_node *to_uncover);
t_node **add_cols(t_node *root, int number);
t_node *add_node(t_node *col);
t_node *add_node_with_coord(t_node *col, t_coord row, int type);
t_node **add_cols(t_node *root, int number);
void print_solution(t_node **sol, int amount, int size);
void add_rows(int *types, int amount, int size, t_node **cols_arr);
void link_row(int *col_nums, t_node **cols_arr);
void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type);
void search(t_node* root, t_node **solution, int k, int amount);
int cover(t_node *to_cover);
int uncover(t_node *to_uncover);
int *get_types(char **ttr);
int get_amount(char **ttr);
unsigned int ft_sqrt_ceil(unsigned int num);
void fill_map(char *map, int size, t_node *ttr, int letter);
void init(char **argv);
#endif

97
inc/libft.h Normal file
View file

@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* libft.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 12:21:43 by gtertysh #+# #+# */
/* Updated: 2016/12/06 20:08:35 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef LIBFT_H
# define LIBFT_H
# include <string.h>
# include <stdlib.h>
# include <unistd.h>
typedef struct s_list
{
void *content;
size_t content_size;
struct s_list *next;
} t_list;
void *ft_memset(void *b, int c, size_t len);
void ft_bzero(void *s, size_t n);
void *ft_memcpy(void *dst, const void *src, size_t len);
void *ft_memccpy(void *dst, const void *src, int c, size_t n);
void *ft_memmove(void *dst, const void *src, size_t len);
void *ft_memchr(const void *s, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
size_t ft_strlen(const char *s);
char *ft_strdup(const char *s1);
char *ft_strcpy(char *dst, const char *src);
char *ft_strncpy(char *dst, const char *src, size_t len);
char *ft_strcat(char *s1, const char *s2);
char *ft_strncat(char *s1, const char *s2, size_t n);
size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strchr(const char *s, int c);
char *ft_strrchr(const char *s, int c);
char *ft_strstr(const char *big, const char *little);
char *ft_strnstr(const char *big, const char *little,
size_t len);
int ft_strcmp(const char *s1, const char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_atoi(const char *str);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isalpha(int c);
int ft_isalnum(int c);
int ft_isascii(int c);
int ft_isprint(int c);
int ft_tolower(int c);
int ft_toupper(int c);
void *ft_memalloc(size_t size);
void ft_memdel(void **ap);
char *ft_strnew(size_t size);
void ft_strdel(char **as);
void ft_strclr(char *s);
void ft_striter(char *s, void (*f)(char *));
void ft_striteri(char *s, void (*f)(unsigned int, char *));
char *ft_strmap(char const *s, char (*f)(char));
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
int ft_strequ(char const *s1, char const *s2);
int ft_strnequ(char const *s1, char const *s2, size_t n);
char *ft_strsub(char const *s, unsigned int start, size_t len);
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s);
char **ft_strsplit(char const *s, char c);
char *ft_itoa(int c);
void ft_putchar(char c);
void ft_putstr(char const *s);
void ft_putendl(char const *s);
void ft_putnbr(int n);
void ft_putchar_fd(char c, int fd);
void ft_putstr_fd(char const *s, int fd);
void ft_putendl_fd(char const *s, int fd);
void ft_putnbr_fd(int n, int fd);
t_list *ft_lstnew(void const *content, size_t content_size);
void ft_lstdelone(t_list **alst, void (*del)(void *, size_t));
void ft_lstdel(t_list **alst, void (*del)(void *, size_t));
void ft_lstadd(t_list **alst, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
t_list *ft_lststrsplit(const char *s, char c);
t_list *ft_lstfind(t_list *lst, void *data, size_t size);
t_list *ft_lst_at(t_list *l, unsigned int at);
void ft_lstadd_back(t_list **l, void *data, size_t size);
void *ft_realloc(void *old, unsigned int new_size,
unsigned int old_size);
#endif

View file

@ -76,7 +76,6 @@ SRC = ft_memset.c \
ft_lstfind.c \
ft_lst_at.c \
ft_lstadd_back.c \
ft_lstpop.c \
\
ft_realloc.c

View file

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstpop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gtertysh <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/04 18:30:18 by gtertysh #+# #+# */
/* Updated: 2016/12/06 19:46:45 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstpop(t_list **alst)
{
t_list *first;
first = NULL;
if (alst)
{
first = *alst;
*alst = (*alst)->next;
}
return (first);
}

View file

@ -86,7 +86,6 @@ void ft_lstdel(t_list **alst, void (*del)(void *, size_t));
void ft_lstadd(t_list **alst, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(t_list *elem));
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem));
t_list *ft_lstpop(t_list **alst);
t_list *ft_lststrsplit(const char *s, char c);
t_list *ft_lstfind(t_list *lst, void *data, size_t size);

106
src/build_matrix.c Normal file
View file

@ -0,0 +1,106 @@
#include "fillit.h"
unsigned int ft_sqrt_ceil(unsigned int num)
{
unsigned int i;
i = 1;
while (i * i < num)
i++;
return (i);
}
void put_figure(char **char_matrix, t_ttrmn t, int y, int x)
{
int i;
int j;
int t_x;
int runner;
t_x = x;
i = 0;
runner = 0;
while (i < t.y)
{
x = t_x;
j = 0;
while (j < t.x)
{
char_matrix[y][x] = t.t[runner];
j++;
x++;
runner++;
}
}
}
void print_char_matrix(char **matrix, int size)
{
int y;
y = 0;
while (y < size)
{
printf("%s\n", matrix[y]);
y++;
}
}
int **build_matrix(t_ttrmn **ttr)
{
int **matrix;
char **char_matrix;
int i;
int j;
int amount;
int size;
int k;
int curr_line;
amount = 0;
i = 0;
matrix = malloc(sizeof(int*) * HOW_MUCH);
while (i < HOW_MUCH)
{
j = 0;
matrix[i] = malloc(sizeof(int) * HOW_MUCH);
while (j < HOW_MUCH)
matrix[i][j++] = -1;
i++;
}
while(ttr[amount])
amount++;
size = ft_sqrt_ceil(amount * 4);
if (size < 4)
size = 4;
char_matrix = malloc(sizeof(char *) * size + 1);
i = 0;
while (i < size)
{
char_matrix[i] = malloc(sizeof(char) * size + 1);
char_matrix[i][size] = 0;
i++;
}
char_matrix[size] = 0;
i = 0;
curr_line = 0;
while (i < amount)
{
k = 0;
while (k < size - ttr[i]->y)
{
j = 0;
while (j < size - ttr[i]->x)
{
put_figure(char_matrix, *ttr[i], k, j);
//print_char_matrix(char_matrix, size);
//count_cover(matrix, char_matrix, curr_line++);
//clear_char_matrix(&char_matrix, size);
j++;
}
k++;
}
i++;
}
return (matrix);
}

View file

@ -10,19 +10,7 @@
/* */
/* ************************************************************************** */
#include "../inc/fillit.h"
#include <stdlib.h>
t_node *init_root(int size)
{
t_node *new;
new = (t_node*)malloc(sizeof(t_node));
new->left = new->right = new->down = new->up = new;
new->column = NULL;
new->size = size;
return (new);
}
#include "fillit.h"
t_node *add_column(t_node *root)
{
@ -33,20 +21,17 @@ t_node *add_column(t_node *root)
new_col->up = new_col;
new_col->left = root->left;
new_col->right = root;
root->left->right = new_col;
root->left = new_col;
new_col->row = -1;
new_col->column = NULL;
return (new_col);
}
t_node *add_node_with_coord(t_node *col, t_coord coord, int type)
t_node *add_node(t_node *col, int row)
{
t_node *new_node;
new_node = (t_node*)malloc(sizeof(t_node));
new_node->coord = coord;
new_node->type = type;
new_node->column = col;
new_node->row = row;
new_node->up = col->up;
new_node->down = col;
col->up->down = new_node;
@ -54,102 +39,10 @@ t_node *add_node_with_coord(t_node *col, t_coord coord, int type)
return (new_node);
}
t_node *add_node(t_node *col)
void cover(t_node *to_cover)
{
t_node *new_node;
new_node = (t_node*)malloc(sizeof(t_node));
new_node->column = col;
new_node->up = col->up;
new_node->down = col;
col->up->down = new_node;
col->up = new_node;
return (new_node);
}
t_node **add_cols(t_node *root, int number)
{
t_node **array;
int i;
array = (t_node**)malloc(number * sizeof(t_node*));
i = 0;
while (i < number)
array[i++] = add_column(root);
return (array);
}
int *get_coords(t_coord coord, int amount, int type, int size) {
int *result;
int i;
t_coord *start;
result = (int *) malloc(sizeof(int) * 5);
start = g_templates[type].c;
i = 0;
while (i < 4)
{
result[i + 1] = (coord.x + coord.y * size + start[i].x + start[i].y * size) + amount;
i++;
}
return (result);
}
void add_row(int *col_numbers, t_node **cols_arr, t_coord coord, int type)
{
add_node_with_coord(cols_arr[col_numbers[0]], coord, type);
add_node(cols_arr[col_numbers[1]]);
add_node(cols_arr[col_numbers[2]]);
add_node(cols_arr[col_numbers[3]]);
add_node(cols_arr[col_numbers[4]]);
}
void link_row(int *col_nums, t_node **cols_arr)
{
cols_arr[col_nums[0]]->up->left = cols_arr[col_nums[4]]->up;
cols_arr[col_nums[0]]->up->right = cols_arr[col_nums[1]]->up;
cols_arr[col_nums[1]]->up->left = cols_arr[col_nums[0]]->up;
cols_arr[col_nums[1]]->up->right = cols_arr[col_nums[2]]->up;
cols_arr[col_nums[2]]->up->left = cols_arr[col_nums[1]]->up;
cols_arr[col_nums[2]]->up->right = cols_arr[col_nums[3]]->up;
cols_arr[col_nums[3]]->up->left = cols_arr[col_nums[2]]->up;
cols_arr[col_nums[3]]->up->right = cols_arr[col_nums[4]]->up;
cols_arr[col_nums[4]]->up->left = cols_arr[col_nums[3]]->up;
cols_arr[col_nums[4]]->up->right = cols_arr[col_nums[0]]->up;
}
void add_rows(int *types, int amount, int size, t_node **cols_arr)
{
int i = 0;
int k = 0;
int l = 0;
int *col_num;
t_coord coord;
while (i < amount)
{
l = 0;
while (l <= size - g_templates[types[i]].height)
{
k = 0;
while (k <= size - g_templates[types[i]].width)
{
coord.x = k;
coord.y = l;
col_num = get_coords(coord, amount, types[i], size);
col_num[0] = i;
add_row(col_num, cols_arr, coord, types[i]);
link_row(col_num, cols_arr);
k++;
}
l++;
}
i++;
}
}
int cover(t_node *to_cover)
{
static t_node *step_vert;
static t_node *step_horiz;
void *step_vert;
t_node *step_horiz;
to_cover->left->right = to_cover->right;
to_cover->right->left = to_cover->left;
@ -164,13 +57,12 @@ int cover(t_node *to_cover)
}
step_vert = step_vert->down;
}
return (1);
}
int uncover(t_node *to_uncover)
void uncover(t_col *to_uncover)
{
static t_node *step_vert;
static t_node *step_horiz;
void *step_vert;
t_node *step_horiz;
step_vert = to_uncover->up;
while (step_vert != to_uncover)
@ -186,74 +78,5 @@ int uncover(t_node *to_uncover)
}
to_uncover->left->right = to_uncover;
to_uncover->right->left = to_uncover;
return (1);
}
void fill_map(char *map, int size, t_node *ttr, int letter)
{
int i;
char a;
a = 'A' + letter - 1;
i = 0;
while (i < 4)
{
ft_memcpy((map + (ttr->coord.y + g_templates[ttr->type].c[i].y) * size +
ttr->coord.x + g_templates[ttr->type].c[i].x), &a, 1);
i++;
}
}
void print_solution(t_node **solution, int amount, int size)
{
char *map;
int map_size;
int i;
map_size = size * size + 1;
map = malloc(sizeof(char) * map_size);
map[map_size] = 0;
i = 0;
while (i++ < amount)
fill_map(map, size, solution[i - 1], i);
i = 0;
while (i < map_size - 1)
{
if (map[i] >= 'A' && map[i] <= 'Z')
ft_putchar(map[i]);
else
ft_putchar('.');
if ((i + 1) % size == 0)
ft_putchar('\n');
i++;
}
exit(0);
}
void search(t_node* root,t_node **solution, int k, int amount)
{
t_node *current_col;
t_node *row;
t_node *j;
if (k == amount)
print_solution(solution, amount, root->size);
current_col = root->right;
cover(current_col);
row = current_col->down;
while (row != current_col)
{
solution[k] = row;
j = row->right;
while (j != row && cover(j->column))
j = j->right;
search(root, solution, k + 1, amount);
j = row->left;
while (j != row && uncover(j->column))
j = j->left;
row = row->down;
}
uncover(current_col);
return ;
}

View file

@ -1,4 +1,4 @@
#include "fillit.h"
#include "../inc/fillit.h"
unsigned int ft_sqrt_ceil(unsigned int num)
{
@ -9,3 +9,163 @@ unsigned int ft_sqrt_ceil(unsigned int num)
i++;
return (i);
}
void clear_map(t_map *map)
{
int i;
i = 0;
while (i < 16)
map->line[i++] = 0;
}
void convert_tetramino(char *s, t_map *map, int index)
{
uint16_t temp;
int i;
int y;
int sharp_count;
map->figure[index].offset_y = 0;
sharp_count = 0;
i = 0;
y = 1;
while (i < 16 && sharp_count < 4)
{
temp = 0;
while (i < y * 4)
{
temp = temp << 1;
if (s[i++] == '#' && ++sharp_count)
temp = temp | 0x01;
}
temp = temp << 12;
map->figure[index].line[y - 1] = temp;
y++;
}
map->figure[index].height = y - 1;
}
void map_initialization(char **tetramino_table, t_map *map)
{
unsigned int i;
i = 0;
while (*tetramino_table)
convert_tetramino(*tetramino_table++, map, i++);
map->figure_amount = i;
map->size = ft_sqrt_ceil(i * 4);
map->mask = 1;
map->mask <<= 16 - map->size;
map->ok = 0;
clear_map(map);
}
void insert_tetramino(t_map *map, int index)
{
int i;
int k;
t_tetrominoes *temp;
temp = &map->figure[index];
i = temp->offset_y;
k = i + temp->height;
while (i < k)
{
map->line[i] |= temp->line[i];
i++;
}
}
void erase_tetramino(t_map *map, int index)
{
int i;
int k;
t_tetrominoes *temp;
temp = &map->figure[index];
i = temp->offset_y;
k = 0;
while (k < temp->height)
{
map->line[i + k] ^= temp->line[i + k];
k++;
}
}
int can_insert(t_map *map, int index)
{
int i;
int k;
t_tetrominoes *temp;
temp = &map->figure[index];
i = temp->offset_y;
k = i + temp->height;
while (i < k)
{
if (map->line[i] & temp->line[i])
return (0);
i++;
}
return (1);
}
int shift_tetramino(t_map *map, int index)
{
int right_shift;
unsigned int i;
unsigned int k;
t_tetrominoes *temp;
temp = &map->figure[index];
i = temp->offset_y;
k = i + temp->height;
right_shift = 1;
while (i < k)
if (temp->line[i++] & map->mask)
right_shift = 0;
i = temp->offset_y;
if (right_shift && ++(temp->offset_x))
while (i < k)
temp->line[i++] >>= 1;
else if (k < map->size && ++(temp->offset_y)) {
while (k-- > i) {
temp->line[k] <<= temp->offset_x;
temp->line[k + 1] = temp->line[k];
}
temp->offset_x = 0;
}
else
return (0);
return (1);
}
t_map algorithm(t_map map, unsigned int index)
{
int ins = 0;
t_map copy;
if (index == map.figure_amount) {
map.ok = 1;
return map;
}
while (!(ins = can_insert(&map, index)))
if (!shift_tetramino(&map, index))
break;
if (ins)
{
copy = map;
insert_tetramino(&map, index);
map = algorithm(map, index + 1);
if (!map.ok)
{
shift_tetramino(&copy, index);
map = algorithm(copy, index);
}
}
return map;
}

View file

@ -1,11 +0,0 @@
#include "fillit.h"
int get_amount(char **ttr)
{
int amount;
amount = 0;
while (ttr[amount])
amount++;
return (amount);
}

View file

@ -1,31 +0,0 @@
#include "fillit.h"
#include <stdlib.h>
int *get_types(char **ttr)
{
int *types;
int i;
int j;
int size;
i = 0;
size = 0;
while (ttr[size])
size++;
types = malloc(sizeof(int) * size);
while (i < size)
{
j = 0;
while (j < 19)
{
if (ft_strcmp(ttr[i], g_templates[j].t) == 0)
{
types[i] = j;
break;
}
j++;
}
i++;
}
return (types);
}

View file

@ -10,12 +10,10 @@
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "../inc/fillit.h"
/*
** definition of global variable
** that contains all possible tetrominoes
** inside 4x4 square with initial coordinates at [0][0]
** bad global variable
*/
t_ttrmn g_templates[19] =
@ -37,7 +35,7 @@ t_ttrmn g_templates[19] =
{"##..##..........", 2, 2, {{0, 0}, {0, 1}, {1, 0}, {1, 1}}},
{"##...##.........", 3, 2, {{0, 0}, {0, 1}, {1, 1}, {1, 2}}},
{".#..##..#.......", 2, 3, {{0, 1}, {1, 0}, {1, 1}, {2, 0}}},
{".##.##..........", 3, 2, {{0, 1}, {0, 2}, {1, 0}, {1, 1}}},
{".##.##..........", 3, 2, {{0, 1}, {0, 2}, {1, 0}, {1, 2}}},
{"#...##...#......", 2, 3, {{0, 0}, {1, 0}, {1, 1}, {2, 1}}}
};
@ -47,23 +45,19 @@ int main(int argc, char **argv)
char **table;
char **ttr;
int i;
int *types;
int amount;
int size;
t_node *r;
t_node **cols_arr;
t_node **ans;
//int *types;
ans = NULL;
i = 0;
if (argc != 2)
ft_putstr("error\n");
else
init(argv[1]);
string = read_file(argv[1]);
//printf("string:\n\n\n%s\n", string);
if (argc != 2)
{
ft_putstr("error\n");
return (1);
}
if (!(string = read_file(argv[1])))
{
ft_putstr("error\n");
return (1);
} //printf("string:\n\n\n%s\n", string);
table = to_table(&string);
//printf("print_table:\n\n\n");
@ -77,23 +71,13 @@ int main(int argc, char **argv)
//print_one_string(ttr);
//printf("test check:\n\n");
//test_check(ttr, g_templates);
//test_check(ttr, templates);
types = get_types(ttr);
amount = get_amount(ttr);
ans = (t_node**)malloc(sizeof(t_node*) * amount);
size = ft_sqrt_ceil(amount * 4);
while (1)
while (i < 19)
{
r = init_root(size);
cols_arr = add_cols(r, (size * size + amount));
add_rows(types, amount, size, cols_arr);
search(r, ans, 0 ,amount);
size++;
print_one_string(g_templates[i]);
i++;
}
//print_ans(ans);
//printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y);
//to_letters(ttr);
//printf("to latters:\n\n");

View file

@ -14,7 +14,7 @@
// Test func. Compare figures from file
// with templates
void test_check(char **ttr, t_ttrmn *templates)
void test_check(char **ttr, t_ttrmn *tamplates)
{
int i;
int j;
@ -25,12 +25,12 @@ void test_check(char **ttr, t_ttrmn *templates)
j = 0;
while(j < 19)
{
if ((ft_memcmp(ttr[i], templates[j].t, 17) == 0))
{
printf("\n\ninput:\n");
printf("%s\n", ttr[i]);
printf("\n\ntemplate:\n");
printf("%s\n", tamplates[j].t);
if ((ft_memcmp(ttr[i], tamplates[j].t, 17) == 0))
printf(" match!\n");
print_one_string(templates[j]);
break;
}
j++;
}
i++;

View file

@ -25,7 +25,7 @@ void print_one_string(t_ttrmn ttr)
ft_putchar('\n');
i++;
}
printf("x = %d, y = %d\n", ttr.width, ttr.height);
printf("x = %d, y = %d\n", ttr.x, ttr.y);
printf("x1[%d] y1[%d]\nx2[%d] y2[%d]\nx3[%d] y3[%d]\nx4[%d] y4[%d]\n", ttr.c[0].x, ttr.c[0].y, ttr.c[1].x, ttr.c[1].y, ttr.c[2].x, ttr.c[2].y, ttr.c[3].x, ttr.c[3].y);
ft_putstr("\n");
}

33
src/to_strct_array.c Normal file
View file

@ -0,0 +1,33 @@
#include "fillit.h"
int *to_strct_array(char **ttr, t_ttrmn *templates)
{
t_ttrmn **ret;
int amount;
int i;
int j;
int k;
amount = 0;
i = 0;
k = 0;
while (ttr[amount])
amount++;
ret = malloc(sizeof(ret) * (amount + 1));
while (k < amount)
ret[k++] = malloc(sizeof(t_ttrmn));
ret[k] = 0;
while (i < amount)
{
j = 0;
while(ft_strcmp(templates[j].t, ttr[i]) != 0)
j++;
//printf("%d\n", j);
ft_strcpy(ret[i]->t, ttr[i]);
ret[i]->x = templates[j].x;
ret[i]->y = templates[j].y;
i++;
}
return (0);
}

View file

@ -1,3 +1,8 @@
####
....
....
....
....
##..
.#..
@ -12,8 +17,3 @@
##..
##..
....
####
....
....
....

View file

@ -1,34 +0,0 @@
####
....
....
....
####
....
....
....
####
....
....
....
####
....
....
....
####
....
....
....
####
....
....
....
####
....
....
....

View file

@ -1,19 +0,0 @@
....
##..
.#..
.#..
....
####
....
....
#...
###.
....
....
....
##..
.##.
....

View file

@ -1,39 +0,0 @@
...#
...#
...#
...#
....
....
....
####
.###
...#
....
....
....
..##
.##.
....
....
.##.
.##.
....
....
....
##..
.##.
##..
.#..
.#..
....
....
###.
.#..
....

View file

@ -1,4 +0,0 @@
....
..##
.##.
....

View file

@ -1,119 +0,0 @@
...#
...#
...#
...#
....
....
....
####
.###
...#
....
....
....
..##
.##.
....
....
.##.
.##.
....
....
....
##..
.##.
##..
.#..
.#..
....
....
###.
.#..
....
...#
...#
...#
...#
....
....
....
####
.###
...#
....
....
....
..##
.##.
....
....
.##.
.##.
....
....
....
##..
.##.
##..
.#..
.#..
....
....
###.
.#..
....
...#
...#
...#
...#
....
....
....
####
.###
...#
....
....
....
..##
.##.
....
....
.##.
.##.
....
....
....
##..
.##.
##..
.#..
.#..
....
....
###.
.#..
....