Ajout de l'IA (utilisation de random)
This commit is contained in:
159
jeu_ia.c
159
jeu_ia.c
@@ -2,29 +2,34 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <graph.h>
|
#include <graph.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "jeu.h"
|
||||||
#include "jeu_ia.h"
|
#include "jeu_ia.h"
|
||||||
|
|
||||||
void jouerModeIA(struct EtatJeu *etatJeu) {
|
void jouerModeIA(struct EtatJeu *etatJeu) {
|
||||||
|
int gagnant;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
if (etatJeu->tourJoueur == 1) {
|
if (etatJeu->tourJoueur == 1) {
|
||||||
if (SourisCliquee()) {
|
if (SourisCliquee()) {
|
||||||
printf("Clic détecté sur la grille à : (%d, %d)\n", _X, _Y);
|
if (etatJeu->phase == 0) {
|
||||||
gererClicIA(etatJeu);
|
gererPlacementInitialIA(etatJeu);
|
||||||
|
} else {
|
||||||
int gagnant = verifierVictoire(*etatJeu);
|
gererTourJoueurIA(etatJeu);
|
||||||
if (gagnant != 0) {
|
|
||||||
afficherVictoire(gagnant);
|
|
||||||
while (!SourisCliquee()) {
|
|
||||||
usleep(100000);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
usleep(500000);
|
usleep(500000);
|
||||||
jouerCoupIA(etatJeu);
|
if (etatJeu->phase == 0) {
|
||||||
|
placementIA(etatJeu);
|
||||||
int gagnant = verifierVictoire(*etatJeu);
|
} else if (etatJeu->phase == 1) {
|
||||||
|
deplacementIA(etatJeu);
|
||||||
|
} else {
|
||||||
|
blocageIA(etatJeu);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (etatJeu->phase > 0) {
|
||||||
|
gagnant = verifierVictoire(*etatJeu);
|
||||||
if (gagnant != 0) {
|
if (gagnant != 0) {
|
||||||
afficherVictoire(gagnant);
|
afficherVictoire(gagnant);
|
||||||
while (!SourisCliquee()) {
|
while (!SourisCliquee()) {
|
||||||
@@ -37,40 +42,96 @@ void jouerModeIA(struct EtatJeu *etatJeu) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void gererClicIA(struct EtatJeu *etatJeu) {
|
void gererPlacementInitialIA(struct EtatJeu *etatJeu) {
|
||||||
int x = _X;
|
int x, y, i, j;
|
||||||
int y = _Y;
|
|
||||||
int largeurFenetre = 800;
|
int largeurFenetre = 800;
|
||||||
int hauteurFenetre = 600;
|
int hauteurFenetre = 600;
|
||||||
int marge = 50;
|
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)) {
|
if (tailleCase * etatJeu->tailleGrille > (hauteurFenetre - 2 * marge)) {
|
||||||
tailleCase = (hauteurFenetre - 2 * marge) / etatJeu->tailleGrille;
|
tailleCase = (hauteurFenetre - 2 * marge) / etatJeu->tailleGrille;
|
||||||
}
|
}
|
||||||
|
|
||||||
int startX = (largeurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2;
|
startX = (largeurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2;
|
||||||
int startY = (hauteurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2;
|
startY = (hauteurFenetre - (tailleCase * etatJeu->tailleGrille)) / 2;
|
||||||
|
|
||||||
x -= startX;
|
x -= startX;
|
||||||
y -= startY;
|
y -= startY;
|
||||||
|
|
||||||
int i = y / tailleCase;
|
i = y / tailleCase;
|
||||||
int j = x / tailleCase;
|
j = x / tailleCase;
|
||||||
|
|
||||||
if (i >= 0 && i < etatJeu->tailleGrille &&
|
if (i >= 0 && i < etatJeu->tailleGrille &&
|
||||||
j >= 0 && j < etatJeu->tailleGrille &&
|
j >= 0 && j < etatJeu->tailleGrille &&
|
||||||
etatJeu->grille[i][j] == 0) {
|
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;
|
etatJeu->tourJoueur = 2;
|
||||||
|
|
||||||
EffacerEcran(CouleurParNom("white"));
|
|
||||||
dessinerGrille(*etatJeu);
|
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;
|
int i, j;
|
||||||
do {
|
do {
|
||||||
i = rand() % etatJeu->tailleGrille;
|
i = rand() % etatJeu->tailleGrille;
|
||||||
@@ -78,8 +139,52 @@ void jouerCoupIA(struct EtatJeu *etatJeu) {
|
|||||||
} while (etatJeu->grille[i][j] != 0);
|
} while (etatJeu->grille[i][j] != 0);
|
||||||
|
|
||||||
etatJeu->grille[i][j] = 2;
|
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;
|
etatJeu->tourJoueur = 1;
|
||||||
|
|
||||||
EffacerEcran(CouleurParNom("white"));
|
|
||||||
dessinerGrille(*etatJeu);
|
dessinerGrille(*etatJeu);
|
||||||
}
|
}
|
||||||
|
|||||||
13
jeu_ia.h
13
jeu_ia.h
@@ -1,10 +1,13 @@
|
|||||||
#ifndef JEU_IA_H
|
#ifndef JEU_IA_H_INCLUS
|
||||||
#define JEU_IA_H
|
#define JEU_IA_H_INCLUS
|
||||||
|
|
||||||
#include "jeu.h"
|
#include "jeu.h"
|
||||||
|
|
||||||
void jouerModeIA(struct EtatJeu *etatJeu);
|
void jouerModeIA(struct EtatJeu *etatJeu);
|
||||||
void gererClicIA(struct EtatJeu *etatJeu);
|
void gererPlacementInitialIA(struct EtatJeu *etatJeu);
|
||||||
void jouerCoupIA(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 */
|
||||||
|
|||||||
Reference in New Issue
Block a user