-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_fltalg.c
118 lines (106 loc) · 2.57 KB
/
ft_fltalg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fltalg.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpaziuk <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/04 12:29:47 by mpaziuk #+# #+# */
/* Updated: 2016/12/04 12:29:49 by mpaziuk ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
char **g_map;
void ft_mapinit(size_t size)
{
size_t i;
i = 0;
g_map = (char **)malloc(sizeof(char *) * (size + 1));
while (i < size)
{
g_map[i] = ft_strnew(size);
ft_memset((void*)g_map[i], '.', size);
i++;
}
g_map[i] = NULL;
}
int ft_can_place(int x, int y, t_fig *figure)
{
register int i;
register int j;
int width;
int height;
ft_sfparam(figure, &width, &height);
i = 0;
while (i < height)
{
j = 0;
while (j < width)
{
if (g_map[y + i][x + j] != '.' && (figure->figure)[i][j] != '.'
&& figure->figure[i][j])
return (0);
j++;
}
i++;
}
return (1);
}
void ft_w2map(int x, int y, t_fig *figure)
{
register int i;
register int j;
int width;
int height;
i = 0;
ft_sfparam(figure, &width, &height);
while (i < height)
{
j = 0;
while (j < width)
{
if ((figure->figure)[i][j] != '.' && (figure->figure)[i][j])
g_map[y + i][x + j] = figure->figure[i][j];
j++;
}
i++;
}
}
int ft_fillit(t_fig *figure, size_t f_size)
{
int var[4];
char **map_tmp;
var[1] = -1;
ft_sfparam(figure, &var[2], &var[3]);
while (++var[1] + var[3] <= (int)f_size)
{
var[0] = -1;
while (++var[0] + var[2] <= (int)f_size)
{
if (ft_can_place(var[0], var[1], figure))
{
map_tmp = ft_mapclone(g_map);
ft_w2map(var[0], var[1], figure);
if (figure->next == NULL)
return (ft_delmap(&map_tmp));
else if (ft_fillit(figure->next, f_size))
return (ft_delmap(&map_tmp));
g_map = map_tmp;
}
}
}
return (0);
}
char **ft_fillit_ctrl(t_fig *figure)
{
size_t size;
size = ft_csize(figure);
ft_mapinit(size);
while (!ft_fillit(figure, size))
{
size++;
free(g_map);
ft_mapinit(size);
}
return (g_map);
}