Archived
1
0

started norm

This commit is contained in:
Adam Joly
2024-01-26 17:56:40 +01:00
parent 02875e94a6
commit 20130c1522
92 changed files with 644 additions and 462 deletions

47
map_error/ft_check_file.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/26 14:29:02 by adjoly #+# #+# */
/* Updated: 2024/01/26 16:10:23 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../so_long.h"
char ft_check_file(char *file_name)
{
int fd;
fd = open(file_name, O_RDONLY);
if (fd < 1)
{
close(fd);
return (1);
}
close(fd);
return (0);
}
char ft_valid_file_ext(char *file_name)
{
unsigned short file_len;
file_len = ft_strlen(file_name) - 1;
if (file_len <= 3)
return (1);
else if (file_name[file_len] == 'r' && file_name[file_len - 1] == 'e' \
&& file_name[file_len - 2] == 'b' && file_name[file_len - 3] == '.')
return (0);
return (1);
}
char ft_is_empty(char **map)
{
if (map[0][0] == '\0')
return (1);
return (0);
}

BIN
map_error/ft_check_file.o Normal file

Binary file not shown.

View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_map_content.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/26 01:31:27 by adjoly #+# #+# */
/* Updated: 2024/01/26 16:10:32 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../so_long.h"
char ft_check_reselement(t_elemcount *count)
{
if (count->p_count != 1)
return (1);
else if (count->e_count != 1)
return (2);
else if (count->c_count < 1)
return (3);
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;
count = malloc(sizeof(t_elemcount *));
check_result = ft_check_element(map, p_coords, count);
if (check_result == 1)
{
free(count);
free(p_coords);
ft_send_error("Invalid number of player\n", map);
}
if (check_result == 2)
{
free(count);
free(p_coords);
ft_send_error("Invalid number of exit\n", map);
}
if (check_result == 3)
{
free(count);
free(p_coords);
ft_send_error("Invalid number of collectible\n", map);
}
}

Binary file not shown.

View File

@ -0,0 +1,103 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_map_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/17 10:17:52 by adjoly #+# #+# */
/* Updated: 2024/01/26 16:10:46 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../so_long.h"
char ft_checkcol(char **map, char c, unsigned short col)
{
unsigned short i;
i = 0;
while (map[i] && i < USHRT_MAX)
{
if (map[i][col] != c)
return (1);
i++;
}
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);
}
char ft_valid_char(char **map)
{
size_t y;
size_t x;
y = 0;
while (map[y])
{
x = 0;
while (map[y][x])
{
if (ft_check_charset(map[y][x], "01CEP") == 1)
return (1);
x++;
}
y++;
}
return (0);
}
char ft_checkline(char *map_line, char c)
{
unsigned short i;
i = 0;
while (*map_line && i < USHRT_MAX)
{
if (*map_line != c)
return (1);
map_line++;
i++;
}
return (0);
}
void ft_check_map_error(char **map)
{
t_coords *p_coords;
p_coords = malloc(sizeof(t_coords));
if (!p_coords)
ft_send_error("Memory allocation failed\n", map);
if (ft_is_empty(map) == 1)
{
free(p_coords);
ft_send_error("Map is empty", map);
}
if (ft_valid_char(map) == 1)
{
free(p_coords);
ft_send_error("Invalid character in map\n", map);
}
if (ft_is_rectangular(map) == 1)
{
free(p_coords);
ft_send_error("Map is not rectengular\n", map);
}
ft_check_map_content(map, p_coords);
ft_check_map_state(map, p_coords);
free(p_coords);
}

Binary file not shown.

View File

@ -0,0 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_map_state.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/26 14:36:23 by adjoly #+# #+# */
/* Updated: 2024/01/26 16:10:55 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../so_long.h"
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)
{
size_t x;
size_t y;
ft_flood(p_coords->x, p_coords->y, map);
y = 0;
while (map[y])
{
x = 0;
while (map[y][x])
{
if (map[y][x] == 'C' || map[y][x] == 'E')
return (1);
x++;
}
y++;
}
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);
}
void ft_check_map_state(char **map, t_coords *p_coords)
{
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);
}
}

Binary file not shown.

View File

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check_map_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/26 15:28:38 by adjoly #+# #+# */
/* Updated: 2024/01/26 16:12:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "../so_long.h"
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);
}
char ft_check_charset(char c, char *charset)
{
unsigned short i;
i = 0;
while (charset[i] && i < USHRT_MAX)
{
if (c == charset[i])
return (0);
i++;
}
return (1);
}

Binary file not shown.