-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlines_check.c
108 lines (98 loc) · 2.44 KB
/
lines_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lines_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhlim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/13 15:34:45 by zhlim #+# #+# */
/* Updated: 2023/07/21 21:00:15 by zhlim ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
size_t ft_strlen_sl(const char *s)
{
int i;
i = 0;
while (s[i] && s[i] != '\n')
i++;
return (i);
}
void check_first_last_rows(t_map *map)
{
int i;
int j;
char *lines[2];
i = 0;
j = 0;
lines[0] = map->grid[0];
lines[1] = map->grid[map->rows - 1];
while (i < 2)
{
while (lines[i][j] && lines[i][j] != '\n')
{
if (lines[i][j] != '1')
free_error_exit(map, "Walls are not closed");
j++;
}
i++;
j = 0;
}
}
void check_coordinate(t_map *map, char c, int col, int row)
{
if (c != EMPTY && c != WALL)
{
if (c == PLAYER)
{
map->start_count++;
map->player.x = col;
map->player.y = row;
}
else if (c == EXIT)
{
map->exit_count++;
map->exit.x = col;
map->exit.y = row;
}
else if (c == COLLECTIBLE)
map->collectible_count++;
else
free_error_exit(map, "Unknown character include");
}
}
int check_line(t_map *map, int row)
{
int i;
char *line;
i = 0;
line = map->grid[row];
if (line[i] != '1')
free_error_exit(map, "Walls are not closed");
while (line[++i] && line[i] != '\n')
check_coordinate(map, line[i], i, row);
if (line[i - 1] != '1')
free_error_exit(map, "Walls are not closed");
return (i);
}
void lines_check(t_map *map)
{
int line_length;
int i;
i = 0;
line_length = 0;
if (map->grid != NULL)
{
line_length = ft_strlen_sl(map->grid[0]);
map->columns = line_length;
check_first_last_rows(map);
}
while (i < map->rows)
{
if (*(map->grid[i]) == '\n')
break ;
else if (check_line(map, i) != line_length)
free_error_exit(map, "Map is not rectangular");
i++;
}
}