formatage
This commit is contained in:
102
function.c
102
function.c
@@ -1,4 +1,5 @@
|
|||||||
#include "function.h"
|
#include "function.h"
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -37,14 +38,15 @@ creatArea2D (const unsigned int N)
|
|||||||
return tab2d;
|
return tab2d;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free2D(unsigned short int **tab, int N)
|
void
|
||||||
|
free2D (unsigned short int **tab, int N)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < N; ++i)
|
for (i = 0; i < N; ++i)
|
||||||
{
|
{
|
||||||
free(tab[i]);
|
free (tab[i]);
|
||||||
}
|
}
|
||||||
free(tab);
|
free (tab);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +58,6 @@ canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase)
|
|||||||
if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
|
if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
|
||||||
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
|
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
|
||||||
{
|
{
|
||||||
|
|
||||||
if (valueOfNPlusOneCase == TARGET)
|
if (valueOfNPlusOneCase == TARGET)
|
||||||
{
|
{
|
||||||
// Box on target
|
// Box on target
|
||||||
@@ -68,7 +69,6 @@ canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase)
|
|||||||
if ((valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == EMPTY)
|
if ((valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == EMPTY)
|
||||||
|| (valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == TARGET))
|
|| (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
|
||||||
@@ -98,18 +98,19 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
|
|||||||
{
|
{
|
||||||
short int valueOfNCase
|
short int valueOfNCase
|
||||||
= tab[playerPos->x + direction.x][playerPos->y + direction.y];
|
= tab[playerPos->x + direction.x][playerPos->y + direction.y];
|
||||||
|
|
||||||
vect add = plusVect(*playerPos, direction);
|
vect add = plusVect (*playerPos, direction);
|
||||||
add = plusVect(add,direction);
|
add = plusVect (add, direction);
|
||||||
short int valueOfNPlusOneCase;
|
short int valueOfNPlusOneCase;
|
||||||
|
|
||||||
if(add.x < 0 || add.y < 0)
|
if (add.x < 0 || add.y < 0)
|
||||||
{
|
{
|
||||||
valueOfNPlusOneCase = WALL;
|
valueOfNPlusOneCase = WALL;
|
||||||
}else
|
}
|
||||||
{
|
else
|
||||||
valueOfNPlusOneCase = tab[add.x][add.y];
|
{
|
||||||
}
|
valueOfNPlusOneCase = tab[add.x][add.y];
|
||||||
|
}
|
||||||
|
|
||||||
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];
|
||||||
@@ -179,7 +180,10 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
|
inGameLoop (unsigned short int **tab2d,
|
||||||
|
int N,
|
||||||
|
vect *playerPos,
|
||||||
|
vect *targets,
|
||||||
int nbr_targets)
|
int nbr_targets)
|
||||||
{
|
{
|
||||||
vect direction = { 0, 0 };
|
vect direction = { 0, 0 };
|
||||||
@@ -187,7 +191,6 @@ inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
|
|||||||
bool finish = false;
|
bool finish = false;
|
||||||
while (!finish)
|
while (!finish)
|
||||||
{
|
{
|
||||||
|
|
||||||
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 ();
|
||||||
@@ -227,12 +230,12 @@ inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
|
|||||||
printf ("\n");
|
printf ("\n");
|
||||||
if (isWin (tab2d, targets, nbr_targets))
|
if (isWin (tab2d, targets, nbr_targets))
|
||||||
{
|
{
|
||||||
puts("Win!");
|
puts ("Win!");
|
||||||
finish = true;
|
finish = true;
|
||||||
}
|
}
|
||||||
if (islose(tab2d, N))
|
if (islose (tab2d, N))
|
||||||
{
|
{
|
||||||
puts("lose!");
|
puts ("lose!");
|
||||||
finish = true;
|
finish = true;
|
||||||
}
|
}
|
||||||
screenDisplay (tab2d, N);
|
screenDisplay (tab2d, N);
|
||||||
@@ -301,7 +304,7 @@ blockBox (unsigned short int **tab2d, vect box_coor)
|
|||||||
{
|
{
|
||||||
int nbr_touch = 0;
|
int nbr_touch = 0;
|
||||||
vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } };
|
vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } };
|
||||||
int indice_card[4] = {0};
|
int indice_card[4] = { 0 };
|
||||||
int id_box = -1;
|
int id_box = -1;
|
||||||
int i;
|
int i;
|
||||||
for (i = 0; i < 4; ++i)
|
for (i = 0; i < 4; ++i)
|
||||||
@@ -311,41 +314,43 @@ blockBox (unsigned short int **tab2d, vect box_coor)
|
|||||||
if (val == BOX || val == WALL || val == BOX_ON_TARGET)
|
if (val == BOX || val == WALL || val == BOX_ON_TARGET)
|
||||||
{
|
{
|
||||||
nbr_touch++;
|
nbr_touch++;
|
||||||
if(val == BOX || val == BOX_ON_TARGET)
|
if (val == BOX || val == BOX_ON_TARGET)
|
||||||
{
|
{
|
||||||
id_box = nbr_touch -1;
|
id_box = nbr_touch - 1;
|
||||||
}
|
}
|
||||||
indice_card[nbr_touch - 1] = i;
|
indice_card[nbr_touch - 1] = i;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nbr_touch == 0)
|
if (nbr_touch == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (nbr_touch >= 3)
|
if (nbr_touch >= 3)
|
||||||
{
|
|
||||||
if(id_box != -1)
|
|
||||||
{
|
{
|
||||||
vect test = plusVect(card[indice_card[(id_box+1) %3]], card[indice_card[(id_box+2) %3]]);
|
if (id_box != -1)
|
||||||
if (test.x == 0 && test.y == 0) return false;
|
{
|
||||||
|
vect test = plusVect (card[indice_card[(id_box + 1) % 3]],
|
||||||
|
card[indice_card[(id_box + 2) % 3]]);
|
||||||
|
if (test.x == 0 && test.y == 0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (nbr_touch == 2)
|
if (nbr_touch == 2)
|
||||||
{
|
{
|
||||||
|
|
||||||
int id1 = indice_card[0];
|
int id1 = indice_card[0];
|
||||||
int id2 = indice_card[1];
|
int id2 = indice_card[1];
|
||||||
vect add = plusVect (card[id1], card[id2]);
|
vect add = plusVect (card[id1], card[id2]);
|
||||||
if(lengthVect (add) == 2)
|
if (lengthVect (add) == 2)
|
||||||
{
|
{
|
||||||
if (id_box == -1) return true;
|
if (id_box == -1)
|
||||||
vect test = plusVect(card[indice_card[id_box]], card[indice_card[(id_box+1) %2]]);
|
return true;
|
||||||
test = plusVect(test, box_coor);
|
vect test = plusVect (card[indice_card[id_box]],
|
||||||
if (tab2d[test.x][test.y] == WALL) return true;
|
card[indice_card[(id_box + 1) % 2]]);
|
||||||
}
|
test = plusVect (test, box_coor);
|
||||||
|
if (tab2d[test.x][test.y] == WALL)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,8 +367,10 @@ blockBox (unsigned short int **tab2d, vect box_coor)
|
|||||||
while (!wall)
|
while (!wall)
|
||||||
{
|
{
|
||||||
unsigned short int val = tab2d[current.x][current.y];
|
unsigned short int val = tab2d[current.x][current.y];
|
||||||
if (val == WALL) wall = true;
|
if (val == WALL)
|
||||||
if (val == TARGET) return false;
|
wall = true;
|
||||||
|
if (val == TARGET)
|
||||||
|
return false;
|
||||||
|
|
||||||
vect cur_nor = plusVect (current, normal);
|
vect cur_nor = plusVect (current, normal);
|
||||||
val = tab2d[cur_nor.x][cur_nor.y];
|
val = tab2d[cur_nor.x][cur_nor.y];
|
||||||
@@ -371,10 +378,9 @@ blockBox (unsigned short int **tab2d, vect box_coor)
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
perror("error in blockBox");
|
perror ("error in blockBox");
|
||||||
exit(0);
|
exit (0);
|
||||||
}
|
}
|
||||||
|
|||||||
25
function.h
25
function.h
@@ -1,8 +1,8 @@
|
|||||||
#ifndef FONCTION_H
|
#ifndef FONCTION_H
|
||||||
#define FONCTION_H
|
#define FONCTION_H
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define EMPTY 0
|
#define EMPTY 0
|
||||||
@@ -15,8 +15,8 @@
|
|||||||
|
|
||||||
typedef struct Vecteur
|
typedef struct Vecteur
|
||||||
{
|
{
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
} vect;
|
} vect;
|
||||||
|
|
||||||
typedef struct Score
|
typedef struct Score
|
||||||
@@ -27,17 +27,14 @@ typedef struct Score
|
|||||||
unsigned int move_box;
|
unsigned int move_box;
|
||||||
} score;
|
} score;
|
||||||
|
|
||||||
|
unsigned short int **creatArea2D (const unsigned int N);
|
||||||
unsigned short int **creatArea2D(const unsigned int N);
|
void free2D (unsigned short int **tab, int N);
|
||||||
void free2D(unsigned short int **tab, int N);
|
void screenDisplay (unsigned short int **tab, int size);
|
||||||
void screenDisplay( unsigned short int **tab,int size);
|
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
||||||
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, int nbr_targets);
|
vect *targets, int nbr_targets);
|
||||||
bool isWin(unsigned short int **tab2d, vect *targets, int nbr_targets);
|
bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets);
|
||||||
bool islose(unsigned short int ** tab2d,const int N);
|
bool islose (unsigned short int **tab2d, const int N);
|
||||||
bool blockBox(unsigned short int **tab2d, vect box_coor);
|
bool blockBox (unsigned short int **tab2d, vect box_coor);
|
||||||
vect plusVect (vect one, vect two);
|
vect plusVect (vect one, vect two);
|
||||||
|
|
||||||
#endif // FONCTION_H
|
#endif // FONCTION_H
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user