diff --git a/blocus b/blocus index 8af1bfd..c781f85 100755 Binary files a/blocus and b/blocus differ diff --git a/jeu.h b/jeu.h index 3a3398a..75ea664 100644 --- a/jeu.h +++ b/jeu.h @@ -1,24 +1,17 @@ #ifndef JEU_H -#define JEU_H +#define JEH_H struct EtatJeu { int tailleGrille; int mode; int tourJoueur; int phase; - int positionSourisJoueur1x; - int positionSourisJoueur1y; - int positionSourisJoueur2x; - int positionSourisJoueur2y; int** grille; }; struct EtatJeu initialiserJeu(int tailleGrille, int mode); -void jouerAvecIA(struct EtatJeu etatJeu); -void gererClicSouris(struct EtatJeu *etatJeu); void dessinerGrille(struct EtatJeu etatJeu); -void DessinerCercle(int x, int y, int rayon); int verifierVictoire(struct EtatJeu etatJeu); void afficherVictoire(int gagnant); -#endif /* JEU_H */ \ No newline at end of file +#endif \ No newline at end of file diff --git a/jeu_humain.c b/jeu_humain.c new file mode 100644 index 0000000..fc38cc3 --- /dev/null +++ b/jeu_humain.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include "jeu_humain.h" + +void jouerModeHumain(struct EtatJeu *etatJeu) { + while (1) { + if (SourisCliquee()) { + printf("Clic détecté sur la grille à : (%d, %d)\n", _X, _Y); + gererClicHumain(etatJeu); + + int gagnant = verifierVictoire(*etatJeu); + if (gagnant != 0) { + afficherVictoire(gagnant); + printf("En attente d'un clic pour retourner au menu...\n"); + while (!SourisCliquee()) { + usleep(100000); + } + break; + } + } + usleep(20000); + } +} + +void gererClicHumain(struct EtatJeu *etatJeu) { + int x = _X; + int y = _Y; + int largeurFenetre = 800; + int hauteurFenetre = 600; + int marge = 50; + + int tailleCase = (largeurFenetre - 2 * marge) / etatJeu->tailleGrille; + if (tailleCase * etatJeu->tailleGrille > (hauteurFenetre - 2 * marge)) { + tailleCase = (hauteurFenetre - 2 * marge) / etatJeu->tailleGrille; + } + + int startX = (largeurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; + int startY = (hauteurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; + + x -= startX; + y -= startY; + + int i = y / tailleCase; + int j = x / tailleCase; + + if (i >= 0 && i < etatJeu->tailleGrille && + j >= 0 && j < etatJeu->tailleGrille && + etatJeu->grille[i][j] == 0) { + + etatJeu->grille[i][j] = etatJeu->tourJoueur; + etatJeu->tourJoueur = (etatJeu->tourJoueur == 1) ? 2 : 1; + + EffacerEcran(CouleurParNom("white")); + dessinerGrille(*etatJeu); + } +} diff --git a/jeu_humain.h b/jeu_humain.h new file mode 100644 index 0000000..f70c92f --- /dev/null +++ b/jeu_humain.h @@ -0,0 +1,9 @@ +#ifndef JEU_HUMAIN_H +#define JEU_HUMAIN_H + +#include "jeu.h" + +void jouerModeHumain(struct EtatJeu *etatJeu); +void gererClicHumain(struct EtatJeu *etatJeu); + +#endif diff --git a/jeu_ia.c b/jeu_ia.c new file mode 100644 index 0000000..28e5cb6 --- /dev/null +++ b/jeu_ia.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include "jeu_ia.h" + +void jouerModeIA(struct EtatJeu *etatJeu) { + while (1) { + if (etatJeu->tourJoueur == 1) { + if (SourisCliquee()) { + printf("Clic détecté sur la grille à : (%d, %d)\n", _X, _Y); + gererClicIA(etatJeu); + + int gagnant = verifierVictoire(*etatJeu); + if (gagnant != 0) { + afficherVictoire(gagnant); + while (!SourisCliquee()) { + usleep(100000); + } + break; + } + } + } else { + usleep(500000); + jouerCoupIA(etatJeu); + + int gagnant = verifierVictoire(*etatJeu); + if (gagnant != 0) { + afficherVictoire(gagnant); + while (!SourisCliquee()) { + usleep(100000); + } + break; + } + } + usleep(20000); + } +} + +void gererClicIA(struct EtatJeu *etatJeu) { + int x = _X; + int y = _Y; + int largeurFenetre = 800; + int hauteurFenetre = 600; + int marge = 50; + + int tailleCase = (largeurFenetre - 2 * marge) / etatJeu->tailleGrille; + if (tailleCase * etatJeu->tailleGrille > (hauteurFenetre - 2 * marge)) { + tailleCase = (hauteurFenetre - 2 * marge) / etatJeu->tailleGrille; + } + + int startX = (largeurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; + int startY = (hauteurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; + + x -= startX; + y -= startY; + + int i = y / tailleCase; + int j = x / tailleCase; + + if (i >= 0 && i < etatJeu->tailleGrille && + j >= 0 && j < etatJeu->tailleGrille && + etatJeu->grille[i][j] == 0) { + + etatJeu->grille[i][j] = etatJeu->tourJoueur; + etatJeu->tourJoueur = 2; + + EffacerEcran(CouleurParNom("white")); + dessinerGrille(*etatJeu); + } +} + +void jouerCoupIA(struct EtatJeu *etatJeu) { + int i, j; + do { + i = rand() % etatJeu->tailleGrille; + j = rand() % etatJeu->tailleGrille; + } while (etatJeu->grille[i][j] != 0); + + etatJeu->grille[i][j] = 2; + etatJeu->tourJoueur = 1; + + EffacerEcran(CouleurParNom("white")); + dessinerGrille(*etatJeu); +} diff --git a/jeu_ia.h b/jeu_ia.h new file mode 100644 index 0000000..b331105 --- /dev/null +++ b/jeu_ia.h @@ -0,0 +1,10 @@ +#ifndef JEU_IA_H +#define JEU_IA_H + +#include "jeu.h" + +void jouerModeIA(struct EtatJeu *etatJeu); +void gererClicIA(struct EtatJeu *etatJeu); +void jouerCoupIA(struct EtatJeu *etatJeu); + +#endif