-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfill_tools.c
More file actions
102 lines (93 loc) · 2.26 KB
/
fill_tools.c
File metadata and controls
102 lines (93 loc) · 2.26 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fill_tools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akittie <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 18:06:02 by akittie #+# #+# */
/* Updated: 2019/11/25 20:13:09 by akittie ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "utils.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int is_fit(char **map, t_tetr t, int x, int y)
{
int i;
int j;
i = -1;
while (++i < t.x)
{
j = -1;
while (++j < t.y)
{
if (map[x + i][y + j] != '.' && t.tetr[i][j] == '#')
{
return (0);
}
if (map[x + i][y + j] == '.' && t.tetr[i][j] == '#')
{
map[x + i][y + j] = 'A' + t.index;
}
}
}
return (1);
}
char **map_cpy(char **map, int maps_size)
{
int i;
char **res;
if (!(res = malloc(sizeof(char*) * (maps_size + 1))))
ft_error();
res[maps_size] = NULL;
i = -1;
while (++i < maps_size)
{
if (!(res[i] = ft_strdup(map[i])))
ft_error();
}
return (res);
}
void map_free(char ***m, int map_size)
{
char **map;
int i;
i = 0;
map = *m;
while (i < map_size)
{
free(map[i]);
map[i++] = 0;
}
free(*m);
}
int fill_with(char **map, int index, t_tetr *t_arr, int maps_size)
{
int i;
int j;
char **new_map;
if (t_arr[index].x == -1)
{
print_map(map, maps_size);
map_free(&map, maps_size);
exit(0);
}
i = -1;
while (++i <= maps_size - t_arr[index].x)
{
j = -1;
while (++j <= maps_size - t_arr[index].y)
{
new_map = map_cpy(map, maps_size);
if (is_fit(new_map, t_arr[index], i, j))
if (fill_with(new_map, index + 1, t_arr, maps_size))
return (1);
map_free(&new_map, maps_size);
}
}
return (0);
}