encore meilleur

This commit is contained in:
2024-12-14 15:26:00 +01:00
parent af546b8440
commit fa7d5ce51a
5 changed files with 456 additions and 450 deletions

162
display.c
View File

@@ -10,108 +10,110 @@
void screenDisplay (unsigned short int **tab, int size) void screenDisplay (unsigned short int **tab, int size)
{ {
// puts("0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 "); // puts("0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ");
char element[7] = { ' ', '#', 'S', '.', '*', '@', '+' }; char element[7] = { ' ', '#', 'S', '.', '*', '@', '+' };
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{ {
printf ("%c ", element[tab[i][j]]); for (int j = 0; j < size; ++j)
{
printf ("%c ", element[tab[i][j]]);
}
puts ("");
} }
puts ("");
}
} }
void screenDisplaySDL (unsigned short int **tab, dis *display_user) void screenDisplaySDL (unsigned short int **tab, dis *display_user)
{ {
int size = display_user->size_window / display_user->size_box; int size = display_user->size_window / display_user->size_box;
int i, j; int i, j;
for (i = 0; i < display_user->size_box; ++i) for (i = 0; i < display_user->size_box; ++i)
{
for (j = 0; j < display_user->size_box; ++j)
{ {
SDL_Surface *img; for (j = 0; j < display_user->size_box; ++j)
SDL_Texture *texture; {
vect pos = { i * size, j * size }; SDL_Surface *img;
switch (tab[j][i]) SDL_Texture *texture;
{ vect pos = { i * size, j * size };
case EMPTY: switch (tab[j][i])
img = IMG_Load ("empty.png"); {
break; case EMPTY:
case WALL: img = IMG_Load ("empty.png");
img = IMG_Load ("wall.png"); break;
break; case WALL:
case PLAYER: img = IMG_Load ("wall.png");
img = IMG_Load ("player.png"); break;
break; case PLAYER:
case TARGET: img = IMG_Load ("player.png");
img = IMG_Load ("target.png"); break;
break; case TARGET:
case BOX: img = IMG_Load ("target.png");
img = IMG_Load ("box.png"); break;
break; case BOX:
case BOX_ON_TARGET: img = IMG_Load ("box.png");
img = IMG_Load ("box_on_target.png"); break;
break; case BOX_ON_TARGET:
case PLAYER_ON_TARGET: img = IMG_Load ("box_on_target.png");
img = IMG_Load ("player_on_target.png"); break;
break; case PLAYER_ON_TARGET:
} img = IMG_Load ("player_on_target.png");
texture = SDL_CreateTextureFromSurface (display_user->renderer, img); break;
displayImage (display_user->renderer, texture, pos, size); }
SDL_FreeSurface (img); texture
SDL_DestroyTexture (texture); = SDL_CreateTextureFromSurface (display_user->renderer, img);
displayImage (display_user->renderer, texture, pos, size);
SDL_FreeSurface (img);
SDL_DestroyTexture (texture);
}
} }
}
SDL_RenderPresent (display_user->renderer); SDL_RenderPresent (display_user->renderer);
} }
int getMaxSize () int getMaxSize ()
{ {
SDL_Init (SDL_INIT_VIDEO); // init if error SDL_Init (SDL_INIT_VIDEO); // init if error
SDL_DisplayMode display; SDL_DisplayMode display;
SDL_GetCurrentDisplayMode (0, &display); // get dim display user SDL_GetCurrentDisplayMode (0, &display); // get dim display user
int result = 0; int result = 0;
if (display.w <= display.h) if (display.w <= display.h)
{ {
result = display.w; result = display.w;
} }
else else
{ {
result = display.h; result = display.h;
} }
SDL_Quit (); SDL_Quit ();
return (result - 50); // margin return (result - 50); // margin
} }
void initSDL (dis *display_user) void initSDL (dis *display_user)
{ {
SDL_Init (SDL_INIT_VIDEO); SDL_Init (SDL_INIT_VIDEO);
display_user->window = SDL_CreateWindow ( display_user->window
"Sokoman", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, = SDL_CreateWindow ("Sokoman", SDL_WINDOWPOS_CENTERED,
display_user->size_window, display_user->size_window, SDL_WINDOW_SHOWN); SDL_WINDOWPOS_CENTERED, display_user->size_window,
if (!display_user->window) display_user->size_window, SDL_WINDOW_SHOWN);
{ if (!display_user->window)
SDL_Quit (); {
perror ("Window null"); SDL_Quit ();
exit (-1); perror ("Window null");
} exit (-1);
}
display_user->renderer display_user->renderer
= SDL_CreateRenderer (display_user->window, -1, SDL_RENDERER_SOFTWARE); = SDL_CreateRenderer (display_user->window, -1, SDL_RENDERER_SOFTWARE);
if (!display_user->renderer) if (!display_user->renderer)
{ {
SDL_Quit (); SDL_Quit ();
perror ("Renderer null"); perror ("Renderer null");
exit (-1); exit (-1);
} }
} }
void displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos, void displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos,
int size) int size)
{ {
SDL_Rect rect = { pos.x, pos.y, size, size }; SDL_Rect rect = { pos.x, pos.y, size, size };
SDL_RenderCopy (renderer, texture, NULL, &rect); SDL_RenderCopy (renderer, texture, NULL, &rect);
} }

