/
ft_solve.c
72 lines (66 loc) · 1.79 KB
/
ft_solve.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_solve.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: plefebvr <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/03/12 17:46:21 by plefebvr #+# #+# */
/* Updated: 2016/03/14 22:28:46 by plefebvr ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
static void ft_print_map(char **map)
{
int i;
i = 0;
while (map[i] != NULL)
{
ft_putendl(map[i]);
i++;
}
}
static int ft_try(t_tetros *tetro, char **map, int size)
{
if (tetro != NULL)
{
while (tetro->x * tetro->y < size * size)
{
if ((ft_can_place(tetro, map, size)) == 1)
{
map = ft_place(tetro, map);
if ((ft_try(tetro->next, map, size)) == 1)
return (1);
map = ft_delete_tetro(tetro, map, size);
}
if (tetro->x < size)
tetro->x++;
else
{
tetro->y++;
tetro->x = 0;
}
}
tetro->y = 0;
tetro->x = 0;
return (0);
}
return (1);
}
void ft_solve(t_tetros *tetro, int nb_tetros)
{
char **map;
int size;
size = nb_tetros / 2;
map = ft_gen_map(size);
ft_adj_tetro(tetro);
while (ft_try(tetro, map, size) == 0)
{
tetro->x = 0;
tetro->y = 0;
size++;
ft_free_tetros(map, size);
map = ft_gen_map(size);
}
ft_print_map(map);
}