From 2c61fbe868bff9ecad8053287ef13768a61c9893 Mon Sep 17 00:00:00 2001 From: Aubin DORIVAL Date: Thu, 12 Dec 2024 20:33:05 +0100 Subject: [PATCH] win --- function.c | 29 +++++++++++++++++++++++------ function.h | 4 +++- main.c | 2 +- test2.txt | 3 +++ 4 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 test2.txt diff --git a/function.c b/function.c index 838eb28..94da0bb 100644 --- a/function.c +++ b/function.c @@ -82,8 +82,7 @@ canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase) 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]; @@ -157,13 +156,12 @@ move (unsigned short int **tab, vect *playerPos, vect direction) } } -void -inGameLoop (unsigned short int **tab2d, vect *playerPos) +void inGameLoop (unsigned short int **tab2d, vect *playerPos, vect *targets, int nbr_targets) { vect direction = { 0, 0 }; char input; - - while (1) + bool finish = false; + while (!finish) { printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : "); @@ -203,6 +201,25 @@ inGameLoop (unsigned short int **tab2d, vect *playerPos) move (tab2d, playerPos, direction); printf ("\n"); + if (isWin(tab2d, targets, nbr_targets)) + { + finish = true; + } screenDisplay (tab2d, 32); } } + +bool isWin(unsigned short int **tab2d, vect *targets, int nbr_targets) +{ + int i; + for (i = 0; i < nbr_targets; ++i) + { + int x = targets[i].x; + int y = targets[i].y; + if (tab2d[x][y] != BOX_ON_TARGET) + { + return false; + } + } + return true; +} diff --git a/function.h b/function.h index 423b9f2..a18eb0e 100644 --- a/function.h +++ b/function.h @@ -2,6 +2,7 @@ #define FONCTION_H #include +#include #define EMPTY 0 #define WALL 1 @@ -19,7 +20,8 @@ typedef struct Vecteur unsigned short int **creatArea2D(const unsigned int N); void screenDisplay( unsigned short int **tab,int size); -void inGameLoop(unsigned short int **tab2d,vect *playerPos ); +void inGameLoop (unsigned short int **tab2d, vect *playerPos, vect *targets, int nbr_targets); +bool isWin(unsigned short int **tab2d, vect *targets, int nbr_targets); #endif // FONCTION_H diff --git a/main.c b/main.c index a81628d..4ab415b 100644 --- a/main.c +++ b/main.c @@ -11,7 +11,7 @@ int main() unsigned short int **tab2d = creatArea2D(32); targets = fileToTab2D("test.txt", tab2d, 32, playerPos, &nbr_targets); screenDisplay(tab2d, 32); - inGameLoop(tab2d,playerPos); + inGameLoop(tab2d,playerPos, targets, nbr_targets); free(playerPos); return 0; } diff --git a/test2.txt b/test2.txt new file mode 100644 index 0000000..ef6bcb0 --- /dev/null +++ b/test2.txt @@ -0,0 +1,3 @@ +##### +#@$.# +#####