So_long/srcs/pars_map_2.c

107 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pars_map_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 17:51:27 by lfirmin #+# #+# */
/* Updated: 2024/11/12 17:51:27 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/so_long.h"
char **copy_map(char **original)
{
char **copy;
int i;
int height;
height = 0;
while (original[height])
height++;
copy = malloc(sizeof(char *) * (height + 1));
if (!copy)
return (NULL);
i = 0;
while (original[i])
{
copy[i] = ft_strdup(original[i]);
if (!copy[i])
{
while (i > 0)
free(copy[--i]);
free(copy);
return (NULL);
}
i++;
}
copy[i] = NULL;
return (copy);
}
int is_playable(t_data *data, int x, int y)
{
char **tmp_map;
tmp_map = copy_map(data->map);
if (!tmp_map)
return (clean_data(data), 1);
if (play_check(tmp_map, x, y) == 0)
return (free_map(tmp_map), 0);
return (free_map(tmp_map), 1);
}
int play_check(char **map, int x, int y)
{
if (map[y][x] == 'P')
return (0);
if (map[y][x] != '0' && map[y][x] != 'C' && map[y][x] != 'E')
return (2);
map[y][x] = '1';
if (play_check(map, x + 1, y) == 0)
return (0);
if (play_check(map, x - 1, y) == 0)
return (0);
if (play_check(map, x, y + 1) == 0)
return (0);
if (play_check(map, x, y - 1) == 0)
return (0);
return (1);
}
void free_map(char **map)
{
int i;
i = 0;
while (map[i])
free(map[i++]);
free(map);
}
int check_playable(t_data *data)
{
int i;
int j;
i = 1;
j = 1;
while (data->map[i + 1])
{
j = 1;
while (data->map[i][j + 1])
{
if (data->map[i][j] == 'C' || data->map[i][j] == 'E')
{
if (is_playable(data, j, i) == 1)
return (1);
}
j++;
}
i++;
}
return (0);
}