86 lines
2.3 KiB
C
86 lines
2.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <graph.h>
|
|
#include <unistd.h>
|
|
#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);
|
|
}
|