158 lines
3.6 KiB
C
158 lines
3.6 KiB
C
#include "cub.h"
|
|
|
|
int has_playable_neighbor(int x, int y, char **map)
|
|
{
|
|
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);
|
|
}
|
|
|
|
int check_isolated_walls(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 check_map_char(char **map)
|
|
{
|
|
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);
|
|
}
|
|
|
|
void find_player_pos(char **map, int *player)
|
|
{
|
|
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++;
|
|
}
|
|
}
|
|
|
|
int is_closed_at(int x, int y, char **map)
|
|
{
|
|
if (!map[y] || !map[y][x] || map[y][x] == '\0')
|
|
return (0);
|
|
if (map[y][x] == ' ')
|
|
return (0);
|
|
return (1);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|