From d5abc56d1b3de955cdc228e9fca3e7bedd3545eb Mon Sep 17 00:00:00 2001 From: Aubin DORIVAL Date: Fri, 13 Dec 2024 12:11:20 +0100 Subject: [PATCH] time + fix leak memories --- function.c | 15 ++++++++++++--- function.h | 11 +++++++++++ main.c | 10 +++++++++- read.c | 2 +- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/function.c b/function.c index 294db29..8d97378 100644 --- a/function.c +++ b/function.c @@ -37,6 +37,17 @@ creatArea2D (const unsigned int N) return tab2d; } +void free2D(unsigned short int **tab, int N) +{ + int i; + for (i = 0; i < N; ++i) + { + free(tab[i]); + } + free(tab); + return; +} + short int canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase) { @@ -290,7 +301,7 @@ blockBox (unsigned short int **tab2d, vect box_coor) { int nbr_touch = 0; vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } }; - int *indice_card = NULL; + int indice_card[4] = {0}; int id_box = -1; int i; for (i = 0; i < 4; ++i) @@ -304,8 +315,6 @@ blockBox (unsigned short int **tab2d, vect box_coor) { id_box = nbr_touch -1; } - - indice_card = realloc (indice_card, sizeof (int) * nbr_touch); indice_card[nbr_touch - 1] = i; } diff --git a/function.h b/function.h index 08b6297..daac57c 100644 --- a/function.h +++ b/function.h @@ -3,6 +3,7 @@ #include #include +#include #define EMPTY 0 #define WALL 1 @@ -11,14 +12,24 @@ #define BOX_ON_TARGET 4 #define PLAYER 5 #define PLAYER_ON_TARGET 6 + typedef struct Vecteur { int x; int y; } vect; +typedef struct Score +{ + time_t before; + time_t after; + unsigned int move_player; + unsigned int move_box; +} score; + unsigned short int **creatArea2D(const unsigned int N); +void free2D(unsigned short int **tab, int N); void screenDisplay( unsigned short int **tab,int size); void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, int nbr_targets); bool isWin(unsigned short int **tab2d, vect *targets, int nbr_targets); diff --git a/main.c b/main.c index 3183ba6..7946b65 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include #include "function.h" #include "read.h" #include "display.h" @@ -10,11 +11,18 @@ int main() vect *playerPos = (vect *)malloc(sizeof(vect)); vect *targets; int nbr_targets; + score playerScore; + playerScore.before = time(NULL); unsigned short int **tab2d = creatArea2D(SIZE_PLAY); targets = fileToTab2D("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); screenDisplay(tab2d, SIZE_PLAY); - printf("player (%d, %d)", playerPos->x, playerPos->y); inGameLoop(tab2d, SIZE_PLAY, playerPos, targets, nbr_targets); + + + playerScore.after = time(NULL); + printf("%ld\n", playerScore.after - playerScore.before); + free2D(tab2d, SIZE_PLAY); free(playerPos); + free(targets); return 0; } diff --git a/read.c b/read.c index b65b2b4..821ea73 100644 --- a/read.c +++ b/read.c @@ -48,6 +48,6 @@ fileToTab2D (const char *name_file, unsigned short int **tab, const unsigned N, exit (-1); } } - + fclose(file); return targets; }