226 lines
6.0 KiB
C
226 lines
6.0 KiB
C
#include "function.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
unsigned short int **creatArea2D(const unsigned int N)
|
|
{
|
|
unsigned short int **tab2d;
|
|
tab2d = calloc(N, sizeof(unsigned short int* ));
|
|
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;
|
|
}
|
|
}
|
|
|
|
if (fail)
|
|
{
|
|
unsigned int j;
|
|
for (j = 0; j < i; ++j)
|
|
{
|
|
free(tab2d[j]);
|
|
}
|
|
free(tab2d);
|
|
return NULL;
|
|
}
|
|
return tab2d;
|
|
}
|
|
|
|
short int canIGoDirection(short int valueOfNCase, short int valueOfNPlusOneCase)
|
|
{
|
|
if(valueOfNCase!=WALL)
|
|
{
|
|
if((valueOfNCase==BOX && valueOfNPlusOneCase==EMPTY) ||( valueOfNCase==BOX && valueOfNPlusOneCase==TARGET))
|
|
{
|
|
|
|
if(valueOfNPlusOneCase==TARGET)
|
|
{
|
|
// Box on target
|
|
return 3;
|
|
}
|
|
//move the box
|
|
return 2;
|
|
}
|
|
if((valueOfNCase==BOX_ON_TARGET && valueOfNPlusOneCase==EMPTY )|| (valueOfNCase==BOX_ON_TARGET && valueOfNPlusOneCase==TARGET))
|
|
{
|
|
|
|
if(valueOfNPlusOneCase==TARGET)
|
|
{
|
|
// Box on target and player on target too
|
|
return 6;
|
|
}
|
|
//move the box but player on a target
|
|
return 5;
|
|
}
|
|
if(valueOfNCase==TARGET)
|
|
{
|
|
//move player on target
|
|
return 4;
|
|
}
|
|
|
|
if(valueOfNCase==EMPTY)
|
|
{
|
|
// move player
|
|
return 1;
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void move(unsigned short int **tab,vect *playerPos, vect direction)
|
|
{
|
|
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 returnValue = canIGoDirection(valueOfNCase,valueOfNPlusOneCase);
|
|
short int playerState =tab[playerPos->x][playerPos->y];
|
|
|
|
switch(returnValue)
|
|
{
|
|
case 0:
|
|
|
|
break;
|
|
case 1:
|
|
//move player
|
|
if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
|
|
else{ tab[playerPos->x][playerPos->y] = EMPTY;}
|
|
tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER;
|
|
break;
|
|
case 2:
|
|
//move player and the box
|
|
if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
|
|
else{ tab[playerPos->x][playerPos->y] = EMPTY;}
|
|
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
|
|
if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
|
|
else{ tab[playerPos->x][playerPos->y] = EMPTY;}
|
|
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
|
|
if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
|
|
else{ tab[playerPos->x][playerPos->y] = EMPTY;}
|
|
tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER_ON_TARGET;
|
|
break;
|
|
case 5:
|
|
//move box and player on a target
|
|
if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
|
|
else{ tab[playerPos->x][playerPos->y] = EMPTY;}
|
|
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
|
|
if(playerState==PLAYER_ON_TARGET){tab[playerPos->x][playerPos->y] = TARGET;}
|
|
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][playerPos->y+direction.y] = PLAYER_ON_TARGET;
|
|
break;
|
|
default:
|
|
printf("Commande inconnue !\n");
|
|
|
|
|
|
}
|
|
|
|
if(returnValue!=0)
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
}
|
|
|