View File

@@ -8,365 +8,369 @@
unsigned short int **creatArea2D (const unsigned int N) unsigned short int **creatArea2D (const unsigned int N)
{ {
unsigned short int **tab2d; unsigned short int **tab2d;
tab2d = calloc (N, sizeof (unsigned short int *)); tab2d = calloc (N, sizeof (unsigned short int *));
if (tab2d == NULL) if (tab2d == NULL)
{
return NULL;
}
bool fail = false;
unsigned int i;
for (i = 0; i < N && !fail; ++i)
{
tab2d[i] = calloc (N, sizeof (unsigned short int));
if (tab2d[i] == NULL)
{ {
fail = true; return NULL;
} }
}
if (fail) bool fail = false;
{ unsigned int i;
unsigned int j; for (i = 0; i < N && !fail; ++i)
for (j = 0; j < i; ++j)
{ {
free (tab2d[j]); tab2d[i] = calloc (N, sizeof (unsigned short int));
if (tab2d[i] == NULL)
{
fail = true;
}
} }
free (tab2d);
return NULL; if (fail)
} {
return tab2d; unsigned int j;
for (j = 0; j < i; ++j)
{
free (tab2d[j]);
}
free (tab2d);
return NULL;
}
return tab2d;
} }
void free2D (unsigned short int **tab, int N) void free2D (unsigned short int **tab, int N)
{ {
int i; int i;
for (i = 0; i < N; ++i) for (i = 0; i < N; ++i)
{ {
free (tab[i]); free (tab[i]);
} }
free (tab); free (tab);
return; return;
} }
short int canIGoDirection (short int valueOfNCase, short int canIGoDirection (short int valueOfNCase,
short int valueOfNPlusOneCase) short int valueOfNPlusOneCase)
{ {
if (valueOfNCase != WALL) if (valueOfNCase != WALL)
{
if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
{ {
if (valueOfNPlusOneCase == TARGET) if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
{ || (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
// Box on target {
return 3; if (valueOfNPlusOneCase == TARGET)
} {
// move the box // Box on target
return 2; return 3;
} }
if ((valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == EMPTY) // move the box
|| (valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == TARGET)) return 2;
{ }
if (valueOfNPlusOneCase == TARGET) if ((valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == EMPTY)
{ || (valueOfNCase == BOX_ON_TARGET
// Box on target and player on target too && valueOfNPlusOneCase == TARGET))
return 6; {
} if (valueOfNPlusOneCase == TARGET)
// move the box but player on a target {
return 5; // Box on target and player on target too
} return 6;
if (valueOfNCase == TARGET) }
{ // move the box but player on a target
// move player on target return 5;
return 4; }
if (valueOfNCase == TARGET)
{
// move player on target
return 4;
}
if (valueOfNCase == EMPTY)
{
// move player
return 1;
}
} }
if (valueOfNCase == EMPTY) return 0;
{
// move player
return 1;
}
}
return 0;
} }
void move (unsigned short int **tab, vect *playerPos, vect direction) void move (unsigned short int **tab, vect *playerPos, vect direction)
{ {
short int valueOfNCase short int valueOfNCase
= tab[playerPos->x + direction.x][playerPos->y + direction.y]; = tab[playerPos->x + direction.x][playerPos->y + direction.y];
vect add = plusVect (*playerPos, direction); vect add = plusVect (*playerPos, direction);
add = plusVect (add, direction); add = plusVect (add, direction);
short int valueOfNPlusOneCase; short int valueOfNPlusOneCase;
if (add.x < 0 || add.y < 0) if (add.x < 0 || add.y < 0)
{
valueOfNPlusOneCase = WALL;
}
else
{
valueOfNPlusOneCase = tab[add.x][add.y];
}
short int returnValue = canIGoDirection (valueOfNCase, valueOfNPlusOneCase);
short int playerState = tab[playerPos->x][playerPos->y];
switch (returnValue)
{
case 0:
break;
case 1:
// move player
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
break;
case 2:
// move player and the box
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX;
break;
case 3:
// move player and the box is well-placed
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET;
break;
case 4:
// move player on a target
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break;
case 5:
// move box and player on a target
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX;
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break;
case 6:
// move player on a target and box on a target
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET;
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break;
default:
printf ("Commande inconnue !\n");
}
if (returnValue != 0)
{
if (playerState == PLAYER_ON_TARGET)
{ {
tab[playerPos->x][playerPos->y] = TARGET; valueOfNPlusOneCase = WALL;
} }
else else
{ {
tab[playerPos->x][playerPos->y] = EMPTY; valueOfNPlusOneCase = tab[add.x][add.y];
}
short int returnValue
= canIGoDirection (valueOfNCase, valueOfNPlusOneCase);
short int playerState = tab[playerPos->x][playerPos->y];
switch (returnValue)
{
case 0:
break;
case 1:
// move player
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
break;
case 2:
// move player and the box
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX;
break;
case 3:
// move player and the box is well-placed
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET;
break;
case 4:
// move player on a target
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break;
case 5:
// move box and player on a target
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX;
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break;
case 6:
// move player on a target and box on a target
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET;
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break;
default:
printf ("Commande inconnue !\n");
}
if (returnValue != 0)
{
if (playerState == PLAYER_ON_TARGET)
{
tab[playerPos->x][playerPos->y] = TARGET;
}
else
{
tab[playerPos->x][playerPos->y] = EMPTY;
}
playerPos->x = playerPos->x + direction.x;
playerPos->y = playerPos->y + direction.y;
} }
playerPos->x = playerPos->x + direction.x;
playerPos->y = playerPos->y + direction.y;
}
} }
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
vect *targets, int nbr_targets, dis *display_user) vect *targets, int nbr_targets, dis *display_user)
{ {
vect direction = { 0, 0 }; vect direction = { 0, 0 };
char input; char input;
bool finish = false; bool finish = false;
while (!finish) while (!finish)
{
printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : ");
input = getchar ();
if (input == 'x')
{ {
break; // Quitter le jeu printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : ");
}
switch (input) input = getchar ();
{
case 'd':
direction.y = 1;
direction.x = 0;
break; if (input == 'x')
case 'q': {
direction.y = -1; break; // Quitter le jeu
direction.x = 0; }
break;
case 'z':
direction.x = -1;
direction.y = 0;
break;
case 's':
direction.x = 1;
direction.y = 0;
break;
default:
direction.x = 0;
direction.y = 0;
}
system ("clear"); switch (input)
move (tab2d, playerPos, direction); {
case 'd':
direction.y = 1;
direction.x = 0;
printf ("\n"); break;
if (isWin (tab2d, targets, nbr_targets)) case 'q':
{ direction.y = -1;
puts ("Win!"); direction.x = 0;
finish = true; break;
case 'z':
direction.x = -1;
direction.y = 0;
break;
case 's':
direction.x = 1;
direction.y = 0;
break;
default:
direction.x = 0;
direction.y = 0;
}
system ("clear");
move (tab2d, playerPos, direction);
printf ("\n");
if (isWin (tab2d, targets, nbr_targets))
{
puts ("Win!");
finish = true;
}
if (islose (tab2d, N))
{
puts ("lose!");
finish = true;
}
screenDisplay (tab2d, N);
screenDisplaySDL (tab2d, display_user);
} }
if (islose (tab2d, N))
{
puts ("lose!");
finish = true;
}
screenDisplay (tab2d, N);
screenDisplaySDL (tab2d, display_user);
}
} }
bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets) bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets)
{ {
int i; int i;
for (i = 0; i < nbr_targets; ++i) for (i = 0; i < nbr_targets; ++i)
{
int x = targets[i].x;
int y = targets[i].y;
if (tab2d[x][y] != BOX_ON_TARGET)
{ {
return false; int x = targets[i].x;
int y = targets[i].y;
if (tab2d[x][y] != BOX_ON_TARGET)
{
return false;
}
} }
} return true;
return true;
} }
bool islose (unsigned short int **tab2d, const int N) bool islose (unsigned short int **tab2d, const int N)
{ {
int x = 0, y = 0; int x = 0, y = 0;
for (x = 0; x < N; ++x) for (x = 0; x < N; ++x)
{
for (y = 0; y < N; ++y)
{ {
bool isblock = false; for (y = 0; y < N; ++y)
if (tab2d[x][y] == BOX) {
{ bool isblock = false;
vect box; if (tab2d[x][y] == BOX)
box.x = x; {
box.y = y; vect box;
isblock = blockBox (tab2d, box); box.x = x;
} box.y = y;
isblock = blockBox (tab2d, box);
}
if (isblock) if (isblock)
{ {
return true; return true;
} }
}
} }
} return false;
return false;
} }
vect plusVect (vect one, vect two) vect plusVect (vect one, vect two)
{ {
vect result; vect result;
result.x = one.x + two.x; result.x = one.x + two.x;
result.y = one.y + two.y; result.y = one.y + two.y;
return result; return result;
} }
int lengthVect (vect vector) { return abs (vector.x) + abs (vector.y); } int lengthVect (vect vector) { return abs (vector.x) + abs (vector.y); }
bool blockBox (unsigned short int **tab2d, vect box_coor) bool blockBox (unsigned short int **tab2d, vect box_coor)
{ {
int nbr_touch = 0; int nbr_touch = 0;
vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } }; vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } };
int indice_card[4] = { 0 }; int indice_card[4] = { 0 };
int id_box = -1; int id_box = -1;
int i; int i;
for (i = 0; i < 4; ++i) for (i = 0; i < 4; ++i)
{
vect test = plusVect (box_coor, card[i]);
unsigned short int val = tab2d[test.x][test.y];
if (val == BOX || val == WALL || val == BOX_ON_TARGET)
{ {
nbr_touch++; vect test = plusVect (box_coor, card[i]);
if (val == BOX || val == BOX_ON_TARGET) unsigned short int val = tab2d[test.x][test.y];
{ if (val == BOX || val == WALL || val == BOX_ON_TARGET)
id_box = nbr_touch - 1; {
} nbr_touch++;
indice_card[nbr_touch - 1] = i; if (val == BOX || val == BOX_ON_TARGET)
{
id_box = nbr_touch - 1;
}
indice_card[nbr_touch - 1] = i;
}
} }
} if (nbr_touch == 0)
if (nbr_touch == 0)
return false;
if (nbr_touch >= 3)
{
if (id_box != -1)
{
vect test = plusVect (card[indice_card[(id_box + 1) % 3]],
card[indice_card[(id_box + 2) % 3]]);
if (test.x == 0 && test.y == 0)
return false; return false;
if (nbr_touch >= 3)
{
if (id_box != -1)
{
vect test = plusVect (card[indice_card[(id_box + 1) % 3]],
card[indice_card[(id_box + 2) % 3]]);
if (test.x == 0 && test.y == 0)
return false;
}
return true;
}
if (nbr_touch == 2)
{
int id1 = indice_card[0];
int id2 = indice_card[1];
vect add = plusVect (card[id1], card[id2]);
if (lengthVect (add) == 2)
{
if (id_box == -1)
return true;
vect test = plusVect (card[indice_card[id_box]],
card[indice_card[(id_box + 1) % 2]]);
test = plusVect (test, box_coor);
if (tab2d[test.x][test.y] == WALL)
return true;
}
return false;
}
vect normal = card[indice_card[0]];
vect director1 = card[(indice_card[0] + 1) % 4];
vect director2 = card[(indice_card[0] + 1) % 4];
vect dir_test[2] = { director1, director2 };
bool wall = false;
for (int i = 0; i < 2; ++i)
{
vect current = plusVect (box_coor, dir_test[i]);
while (!wall)
{
unsigned short int val = tab2d[current.x][current.y];
if (val == WALL)
wall = true;
if (val == TARGET)
return false;
vect cur_nor = plusVect (current, normal);
val = tab2d[cur_nor.x][cur_nor.y];
if (val != WALL || val != BOX || val != BOX_ON_TARGET)
{
return false;
}
}
} }
return true; return true;
} perror ("error in blockBox");
exit (0);
if (nbr_touch == 2)
{
int id1 = indice_card[0];
int id2 = indice_card[1];
vect add = plusVect (card[id1], card[id2]);
if (lengthVect (add) == 2)
{
if (id_box == -1)
return true;
vect test = plusVect (card[indice_card[id_box]],
card[indice_card[(id_box + 1) % 2]]);
test = plusVect (test, box_coor);
if (tab2d[test.x][test.y] == WALL)
return true;
}
return false;
}
vect normal = card[indice_card[0]];
vect director1 = card[(indice_card[0] + 1) % 4];
vect director2 = card[(indice_card[0] + 1) % 4];
vect dir_test[2] = { director1, director2 };
bool wall = false;
for (int i = 0; i < 2; ++i)
{
vect current = plusVect (box_coor, dir_test[i]);
while (!wall)
{
unsigned short int val = tab2d[current.x][current.y];
if (val == WALL)
wall = true;
if (val == TARGET)
return false;
vect cur_nor = plusVect (current, normal);
val = tab2d[cur_nor.x][cur_nor.y];
if (val != WALL || val != BOX || val != BOX_ON_TARGET)
{
return false;
}
}
}
return true;
perror ("error in blockBox");
exit (0);
} }

