-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill_piece.c
118 lines (108 loc) · 2.56 KB
/
fill_piece.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fill_peace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlenskyi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/11 15:56:57 by dlenskyi #+# #+# */
/* Updated: 2019/01/11 15:56:58 by dlenskyi ### ########.fr */
/* */
/* ************************************************************************** */
#include "filler.h"
int is_safe(int x, int y, t_fill_gen *g)
{
int k;
int i;
int j;
i = -1;
k = 0;
while (g->piece.size[++i])
{
j = -1;
while (g->piece.size[i][++j])
{
if (g->piece.size[i][j] == '*' &&
((((y + i >= g->map.y || x + j >= g->map.x) ||
(y + i < 0 || x + j < 0))) ||
(g->map.size[y + i][x + j] == g->player.robot ||
g->map.size[y + i][x + j] == g->player.robot + 32)))
return (0);
if (g->piece.size[i][j] == '*' && (g->map.size[y + i][x + j] ==
g->player.human))
k++;
}
}
return (k == 1 ? 1 : 0);
}
void print_left(t_fill_gen *g)
{
int x;
int y;
x = -1;
while (++x < g->map.x)
{
y = -1;
while (++y < g->map.y)
{
if (is_safe(x, y, g))
{
g->fill.x = x;
g->fill.y = y;
return ;
}
}
}
}
void print_right(t_fill_gen *g)
{
int x;
int y;
x = g->map.x;
while (--x >= 0)
{
y = g->map.y;
while (--y >= 0)
{
if (is_safe(x, y, g))
{
g->fill.x = x;
g->fill.y = y;
return ;
}
}
}
}
void fill_piece(t_fill_gen *g)
{
validate_piece(g);
if (g->fill.fill == 1)
put_top_pos(g);
else if (g->fill.fill == 4)
put_bot_pos(g);
else if (g->fill.fill == 2)
put_left_pos(g);
else if (g->fill.fill == 3)
put_right_pos(g);
else if (g->fill.fill == 5)
print_left(g);
else if (g->fill.fill == 6)
print_right(g);
ft_printf("%d %d\n", g->fill.y, g->fill.x);
}
void if_the_best(t_fill_gen *g)
{
char *line;
if (get_next_line(0, &line) < 1)
return ;
if (g->map.size)
ft_del_strsplit(g->map.size);
get_map_data(g, line);
free(line);
if (get_next_line(0, &line) < 1)
quit("Reading error\n", g);
if (g->piece.size)
ft_del_strsplit(g->piece.size);
get_piece_data(line, g);
fill_piece(g);
}