la lose fonctionne mais il faut corriger deplacement

This commit is contained in:
2024-12-12 22:54:31 +01:00
parent 21885b0f99
commit 14777da370
4 changed files with 161 additions and 14 deletions

View File

@@ -3,6 +3,7 @@
void screenDisplay( unsigned short int **tab,int size)
{
puts("0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ");
char element[7] ={' ','#','S','.','*','@','+'} ;
for(int i=0;i<size;++i)
{

View File

@@ -82,7 +82,8 @@ 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];
@@ -156,7 +157,9 @@ void move (unsigned short int **tab, vect *playerPos, vect direction)
}
}
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, int nbr_targets)
void
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
int nbr_targets)
{
vect direction = { 0, 0 };
char input;
@@ -197,29 +200,169 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targe
direction.y = 0;
}
system ("clear");
//system ("clear");
move (tab2d, playerPos, direction);
printf ("\n");
if (isWin(tab2d, targets, nbr_targets))
{
finish = true;
}
if (isWin (tab2d, targets, nbr_targets))
{
puts("Win!");
finish = true;
}
if (islose(tab2d, N))
{
puts("lose!");
finish = true;
}
screenDisplay (tab2d, N);
}
}
bool isWin(unsigned short int **tab2d, vect *targets, int nbr_targets)
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;
int x = targets[i].x;
int y = targets[i].y;
if (tab2d[x][y] != BOX_ON_TARGET)
{
return false;
}
}
}
return true;
}
bool
islose (unsigned short int **tab2d, const int N)
{
int x = 0, y = 0;
for (x = 0; x < N; ++x)
{
for (y = 0; y < N; ++y)
{
bool isblock = false;
if (tab2d[x][y] == BOX)
{
vect box;
box.x = x;
box.y = y;
isblock = blockBox (tab2d, N, box);
}
if (isblock)
{
printf("%d %d", x, y);
return true;
}
}
}
return false;
}
vect
plusVect (vect one, vect two)
{
vect result;
result.x = one.x + two.x;
result.y = one.y + two.y;
return result;
}
int
lengthVect (vect vector)
{
return abs (vector.x) + abs (vector.y);
}
bool
blockBox (unsigned short int **tab2d, const int N, vect box_coor)
{
int nbr_touch = 0;
vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } };
int *indice_card = NULL;
int id_box = -1;
int i;
for (i = 0; i < 4; ++i)
{
vect test = plusVect (box_coor, card[i]);
unsigned short int val = tab2d[test.x][test.y];
if (val == BOX || val == WALL || val == BOX_ON_TARGET)
{
nbr_touch++;
if(val == BOX || val == BOX_ON_TARGET)
{
id_box = nbr_touch -1;
}
indice_card = realloc (indice_card, sizeof (int) * nbr_touch);
indice_card[nbr_touch - 1] = i;
printf("i = %d, (%d, %d)\n", i, test.x, test.y);
}
}
printf("nbr_touch = %d\n", nbr_touch);
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;
}
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);
printf("Vect test : %d %d\n", test.x, test.y);
if (tab2d[test.x][test.y] == WALL) return true;
}
return false;
}
vect normal = card[indice_card[0]];
vect director1 = card[(indice_card[0] + 1) % 4];
vect director2 = card[(indice_card[0] + 1) % 4];
vect dir_test[2] = { director1, director2 };
bool wall = false;
vect current = plusVect (box_coor, director1);
for (int i = 0; i < 2; ++i)
{
vect current = plusVect (box_coor, dir_test[i]);
while (!wall)
{
unsigned short int val = tab2d[current.x][current.y];
if (val == WALL) wall = true;
if (val == TARGET) return false;
vect cur_nor = plusVect (current, normal);
val = tab2d[cur_nor.x][cur_nor.y];
if (val != WALL || val != BOX || val != BOX_ON_TARGET)
{
puts("false");
return false;
}
}
puts("fait");
}
return true;
perror("error in blockBox");
exit(0);
}

View File

@@ -22,6 +22,8 @@ unsigned short int **creatArea2D(const unsigned 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, const int N, vect box_coor);
#endif // FONCTION_H

1
main.c
View File

@@ -13,6 +13,7 @@ int main()
unsigned short int **tab2d = creatArea2D(SIZE_PLAY);
targets = fileToTab2D("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
screenDisplay(tab2d, SIZE_PLAY);
printf("player (%d, %d)", playerPos->x, playerPos->y);
inGameLoop(tab2d, SIZE_PLAY, playerPos, targets, nbr_targets);
free(playerPos);
return 0;