2push
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
@@ -20,5 +20,7 @@ int parsing(t_data *data)
|
||||
close(data->parsing.fd_map_dup);
|
||||
if (check_colors(data->texture) == 1)
|
||||
return (1);
|
||||
if (validate_map(data->parsing.raw_map, data->parsing.player) == 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,4 +32,3 @@ void free_data(t_data *data)
|
||||
if (data->texture)
|
||||
free(data->texture);
|
||||
}
|
||||
|
||||
|
||||
@@ -34,4 +34,12 @@ void free_char_array(char **array)
|
||||
while (array[i])
|
||||
free(array[i++]);
|
||||
free(array);
|
||||
}
|
||||
|
||||
int ft_arrlen(char **arr)
|
||||
{
|
||||
int i;
|
||||
while(arr[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
Reference in New Issue
Block a user