-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_check.c
123 lines (113 loc) · 2.8 KB
/
map_check.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
119
120
121
122
123
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qduong <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/20 16:40:12 by qduong #+# #+# */
/* Updated: 2022/04/22 18:27:28 by qduong ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <stdio.h>
void parse_map(char **argv, t_vars *mlx)
{
int fd;
int i;
mlx->map = ft_calloc(mlx->game.dim.y + 1, sizeof(char *));
i = 0;
fd = open(*argv, O_RDONLY);
while (i < mlx->game.dim.y)
{
mlx->map[i] = get_next_line(fd);
//printf("Length x:[%d]%zu\n", i, ft_len(mlx->map[i]));
if (ft_len(mlx->map[i]) != (size_t)(mlx->game.dim.x))
{
freeme(mlx, i, fd);
exit(ft_putstr_fd("Map not straight", 2));
}
//printf("Map string[%i]:%s", i, mlx->map[i]);
i++;
}
close(fd);
return ;
}
void check_wall(t_vars *mlx)
{
int y;
int x;
int i;
i = 0;
y = 0;
x = 0;
while (y < mlx->game.dim.y)
{
x = 0;
while (x < mlx->game.dim.x)
{
if (x == 0 || x == mlx->game.dim.x - 1 || \
y == 0 || y == mlx->game.dim.y - 1)
{
if ((mlx->map[y])[x] != '1')
{
freeme2(mlx);
exit(ft_putstr_fd("Not walled", 2));
}
}
x++;
}
y++;
}
}
//if 2 players-> 2nd player will be replaced by empty space
int check_p(t_vars *mlx, int *y, int *x)
{
if (mlx->map[*y][*x] == 'P' && mlx->game.start == 0)
{
mlx->game.start++;
mlx->game.pos.x = *x;
mlx->game.pos.y = *y;
return (0);
}
else if (mlx->map[*y][*x] == 'P' && mlx->game.start > 0)
{
mlx->map[*y][*x] = '0';
return (0);
}
return (1);
}
void check_items(t_vars *mlx)
{
int y;
int x;
y = -1;
while (++y < mlx->game.dim.y)
{
x = -1;
while (++x < mlx->game.dim.x)
{
if (!check_p(mlx, &y, &x))
continue ;
else if (mlx->map[y][x] == 'E')
mlx->game.exit++;
else if (mlx->map[y][x] == 'C')
mlx->game.collectibles++;
else if (mlx->map[y][x] != '0' && mlx->map[y][x] != '1')
{
freeme2(mlx);
exit (ft_putstr_fd("Map contains wrong characters", 2));
}
}
}
}
void full_check_map(t_vars *mlx)
{
check_wall(mlx);
check_items(mlx);
if (mlx->game.start < 1 || mlx->game.collectibles < 1 || mlx->game.exit < 1)
{
freeme2(mlx);
exit (ft_putstr_fd("Missing P, E or C", 2));
}
}