View File

@@ -18,24 +18,24 @@
typedef struct Vecteur typedef struct Vecteur
{ {
int x; int x;
int y; int y;
} vect; } vect;
typedef struct Score typedef struct Score
{ {
time_t before; time_t before;
time_t after; time_t after;
unsigned int move_player; unsigned int move_player;
unsigned int move_box; unsigned int move_box;
} score; } score;
typedef struct essential_sdl typedef struct essential_sdl
{ {
SDL_Window *window; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
unsigned int size_window; unsigned int size_window;
unsigned int size_box; unsigned int size_box;
} dis; } dis;
unsigned short int **creatArea2D (const unsigned int N); unsigned short int **creatArea2D (const unsigned int N);

50
main.c
View File

@@ -11,34 +11,34 @@
int main () int main ()
{ {
dis display_user; dis display_user;
display_user.size_window = getMaxSize (); display_user.size_window = getMaxSize ();
display_user.size_box = SIZE_PLAY; display_user.size_box = SIZE_PLAY;
initSDL (&display_user); initSDL (&display_user);
vect *playerPos = (vect *)malloc (sizeof (vect)); vect *playerPos = (vect *)malloc (sizeof (vect));
vect *targets; vect *targets;
int nbr_targets; int nbr_targets;
score playerScore; score playerScore;
unsigned short int **tab2d = creatArea2D (SIZE_PLAY); unsigned short int **tab2d = creatArea2D (SIZE_PLAY);
playerScore.before = time (NULL); playerScore.before = time (NULL);
targets targets
= fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
screenDisplaySDL (tab2d, &display_user); screenDisplaySDL (tab2d, &display_user);
screenDisplay (tab2d, SIZE_PLAY); screenDisplay (tab2d, SIZE_PLAY);
inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets, inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets,
&display_user); &display_user);
playerScore.after = time (NULL); playerScore.after = time (NULL);
printf ("%ld\n", playerScore.after - playerScore.before); printf ("%ld\n", playerScore.after - playerScore.before);
SDL_DestroyWindow (display_user.window); SDL_DestroyWindow (display_user.window);
SDL_DestroyRenderer (display_user.renderer); SDL_DestroyRenderer (display_user.renderer);
free2D (tab2d, SIZE_PLAY); free2D (tab2d, SIZE_PLAY);
free (playerPos); free (playerPos);
free (targets); free (targets);
SDL_Quit (); SDL_Quit ();
return 0; return 0;
} }

