clang
This commit is contained in:
320
function.c
320
function.c
@@ -1,202 +1,208 @@
|
|||||||
#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 *));
|
||||||
if (tab2d == NULL)
|
if (tab2d == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool fail = false;
|
bool fail = false;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
for (i = 0; i < N && !fail; ++i)
|
for (i = 0; i < N && !fail; ++i)
|
||||||
{
|
|
||||||
tab2d[i] = calloc(N, sizeof(unsigned short int));
|
|
||||||
if (tab2d[i] == NULL)
|
|
||||||
{
|
{
|
||||||
fail = true;
|
tab2d[i] = calloc (N, sizeof (unsigned short int));
|
||||||
|
if (tab2d[i] == NULL)
|
||||||
|
{
|
||||||
|
fail = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (fail)
|
if (fail)
|
||||||
{
|
|
||||||
unsigned int j;
|
|
||||||
for (j = 0; j < i; ++j)
|
|
||||||
{
|
{
|
||||||
free(tab2d[j]);
|
unsigned int j;
|
||||||
|
for (j = 0; j < i; ++j)
|
||||||
|
{
|
||||||
|
free (tab2d[j]);
|
||||||
|
}
|
||||||
|
free (tab2d);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
free(tab2d);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
// Box on target
|
// Box on target
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
//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)
|
||||||
{
|
{
|
||||||
// Box on target and player on target too
|
// Box on target and player on target too
|
||||||
return 6;
|
return 6;
|
||||||
}
|
}
|
||||||
//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)
|
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
|
||||||
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];
|
||||||
|
|
||||||
switch(returnValue)
|
switch (returnValue)
|
||||||
{
|
{
|
||||||
case 0:
|
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;
|
break;
|
||||||
tab[playerPos->x+direction.x*2][playerPos->y+direction.y*2] = BOX_ON_TARGET;
|
case 1:
|
||||||
break;
|
// move player
|
||||||
case 4:
|
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||||
//move player on a target
|
break;
|
||||||
|
case 2:
|
||||||
|
// move player and the box
|
||||||
|
|
||||||
tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER_ON_TARGET;
|
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||||
break;
|
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||||
case 5:
|
= BOX;
|
||||||
//move box and player on a target
|
break;
|
||||||
|
case 3:
|
||||||
|
// move player and the box is well-placed
|
||||||
|
|
||||||
tab[playerPos->x+direction.x*2][playerPos->y+direction.y*2] = BOX;
|
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||||
tab[playerPos->x+direction.x][playerPos->y+direction.y] = PLAYER_ON_TARGET;
|
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||||
break;
|
= BOX_ON_TARGET;
|
||||||
case 6:
|
break;
|
||||||
//move player on a target and box on a target
|
case 4:
|
||||||
|
// move player 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;
|
tab[playerPos->x + direction.x][playerPos->y + direction.y]
|
||||||
break;
|
= 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
direction.y = -1;
|
||||||
|
direction.x = 0;
|
||||||
|
break;
|
||||||
|
case 'z':
|
||||||
|
direction.x = -1;
|
||||||
|
direction.y = 0;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
direction.x = 1;
|
||||||
|
direction.y = 0;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
printf("Commande inconnue !\n");
|
direction.x = 0;
|
||||||
|
direction.y = 0;
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
system ("clear");
|
||||||
|
move (tab2d, playerPos, direction);
|
||||||
|
|
||||||
switch (input) {
|
printf ("\n");
|
||||||
case 'd':
|
screenDisplay (tab2d, 32);
|
||||||
direction.y=1;
|
|
||||||
direction.x=0;
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
direction.y=-1;
|
|
||||||
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");
|
|
||||||
move(tab2d,playerPos, direction);
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
screenDisplay(tab2d, 32);
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
main.c
6
main.c
@@ -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;
|
||||||
|
|||||||
91
read.c
91
read.c
@@ -1,54 +1,57 @@
|
|||||||
|
#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;
|
||||||
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 '#':
|
|
||||||
tab[x][y] = WALL;
|
|
||||||
break;
|
|
||||||
case '$':
|
|
||||||
tab[x][y] = BOX;
|
|
||||||
break;
|
|
||||||
case '.':
|
|
||||||
targets = realloc(targets, sizeof(vect)*(++nbr_targets[0]));
|
|
||||||
printf("nbr %d, pointeur %p\n", nbr_targets[0], targets);
|
|
||||||
targets[nbr_targets[0] - 1].x = x;
|
|
||||||
printf("%d : %d\n", targets[nbr_targets[0] - 1].x, nbr_targets[0] -1 );
|
|
||||||
targets[nbr_targets[0] - 1].y = y;
|
|
||||||
printf("%d\n", targets[nbr_targets[0] - 1].y);
|
|
||||||
tab[x][y] = TARGET;
|
|
||||||
break;
|
|
||||||
case '@':
|
|
||||||
player->x = x;
|
|
||||||
player->y = y;
|
|
||||||
tab[x][y] = PLAYER;
|
|
||||||
break;
|
|
||||||
case '\n':
|
|
||||||
y = 0;
|
|
||||||
++x;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
tab[x][y] = EMPTY;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
++y;
|
|
||||||
|
|
||||||
if (x >= N || y >= N)
|
|
||||||
{
|
{
|
||||||
perror("Level out of range !");
|
char current = fgetc (file);
|
||||||
exit(-1);
|
switch (current)
|
||||||
|
{
|
||||||
|
case '#':
|
||||||
|
tab[x][y] = WALL;
|
||||||
|
break;
|
||||||
|
case '$':
|
||||||
|
tab[x][y] = BOX;
|
||||||
|
break;
|
||||||
|
case '.':
|
||||||
|
targets = realloc (targets, sizeof (vect) * (++nbr_targets[0]));
|
||||||
|
printf ("nbr %d, pointeur %p\n", nbr_targets[0], targets);
|
||||||
|
targets[nbr_targets[0] - 1].x = x;
|
||||||
|
printf ("%d : %d\n", targets[nbr_targets[0] - 1].x,
|
||||||
|
nbr_targets[0] - 1);
|
||||||
|
targets[nbr_targets[0] - 1].y = y;
|
||||||
|
printf ("%d\n", targets[nbr_targets[0] - 1].y);
|
||||||
|
tab[x][y] = TARGET;
|
||||||
|
break;
|
||||||
|
case '@':
|
||||||
|
player->x = x;
|
||||||
|
player->y = y;
|
||||||
|
tab[x][y] = PLAYER;
|
||||||
|
break;
|
||||||
|
case '\n':
|
||||||
|
y = 0;
|
||||||
|
++x;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
tab[x][y] = EMPTY;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
++y;
|
||||||
|
|
||||||
|
if (x >= N || y >= N)
|
||||||
|
{
|
||||||
|
perror ("Level out of range !");
|
||||||
|
exit (-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return targets;
|
return targets;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user