ça marche bordel

This commit is contained in:
cyjullien1
2024-12-12 17:21:20 +01:00
parent d1065e68fe
commit 60d3e09293
4 changed files with 123 additions and 32 deletions

View File

@@ -3,11 +3,12 @@
void screenDisplay( unsigned short int **tab,int size) void screenDisplay( unsigned short int **tab,int size)
{ {
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) for(int j=0;j<size;++j)
{ {
printf("%hu ",tab[i][j]); printf("%c ",element[tab[i][j]]);
} }
puts(""); puts("");
} }

View File

@@ -64,79 +64,162 @@ short int canIGoDirection(short int valueOfNCase, short int valueOfNPlusOneCase)
//move the box but player on a target //move the box but player on a target
return 5; return 5;
} }
if(valueOfNCase==TARGET) if(valueOfNCase==TARGET)
{ {
//move player on target //move player on target
return 4; return 4;
} }
if(valueOfNCase==EMPTY)
{
// 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=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 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];
switch(returnValue) switch(returnValue)
{ {
case 0: case 0:
break; break;
case 1: case 1:
//move player //move player
if(playerState==PLAYER_ON_TARGET){tab[playerPos.x][playerPos.y] = TARGET;} if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
else{ tab[playerPos.x][playerPos.y] = EMPTY;} else{ tab[playerPos->x][playerPos->y] = EMPTY;}
tab[playerPos.x+direction.x][playerPos.y+direction.y] = PLAYER; tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER;
break; break;
case 2: case 2:
//move player and the box //move player and the box
if(playerState==PLAYER_ON_TARGET){tab[playerPos.x][playerPos.y] = TARGET;} if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
else{ tab[playerPos.x][playerPos.y] = EMPTY;} else{ tab[playerPos->x][playerPos->y] = EMPTY;}
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
if(playerState==PLAYER_ON_TARGET){tab[playerPos.x][playerPos.y] = TARGET;} if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
else{ tab[playerPos.x][playerPos.y] = EMPTY;} else{ tab[playerPos->x][playerPos->y] = EMPTY;}
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
if(playerState==PLAYER_ON_TARGET){tab[playerPos.x][playerPos.y] = TARGET;} if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
else{ tab[playerPos.x][playerPos.y] = EMPTY;} else{ tab[playerPos->x][playerPos->y] = EMPTY;}
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
if(playerState==PLAYER_ON_TARGET){tab[playerPos.x][playerPos.y] = TARGET;} if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
else{ tab[playerPos.x][playerPos.y] = EMPTY;} else{ tab[playerPos->x][playerPos->y] = EMPTY;}
tab[playerPos.x+direction.x*2][playerPos.y+direction.y*2] = BOX; tab[playerPos->x+direction.x*2][playerPos->y+direction.y*2] = BOX;
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 6: case 6:
//move player on a target and box on a target //move player on a target and box on a target
if(playerState==PLAYER_ON_TARGET){tab[playerPos.x][playerPos.y] = TARGET;} if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
else{ tab[playerPos.x][playerPos.y] = EMPTY;} else{ tab[playerPos->x][playerPos->y] = EMPTY;}
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;
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;
default:
printf("Commande inconnue !\n");
} }
if(returnValue!=0) if(returnValue!=0)
{ {
playerPos.x = playerPos.x+direction.x;
playerPos.y = playerPos.y+direction.y; playerPos->x = playerPos->x+direction.x;
playerPos->y = playerPos->y+direction.y;
}
direction.x = 0;
direction.y = 0;
}
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 )
{
vect direction={0,0};
char input;
while (1) {
printf("Utilisez Z/Q/S/D pour bouger, X pour quitter : ");
input = getchar();
if (input == 'x') {
break; // Quitter le jeu
}
switch (input) {
case 'd':
direction.y=1;
direction.x=0;
move(tab2d,playerPos, direction);
break;
case 'q':
direction.y=-1;
direction.x=0;
move(tab2d,playerPos, direction);
break;
case 'z':
direction.x=-1;
direction.y=0;
move(tab2d,playerPos, direction);
break;
case 's':
direction.x=1;
direction.y=0;
move(tab2d,playerPos, direction);
break;
default:
direction.x=0;
direction.y=0;
}
printf("\n");
screenDisplay(tab2d, 32);
}
}

View File

@@ -12,12 +12,15 @@
#define PLAYER_ON_TARGET 6 #define PLAYER_ON_TARGET 6
typedef struct Vecteur typedef struct Vecteur
{ {
short int x; int x;
short int y; int y;
} vect; } vect;
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 getStartPlayerPos(unsigned short int **tab2d, short int size, vect *playerPos );
void inGameLoop(unsigned short int **tab2d,vect *playerPos );
#endif // FONCTION_H #endif // FONCTION_H

4
main.c
View File

@@ -9,5 +9,9 @@ int main()
fileToTab2D("test.txt", tab2d, 32); fileToTab2D("test.txt", tab2d, 32);
screenDisplay(tab2d, 32); screenDisplay(tab2d, 32);
printf("main\n"); printf("main\n");
vect *playerPos = (vect *)malloc(sizeof(vect));
getStartPlayerPos(tab2d,32,playerPos);
inGameLoop(tab2d,playerPos);
free(playerPos);
return 0; return 0;
} }