need little fixes but overall working
This commit is contained in:
@ -6,13 +6,16 @@
|
|||||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/17 10:17:52 by adjoly #+# #+# */
|
/* Created: 2024/01/17 10:17:52 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/01/22 13:24:41 by adjoly ### ########.fr */
|
/* Updated: 2024/01/24 12:35:46 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "libft/libft.h"
|
||||||
#include "printf/ft_printf.h"
|
#include "printf/ft_printf.h"
|
||||||
#include "so_long.h"
|
#include "so_long.h"
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
char ft_check_charset(char c, char *charset)
|
char ft_check_charset(char c, char *charset)
|
||||||
{
|
{
|
||||||
@ -34,7 +37,11 @@ char ft_check_file(char *file_name)
|
|||||||
|
|
||||||
fd = open(file_name, O_RDONLY);
|
fd = open(file_name, O_RDONLY);
|
||||||
if (fd < 1)
|
if (fd < 1)
|
||||||
|
{
|
||||||
|
close(fd);
|
||||||
return (1);
|
return (1);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +56,7 @@ char ft_valid_char(char **map)
|
|||||||
x = 0;
|
x = 0;
|
||||||
while (map[y][x])
|
while (map[y][x])
|
||||||
{
|
{
|
||||||
if (ft_check_charset(map[y][x], "01CEP\n") == 1)
|
if (ft_check_charset(map[y][x], "01CEP") == 1)
|
||||||
return (1);
|
return (1);
|
||||||
x++;
|
x++;
|
||||||
}
|
}
|
||||||
@ -58,93 +65,154 @@ char ft_valid_char(char **map)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
char ft_check_player(char **map)
|
char ft_check_element(char **map, t_coords *p_coords)
|
||||||
{
|
{
|
||||||
size_t y;
|
size_t y;
|
||||||
size_t x;
|
size_t x;
|
||||||
size_t p_count;
|
size_t p_count;
|
||||||
|
size_t e_count;
|
||||||
y = 0;
|
|
||||||
p_count = 0;
|
|
||||||
while (map[y])
|
|
||||||
{
|
|
||||||
x = 0;
|
|
||||||
while (map[y][x])
|
|
||||||
{
|
|
||||||
if (map[y][x] == 'P')
|
|
||||||
p_count++;
|
|
||||||
x++;
|
|
||||||
}
|
|
||||||
y++;
|
|
||||||
}
|
|
||||||
if (p_count == 1)
|
|
||||||
return (0);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char ft_check_exit(char **map)
|
|
||||||
{
|
|
||||||
size_t y;
|
|
||||||
size_t x;
|
|
||||||
size_t p_count;
|
|
||||||
|
|
||||||
y = 0;
|
|
||||||
p_count = 0;
|
|
||||||
while (map[y])
|
|
||||||
{
|
|
||||||
x = 0;
|
|
||||||
while (map[y][x])
|
|
||||||
{
|
|
||||||
if (map[y][x] == 'E')
|
|
||||||
p_count++;
|
|
||||||
x++;
|
|
||||||
}
|
|
||||||
y++;
|
|
||||||
}
|
|
||||||
if (p_count == 1)
|
|
||||||
return (0);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char ft_check_collectible(char **map)
|
|
||||||
{
|
|
||||||
size_t y;
|
|
||||||
size_t x;
|
|
||||||
size_t c_count;
|
size_t c_count;
|
||||||
|
|
||||||
y = 0;
|
y = 0;
|
||||||
|
p_count = 0;
|
||||||
|
e_count = 0;
|
||||||
c_count = 0;
|
c_count = 0;
|
||||||
while (map[y])
|
while (map[y])
|
||||||
{
|
{
|
||||||
x = 0;
|
x = 0;
|
||||||
while (map[y][x])
|
while (map[y][x])
|
||||||
{
|
{
|
||||||
if (map[y][x] == 'C')
|
if (map[y][x] == 'P')
|
||||||
|
{
|
||||||
|
p_count++;
|
||||||
|
p_coords->x = x;
|
||||||
|
p_coords->y = y;
|
||||||
|
}
|
||||||
|
else if (map[y][x] == 'E')
|
||||||
|
e_count++;
|
||||||
|
else if (map[y][x] == 'C')
|
||||||
c_count++;
|
c_count++;
|
||||||
x++;
|
x++;
|
||||||
}
|
}
|
||||||
y++;
|
y++;
|
||||||
}
|
}
|
||||||
if (c_count >= 1)
|
if (p_count != 1)
|
||||||
return (0);
|
|
||||||
return (1);
|
return (1);
|
||||||
|
else if (e_count != 1)
|
||||||
|
return (2);
|
||||||
|
else if (c_count < 1)
|
||||||
|
return (3);
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*char ft_floodfill(char **map)
|
void ft_flood(int x, int y, char **map)
|
||||||
{
|
{
|
||||||
while (expression)
|
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);
|
||||||
}
|
}
|
||||||
// partir dans toutes les direction a partir du P et sur tout les point suivant
|
}
|
||||||
}*/
|
|
||||||
|
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_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
char ft_is_empty(char **map)
|
||||||
|
{
|
||||||
|
if (map[0][0] == '\0')
|
||||||
|
return (1);
|
||||||
|
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_file_ext(char *file_name)
|
char ft_valid_file_ext(char *file_name)
|
||||||
{
|
{
|
||||||
unsigned short file_len;
|
unsigned short file_len;
|
||||||
|
|
||||||
file_len = ft_strlen(file_name) - 1;
|
file_len = ft_strlen(file_name) - 1;
|
||||||
if (file_len < 3)
|
if (file_len <= 3)
|
||||||
return (1);
|
return (1);
|
||||||
else if (file_name[file_len] == 'r' && file_name[file_len - 1] == 'e' \
|
else if (file_name[file_len] == 'r' && file_name[file_len - 1] == 'e' \
|
||||||
&& file_name[file_len - 2] == 'b' && file_name[file_len - 3] == '.')
|
&& file_name[file_len - 2] == 'b' && file_name[file_len - 3] == '.')
|
||||||
@ -163,15 +231,50 @@ void ft_send_error(char *msg, char **map)
|
|||||||
|
|
||||||
void ft_check_map_error(char **map)
|
void ft_check_map_error(char **map)
|
||||||
{
|
{
|
||||||
|
char check_result;
|
||||||
|
t_coords *p_coords;
|
||||||
|
|
||||||
|
p_coords = malloc(sizeof(t_coords));
|
||||||
|
if (ft_is_empty(map) == 1)
|
||||||
|
{
|
||||||
|
free(p_coords);
|
||||||
|
ft_send_error("Map is empty", map);
|
||||||
|
}
|
||||||
if (ft_valid_char(map) == 1)
|
if (ft_valid_char(map) == 1)
|
||||||
|
{
|
||||||
|
free(p_coords);
|
||||||
ft_send_error("Invalid character in map\n", map);
|
ft_send_error("Invalid character in map\n", map);
|
||||||
else if (ft_check_player(map) == 1)
|
}
|
||||||
|
if (ft_is_rectangular(map) == 1)
|
||||||
|
{
|
||||||
|
free(p_coords);
|
||||||
|
ft_send_error("Map is not rectengular\n", map);
|
||||||
|
}
|
||||||
|
check_result = ft_check_element(map, p_coords);
|
||||||
|
if (check_result == 1)
|
||||||
|
{
|
||||||
|
free(p_coords);
|
||||||
ft_send_error("Invalid number of player\n", map);
|
ft_send_error("Invalid number of player\n", map);
|
||||||
else if (ft_check_collectible(map) == 1)
|
}
|
||||||
ft_send_error("Invalid number of collectible\n", map);
|
if (check_result == 2)
|
||||||
else if (ft_check_exit(map) == 1)
|
{
|
||||||
|
free(p_coords);
|
||||||
ft_send_error("Invalid number of exit\n", map);
|
ft_send_error("Invalid number of exit\n", map);
|
||||||
// else if ()
|
}
|
||||||
// else if (ft_floodfill(map) == 1)
|
if (check_result == 3)
|
||||||
// ft_send_error("Map is cannot be finished\n", map);
|
{
|
||||||
|
free(p_coords);
|
||||||
|
ft_send_error("Invalid number of collectible\n", map);
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
free(p_coords);
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/07 19:13:01 by adjoly #+# #+# */
|
/* Created: 2024/01/07 19:13:01 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/01/21 15:29:10 by adjoly ### ########.fr */
|
/* Updated: 2024/01/24 12:16:14 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -21,6 +21,7 @@ void ft_move_up(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
||||||
win->c_count--;
|
win->c_count--;
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->p_coords->y == win->e_coords->y
|
else if (win->p_coords->y == win->e_coords->y
|
||||||
&& win->e_coords->x == win->p_coords->x
|
&& win->e_coords->x == win->p_coords->x
|
||||||
@ -29,6 +30,7 @@ void ft_move_up(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->map[win->p_coords->y - 1][win->p_coords->x] == 'E'
|
else if (win->map[win->p_coords->y - 1][win->p_coords->x] == 'E'
|
||||||
&& win->c_count == 0)
|
&& win->c_count == 0)
|
||||||
@ -41,6 +43,7 @@ void ft_move_up(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +56,7 @@ void ft_move_down(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
||||||
win->c_count--;
|
win->c_count--;
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->p_coords->y == win->e_coords->y
|
else if (win->p_coords->y == win->e_coords->y
|
||||||
&& win->e_coords->x == win->p_coords->x
|
&& win->e_coords->x == win->p_coords->x
|
||||||
@ -61,6 +65,7 @@ void ft_move_down(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->map[win->p_coords->y + 1][win->p_coords->x] == 'E'
|
else if (win->map[win->p_coords->y + 1][win->p_coords->x] == 'E'
|
||||||
&& win->c_count == 0)
|
&& win->c_count == 0)
|
||||||
@ -73,6 +78,7 @@ void ft_move_down(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,6 +91,7 @@ void ft_move_left(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
||||||
win->c_count--;
|
win->c_count--;
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->p_coords->y == win->e_coords->y
|
else if (win->p_coords->y == win->e_coords->y
|
||||||
&& win->e_coords->x == win->p_coords->x
|
&& win->e_coords->x == win->p_coords->x
|
||||||
@ -93,6 +100,7 @@ void ft_move_left(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->map[win->p_coords->y][win->p_coords->x - 1] == 'E'
|
else if (win->map[win->p_coords->y][win->p_coords->x - 1] == 'E'
|
||||||
&& win->c_count == 0)
|
&& win->c_count == 0)
|
||||||
@ -105,6 +113,7 @@ void ft_move_left(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,6 +126,7 @@ void ft_move_right(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
||||||
win->c_count--;
|
win->c_count--;
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->p_coords->y == win->e_coords->y
|
else if (win->p_coords->y == win->e_coords->y
|
||||||
&& win->e_coords->x == win->p_coords->x
|
&& win->e_coords->x == win->p_coords->x
|
||||||
@ -125,6 +135,7 @@ void ft_move_right(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
else if (win->map[win->p_coords->y][win->p_coords->x + 1] == 'E'
|
else if (win->map[win->p_coords->y][win->p_coords->x + 1] == 'E'
|
||||||
&& win->c_count == 0)
|
&& win->c_count == 0)
|
||||||
@ -137,6 +148,7 @@ void ft_move_right(t_window *win)
|
|||||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
||||||
win->mov_count++;
|
win->mov_count++;
|
||||||
|
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,6 +167,5 @@ int ft_key_event(int key, void *param)
|
|||||||
ft_move_left(win);
|
ft_move_left(win);
|
||||||
else if (key == 7 || key == 79)
|
else if (key == 7 || key == 79)
|
||||||
ft_move_right(win);
|
ft_move_right(win);
|
||||||
ft_printf("Mouvement : %d\n", win->mov_count);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
74
get_map.c
74
get_map.c
@ -6,65 +6,77 @@
|
|||||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/08 13:13:18 by adjoly #+# #+# */
|
/* Created: 2024/01/08 13:13:18 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/01/19 17:14:11 by adjoly ### ########.fr */
|
/* Updated: 2024/01/23 11:45:20 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "printf/ft_printf.h"
|
||||||
#include "so_long.h"
|
#include "so_long.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
size_t ft_countline_fd(char *file_name)
|
size_t ft_filesize(char *file_name)
|
||||||
{
|
{
|
||||||
size_t line_count;
|
size_t i;
|
||||||
|
int fd;
|
||||||
ssize_t rd;
|
ssize_t rd;
|
||||||
char *buf;
|
char *buf;
|
||||||
size_t i;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
fd = open(file_name, O_RDONLY);
|
fd = open(file_name, O_RDONLY);
|
||||||
if (fd < 1)
|
buf = ft_calloc(1, 1);
|
||||||
|
if (!buf)
|
||||||
return (0);
|
return (0);
|
||||||
i = 0;
|
i = 0;
|
||||||
line_count = 0;
|
while (buf && i < SIZE_MAX)
|
||||||
buf = ft_calloc(1, 1);
|
|
||||||
while (i < ULONG_MAX)
|
|
||||||
{
|
{
|
||||||
rd = read(fd, buf, 1);
|
rd = read(fd, buf, 1);
|
||||||
if (rd == -1 || rd == 0)
|
if (rd <= 0)
|
||||||
break ;
|
break ;
|
||||||
else if (buf[0] == '\n')
|
|
||||||
line_count++;
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
close(fd);
|
|
||||||
free(buf);
|
free(buf);
|
||||||
return (line_count);
|
close(fd);
|
||||||
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
char **ft_read_map(char *file_name)
|
char **ft_getmap(int fd, char **map_read, char *buf, char *tmp)
|
||||||
{
|
{
|
||||||
char **map_read;
|
ssize_t rd;
|
||||||
int fd;
|
|
||||||
size_t i;
|
size_t i;
|
||||||
size_t ln_count;
|
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
ln_count = ft_countline_fd(file_name);
|
while (buf && i < SIZE_MAX)
|
||||||
if (ln_count == 0)
|
|
||||||
return (NULL);
|
|
||||||
fd = open(file_name, O_RDONLY);
|
|
||||||
map_read = ft_calloc(sizeof(char *), ln_count);
|
|
||||||
if (!map_read)
|
|
||||||
{
|
{
|
||||||
close (fd);
|
rd = read(fd, buf, 1);
|
||||||
return (NULL);
|
if (rd <= 0)
|
||||||
}
|
|
||||||
while (i < ULONG_MAX)
|
|
||||||
{
|
|
||||||
map_read[i] = get_next_line(fd);
|
|
||||||
if (map_read[i] == NULL)
|
|
||||||
break ;
|
break ;
|
||||||
|
tmp[i] = buf[0];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
close(fd);
|
close(fd);
|
||||||
|
tmp[i] = '\0';
|
||||||
|
map_read = ft_split(tmp, '\n');
|
||||||
|
return (map_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **ft_read_map(char *file_name, char **map_read)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char *tmp;
|
||||||
|
char *buf;
|
||||||
|
size_t filesize;
|
||||||
|
|
||||||
|
filesize = ft_filesize(file_name);
|
||||||
|
if (filesize == 0)
|
||||||
|
return (NULL);
|
||||||
|
tmp = ft_calloc(filesize + 1, 1);
|
||||||
|
if (!tmp)
|
||||||
|
return (NULL);
|
||||||
|
buf = ft_calloc(1, 1);
|
||||||
|
if (!buf)
|
||||||
|
return (NULL);
|
||||||
|
fd = open(file_name, O_RDONLY);
|
||||||
|
map_read = ft_getmap(fd, map_read, buf, tmp);
|
||||||
|
free(buf);
|
||||||
|
free(tmp);
|
||||||
return (map_read);
|
return (map_read);
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/ft_atoi.o
BIN
libft/ft_atoi.o
Binary file not shown.
BIN
libft/ft_bzero.o
BIN
libft/ft_bzero.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/ft_itoa.o
BIN
libft/ft_itoa.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/ft_split.o
BIN
libft/ft_split.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
libft/libft.a
BIN
libft/libft.a
Binary file not shown.
50
main.c
50
main.c
@ -6,11 +6,16 @@
|
|||||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/06 16:18:56 by adjoly #+# #+# */
|
/* Created: 2024/01/06 16:18:56 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/01/22 13:46:27 by adjoly ### ########.fr */
|
/* Updated: 2024/01/24 12:17:09 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "MacroLibX/includes/mlx.h"
|
||||||
|
#include "printf/ft_printf.h"
|
||||||
#include "so_long.h"
|
#include "so_long.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
void ft_freeimg(t_window *win)
|
void ft_freeimg(t_window *win)
|
||||||
{
|
{
|
||||||
@ -22,19 +27,30 @@ void ft_freeimg(t_window *win)
|
|||||||
free (win->img);
|
free (win->img);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t ft_mapsize(char **map)
|
||||||
void ft_freemap(char **map)
|
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (map[i] && i < ULONG_MAX)
|
while (*map && i < SIZE_MAX)
|
||||||
|
{
|
||||||
|
map++;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_freemap(char **map)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (map[i])
|
||||||
{
|
{
|
||||||
// ft_printf("freed : %s\n", map[i]);
|
|
||||||
free(map[i]);
|
free(map[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
// free(map);
|
free(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
int win_close(int event, void *param)
|
int win_close(int event, void *param)
|
||||||
@ -51,11 +67,12 @@ void ft_exit(t_window *win)
|
|||||||
{
|
{
|
||||||
ft_freeimg(win);
|
ft_freeimg(win);
|
||||||
mlx_destroy_window(win->mlx, win->win);
|
mlx_destroy_window(win->mlx, win->win);
|
||||||
|
mlx_loop_end(win->mlx);
|
||||||
mlx_destroy_display(win->mlx);
|
mlx_destroy_display(win->mlx);
|
||||||
ft_freemap(win->map);
|
ft_freemap(win->map);
|
||||||
// free(win->p_coords);
|
free(win->p_coords);
|
||||||
// free(win->e_coords);
|
free(win->e_coords);
|
||||||
// free(win);
|
free(win);
|
||||||
exit (EXIT_SUCCESS);
|
exit (EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,6 +80,7 @@ int main(int ac, char **av)
|
|||||||
{
|
{
|
||||||
t_window *win;
|
t_window *win;
|
||||||
char **map;
|
char **map;
|
||||||
|
t_coords map_size;
|
||||||
|
|
||||||
(void) ac;
|
(void) ac;
|
||||||
map = NULL;
|
map = NULL;
|
||||||
@ -72,12 +90,18 @@ int main(int ac, char **av)
|
|||||||
ft_send_error("Invalid map file extension (not .ber)\n", map);
|
ft_send_error("Invalid map file extension (not .ber)\n", map);
|
||||||
if (ft_check_file(av[1]) == 1)
|
if (ft_check_file(av[1]) == 1)
|
||||||
ft_send_error("File cannot be opened or doesn't exist\n", map);
|
ft_send_error("File cannot be opened or doesn't exist\n", map);
|
||||||
map = ft_read_map(av[1]);
|
map = ft_read_map(av[1], map);
|
||||||
if (!map)
|
if (!map)
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
if (!map[0])
|
if (!map[0])
|
||||||
ft_send_error("Map is empty", map);
|
ft_send_error("Map is empty\n", map);
|
||||||
ft_check_map_error(map);
|
ft_check_map_error(map);
|
||||||
|
ft_freemap(map);
|
||||||
|
map = ft_read_map(av[1], map);
|
||||||
|
if (!map)
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
|
map_size.x = ft_strlen(map[0]);
|
||||||
|
map_size.y = ft_mapsize(map);
|
||||||
win = malloc(sizeof(map) + sizeof(t_window));
|
win = malloc(sizeof(map) + sizeof(t_window));
|
||||||
if (!win)
|
if (!win)
|
||||||
{
|
{
|
||||||
@ -92,13 +116,13 @@ int main(int ac, char **av)
|
|||||||
if (!win->mlx)
|
if (!win->mlx)
|
||||||
// free map img win;
|
// free map img win;
|
||||||
return (0);
|
return (0);
|
||||||
win->win = mlx_new_window(win->mlx, 1600, 900, "so_fluffy");
|
win->win = mlx_new_window(win->mlx, map_size.x * T_SIZE, map_size.y * T_SIZE, "so_fluffy");
|
||||||
if (!win->win)
|
if (!win->win)
|
||||||
// free map img win ; destroy mlx
|
// free map img win ; destroy mlx
|
||||||
return (0);
|
return (0);
|
||||||
win->map = map;
|
win->map = map;
|
||||||
win->mov_count = 0;
|
win->mov_count = 0;
|
||||||
mlx_on_event(win->mlx, win->win, MLX_WINDOW_EVENT, win_close, win->mlx);
|
mlx_on_event(win->mlx, win->win, MLX_WINDOW_EVENT, win_close, win);
|
||||||
mlx_on_event(win->mlx, win->win, MLX_KEYDOWN, ft_key_event, win);
|
mlx_on_event(win->mlx, win->win, MLX_KEYDOWN, ft_key_event, win);
|
||||||
ft_alloc_img(win);
|
ft_alloc_img(win);
|
||||||
ft_printmap(win->map, win);
|
ft_printmap(win->map, win);
|
||||||
|
6
map.ber
6
map.ber
@ -1,5 +1,5 @@
|
|||||||
1111111111111
|
1111111111111
|
||||||
10010000000C1
|
100100000C1E1
|
||||||
1000011111001
|
100P010011101
|
||||||
1P0011E000001
|
10C0110000001
|
||||||
1111111111111
|
1111111111111
|
5
map.txt.ber
Normal file
5
map.txt.ber
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
1111111111111
|
||||||
|
100100000C1E1
|
||||||
|
100P010011101
|
||||||
|
10C0110000001
|
||||||
|
1111111111111
|
BIN
print_map.o
BIN
print_map.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/01/06 16:19:42 by adjoly #+# #+# */
|
/* Created: 2024/01/06 16:19:42 by adjoly #+# #+# */
|
||||||
/* Updated: 2024/01/21 15:42:52 by adjoly ### ########.fr */
|
/* Updated: 2024/01/24 12:15:45 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -60,8 +60,7 @@ int ft_key_event(int key, void *param);
|
|||||||
|
|
||||||
void ft_freemap(char **map);
|
void ft_freemap(char **map);
|
||||||
int check_wall(char **map, t_coords *player);
|
int check_wall(char **map, t_coords *player);
|
||||||
size_t ft_countline_fd(char *file_name);
|
char **ft_read_map(char *file_name, char **map_read);
|
||||||
char **ft_read_map(char *file_name);
|
|
||||||
|
|
||||||
char ft_check_file(char *file_name);
|
char ft_check_file(char *file_name);
|
||||||
void ft_exit(t_window *win);
|
void ft_exit(t_window *win);
|
||||||
@ -74,5 +73,6 @@ void ft_alloc_img(t_window *win);
|
|||||||
char ft_valid_file_ext(char *file_name);
|
char ft_valid_file_ext(char *file_name);
|
||||||
void ft_send_error(char *msg, char **map);
|
void ft_send_error(char *msg, char **map);
|
||||||
void ft_check_map_error(char **map);
|
void ft_check_map_error(char **map);
|
||||||
|
size_t ft_mapsize(char **map);
|
||||||
|
|
||||||
#endif
|
#endif
|
Reference in New Issue
Block a user