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