From ee26a60a0993b26a01f0b30f8711738444219663 Mon Sep 17 00:00:00 2001 From: Aubin DORIVAL Date: Thu, 12 Dec 2024 18:47:59 +0100 Subject: [PATCH] rm findplayer but add it in read, + get all targets --- function.c | 15 --------------- function.h | 1 - main.c | 16 +++++++++++----- read.c | 24 +++++++++++++++++++----- read.h | 5 +++-- 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/function.c b/function.c index 3040d55..2689198 100644 --- a/function.c +++ b/function.c @@ -145,21 +145,6 @@ void move(unsigned short int **tab,vect *playerPos, vect direction) } -void getStartPlayerPos(unsigned short int **tab2d, short int size, vect *playerPos ) -{ - for(int i=0;ix=i; - playerPos->y=j; - } - } - } -} - void inGameLoop(unsigned short int **tab2d,vect *playerPos ) diff --git a/function.h b/function.h index 847d3e6..423b9f2 100644 --- a/function.h +++ b/function.h @@ -19,7 +19,6 @@ typedef struct Vecteur unsigned short int **creatArea2D(const unsigned int N); void screenDisplay( unsigned short int **tab,int size); -void getStartPlayerPos(unsigned short int **tab2d, short int size, vect *playerPos ); void inGameLoop(unsigned short int **tab2d,vect *playerPos ); #endif // FONCTION_H diff --git a/main.c b/main.c index 24b43bb..0c7561e 100644 --- a/main.c +++ b/main.c @@ -5,12 +5,18 @@ int main() { - unsigned short int **tab2d = creatArea2D(32); - fileToTab2D("test.txt", tab2d, 32); - screenDisplay(tab2d, 32); - printf("main\n"); vect *playerPos = (vect *)malloc(sizeof(vect)); - getStartPlayerPos(tab2d,32,playerPos); + vect *targets = malloc(sizeof(vect)); + int nbr_targets; + unsigned short int **tab2d = creatArea2D(32); + targets = fileToTab2D("test.txt", tab2d, 32, playerPos, &nbr_targets); + screenDisplay(tab2d, 32); + printf("%d, %d\n", playerPos->x, playerPos->y); + + + for (int i = 0;i < nbr_targets; ++i) { + printf("%d, %d\n", targets[i].x, targets[i].y); + } inGameLoop(tab2d,playerPos); free(playerPos); return 0; diff --git a/read.c b/read.c index 87f5413..fbecae7 100644 --- a/read.c +++ b/read.c @@ -2,17 +2,17 @@ #include #include "function.h" -void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N) +vect* fileToTab2D(const char* name_file, unsigned short int **tab, + const unsigned N, vect *player, int *nbr_targets) { + vect *targets = malloc(sizeof(vect)); + (*nbr_targets) = 0; FILE *file = fopen(name_file, "r"); - unsigned int x = 0, y = 0; + unsigned int x = 0, y = 1; while(!feof(file)) { char current =fgetc(file); switch (current) { - case ' ': - tab[x][y] = EMPTY; - break; case '#': tab[x][y] = WALL; break; @@ -20,21 +20,35 @@ void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned tab[x][y] = BOX; break; case '.': + targets = realloc(targets, sizeof(vect)*(++nbr_targets[0])); + printf("nbr %d, pointeur %p\n", nbr_targets[0], targets); + targets[nbr_targets[0] - 1].x = x; + printf("%d : %d\n", targets[nbr_targets[0] - 1].x, nbr_targets[0] -1 ); + targets[nbr_targets[0] - 1].y = y; + printf("%d\n", targets[nbr_targets[0] - 1].y); tab[x][y] = TARGET; break; case '@': + player->x = x; + player->y = y; tab[x][y] = PLAYER; break; case '\n': y = 0; ++x; break; + default: + tab[x][y] = EMPTY; + break; } ++y; + if (x >= N || y >= N) { perror("Level out of range !"); exit(-1); } } + + return targets; } diff --git a/read.h b/read.h index a265059..0160396 100644 --- a/read.h +++ b/read.h @@ -1,5 +1,6 @@ #ifndef READ_H #define READ_H - -void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N); +#include "function.h" +vect* fileToTab2D(const char* name_file, unsigned short int **tab, + const unsigned N, vect *player, int *nbr_targets); #endif // !READ_H