files :
– Affichage.cpp
#include "Affichage.h"
#include <stdio.h>
void afficherMatrices(Matrice *m) {
printf("Jeu:n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c ", m->jeu[i][j].type);
}
printf("n");
}
printf("Gelatine:n");
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
printf("%c ", m->gelatine[i][j].hasGelatine ? 'G' : ' ');
}
printf("n");
}
}
void lirePionAChanger(int *x1, int *y1, int *x2, int *y2) {
printf("Entrez les coordonnées du premier pion (x y): ");
scanf("%d %d", x1, y1);
printf("Premier pion: (%d, %d)n", *x1, *y1); // Debug
printf("Entrez les coordonnées du deuxième pion (x y): ");
scanf("%d %d", x2, y2);
printf("Deuxième pion: (%d, %d)n", *x2, *y2); // Debug
}
– Affichage.h
#ifndef AFFICHAGE_H
#define AFFICHAGE_H
#include "Matrice.h"
void afficherMatrices(Matrice *m);
void lirePionAChanger(int *x1, int *y1, int *x2, int *y2);
#endif
– main.cpp
#include "Queue.h"
#include "Matrice.h"
#include "Affichage.h"
#include <stdio.h>
#include <string.h>
int main() {
Queue queue;
Matrice matrice;
initQueue(&queue);
initMatrice(&matrice);
afficherMatrices(&matrice); // Affichage initial
int niveau = 1;
while (niveau <= 3) {
printf("Niveau %dn", niveau);
Action action;
Action initAction = {"INITIALISATION", 0, 0, 0, 0};
addAction(&queue, initAction);
while (getAction(&queue, &action)) {
printf("Action: %sn", action.name);
if (strcmp(action.name, "AFFICHAGE") == 0) {
afficherMatrices(&matrice);
} else if (strcmp(action.name, "LECTURE") == 0) {
int x1, y1, x2, y2;
lirePionAChanger(&x1, &y1, &x2, &y2);
Action deplacementAction = {"DEPLACEMENT", x1, y1, x2, y2};
addAction(&queue, deplacementAction);
} else if (strcmp(action.name, "CALCUL") == 0) {
calcul(&matrice, &queue, action.x1, action.y1, action.x2, action.y2);
} else if (strcmp(action.name, "SUPPRESSION-V") == 0) {
suppressionV(&matrice, action.x1, action.y1, action.x2, action.y2);
} else if (strcmp(action.name, "SUPPRESSION-H") == 0) {
suppressionH(&matrice, action.x1, action.y1, action.x2, action.y2);
} else if (strcmp(action.name, "VERIFICATION") == 0) {
verification(&matrice, &queue);
} else if (strcmp(action.name, "INITIALISATION") == 0) {
initMatrice(&matrice);
Action calculAction = {"CALCUL", 0, 0, 0, 0};
addAction(&queue, calculAction);
} else if (strcmp(action.name, "FINNIVEAU") == 0) {
printf("Fin du niveau %dn", niveau);
niveau++;
initQueue(&queue); // Réinitialiser la queue pour le prochain niveau
initMatrice(&matrice); // Réinitialiser la matrice pour le prochain niveau
break; // Sortir de la boucle while intérieure pour passer au niveau suivant
}
}
}
printf("Félicitations ! Vous avez terminé tous les niveaux.n");
return 0;
}
– Matrice.cpp
#include "Matrice.h"
#include "Queue.h"
#include "Affichage.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h> // Pour printf
void initMatrice(Matrice *m) {
srand(time(NULL));
char pions[] = {'J', 'V', 'B', 'R', 'M'};
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
m->jeu[i][j].type = pions[rand() % 5];
m->jeu[i][j].hasGelatine = rand() % 2;
m->gelatine[i][j].type = ' ';
m->gelatine[i][j].hasGelatine = rand() % 2;
}
}
m->coupsRestants = 10; // Par exemple
}
void suppressionV(Matrice *m, int x1, int y1, int x2, int y2) {
printf("Suppression Verticale de (%d, %d) à (%d, %d)n", x1, y1, x2, y2); // Debug
for (int i = x1; i <= x2; i++) {
if (m->jeu[i][y1].hasGelatine) {
m->jeu[i][y1].hasGelatine = 0;
m->gelatine[i][y1].hasGelatine = 0;
}
for (int j = i; j > 0; j--) {
m->jeu[j][y1] = m->jeu[j-1][y1];
}
m->jeu[0][y1].type = "JVRBM"[rand() % 5]; // Remplir avec un pion aléatoire
}
afficherMatrices(m); // Debug
}
void suppressionH(Matrice *m, int x1, int y1, int x2, int y2) {
printf("Suppression Horizontale de (%d, %d) à (%d, %d)n", x1, y1, x2, y2); // Debug
for (int j = y1; j <= y2; j++) {
if (m->jeu[x1][j].hasGelatine) {
m->jeu[x1][j].hasGelatine = 0;
m->gelatine[x1][j].hasGelatine = 0;
}
for (int i = x1; i > 0; i--) {
m->jeu[i][j] = m->jeu[i-1][j];
}
m->jeu[0][j].type = "JVRBM"[rand() % 5]; // Remplir avec un pion aléatoire
}
afficherMatrices(m); // Debug
}
void verification(Matrice *m, Queue *q) {
int gelatineRestante = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (m->jeu[i][j].hasGelatine) {
gelatineRestante = 1;
break;
}
}
if (gelatineRestante) break;
}
if (!gelatineRestante) {
Action finNiveauAction = {"FINNIVEAU", 0, 0, 0, 0};
addAction(q, finNiveauAction);
} else {
Action lectureAction = {"LECTURE", 0, 0, 0, 0};
addAction(q, lectureAction);
}
}
void calcul(Matrice *m, Queue *q, int x1, int y1, int x2, int y2) {
int troisAlignes = 0;
// Vérifier les alignements horizontaux
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE - 2; j++) {
if (m->jeu[i][j].type == m->jeu[i][j+1].type && m->jeu[i][j].type == m->jeu[i][j+2].type) {
Action suppressionHAction = {"SUPPRESSION-H", i, j, i, j+2};
addAction(q, suppressionHAction);
troisAlignes = 1;
}
}
}
// Vérifier les alignements verticaux
for (int j = 0; j < SIZE; j++) {
for (int i = 0; i < SIZE - 2; i++) {
if (m->jeu[i][j].type == m->jeu[i+1][j].type && m->jeu[i][j].type == m->jeu[i+2][j].type) {
Action suppressionVAction = {"SUPPRESSION-V", i, j, i+2, j};
addAction(q, suppressionVAction);
troisAlignes = 1;
}
}
}
if (!troisAlignes) {
Action verificationAction = {"VERIFICATION", 0, 0, 0, 0};
addAction(q, verificationAction);
} else {
// Afficher les matrices après chaque calcul
afficherMatrices(m); // Debug
}
}
void deplacement(Matrice *m, int x1, int y1, int x2, int y2) {
printf("Déplacement de (%d, %d) à (%d, %d)n", x1, y1, x2, y2); // Debug
Case temp = m->jeu[x1][y1];
m->jeu[x1][y1] = m->jeu[x2][y2];
m->jeu[x2][y2] = temp;
afficherMatrices(m); // Debug
}
– Matrice.h
#ifndef MATRICE_H
#define MATRICE_H
#include "Queue.h"
#define SIZE 20
typedef struct {
char type;
int hasGelatine;
} Case;
typedef struct {
Case jeu[SIZE][SIZE];
Case gelatine[SIZE][SIZE];
int coupsRestants;
} Matrice;
void initMatrice(Matrice *m);
void suppressionV(Matrice *m, int x1, int y1, int x2, int y2);
void suppressionH(Matrice *m, int x1, int y1, int x2, int y2);
void verification(Matrice *m, Queue *q);
void calcul(Matrice *m, Queue *q, int x1, int y1, int x2, int y2);
void deplacement(Matrice *m, int x1, int y1, int x2, int y2);
#endif
– Queue.cpp
#include "Queue.h"
#include <string.h>
#include <stdio.h>
void initQueue(Queue *q) {
q->top = 0;
q->next = 0;
q->size = MAX_QUEUE_SIZE;
}
int addAction(Queue *q, Action action) {
if ((q->next + 1) % q->size == q->top) {
// Queue is full
return 0;
}
q->actions[q->next] = action;
q->next = (q->next + 1) % q->size;
return 1;
}
int getAction(Queue *q, Action *action) {
if (q->top == q->next) {
// Queue is empty
return 0;
}
*action = q->actions[q->top];
q->top = (q->top + 1) % q->size;
return 1;
}
– Queue.h
#ifndef QUEUE_H
#define QUEUE_H
#define MAX_QUEUE_SIZE 100
typedef struct {
char name[20];
int x1, y1;
int x2, y2;
} Action;
typedef struct {
Action actions[MAX_QUEUE_SIZE];
int top;
int next;
int size;
} Queue;
void initQueue(Queue *q);
int addAction(Queue *q, Action action);
int getAction(Queue *q, Action *action);
#endif
Hi, My name is Samy. I’m in first year for a Bachelor in computer science in Belgium.
Here, I have a problem with my code for a project. When launch the executable that I have created by using the line : gcc -g main.cpp Queue.cpp Matrice.cpp Affichage.cpp -o candy_crush.exe
The program goes in an infinite loop, and I keep searching where is the problem but I can’t find it.
I’m going to keep searching, but if anyone has a solution to help me move on to my next problems it would be a huge help
Samy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.