int invalid_ext(char *filename)
{
char *temp;
temp = ft_strnstr(filename, ".ber", ft_strlen(filename));
if (temp == NULL)
return (1); // .ber을 발견하지 못함.
else if (ft_strlen(temp) > 4)
return (1); // .ber을 발견했으나 .ber로 끝나지 않음.
else
return (0);
}
strnstr은 두번째인자(.ber)이 첫번째인자에 있다면 그 위치를 반환해줍니다.
.ber을 찾았더라도 filename의 끝이 .ber로 끝나지 않는다면 유효하지 않습니다.
void malloc_map(t_game *game, char *filename)
{
char *line;
int fd;
int i;
fd = open_file(filename);
line = get_next_line(fd);
game->map.height = 0;
game->map.width = ft_strlen(line) - 1; //맵의 너비를 구함
while (line)
{
free(line);
line = get_next_line(fd);
game->map.height++; //맵의 높이를 구함
}
close(fd);
game->map.pos = (char **)malloc(game->map.height * sizeof(char *));
i = -1;
while (++i < game->map.height)
game->map.pos[i] = (char *)malloc(game->map.width * sizeof(char));
}
우선 맵파일의 첫번째 줄의 너비를 기준으로 2차원 배열의 형태로 동적할당을 받습니다.
그 다음 맵파일의 모든 줄을 다시 읽어들여 맵이 사각형인지 확인합니다.
while (1)
{
line = get_next_line(fd);
if (line == NULL)
break ;
if (game->map.width != ft_strlen(line))
#에러#;
// 기존에 설정했던 map.width와 길이가 다른 줄이 있다면 사각형이 아님
free(line);
}
할당 받은 메모리에 맵의 모든 요소들을 넣어줍니다.
void is_walled(t_game *game)
{
int i;
i = -1;
while (++i < game->map.height)
if (game->map.pos[i][0] != '1' || \
game->map.pos[i][game->map.width - 1] != '1')
exit_err("Not walled.\n");
i = -1;
while (++i < game->map.width)
if (game->map.pos[0][i] != '1' || \
game->map.pos[game->map.height - 1][i] != '1')
exit_err("Not walled.\n");
}
맵이 벽으로 둘러쌓여 있으려면