82
read.c
View File

@@ -5,48 +5,48 @@
vect *fileToTab2D (const char *name_file, unsigned short int **tab, vect *fileToTab2D (const char *name_file, unsigned short int **tab,
const unsigned N, vect *player, int *nbr_targets) const unsigned N, vect *player, int *nbr_targets)
{ {
vect *targets = malloc (sizeof (vect)); vect *targets = malloc (sizeof (vect));
(*nbr_targets) = 0; (*nbr_targets) = 0;
FILE *file = fopen (name_file, "r"); FILE *file = fopen (name_file, "r");
unsigned int x = 0, y = 1; unsigned int x = 0, y = 1;
while (!feof (file)) while (!feof (file))
{
char current = fgetc (file);
switch (current)
{ {
case '#': char current = fgetc (file);
tab[x][y] = WALL; switch (current)
break; {
case 'C': case '#':
tab[x][y] = BOX; tab[x][y] = WALL;
break; break;
case 'I': case 'C':
targets = realloc (targets, sizeof (vect) * (++nbr_targets[0])); tab[x][y] = BOX;
targets[nbr_targets[0] - 1].x = x; break;
targets[nbr_targets[0] - 1].y = y; case 'I':
tab[x][y] = TARGET; targets = realloc (targets, sizeof (vect) * (++nbr_targets[0]));
break; targets[nbr_targets[0] - 1].x = x;
case 'P': targets[nbr_targets[0] - 1].y = y;
player->x = x; tab[x][y] = TARGET;
player->y = y; break;
tab[x][y] = PLAYER; case 'P':
break; player->x = x;
case '\n': player->y = y;
y = 0; tab[x][y] = PLAYER;
++x; break;
break; case '\n':
default: y = 0;
tab[x][y] = EMPTY; ++x;
break; break;
} default:
++y; tab[x][y] = EMPTY;
break;
}
++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);
}
} }
} fclose (file);
fclose (file); return targets;
return targets;
} }