-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_fill_board.c
119 lines (109 loc) · 2.3 KB
/
ft_fill_board.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
119
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fill_board.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: afillion <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/14 15:09:32 by afillion #+# #+# */
/* Updated: 2015/12/17 16:55:54 by afillion ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
void ft_fill_board(char **map, char **tab, int x, int y)
{
int i;
size_t j;
i = 0;
j = 0;
while (tab[i])
{
while (tab[i][j])
{
if (ft_isalpha(tab[i][j]))
map[x + i][y + j] = tab[i][j];
j++;
}
i++;
j = 0;
}
return ;
}
char **map_expand(t_list *lst, int *n)
{
char **map;
map = ft_taballoc(*n);
if (backtrack_map2(lst, map) == 1)
return (map);
if (backtrack_map2(lst, map) == 0)
{
(*n)++;
return (map_expand(lst, n));
}
return (NULL);
}
int backtrack_map2(t_list *lst, char **map)
{
int i;
int j;
if (!lst)
return (1);
i = 0;
j = 0;
while (ft_niketamerelapute(map, lst->tab, &i, &j))
{
ft_fill_board(map, lst->tab, i, j);
if (backtrack_map2(lst->next, map))
{
return (1);
}
else
{
erase_piece(map, lst->tab, i, j);
j++;
}
}
return (0);
}
int ft_niketamerelapute(char **map, char **tab, int *i, int *j)
{
while (map[*i])
{
while (map[*i][*j])
{
if (check_board(map, tab, *i, *j))
{
return (1);
}
else
(*j)++;
}
(*i)++;
*j = 0;
}
return (0);
}
int check_board(char **map, char **tab, int x, int y)
{
int i;
int j;
i = 0;
j = 0;
while (tab[i])
{
j = 0;
if (!(map[x + i]) || ft_strlen(map[x + i] + y) < ft_strlen(tab[i]))
return (0);
while (tab[i][j])
{
if (map[x + i][y + j] == '.' && ft_isalpha(tab[i][j]))
j++;
else if (tab[i][j] == '.')
j++;
else
return (0);
}
i++;
}
return (1);
}