Archived
1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
so_long/map_error/ft_check_map_content.c

91 lines
2.2 KiB
C
Raw Normal View History

2024-01-26 17:56:40 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_map_content.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/26 01:31:27 by adjoly #+# #+# */
2024-01-28 18:26:25 +01:00
/* Updated: 2024/01/27 13:58:18 by adjoly ### ########.fr */
2024-01-26 17:56:40 +01:00
/* */
/* ************************************************************************** */
#include "../so_long.h"
2024-01-28 18:26:25 +01:00
#include <stdlib.h>
2024-01-26 17:56:40 +01:00
char ft_check_reselement(t_elemcount *count)
{
if (count->p_count != 1)
2024-01-28 18:26:25 +01:00
{
free(count);
2024-01-26 17:56:40 +01:00
return (1);
2024-01-28 18:26:25 +01:00
}
2024-01-26 17:56:40 +01:00
else if (count->e_count != 1)
2024-01-28 18:26:25 +01:00
{
free(count);
2024-01-26 17:56:40 +01:00
return (2);
2024-01-28 18:26:25 +01:00
}
2024-01-26 17:56:40 +01:00
else if (count->c_count < 1)
2024-01-28 18:26:25 +01:00
{
free(count);
2024-01-26 17:56:40 +01:00
return (3);
2024-01-28 18:26:25 +01:00
}
free(count);
2024-01-26 17:56:40 +01:00
return (0);
}
char ft_check_element(char **map, t_coords *p_coords, t_elemcount *count)
{
int y;
int x;
y = -1;
count->c_count = 0;
count->e_count = 0;
count->p_count = 0;
while (map[++y])
{
x = -1;
while (map[y][++x])
{
if (map[y][x] == 'P')
{
count->p_count++;
p_coords->x = x;
p_coords->y = y;
}
else if (map[y][x] == 'E')
count->e_count++;
else if (map[y][x] == 'C')
count->c_count++;
}
}
return (ft_check_reselement(count));
}
void ft_check_map_content(char **map, t_coords *p_coords)
{
char check_result;
t_elemcount *count;
2024-01-28 18:26:25 +01:00
count = malloc(sizeof(t_elemcount));
if (!count)
ft_send_error("Memory allocation failed\n", map);
2024-01-26 17:56:40 +01:00
check_result = ft_check_element(map, p_coords, count);
if (check_result == 1)
{
free(p_coords);
ft_send_error("Invalid number of player\n", map);
}
if (check_result == 2)
{
free(p_coords);
ft_send_error("Invalid number of exit\n", map);
}
if (check_result == 3)
{
free(p_coords);
ft_send_error("Invalid number of collectible\n", map);
}
}