diff --git a/jeu_ia.c b/jeu_ia.c index 28e5cb6..f3a3843 100644 --- a/jeu_ia.c +++ b/jeu_ia.c @@ -2,29 +2,34 @@ #include #include #include +#include "jeu.h" #include "jeu_ia.h" void jouerModeIA(struct EtatJeu *etatJeu) { + int gagnant; + 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; + if (etatJeu->phase == 0) { + gererPlacementInitialIA(etatJeu); + } else { + gererTourJoueurIA(etatJeu); } } } else { usleep(500000); - jouerCoupIA(etatJeu); - - int gagnant = verifierVictoire(*etatJeu); + if (etatJeu->phase == 0) { + placementIA(etatJeu); + } else if (etatJeu->phase == 1) { + deplacementIA(etatJeu); + } else { + blocageIA(etatJeu); + } + } + + if (etatJeu->phase > 0) { + gagnant = verifierVictoire(*etatJeu); if (gagnant != 0) { afficherVictoire(gagnant); while (!SourisCliquee()) { @@ -37,40 +42,96 @@ void jouerModeIA(struct EtatJeu *etatJeu) { } } -void gererClicIA(struct EtatJeu *etatJeu) { - int x = _X; - int y = _Y; +void gererPlacementInitialIA(struct EtatJeu *etatJeu) { + int x, y, i, j; int largeurFenetre = 800; int hauteurFenetre = 600; int marge = 50; + int tailleCase; + int startX, startY; - int tailleCase = (largeurFenetre - 2 * marge) / etatJeu->tailleGrille; + x = _X; + y = _Y; + + 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; + startX = (largeurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; + startY = (hauteurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; x -= startX; y -= startY; - int i = y / tailleCase; - int j = x / tailleCase; + i = y / tailleCase; + 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->grille[i][j] = 1; + etatJeu->joueur1.x = j; + etatJeu->joueur1.y = i; etatJeu->tourJoueur = 2; - - EffacerEcran(CouleurParNom("white")); dessinerGrille(*etatJeu); } } -void jouerCoupIA(struct EtatJeu *etatJeu) { +void gererTourJoueurIA(struct EtatJeu *etatJeu) { + int x, y, i, j; + int largeurFenetre = 800; + int hauteurFenetre = 600; + int marge = 50; + int tailleCase; + int startX, startY; + struct Position posActuelle; + struct Position posCliquee; + + x = _X; + y = _Y; + + tailleCase = (largeurFenetre - 2 * marge) / etatJeu->tailleGrille; + if (tailleCase * etatJeu->tailleGrille > (hauteurFenetre - 2 * marge)) { + tailleCase = (hauteurFenetre - 2 * marge) / etatJeu->tailleGrille; + } + + startX = (largeurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; + startY = (hauteurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2; + + x -= startX; + y -= startY; + + i = y / tailleCase; + j = x / tailleCase; + + posActuelle = etatJeu->joueur1; + posCliquee.x = j; + posCliquee.y = i; + + if (i >= 0 && i < etatJeu->tailleGrille && j >= 0 && j < etatJeu->tailleGrille) { + if (etatJeu->phase == 1) { + if (etatJeu->grille[i][j] == 0 && estCaseAdjacente(posActuelle, posCliquee)) { + etatJeu->grille[etatJeu->joueur1.y][etatJeu->joueur1.x] = 0; + etatJeu->grille[i][j] = 1; + etatJeu->joueur1.x = j; + etatJeu->joueur1.y = i; + etatJeu->phase = 2; + dessinerGrille(*etatJeu); + } + } else if (etatJeu->phase == 2) { + if (etatJeu->grille[i][j] == 0) { + etatJeu->grille[i][j] = 3; + etatJeu->phase = 1; + etatJeu->tourJoueur = 2; + dessinerGrille(*etatJeu); + } + } + } +} + +void placementIA(struct EtatJeu *etatJeu) { int i, j; do { i = rand() % etatJeu->tailleGrille; @@ -78,8 +139,52 @@ void jouerCoupIA(struct EtatJeu *etatJeu) { } while (etatJeu->grille[i][j] != 0); etatJeu->grille[i][j] = 2; + etatJeu->joueur2.x = j; + etatJeu->joueur2.y = i; + etatJeu->phase = 1; + etatJeu->tourJoueur = 1; + dessinerGrille(*etatJeu); +} + +void deplacementIA(struct EtatJeu *etatJeu) { + int dx, dy; + int newX, newY; + int trouve = 0; + + for(dx = -1; dx <= 1 && !trouve; dx++) { + for(dy = -1; dy <= 1 && !trouve; dy++) { + if(dx == 0 && dy == 0) continue; + + newX = etatJeu->joueur2.x + dx; + newY = etatJeu->joueur2.y + dy; + + if(newX >= 0 && newX < etatJeu->tailleGrille && + newY >= 0 && newY < etatJeu->tailleGrille && + etatJeu->grille[newY][newX] == 0) { + + etatJeu->grille[etatJeu->joueur2.y][etatJeu->joueur2.x] = 0; + etatJeu->grille[newY][newX] = 2; + etatJeu->joueur2.x = newX; + etatJeu->joueur2.y = newY; + etatJeu->phase = 2; + trouve = 1; + } + } + } + dessinerGrille(*etatJeu); +} + +void blocageIA(struct EtatJeu *etatJeu) { + int i, j; + int trouve = 0; + + do { + i = rand() % etatJeu->tailleGrille; + j = rand() % etatJeu->tailleGrille; + } while (etatJeu->grille[i][j] != 0); + + etatJeu->grille[i][j] = 3; + etatJeu->phase = 1; etatJeu->tourJoueur = 1; - - EffacerEcran(CouleurParNom("white")); dessinerGrille(*etatJeu); } diff --git a/jeu_ia.h b/jeu_ia.h index b331105..20391d0 100644 --- a/jeu_ia.h +++ b/jeu_ia.h @@ -1,10 +1,13 @@ -#ifndef JEU_IA_H -#define JEU_IA_H +#ifndef JEU_IA_H_INCLUS +#define JEU_IA_H_INCLUS #include "jeu.h" void jouerModeIA(struct EtatJeu *etatJeu); -void gererClicIA(struct EtatJeu *etatJeu); -void jouerCoupIA(struct EtatJeu *etatJeu); +void gererPlacementInitialIA(struct EtatJeu *etatJeu); +void gererTourJoueurIA(struct EtatJeu *etatJeu); +void placementIA(struct EtatJeu *etatJeu); +void deplacementIA(struct EtatJeu *etatJeu); +void blocageIA(struct EtatJeu *etatJeu); -#endif +#endif /* JEU_IA_H_INCLUS */