so_long/games_bonus/map.c
Etienne Rey-bethbeder 972031a1f4 Finish
2023-01-09 20:17:34 +01:00

118 lines
3.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/06 14:01:28 by erey-bet #+# #+# */
/* Updated: 2023/01/06 14:49:58 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../so_long_bonus.h"
int is_rectangular(t_data *data, char **map)
{
int x;
int y;
int w_map;
x = 0;
w_map = -1;
while (map[x])
{
y = 0;
while (map[x][y])
{
if (map[x][y] == ' ')
return (0);
y++;
}
if (w_map != -1 && y != w_map)
return (0);
w_map = y;
x++;
}
data->w_map = w_map;
return (1);
}
int is_surrounded_by_wall(char **map, int h_map)
{
int x;
int y;
x = 0;
y = 0;
while (map[x] && map[x][y])
{
if (map[x][y] != '1')
return (0);
y++;
if (!map[x][y] && h_map - 1 != x)
{
x = h_map - 1;
y = 0;
}
}
while (x > 0)
{
if (!map[x] || map[x][0] != '1' || map[x][y - 1] != '1')
return (0);
x--;
}
return (1);
}
int is_possible(char **map_cpy, int x, int y, int *check)
{
if (map_cpy[x][y] == 'E')
return (1);
map_cpy[x][y] = 'x';
if (map_cpy[x][y + 1] != '1' && map_cpy[x][y + 1] != 'x')
if (is_possible(map_cpy, x, y + 1, check))
*check = 1;
if (map_cpy[x + 1][y] != '1' && map_cpy[x + 1][y] != 'x')
if (is_possible(map_cpy, x + 1, y, check))
*check = 1;
if (map_cpy[x - 1][y] != '1' && map_cpy[x - 1][y] != 'x')
if (is_possible(map_cpy, x - 1, y, check))
*check = 1;
if (map_cpy[x][y - 1] != '1' && map_cpy[x][y - 1] != 'x')
if (is_possible(map_cpy, x, y - 1, check))
*check = 1;
if (!has_element(map_cpy, 'C') && *check)
return (1);
return (0);
}
char *get_map(char *argv[], t_data *data)
{
int i;
char **map_cpy;
read_map(argv, data);
if (data->map == NULL)
return ("Error\nProbleme de lecture de la map");
if (!has_element(data->map, 'C') || !has_element(data->map, 'E')
|| !has_element(data->map, 'P') || !has_element(data->map, '1'))
return ("Error\nIl manque des elements");
if (!is_rectangular(data, data->map))
return ("Error\nLa carte n'est pas rectangulaire");
if (!is_surrounded_by_wall(data->map, data->h_map))
return ("Error\nLa carte n'est pas entoure de mur");
if (!too_much(data))
return ("Error\nLa carte comporte des elements invalide");
data = set_position_player(data, get_position(data->map, 'P'));
i = 0;
map_cpy = ft_strdups(data->map);
if (!is_possible(map_cpy, data->y_ply, data->x_ply, &i))
return ("Error\nLa carte est impossible a faire");
i = 0;
while (map_cpy[i])
free(map_cpy[i++]);
free(map_cpy);
return (NULL);
}