This commit is contained in:
2024-12-12 18:49:34 +01:00
parent ee26a60a09
commit f561985efe
3 changed files with 210 additions and 207 deletions

View File

@@ -1,11 +1,10 @@
#include "function.h" #include "function.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
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 *));
@@ -38,11 +37,13 @@ unsigned short int **creatArea2D(const unsigned int N)
return tab2d; return tab2d;
} }
short int canIGoDirection(short int valueOfNCase, short int valueOfNPlusOneCase) short int
canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase)
{ {
if (valueOfNCase != WALL) if (valueOfNCase != WALL)
{ {
if((valueOfNCase==BOX && valueOfNPlusOneCase==EMPTY) ||( valueOfNCase==BOX && valueOfNPlusOneCase==TARGET)) if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
{ {
if (valueOfNPlusOneCase == TARGET) if (valueOfNPlusOneCase == TARGET)
@@ -53,7 +54,8 @@ short int canIGoDirection(short int valueOfNCase, short int valueOfNPlusOneCase)
// move the box // move the box
return 2; return 2;
} }
if((valueOfNCase==BOX_ON_TARGET && valueOfNPlusOneCase==EMPTY )|| (valueOfNCase==BOX_ON_TARGET && valueOfNPlusOneCase==TARGET)) if ((valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == EMPTY)
|| (valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == TARGET))
{ {
if (valueOfNPlusOneCase == TARGET) if (valueOfNPlusOneCase == TARGET)
@@ -75,17 +77,18 @@ short int canIGoDirection(short int valueOfNCase, short int valueOfNPlusOneCase)
// move player // move player
return 1; return 1;
} }
} }
return 0; 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=tab[playerPos->x+direction.x][playerPos->y+direction.y]; short int valueOfNCase
short int valueOfNPlusOneCase=tab[playerPos->x+direction.x*2][playerPos->y+direction.y*2]; = tab[playerPos->x + direction.x][playerPos->y + direction.y];
short int valueOfNPlusOneCase
= tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2];
short int returnValue = canIGoDirection (valueOfNCase, valueOfNPlusOneCase); short int returnValue = canIGoDirection (valueOfNCase, valueOfNPlusOneCase);
short int playerState = tab[playerPos->x][playerPos->y]; short int playerState = tab[playerPos->x][playerPos->y];
@@ -103,70 +106,77 @@ void move(unsigned short int **tab,vect *playerPos, vect direction)
// move player and the box // move player and the box
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER; tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x+direction.x*2][playerPos->y+direction.y*2] = BOX; tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX;
break; break;
case 3: case 3:
// move player and the box is well-placed // move player and the box is well-placed
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER; tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x+direction.x*2][playerPos->y+direction.y*2] = BOX_ON_TARGET; tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET;
break; break;
case 4: case 4:
// move player on a target // move player on a target
tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER_ON_TARGET; tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break; break;
case 5: case 5:
// move box and player on a target // move box and player on a target
tab[playerPos->x+direction.x*2][playerPos->y+direction.y*2] = BOX; tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER_ON_TARGET; = BOX;
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break; break;
case 6: case 6:
// move player on a target and box on a target // 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 * 2][playerPos->y + direction.y * 2]
tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER_ON_TARGET; = BOX_ON_TARGET;
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break; break;
default: default:
printf ("Commande inconnue !\n"); printf ("Commande inconnue !\n");
} }
if (returnValue != 0) if (returnValue != 0)
{ {
if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;} if (playerState == PLAYER_ON_TARGET)
else{ tab[playerPos->x][playerPos->y] = EMPTY;} {
tab[playerPos->x][playerPos->y] = TARGET;
}
else
{
tab[playerPos->x][playerPos->y] = EMPTY;
}
playerPos->x = playerPos->x + direction.x; playerPos->x = playerPos->x + direction.x;
playerPos->y = playerPos->y + direction.y; playerPos->y = playerPos->y + direction.y;
}
} }
} void
inGameLoop (unsigned short int **tab2d, vect *playerPos)
void inGameLoop(unsigned short int **tab2d,vect *playerPos )
{ {
vect direction = { 0, 0 }; vect direction = { 0, 0 };
char input; char input;
while (1) { while (1)
{
printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : "); printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : ");
input = getchar (); input = getchar ();
if (input == 'x') { if (input == 'x')
{
break; // Quitter le jeu break; // Quitter le jeu
} }
switch (input)
{
switch (input) {
case 'd': case 'd':
direction.y = 1; direction.y = 1;
direction.x = 0; direction.x = 0;
@@ -187,7 +197,6 @@ void inGameLoop(unsigned short int **tab2d,vect *playerPos )
default: default:
direction.x = 0; direction.x = 0;
direction.y = 0; direction.y = 0;
} }
system ("clear"); system ("clear");
@@ -195,8 +204,5 @@ void inGameLoop(unsigned short int **tab2d,vect *playerPos )
printf ("\n"); printf ("\n");
screenDisplay (tab2d, 32); screenDisplay (tab2d, 32);
} }
} }

6
main.c
View File

@@ -11,12 +11,6 @@ int main()
unsigned short int **tab2d = creatArea2D(32); unsigned short int **tab2d = creatArea2D(32);
targets = fileToTab2D("test.txt", tab2d, 32, playerPos, &nbr_targets); targets = fileToTab2D("test.txt", tab2d, 32, playerPos, &nbr_targets);
screenDisplay(tab2d, 32); 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;

13
read.c
View File

@@ -1,9 +1,10 @@
#include "function.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "function.h"
vect* fileToTab2D(const char* name_file, unsigned short int **tab, vect *
const unsigned N, vect *player, int *nbr_targets) fileToTab2D (const char *name_file, unsigned short int **tab, const unsigned N,
vect *player, int *nbr_targets)
{ {
vect *targets = malloc (sizeof (vect)); vect *targets = malloc (sizeof (vect));
(*nbr_targets) = 0; (*nbr_targets) = 0;
@@ -12,7 +13,8 @@ vect* fileToTab2D(const char* name_file, unsigned short int **tab,
while (!feof (file)) while (!feof (file))
{ {
char current = fgetc (file); char current = fgetc (file);
switch (current) { switch (current)
{
case '#': case '#':
tab[x][y] = WALL; tab[x][y] = WALL;
break; break;
@@ -23,7 +25,8 @@ vect* fileToTab2D(const char* name_file, unsigned short int **tab,
targets = realloc (targets, sizeof (vect) * (++nbr_targets[0])); targets = realloc (targets, sizeof (vect) * (++nbr_targets[0]));
printf ("nbr %d, pointeur %p\n", nbr_targets[0], targets); printf ("nbr %d, pointeur %p\n", nbr_targets[0], targets);
targets[nbr_targets[0] - 1].x = x; targets[nbr_targets[0] - 1].x = x;
printf("%d : %d\n", targets[nbr_targets[0] - 1].x, nbr_targets[0] -1 ); printf ("%d : %d\n", targets[nbr_targets[0] - 1].x,
nbr_targets[0] - 1);
targets[nbr_targets[0] - 1].y = y; targets[nbr_targets[0] - 1].y = y;
printf ("%d\n", targets[nbr_targets[0] - 1].y); printf ("%d\n", targets[nbr_targets[0] - 1].y);
tab[x][y] = TARGET; tab[x][y] = TARGET;