push3
This commit is contained in:
+107
-141
@@ -1,157 +1,123 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* check_map.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/08/22 15:07:29 by lfirmin #+# #+# */
|
||||
/* Updated: 2025/10/07 10:11:50 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "cub.h"
|
||||
|
||||
int has_playable_neighbor(int x, int y, char **map)
|
||||
int check_char_and_count(char c, int *p)
|
||||
{
|
||||
if (map[y] && map[y][x + 1] &&
|
||||
(map[y][x + 1] == '0' || map[y][x + 1] == 'N' ||
|
||||
map[y][x + 1] == 'S' || map[y][x + 1] == 'E' ||
|
||||
map[y][x + 1] == 'W'))
|
||||
return (1);
|
||||
if (x > 0 && map[y][x - 1] &&
|
||||
(map[y][x - 1] == '0' || map[y][x - 1] == 'N' ||
|
||||
map[y][x - 1] == 'S' || map[y][x - 1] == 'E' ||
|
||||
map[y][x - 1] == 'W'))
|
||||
return (1);
|
||||
if (map[y + 1] && map[y + 1][x] &&
|
||||
(map[y + 1][x] == '0' || map[y + 1][x] == 'N' ||
|
||||
map[y + 1][x] == 'S' || map[y + 1][x] == 'E' ||
|
||||
map[y + 1][x] == 'W'))
|
||||
return (1);
|
||||
if (y > 0 && map[y - 1] && map[y - 1][x] &&
|
||||
(map[y - 1][x] == '0' || map[y - 1][x] == 'N' ||
|
||||
map[y - 1][x] == 'S' || map[y - 1][x] == 'E' ||
|
||||
map[y - 1][x] == 'W'))
|
||||
return (1);
|
||||
return (0);
|
||||
if (c == '1' || c == '0' || c == ' ' || c == '\n')
|
||||
return (0);
|
||||
else if (c == 'S' || c == 'N' || c == 'E' || c == 'W')
|
||||
{
|
||||
(*p)++;
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (2);
|
||||
}
|
||||
|
||||
int check_isolated_walls(char **map)
|
||||
int check_map_char(char **map)
|
||||
{
|
||||
int i = 0;
|
||||
int j;
|
||||
|
||||
while (map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (map[i][j])
|
||||
{
|
||||
if (map[i][j] == '1')
|
||||
{
|
||||
if (!has_playable_neighbor(j, i, map))
|
||||
return (0); // '1' isolé détecté
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (1); // Pas de '1' isolé
|
||||
int i;
|
||||
int j;
|
||||
int p;
|
||||
int ret;
|
||||
|
||||
i = 0;
|
||||
p = 0;
|
||||
while (map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (map[i][j])
|
||||
{
|
||||
ret = check_char_and_count(map[i][j], &p);
|
||||
if (ret != 0)
|
||||
return (ret);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (p != 1)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int check_map_char(char **map)
|
||||
void find_player_pos(char **map, int *player)
|
||||
{
|
||||
int i = 0;
|
||||
int j;
|
||||
int p = 0;
|
||||
|
||||
while (map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (map[i][j])
|
||||
{
|
||||
if (map[i][j] == '1' || map[i][j] == '0' ||
|
||||
map[i][j] == ' ' || map[i][j] == '\n')
|
||||
j++;
|
||||
else if (map[i][j] == 'S' || map[i][j] == 'N' ||
|
||||
map[i][j] == 'E' || map[i][j] == 'W')
|
||||
{
|
||||
j++;
|
||||
p++;
|
||||
}
|
||||
else
|
||||
return (2);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (p != 1)
|
||||
return (1);
|
||||
return (0);
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (map[i][j])
|
||||
{
|
||||
if (map[i][j] == 'S' || map[i][j] == 'N' ||
|
||||
map[i][j] == 'E' || map[i][j] == 'W')
|
||||
{
|
||||
player[0] = j;
|
||||
player[1] = i;
|
||||
player[2] = map[i][j];
|
||||
return ;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void find_player_pos(char **map, int *player)
|
||||
int flood_fill(char **map_cp, int x, int y)
|
||||
{
|
||||
int i = 0;
|
||||
int j;
|
||||
|
||||
while (map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (map[i][j])
|
||||
{
|
||||
if (map[i][j] == 'S' || map[i][j] == 'N' ||
|
||||
map[i][j] == 'E' || map[i][j] == 'W')
|
||||
{
|
||||
player[0] = j;
|
||||
player[1] = i;
|
||||
player[2] = map[i][j];
|
||||
return;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (!map_cp[y] || !map_cp[y][x])
|
||||
return (1);
|
||||
if (map_cp[y][x] == 'v')
|
||||
return (0);
|
||||
if (map_cp[y][x] == '1')
|
||||
{
|
||||
map_cp[y][x] = 'v';
|
||||
return (0);
|
||||
}
|
||||
if (map_cp[y][x] == '0' || map_cp[y][x] == 'N' || map_cp[y][x] == 'S'
|
||||
|| map_cp[y][x] == 'E' || map_cp[y][x] == 'W')
|
||||
{
|
||||
map_cp[y][x] = 'v';
|
||||
if (flood_fill(map_cp, x + 1, y))
|
||||
return (1);
|
||||
if (flood_fill(map_cp, x - 1, y))
|
||||
return (1);
|
||||
if (flood_fill(map_cp, x, y + 1))
|
||||
return (1);
|
||||
if (flood_fill(map_cp, x, y - 1))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int is_closed_at(int x, int y, char **map)
|
||||
int rep_ex_wall(char **map_cp, int x, int y)
|
||||
{
|
||||
if (!map[y] || !map[y][x] || map[y][x] == '\0')
|
||||
return (0);
|
||||
if (map[y][x] == ' ')
|
||||
return (0);
|
||||
return (1);
|
||||
if (y < 0 || x < 0)
|
||||
return (0);
|
||||
if (!map_cp[y] || !map_cp[y][x])
|
||||
return (0);
|
||||
if (map_cp[y][x] == '1')
|
||||
map_cp[y][x] = 'v';
|
||||
if (map_cp[y][x] == 'v')
|
||||
{
|
||||
map_cp[y][x] = 'c';
|
||||
rep_ex_wall(map_cp, x + 1, y);
|
||||
rep_ex_wall(map_cp, x - 1, y);
|
||||
rep_ex_wall(map_cp, x, y + 1);
|
||||
rep_ex_wall(map_cp, x, y - 1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int check_walls(char **map)
|
||||
{
|
||||
int i = 0;
|
||||
int j;
|
||||
|
||||
while (map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (map[i][j])
|
||||
{
|
||||
if (map[i][j] == '0' || map[i][j] == 'N' ||
|
||||
map[i][j] == 'S' || map[i][j] == 'E' || map[i][j] == 'W')
|
||||
{
|
||||
if (!is_closed_at(j + 1, i, map) ||
|
||||
!is_closed_at(j - 1, i, map) ||
|
||||
!is_closed_at(j, i + 1, map) ||
|
||||
!is_closed_at(j, i - 1, map))
|
||||
return (0);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int validate_map(char **map, int *player)
|
||||
{
|
||||
int result;
|
||||
|
||||
result = check_map_char(map);
|
||||
if (result == 1)
|
||||
return (ft_error(ERROR_PLAYER), 1);
|
||||
if (result == 2)
|
||||
return (ft_error(ERROR_CHAR), 1);
|
||||
|
||||
find_player_pos(map, player);
|
||||
|
||||
if (!check_walls(map))
|
||||
return (ft_error(ERROR_NOT_CLOSE), 1);
|
||||
// if (!check_isolated_walls(map))
|
||||
// return (ft_error("Isolated wall detected"), 1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user