Files
42_INT_10_cube3d/srcs/parsing/check_map.c
2022-04-23 20:12:13 +02:00

123 lines
2.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/18 12:37:48 by pblagoje #+# #+# */
/* Updated: 2022/04/23 19:18:16 by pblagoje ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube3d.h"
int set_player(t_map *map, int y, int x)
{
map->plr_x = x;
map->plr_y = y;
map->content[y][x] = '0';
return (1);
}
void set_points(int *y, int *x, int row, int col)
{
y[0] = row - 1;
y[1] = row;
y[2] = row + 1;
x[0] = col - 1;
x[1] = col;
x[2] = col + 1;
}
int check_spaces(t_map *map, int row, int col)
{
int y[3];
int x[3];
int i;
int j;
set_points(y, x, row, col);
i = 0;
while (i < 3)
{
j = 0;
while (j < 3)
{
if (y[i] > 0 && y[i] < map->size_y - 1 \
&& x[j] > 0 && x[j] < map->size_x - 1)
{
if (map->content[y[i]][x[j]] != '1' \
&& map->content[y[i]][x[j]] != ' ')
return (EXIT_FAILURE);
}
j++;
}
i++;
}
return (EXIT_SUCCESS);
}
int check_content(t_map *map)
{
int x;
int y;
int count;
count = 0;
y = 0;
while (map->content[y] != NULL)
{
x = 0;
while (map->content[y][x] != '\0' && map->content[y][x] != '\n')
{
if (map->content[y][x] == ' ' && check_spaces(map, y, x))
return (EXIT_FAILURE);
else if (!ft_strchr(" 01SNWE", map->content[y][x]))
return (EXIT_FAILURE);
else if (ft_strchr("SNWE", map->content[y][x]) \
&& set_player(map, y, x))
count++;
x++;
}
y++;
}
if (count != 1)
return (EXIT_FAILURE);
return (EXIT_SUCCESS);
}
int check_borders(t_map *map)
{
int x;
int y;
y = 0;
while (map->content[y] != NULL)
{
x = 0;
while (map->content[y][x] != '\0' && map->content[y][x] != '\n')
{
if (y == 0 || y == map->size_y - 1)
if (map->content[y][x] != '1' && map->content[y][x] != ' ')
return (EXIT_FAILURE);
if (x == 0 || x == map->size_x - 1)
if (map->content[y][x] != '1' && map->content[y][x] != ' ')
return (EXIT_FAILURE);
x++;
}
y++;
}
return (EXIT_SUCCESS);
}
int check_map(t_map *map)
{
if (check_borders(map) || check_content(map))
{
ft_putstr_fd("Error\nThe map characters are invalid.\n", 2);
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}