-
Notifications
You must be signed in to change notification settings - Fork 0
/
valid.c
executable file
·99 lines (90 loc) · 2.86 KB
/
valid.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ykhayri <ykhayri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/06 17:56:22 by ykhayri #+# #+# */
/* Updated: 2023/02/17 23:25:39 by ykhayri ### ########.fr */
/* */
/* ************************************************************************** */
#include "utils.h"
void free_val(char **map)
{
int i;
i = -1;
while (map[++i])
free(map[i]);
free(map);
}
char **copy_map(t_map *mlx)
{
char **map;
int i;
i = -1;
map = (char **) malloc((mlx->dim[y] + 1) * sizeof(char *));
if (!map)
return (0);
while (++i < mlx->dim[y])
{
map[i] = ft_strdup(mlx->map[i]);
if (!map[i])
{
free_val(map);
return (0);
}
}
map[i] = 0;
return (map);
}
void discover(t_map *mlx, int px, int py)
{
if (mlx->check[py][px] == CO)
mlx->p_pos[2]++;
if (mlx->check[py][px] == EX)
mlx->p_pos[3]++;
mlx->check[py][px] = WL;
if (mlx->check[py + 1][px] != WL)
discover(mlx, px, py + 1);
if (mlx->check[py - 1][px] != WL)
discover(mlx, px, py - 1);
if (mlx->check[py][px - 1] != WL)
discover(mlx, px - 1, py);
if (mlx->check[py][px + 1] != WL)
discover(mlx, px + 1, py);
}
int valid_p(t_map *mlx)
{
int valid;
valid = 0;
mlx->p_pos[2] = 0;
mlx->p_pos[3] = 0;
discover(mlx, mlx->p_pos[x], mlx->p_pos[y]);
if (mlx->p_pos[2] == mlx->max_sc && mlx->p_pos[3] == 1)
valid = 1;
if (valid == 0)
print_info("Error:\nThere is no valid path!", -1);
free_val(mlx->check);
return (valid);
}
void just_wait(t_game *game, char c)
{
if (c == 'r' && MODE == 1)
mlx_put_image_to_window(game->mlx->mlx, game->mlx->mlx_win,
game->link->lr[0], game->link->pos[0], game->link->pos[1]);
else if (c == 'b' && MODE == 1)
mlx_put_image_to_window(game->mlx->mlx, game->mlx->mlx_win,
game->link->ld[0], game->link->pos[0], game->link->pos[1]);
else if (c == 'l' && MODE == 1)
mlx_put_image_to_window(game->mlx->mlx, game->mlx->mlx_win,
game->link->ll[0], game->link->pos[0], game->link->pos[1]);
else if (c == 't' && MODE == 1)
mlx_put_image_to_window(game->mlx->mlx, game->mlx->mlx_win,
game->link->lt[0], game->link->pos[0], game->link->pos[1]);
else if (game->key == 49 && MODE == 1)
just_wait(game, game->prev_att);
else
mlx_put_image_to_window(game->mlx->mlx, game->mlx->mlx_win,
game->link->ld[0], game->link->pos[0], game->link->pos[1]);
}