-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill_map.c
111 lines (101 loc) · 3.35 KB
/
fill_map.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fill_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bvigne <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/28 17:10:48 by bvigne #+# #+# */
/* Updated: 2017/12/01 10:29:43 by bvigne ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
int test_pos(char **map, int pos, int size, t_tetri *tetri)
{
int i;
int j;
i = pos / size;
j = pos % size;
if (i + tetri->block_1.x < size && j + tetri->block_1.y < size &&
i + tetri->block_2.x < size && j + tetri->block_2.y < size &&
i + tetri->block_3.x < size && j + tetri->block_3.y < size &&
i + tetri->block_4.x < size && j + tetri->block_4.y < size &&
i + tetri->block_1.x < size && j + tetri->block_1.y < size &&
i + tetri->block_2.x < size && j + tetri->block_2.y < size &&
i + tetri->block_3.x < size && j + tetri->block_3.y < size &&
i + tetri->block_4.x < size && j + tetri->block_4.y < size &&
map[i + tetri->block_1.x][j + tetri->block_1.y] == '.' &&
map[i + tetri->block_2.x][j + tetri->block_2.y] == '.' &&
map[i + tetri->block_3.x][j + tetri->block_3.y] == '.' &&
map[i + tetri->block_4.x][j + tetri->block_4.y] == '.')
return (1);
return (0);
}
char **reset_pos(char **map, int pos, int size, t_tetri *tetri)
{
int i;
int j;
i = pos / size;
j = pos % size;
map[i + tetri->block_1.x][j + tetri->block_1.y] = '.';
map[i + tetri->block_2.x][j + tetri->block_2.y] = '.';
map[i + tetri->block_3.x][j + tetri->block_3.y] = '.';
map[i + tetri->block_4.x][j + tetri->block_4.y] = '.';
return (map);
}
char **fill_pos(char **map, int pos, int size, t_tetri *tetri)
{
int i;
int j;
i = pos / size;
j = pos % size;
map[i + tetri->block_1.x][j + tetri->block_1.y] = tetri->name;
map[i + tetri->block_2.x][j + tetri->block_2.y] = tetri->name;
map[i + tetri->block_3.x][j + tetri->block_3.y] = tetri->name;
map[i + tetri->block_4.x][j + tetri->block_4.y] = tetri->name;
return (map);
}
int ft_bactracking(char **map, t_tetri *tetri, int size, int pos)
{
if (map_full(map, tetri))
return (1);
if (map[pos / size][pos % size] != '.')
return (ft_bactracking(map, tetri, size, pos + 1));
while (pos <= (size * size) - 1)
{
if (test_pos(map, pos, size, tetri))
{
map = fill_pos(map, pos, size, tetri);
if (map_full(map, tetri))
return (1);
if (tetri->next && ft_bactracking(map, tetri->next, size, 0))
return (1);
else
map = reset_pos(map, pos, size, tetri);
}
pos++;
}
return (0);
}
char **fill_map(t_tetri *chaine)
{
char **map;
int size;
int pos;
int rez;
map = NULL;
pos = 0;
rez = 0;
size = (ft_sqrt_higher(ft_lstcount(chaine)));
while (!rez)
{
map = new_map(size);
rez = ft_bactracking(map, chaine, size, pos);
if (!rez)
{
free_map(map, size);
size++;
}
}
return (map);
}