version 0.0000000000000000000000001

This commit is contained in:
Gregory Tertyshny 2016-12-20 21:46:08 +02:00
parent 7cc5410001
commit d8d007b41f
10 changed files with 67 additions and 331 deletions

6
.gitignore vendored
View file

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

View file

@ -6,7 +6,7 @@
# By: gtertysh <marvin@42.fr> +#+ +:+ +#+ # # By: gtertysh <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/12/14 16:54:48 by gtertysh #+# #+# # # Created: 2016/12/14 16:54:48 by gtertysh #+# #+# #
# Updated: 2016/12/20 20:05:58 by ykolomie ### ########.fr # # Updated: 2016/12/20 20:37:58 by gtertysh ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -21,10 +21,9 @@ SRC_FILES = main.c \
move_up_left.c \ move_up_left.c \
read_file.c \ read_file.c \
string_to_table.c \ string_to_table.c \
to_strct_array.c \
to_letters.c \ to_letters.c \
build_matrix.c \
dancing_links.c \ dancing_links.c \
get_types.c \
test!_check.c \ test!_check.c \
test!_print_one_string.c \ test!_print_one_string.c \
test!_print_table.c \ test!_print_table.c \
@ -46,14 +45,12 @@ LIBFOLDER = ./libft/
FLAGS = -Werror -Wextra -Wall FLAGS = -Werror -Wextra -Wall
STACK = -Wl,-stack_size -Wl,0x10000000000
CC = clang CC = clang
all: $(NAME) all: $(NAME)
$(NAME): $(OBJ) $(LIBFOLDER)$(LIB) $(NAME): $(OBJ) $(LIBFOLDER)$(LIB)
$(CC) $(FLAGS) $(STACK) $(OBJ) $(LIBFLAGS) -o $(NAME) $(CC) $(FLAGS) $(OBJ) $(LIBFLAGS) -o $(NAME)
$(OBJDIR)%.o : $(SRCDIR)%.c $(OBJDIR)%.o : $(SRCDIR)%.c
$(CC) $(FLAGS) $(INC) -c $< -o $@ $(CC) $(FLAGS) $(INC) -c $< -o $@

View file

@ -45,8 +45,10 @@ typedef struct s_node
struct s_node *down; struct s_node *down;
int row; int row;
struct s_node *column; struct s_node *column;
t_coord coord;
} t_node; } t_node;
extern t_ttrmn g_templates[19];
// reads from file // reads from file
char *read_file(char *path); char *read_file(char *path);
@ -77,10 +79,13 @@ void to_letters(char **ttr);
int *to_strct_array(char **ttr, t_ttrmn *templates); int *to_strct_array(char **ttr, t_ttrmn *templates);
t_node *add_column(t_node *root); t_node *add_column(t_node *root);
t_node *add_node(t_node *col, int row); t_node *add_node(t_node *col, int row);
void cover(t_node *to_cover); void cover(t_node *to_cover);
void uncover(t_node *to_uncover); void uncover(t_node *to_uncover);
int *get_types(char **ttr);
#endif #endif

View file

@ -1,106 +0,0 @@
#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

@ -41,7 +41,7 @@ t_node *add_node(t_node *col, int row)
void cover(t_node *to_cover) void cover(t_node *to_cover)
{ {
void *step_vert; t_node *step_vert;
t_node *step_horiz; t_node *step_horiz;
to_cover->left->right = to_cover->right; to_cover->left->right = to_cover->right;
@ -59,9 +59,9 @@ void cover(t_node *to_cover)
} }
} }
void uncover(t_col *to_uncover) void uncover(t_node *to_uncover)
{ {
void *step_vert; t_node *step_vert;
t_node *step_horiz; t_node *step_horiz;
step_vert = to_uncover->up; step_vert = to_uncover->up;

View file

@ -1,4 +1,4 @@
#include "../inc/fillit.h" #include "fillit.h"
unsigned int ft_sqrt_ceil(unsigned int num) unsigned int ft_sqrt_ceil(unsigned int num)
{ {
@ -9,163 +9,3 @@ unsigned int ft_sqrt_ceil(unsigned int num)
i++; i++;
return (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;
}

30
src/get_types.c Normal file
View file

@ -0,0 +1,30 @@
#include "fillit.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,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../inc/fillit.h" #include "fillit.h"
/* /*
** bad global variable ** bad global variable
@ -45,7 +45,11 @@ int main(int argc, char **argv)
char **table; char **table;
char **ttr; char **ttr;
int i; int i;
//int *types; int *types;
int amount;
t_node *r;
t_node **cols_arr;
t_list *ans;
i = 0; i = 0;
if (argc != 2) if (argc != 2)
@ -70,14 +74,19 @@ int main(int argc, char **argv)
//printf("move to up left:\n\n\n"); //printf("move to up left:\n\n\n");
//print_one_string(ttr); //print_one_string(ttr);
//printf("test check:\n\n"); printf("test check:\n\n");
//test_check(ttr, templates); test_check(ttr, g_templates);
while (i < 19)
{
print_one_string(g_templates[i]); types = get_types(ttr);
i++; amount = get_amount(ttr);
} size = ft_sqrt_ceil(amount * 4);
r = init_root();
cols_arr = add_cols(r, (size * size + amount));
add_rows(r, types, amount, cols_arr);
ans = dlx(r);
print_ans(ans);
//printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y); //printf("%s\n %d, %d", strct_ttr[0]->t, strct_ttr[0]->x, strct_ttr[0]->y);
//to_letters(ttr); //to_letters(ttr);
//printf("to latters:\n\n"); //printf("to latters:\n\n");

View file

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

View file

@ -1,33 +0,0 @@
#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);
}