From 83edb77d74bb339f3e1324a51039c78ac503db90 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 24 Jul 2019 18:46:39 +0200 Subject: bsq and c13 passed --- bsq/terrain.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 bsq/terrain.c (limited to 'bsq/terrain.c') diff --git a/bsq/terrain.c b/bsq/terrain.c new file mode 100644 index 0000000..b2ef10a --- /dev/null +++ b/bsq/terrain.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* terrain.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/23 08:56:45 by cacharle #+# #+# */ +/* Updated: 2019/07/24 12:06:11 by samzur ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include "include.h" + +/* +** Free the terrain +*/ + +void destroy_terrain(t_terrain *terrain) +{ + int y; + + if (terrain == NULL) + return ; + if (terrain->matrix == NULL) + return ; + y = 0; + while (y < terrain->rows) + { + if (terrain->matrix[y] == NULL) + break ; + free(terrain->matrix[y++]); + } + free(terrain->matrix); +} + +void print_terrain(t_parsed_terrain *pterrain) +{ + int terrain_len; + char *terrain_str; + + terrain_str = (char*)malloc(sizeof(char) * pterrain->terrain->rows + * (pterrain->terrain->columns + 1)); + terrain_len = terrain_to_string(pterrain, terrain_str); + write(STDOUT_FILENO, terrain_str, terrain_len); + free(terrain_str); +} + +int terrain_to_string(t_parsed_terrain *pterrain, char *str) +{ + int i; + int j; + int str_index; + + str_index = 0; + i = 0; + while (i < pterrain->terrain->rows) + { + j = 0; + while (j < pterrain->terrain->columns) + { + if (pterrain->terrain->matrix[i][j] >= EMPTY) + str[str_index] = pterrain->empty; + else if (pterrain->terrain->matrix[i][j] == OBSTACLE) + str[str_index] = pterrain->obstacle; + else if (pterrain->terrain->matrix[i][j] == FILLED) + str[str_index] = pterrain->filled; + j++; + str_index++; + } + i++; + str[str_index] = '\n'; + str_index++; + } + return (str_index); +} -- cgit