rm findplayer but add it in read, + get all targets

This commit is contained in:
2024-12-12 18:47:59 +01:00
parent 5274a135d6
commit ee26a60a09
5 changed files with 33 additions and 28 deletions

View File

@@ -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;i<size;++i)
{
for(int j=0;j<size;++j)
{
if(tab2d[i][j]==PLAYER)
{
playerPos->x=i;
playerPos->y=j;
}
}
}
}
void inGameLoop(unsigned short int **tab2d,vect *playerPos ) void inGameLoop(unsigned short int **tab2d,vect *playerPos )

View File

@@ -19,7 +19,6 @@ typedef struct Vecteur
unsigned short int **creatArea2D(const unsigned int N); unsigned short int **creatArea2D(const unsigned int N);
void screenDisplay( unsigned short int **tab,int size); 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 ); void inGameLoop(unsigned short int **tab2d,vect *playerPos );
#endif // FONCTION_H #endif // FONCTION_H

16
main.c
View File

@@ -5,12 +5,18 @@
int main() 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)); 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); inGameLoop(tab2d,playerPos);
free(playerPos); free(playerPos);
return 0; return 0;

24
read.c
View File

@@ -2,17 +2,17 @@
#include <stdlib.h> #include <stdlib.h>
#include "function.h" #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"); FILE *file = fopen(name_file, "r");
unsigned int x = 0, y = 0; unsigned int x = 0, y = 1;
while(!feof(file)) while(!feof(file))
{ {
char current =fgetc(file); char current =fgetc(file);
switch (current) { switch (current) {
case ' ':
tab[x][y] = EMPTY;
break;
case '#': case '#':
tab[x][y] = WALL; tab[x][y] = WALL;
break; break;
@@ -20,21 +20,35 @@ void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned
tab[x][y] = BOX; tab[x][y] = BOX;
break; break;
case '.': 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; tab[x][y] = TARGET;
break; break;
case '@': case '@':
player->x = x;
player->y = y;
tab[x][y] = PLAYER; tab[x][y] = PLAYER;
break; break;
case '\n': case '\n':
y = 0; y = 0;
++x; ++x;
break; break;
default:
tab[x][y] = EMPTY;
break;
} }
++y; ++y;
if (x >= N || y >= N) if (x >= N || y >= N)
{ {
perror("Level out of range !"); perror("Level out of range !");
exit(-1); exit(-1);
} }
} }
return targets;
} }

5
read.h
View File

@@ -1,5 +1,6 @@
#ifndef READ_H #ifndef READ_H
#define READ_H #define READ_H
#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);
#endif // !READ_H #endif // !READ_H