Files
Sokoban/function.c
2024-12-12 13:08:18 +01:00

127 lines
3.3 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;
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)
{
int j;
for (j = 0; j < i; ++j)
{
free(tab2d[j]);
}
free(tab2d);
return NULL;
}
return tab2d;
}
void move(unsigned short int **tab,int size,vect playerPos, vect direction)
{
short int valueOfNCase=tab[playerPos.x+direction.x][playePos.y+direction.y];
short int valueOfNPlusOneCase=tab[playerPos.x+direction.x*2][playePos.y+direction.y*2];
short int returnValue CanIGoDirection(valueOfNCase,valueOfNPlusOneCase);
short int playerState =tab[playerPos.x][playePos.y];
switch case(returnValue)
{
case 0:
break;
case 1:
//move player
tab[playePos.x][playerPos.y] = EMPTY;
tab[playerPos.x+direction.x][playePos.y+direction.y] = PLAYER;
break;
case 2:
//move player and the box
tab[playePos.x][playerPos.y] = EMPTY;
tab[playerPos.x+direction.x][playePos.y+direction.y] = PLAYER;
tab[playerPos.x+direction.x*2][playePos.y+direction.y*2] = BOX;
break;
case 3:
//move player and the box is well-placed
tab[playePos.x][playerPos.y] = EMPTY;
tab[playerPos.x+direction.x][playePos.y+direction.y] = PLAYER;
tab[playerPos.x+direction.x*2][playePos.y+direction.y*2] = BOX_ON_TARGET;
break;
case 4:
//move player on a target
tab[playePos.x][playerPos.y] = EMPTY;
tab[playerPos.x+direction.x][playePos.y+direction.y] = PLAYER_ON_TARGET;
break;
case 5:
//move player on a target
tab[playePos.x][playerPos.y] = EMPTY;
tab[playerPos.x+direction.x][playePos.y+direction.y] = PLAYER_ON_TARGET;
break;
}
//////pas fini + refaire la 5
player.x = playerPos.x+direction.x;
player.y = playePos.y+direction.y;
}
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;
}
// move player
return 1;
}
return 0;
}