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/ft_check_map_error.c

281 lines
5.0 KiB
C
Raw Normal View History

2024-01-19 15:41:43 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_map_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/17 10:17:52 by adjoly #+# #+# */
2024-01-24 13:45:01 +01:00
/* Updated: 2024/01/24 12:35:46 by adjoly ### ########.fr */
2024-01-19 15:41:43 +01:00
/* */
/* ************************************************************************** */
2024-01-24 13:45:01 +01:00
#include "libft/libft.h"
2024-01-19 15:41:43 +01:00
#include "printf/ft_printf.h"
#include "so_long.h"
2024-01-22 14:13:58 +01:00
#include <limits.h>
2024-01-24 13:45:01 +01:00
#include <stddef.h>
#include <stdlib.h>
2024-01-19 15:41:43 +01:00
char ft_check_charset(char c, char *charset)
{
2024-01-22 14:13:58 +01:00
unsigned short i;
i = 0;
while (charset[i] && i < USHRT_MAX)
2024-01-19 15:41:43 +01:00
{
2024-01-22 14:13:58 +01:00
if (c == charset[i])
return (0);
i++;
2024-01-19 15:41:43 +01:00
}
2024-01-22 14:13:58 +01:00
return (1);
}
char ft_check_file(char *file_name)
{
int fd;
fd = open(file_name, O_RDONLY);
if (fd < 1)
2024-01-24 13:45:01 +01:00
{
close(fd);
2024-01-22 14:13:58 +01:00
return (1);
2024-01-24 13:45:01 +01:00
}
close(fd);
2024-01-19 15:41:43 +01:00
return (0);
}
char ft_valid_char(char **map)
{
size_t y;
size_t x;
y = 0;
while (map[y])
{
x = 0;
while (map[y][x])
{
2024-01-24 13:45:01 +01:00
if (ft_check_charset(map[y][x], "01CEP") == 1)
2024-01-19 15:41:43 +01:00
return (1);
x++;
}
y++;
}
return (0);
}
2024-01-24 13:45:01 +01:00
char ft_check_element(char **map, t_coords *p_coords)
2024-01-22 14:13:58 +01:00
{
size_t y;
size_t x;
size_t p_count;
2024-01-24 13:45:01 +01:00
size_t e_count;
size_t c_count;
2024-01-22 14:13:58 +01:00
y = 0;
p_count = 0;
2024-01-24 13:45:01 +01:00
e_count = 0;
c_count = 0;
2024-01-22 14:13:58 +01:00
while (map[y])
{
x = 0;
while (map[y][x])
{
if (map[y][x] == 'P')
2024-01-24 13:45:01 +01:00
{
2024-01-22 14:13:58 +01:00
p_count++;
2024-01-24 13:45:01 +01:00
p_coords->x = x;
p_coords->y = y;
}
else if (map[y][x] == 'E')
e_count++;
else if (map[y][x] == 'C')
c_count++;
2024-01-22 14:13:58 +01:00
x++;
}
y++;
}
2024-01-24 13:45:01 +01:00
if (p_count != 1)
return (1);
else if (e_count != 1)
return (2);
else if (c_count < 1)
return (3);
return (0);
2024-01-22 14:13:58 +01:00
}
2024-01-24 13:45:01 +01:00
void ft_flood(int x, int y, char **map)
{
if (map[y][x] != '1')
{
map[y][x] = '1';
ft_flood(x - 1, y, map);
ft_flood(x + 1, y, map);
ft_flood(x, y - 1, map);
ft_flood(x, y + 1, map);
}
}
char ft_floodfill(char **map, t_coords *p_coords)
2024-01-22 14:13:58 +01:00
{
size_t x;
2024-01-24 13:45:01 +01:00
size_t y;
2024-01-22 14:13:58 +01:00
2024-01-24 13:45:01 +01:00
ft_flood(p_coords->x, p_coords->y, map);
2024-01-22 14:13:58 +01:00
y = 0;
while (map[y])
{
x = 0;
while (map[y][x])
{
2024-01-24 13:45:01 +01:00
if (map[y][x] == 'C' || map[y][x] == 'E')
return (1);
2024-01-22 14:13:58 +01:00
x++;
}
y++;
}
2024-01-24 13:45:01 +01:00
return (0);
2024-01-22 14:13:58 +01:00
}
2024-01-24 13:45:01 +01:00
char ft_checkline(char *map_line, char c)
2024-01-22 14:13:58 +01:00
{
2024-01-24 13:45:01 +01:00
unsigned short i;
2024-01-22 14:13:58 +01:00
2024-01-24 13:45:01 +01:00
i = 0;
while (*map_line && i < USHRT_MAX)
2024-01-22 14:13:58 +01:00
{
2024-01-24 13:45:01 +01:00
if (*map_line != c)
return (1);
map_line++;
i++;
2024-01-22 14:13:58 +01:00
}
2024-01-24 13:45:01 +01:00
return (0);
2024-01-22 14:13:58 +01:00
}
2024-01-24 13:45:01 +01:00
char ft_checkcol(char **map, char c, unsigned short col)
2024-01-19 15:41:43 +01:00
{
2024-01-24 13:45:01 +01:00
unsigned short i;
i = 0;
while (map[i] && i < USHRT_MAX)
2024-01-19 15:41:43 +01:00
{
2024-01-24 13:45:01 +01:00
if (map[i][col] != c)
return (1);
i++;
2024-01-19 15:41:43 +01:00
}
2024-01-24 13:45:01 +01:00
return (0);
}
char ft_is_closed(char **map)
{
unsigned short size_map;
unsigned short size_line;
unsigned short i;
unsigned short res;
i = 0;
size_map = ft_mapsize(map);
size_line = ft_strlen(map[0]);
res = ft_checkcol(map, '1', 0);
res += ft_checkcol(map, '1', size_line - 1);
res += ft_checkline(map[0], '1');
res += ft_checkline(map[size_map - 1], '1');
if (res > 0)
return (1);
return (0);
}
char ft_is_empty(char **map)
{
if (map[0][0] == '\0')
return (1);
return (0);
}
char ft_is_rectangular(char **map)
{
size_t len_map;
len_map = ft_strlen(*map);
while (*map)
{
if (ft_strlen(*map) != len_map)
return (1);
map++;
}
return (0);
}
2024-01-19 15:41:43 +01:00
char ft_valid_file_ext(char *file_name)
{
unsigned short file_len;
file_len = ft_strlen(file_name) - 1;
2024-01-24 13:45:01 +01:00
if (file_len <= 3)
2024-01-22 14:13:58 +01:00
return (1);
else if (file_name[file_len] == 'r' && file_name[file_len - 1] == 'e' \
2024-01-19 15:41:43 +01:00
&& file_name[file_len - 2] == 'b' && file_name[file_len - 3] == '.')
return (0);
return (1);
}
void ft_send_error(char *msg, char **map)
{
ft_putstr_fd("Error\n", 1);
ft_putstr_fd(msg, 1);
if (map)
ft_freemap(map);
exit(EXIT_SUCCESS);
}
void ft_check_map_error(char **map)
{
2024-01-24 13:45:01 +01:00
char check_result;
t_coords *p_coords;
p_coords = malloc(sizeof(t_coords));
if (ft_is_empty(map) == 1)
{
free(p_coords);
ft_send_error("Map is empty", map);
}
2024-01-19 15:41:43 +01:00
if (ft_valid_char(map) == 1)
2024-01-24 13:45:01 +01:00
{
free(p_coords);
2024-01-19 15:41:43 +01:00
ft_send_error("Invalid character in map\n", map);
2024-01-24 13:45:01 +01:00
}
if (ft_is_rectangular(map) == 1)
{
free(p_coords);
ft_send_error("Map is not rectengular\n", map);
}
check_result = ft_check_element(map, p_coords);
if (check_result == 1)
{
free(p_coords);
2024-01-22 14:13:58 +01:00
ft_send_error("Invalid number of player\n", map);
2024-01-24 13:45:01 +01:00
}
if (check_result == 2)
{
free(p_coords);
2024-01-22 14:13:58 +01:00
ft_send_error("Invalid number of exit\n", map);
2024-01-24 13:45:01 +01:00
}
if (check_result == 3)
{
free(p_coords);
ft_send_error("Invalid number of collectible\n", map);
}
if (ft_is_closed(map) == 1)
{
free(p_coords);
ft_send_error("Map is not closed\n", map);
}
if (ft_floodfill(map, p_coords) == 1)
{
free(p_coords);
ft_send_error("Map is cannot be finished\n", map);
}
free(p_coords);
2024-01-19 15:41:43 +01:00
}