time + fix leak memories

This commit is contained in:
2024-12-13 12:11:20 +01:00
parent 14e05afdce
commit d5abc56d1b
4 changed files with 33 additions and 5 deletions

View File

@@ -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;
}

View File

@@ -3,6 +3,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#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);

10
main.c
View File

@@ -1,4 +1,5 @@
#include <stdio.h>
#include <time.h>
#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;
}

2
read.c
View File

@@ -48,6 +48,6 @@ fileToTab2D (const char *name_file, unsigned short int **tab, const unsigned N,
exit (-1);
}
}
fclose(file);
return targets;
}