-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_to_the_border.c
executable file
·97 lines (90 loc) · 3 KB
/
go_to_the_border.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* go_to_the_border.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ioleksiu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/05/06 18:42:45 by ioleksiu #+# #+# */
/* Updated: 2017/05/08 13:55:08 by ioleksiu ### ########.fr */
/* */
/* ************************************************************************** */
#include "filler.h"
int closest_to_the_left(t_filler *f, int x_dis, int y_dis, int y)
{
x_dis = (x_dis > 0) ? x_dis : (x_dis * -1);
y_dis = (y_dis > 0) ? y_dis : (y_dis * -1);
if (f->xy_cord_send[1] < y || ((f->xy_cord_send[1] == y)
&& (x_dis + y_dis < f->xy_distance[0] + f->xy_distance[1]))
|| f->xy_cord_send[0] == -42)
{
f->xy_distance[0] = x_dis;
f->xy_distance[1] = y_dis;
return (1);
}
return (0);
}
void go_to_the_left_border(t_filler *f, t_list *player, t_list *other_p)
{
t_list *coords_to_place;
t_list *opponents_coords;
int x_dis;
int y_dis;
coords_to_place = player;
opponents_coords = other_p;
while (coords_to_place)
{
while (opponents_coords)
{
x_dis = coords_to_place->x - opponents_coords->x;
y_dis = coords_to_place->y - opponents_coords->y;
if (closest_to_the_left(f, x_dis, y_dis, coords_to_place->y))
{
f->xy_cord_send[0] = coords_to_place->x;
f->xy_cord_send[1] = coords_to_place->y;
}
opponents_coords = opponents_coords->next;
}
opponents_coords = other_p;
coords_to_place = coords_to_place->next;
}
}
int closest_to_the_right(t_filler *f, int x_dis, int y_dis, int y)
{
x_dis = (x_dis > 0) ? x_dis : (x_dis * -1);
y_dis = (y_dis > 0) ? y_dis : (y_dis * -1);
if (f->xy_cord_send[1] > y || ((f->xy_cord_send[1] == y)
&& (x_dis + y_dis < f->xy_distance[0] + f->xy_distance[1]))
|| f->xy_cord_send[0] == -42)
{
f->xy_distance[0] = x_dis;
f->xy_distance[1] = y_dis;
return (1);
}
return (0);
}
void go_to_the_right_border(t_filler *f, t_list *player, t_list *other_p)
{
t_list *coords_to_place;
t_list *opponents_coords;
int x_dis;
int y_dis;
coords_to_place = player;
opponents_coords = other_p;
while (coords_to_place)
{
while (opponents_coords)
{
x_dis = coords_to_place->x - opponents_coords->x;
y_dis = coords_to_place->y - opponents_coords->y;
if (closest_to_the_right(f, x_dis, y_dis, coords_to_place->x))
{
f->xy_cord_send[0] = coords_to_place->x;
f->xy_cord_send[1] = coords_to_place->y;
}
opponents_coords = opponents_coords->next;
}
opponents_coords = other_p;
coords_to_place = coords_to_place->next;
}
}