Commit 2ea5ad0c authored by Erwan's avatar Erwan

Permet de ne plus activer une tuile si la souris est hors du champ de mines

Ajout de commentaire et remise en page du code
parent fb46b69a
......@@ -2,53 +2,90 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define SIZE_OF_HEX 25
struct celluleDemineur{
int line, column;
bool hasMine;
int nearbyMine;
Vector2 pos;
bool isMine;
bool isPressed;
bool ifFlagged;
bool isFlagged;
bool isNeareast;
Color couleurCase;
Vector2 pos;
};
typedef struct celluleDemineur * CelluleDemineur;
bool containsMine(int line, int col, CelluleDemineur** matriceMines);
void initGrille(int nbLineMatrice, int nbColumnMatrice, CelluleDemineur** matriceMines);
CelluleDemineur* recupererVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice);
void generationMines(int nbMines, int nbLineMatrice, int nbColumnMatrice, CelluleDemineur** matriceMines);
void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice);
CelluleDemineur* recupererVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice);
bool revelerVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice);
void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice);
bool revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLigneMatrice, int nbColonneMatrice);
bool revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLineMatrice, int nbColumnMatrice);
void generationMines(int nbMines, int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines);
bool revelerVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice);
void initGrille(int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines);
float distanceEntrePoints(Vector2 a, Vector2 b);
float getDistanceBetweenPoints(Vector2 a, Vector2 b);
void initGrille(int nbLineMatrice, int nbColumnMatrice, CelluleDemineur** matriceMines){
int screenHeight = GetScreenHeight(), screenWidth = GetScreenWidth();
for(int i=0; i<nbLineMatrice; i++){
for(int j=0; j<nbColumnMatrice; j++){
matriceMines[i][j] = malloc(sizeof(struct celluleDemineur));
matriceMines[i][j]->isMine = false;
matriceMines[i][j]->isPressed = false;
matriceMines[i][j]->isFlagged = false;
matriceMines[i][j]->isNeareast = false;
matriceMines[i][j]->nearbyMine = 0;
matriceMines[i][j]->line = i;
matriceMines[i][j]->column = j;
matriceMines[i][j]->couleurCase = DARKGRAY;
//GERE LE DECALLAGE ENTRE LES LIGNES PAIRES ET IMPAIRES
if(i%2 == 0){
matriceMines[i][j]->pos = (Vector2){(1.5*j+1.75)*SIZE_OF_HEX + screenWidth/nbColumnMatrice, (1.5*i+1)*SIZE_OF_HEX + screenHeight/nbLineMatrice};
}else{
matriceMines[i][j]->pos = (Vector2){(1.5*j+1)*SIZE_OF_HEX + screenWidth/nbColumnMatrice, (1.5*i+1)*SIZE_OF_HEX + screenHeight/nbLineMatrice};
}
}
}
}
bool containsMine(int line, int col, CelluleDemineur** matriceMines){
return (matriceMines[line][col]->hasMine == true);
void generationMines(int nbMines, int nbLineMatrice, int nbColumnMatrice, CelluleDemineur** matriceMines){
int lineRand = rand()%nbLineMatrice;
int colRand = rand()%nbColumnMatrice;
for(int i=0; i<nbMines; i++){
while(matriceMines[lineRand][colRand]->isMine){
lineRand = rand()%nbLineMatrice;
colRand = rand()%nbColumnMatrice;
}
matriceMines[lineRand][colRand]->isMine = true;
matriceMines[lineRand][colRand]->nearbyMine = -9;
incrementationVoisinMine(lineRand, colRand, matriceMines, nbLineMatrice, nbColumnMatrice);
}
}
CelluleDemineur* recupererVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice){
CelluleDemineur* recupererVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice){
CelluleDemineur* tabVoisins = calloc(6, sizeof(struct celluleDemineur));
for(int i=0; i<6; i++){
tabVoisins[i] = NULL;
}
//VERIFIE SI LES CASES SONT VALIDES
bool linePrev = (line-1 >= 0), colPrev = (column-1 >= 0);
bool lineNext = (line+1 < nbLigneMatrice), colNext = (column+1 < nbColonneMatrice);
bool lineNext = (line+1 < nbLineMatrice), colNext = (column+1 < nbColumnMatrice);
if(colPrev){tabVoisins[0] = matriceMines[line][column-1];}
if(colNext){tabVoisins[1] = matriceMines[line][column+1];}
......@@ -67,8 +104,8 @@ CelluleDemineur* recupererVoisins(int line, int column, CelluleDemineur** matric
return tabVoisins;
}
void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice){
CelluleDemineur* voisins = recupererVoisins(line, column, matriceMines, nbLigneMatrice, nbColonneMatrice);
void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice){
CelluleDemineur* voisins = recupererVoisins(line, column, matriceMines, nbLineMatrice, nbColumnMatrice);
for(int i=0; i<6; i++){
if(voisins[i] != NULL){
......@@ -78,29 +115,15 @@ void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMin
free(voisins);
}
bool revelerVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice){
CelluleDemineur* voisins = recupererVoisins(line, column, matriceMines, nbLigneMatrice, nbColonneMatrice);
bool res = false, resTemp;
for(int i=0; i<6; i++){
if(voisins[i] != NULL){
resTemp = revelerCase(matriceMines, voisins[i]->line, voisins[i]->column, nbLigneMatrice, nbColonneMatrice);
if(resTemp) res = true;
}
}
free(voisins);
return res;
}
bool revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLigneMatrice, int nbColonneMatrice){
bool revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLineMatrice, int nbColumnMatrice){
bool res = false;
if(!matriceMines[line][column]->ifFlagged && !matriceMines[line][column]->isPressed){
if(!matriceMines[line][column]->isFlagged && !matriceMines[line][column]->isPressed){
matriceMines[line][column]->isPressed = true;
if(matriceMines[line][column]->nearbyMine == 0){
matriceMines[line][column]->couleurCase = LIGHTGRAY;
res = revelerVoisins(line, column, matriceMines, nbLigneMatrice, nbColonneMatrice);
}else if(containsMine(line, column, matriceMines)){
res = revelerVoisins(line, column, matriceMines, nbLineMatrice, nbColumnMatrice);
}else if(matriceMines[line][column]->isMine){
matriceMines[line][column]->couleurCase = MAROON;
res = true;
}else{
......@@ -110,52 +133,25 @@ bool revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLig
return res;
}
void generationMines(int nbMines, int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines){
int lineRand = rand()%nbLigneMatrice;
int colRand = rand()%nbColonneMatrice;
for(int i=0; i<nbMines; i++){
while(containsMine(lineRand, colRand, matriceMines)){
lineRand = rand()%nbLigneMatrice;
colRand = rand()%nbColonneMatrice;
}
matriceMines[lineRand][colRand]->hasMine = true;
matriceMines[lineRand][colRand]->nearbyMine = -9;
incrementationVoisinMine(lineRand, colRand, matriceMines, nbLigneMatrice, nbColonneMatrice);
}
}
void initGrille(int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines){
int screenHeight = GetScreenHeight(), screenWidth = GetScreenWidth();
bool revelerVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice){
CelluleDemineur* voisins = recupererVoisins(line, column, matriceMines, nbLineMatrice, nbColumnMatrice);
bool res = false, resTemp;
for(int i=0; i<nbLigneMatrice; i++){
for(int j=0; j<nbColonneMatrice; j++){
matriceMines[i][j] = malloc(sizeof(struct celluleDemineur));
matriceMines[i][j]->hasMine = false;
matriceMines[i][j]->isPressed = false;
matriceMines[i][j]->ifFlagged = false;
matriceMines[i][j]->isNeareast = false;
matriceMines[i][j]->nearbyMine = 0;
matriceMines[i][j]->line = i;
matriceMines[i][j]->column = j;
matriceMines[i][j]->couleurCase = DARKGRAY;
if(i%2 == 0){
matriceMines[i][j]->pos = (Vector2){(1.5*j+1.75)*SIZE_OF_HEX + screenWidth/nbColonneMatrice, (1.5*i+1)*SIZE_OF_HEX + screenHeight/nbLigneMatrice};
}else{
matriceMines[i][j]->pos = (Vector2){(1.5*j+1)*SIZE_OF_HEX + screenWidth/nbColonneMatrice, (1.5*i+1)*SIZE_OF_HEX + screenHeight/nbLigneMatrice};
}
for(int i=0; i<6; i++){
if(voisins[i] != NULL){
resTemp = revelerCase(matriceMines, voisins[i]->line, voisins[i]->column, nbLineMatrice, nbColumnMatrice);
if(resTemp) res = true;
}
}
free(voisins);
return res;
}
float getDistanceBetweenPoints(Vector2 a, Vector2 b) {
float distanceEntrePoints(Vector2 a, Vector2 b) {
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int main(void)
{
// Initialization
......@@ -167,17 +163,17 @@ int main(void)
srand(time(NULL));
int nbLigneMatrice = 17, nbColonneMatrice = 21, nbMines = 80;
int nbLineMatrice = 17, nbColumnMatrice = 21, nbMines = 80;
int curCaseX = 0, curCaseY = 0, flaggedMines = 0;
bool lose = false, win = false;
bool lose = false, win = false, onTile = false;
float timeEllapsed = 0;
//CREATION DE LA MATRICE DES MINES
CelluleDemineur** matriceMines = malloc(nbLigneMatrice * sizeof(CelluleDemineur *));
for(int i=0; i<nbLigneMatrice; i++)
matriceMines[i] = calloc(nbLigneMatrice * nbColonneMatrice, sizeof(CelluleDemineur));
initGrille(nbLigneMatrice, nbColonneMatrice, matriceMines);
generationMines(nbMines, nbLigneMatrice, nbColonneMatrice, matriceMines);
CelluleDemineur** matriceMines = malloc(nbLineMatrice * sizeof(CelluleDemineur *));
for(int i=0; i<nbLineMatrice; i++)
matriceMines[i] = calloc(nbLineMatrice * nbColumnMatrice, sizeof(CelluleDemineur));
initGrille(nbLineMatrice, nbColumnMatrice, matriceMines);
generationMines(nbMines, nbLineMatrice, nbColumnMatrice, matriceMines);
//--------------------------------------------------------------------------------------
// Main game loop
......@@ -185,17 +181,36 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
onTile = false;
if(!win && !lose){
timeEllapsed += GetFrameTime();
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON)){
lose = revelerCase(matriceMines, curCaseX, curCaseY, nbLigneMatrice, nbColonneMatrice);
//RECUPERE LA TUILE SUR LAQUELLE SE TROUVE LA SOURIS
Vector2 mPos = GetMousePosition();
for(int i=0; i<nbLineMatrice; i++){
for(int j=0; j<nbColumnMatrice; j++){
float distanceCur = distanceEntrePoints(mPos, matriceMines[i][j]->pos);
if(distanceCur < SIZE_OF_HEX*SIZE_OF_HEX/2){
matriceMines[i][j]->isNeareast = true;
curCaseX = matriceMines[i][j]->line;
curCaseY = matriceMines[i][j]->column;
onTile = true;
}else{
matriceMines[i][j]->isNeareast = false;
}
}
}
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
//GESTION DES INPUTS
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && onTile){
lose = revelerCase(matriceMines, curCaseX, curCaseY, nbLineMatrice, nbColumnMatrice);
}
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON) && onTile){
if(!matriceMines[curCaseX][curCaseY]->isPressed){
matriceMines[curCaseX][curCaseY]->ifFlagged = !matriceMines[curCaseX][curCaseY]->ifFlagged;
if(matriceMines[curCaseX][curCaseY]->ifFlagged){
matriceMines[curCaseX][curCaseY]->isFlagged = !matriceMines[curCaseX][curCaseY]->isFlagged;
if(matriceMines[curCaseX][curCaseY]->isFlagged){
matriceMines[curCaseX][curCaseY]->couleurCase = GREEN;
flaggedMines++;
}
......@@ -206,40 +221,29 @@ int main(void)
}
}
if(IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON)){
lose = revelerCase(matriceMines, curCaseX, curCaseY, nbLigneMatrice, nbColonneMatrice);
lose = revelerVoisins(curCaseX, curCaseY, matriceMines, nbLigneMatrice, nbColonneMatrice);
if(IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON) && onTile){
lose = revelerCase(matriceMines, curCaseX, curCaseY, nbLineMatrice, nbColumnMatrice);
lose = revelerVoisins(curCaseX, curCaseY, matriceMines, nbLineMatrice, nbColumnMatrice);
}
}else{
//REGENERE LA GRILLE LORS DE LA VICTOIRE OU DE LA DEFAITE
if(IsKeyPressed('R')){
lose = false;
win = false;
flaggedMines = 0;
timeEllapsed = 0;
initGrille(nbLigneMatrice, nbColonneMatrice, matriceMines);
generationMines(nbMines, nbLigneMatrice, nbColonneMatrice, matriceMines);
}
}
Vector2 mPos = GetMousePosition();
for(int i=0; i<nbLigneMatrice; i++){
for(int j=0; j<nbColonneMatrice; j++){
float distanceCur = getDistanceBetweenPoints(mPos, matriceMines[i][j]->pos);
if(distanceCur < SIZE_OF_HEX*SIZE_OF_HEX/2){
matriceMines[i][j]->isNeareast = true;
curCaseX = matriceMines[i][j]->line;
curCaseY = matriceMines[i][j]->column;
}else{
matriceMines[i][j]->isNeareast = false;
}
initGrille(nbLineMatrice, nbColumnMatrice, matriceMines);
generationMines(nbMines, nbLineMatrice, nbColumnMatrice, matriceMines);
}
}
//TEST LA CONDITION DE VICTOIRE
if(flaggedMines == nbMines){
int trueFlagged = 0;
for(int i=0; i<nbLigneMatrice; i++){
for(int j=0; j<nbColonneMatrice; j++){
if(matriceMines[i][j]->hasMine && matriceMines[i][j]->ifFlagged) trueFlagged++;
for(int i=0; i<nbLineMatrice; i++){
for(int j=0; j<nbColumnMatrice; j++){
if(matriceMines[i][j]->isMine && matriceMines[i][j]->isFlagged) trueFlagged++;
}
}
......@@ -253,8 +257,8 @@ int main(void)
ClearBackground(RAYWHITE);
//AFFICHAGE DE LA GRILLE
for(int i=0; i<nbLigneMatrice; i++){
for(int j=0; j<nbColonneMatrice; j++){
for(int i=0; i<nbLineMatrice; i++){
for(int j=0; j<nbColumnMatrice; j++){
DrawPoly((Vector2){matriceMines[i][j]->pos.x, matriceMines[i][j]->pos.y}, 6, SIZE_OF_HEX, 0, GRAY);
DrawPoly((Vector2){matriceMines[i][j]->pos.x, matriceMines[i][j]->pos.y}, 6, SIZE_OF_HEX-3, 0, Fade(matriceMines[i][j]->couleurCase, matriceMines[i][j]->isNeareast ? 0.5f : 1.0f));
......@@ -283,15 +287,18 @@ int main(void)
DrawText("LIGHT GRAY - REVEALED", 930, 420, 18, LIGHTGRAY);
DrawText("GREEN - FLAGGED", 930, 450, 18, LIGHTGRAY);
DrawText("RED - MINE", 930, 480, 18, LIGHTGRAY);
DrawText("ORANGE - CORRECT FLAG", 930, 510, 18, LIGHTGRAY);
//AFFICHAGE FIN DE PARTIE
if(lose || win){
DrawRectangle(0, 440, 1280, 100, Fade(GRAY, 0.8f));
if(lose){
DrawText("GAME OVER", 500, 465, 35, MAROON);
for(int i=0; i<nbLigneMatrice; i++){
for(int j=0; j<nbColonneMatrice; j++){
if(containsMine(i, j, matriceMines)) revelerCase(matriceMines, i, j, nbLigneMatrice, nbColonneMatrice);
for(int i=0; i<nbLineMatrice; i++){
for(int j=0; j<nbColumnMatrice; j++){
//REVELE L'EMPLACEMENT DES MINES NON DECOUVERTES
if(matriceMines[i][j]->isMine) revelerCase(matriceMines, i, j, nbLineMatrice, nbColumnMatrice);
if(matriceMines[i][j]->isMine && matriceMines[i][j]->isFlagged) matriceMines[i][j]->couleurCase = ORANGE;
}
}
}
......@@ -307,7 +314,7 @@ int main(void)
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
for(int i=0; i<nbLigneMatrice; i++)
for(int i=0; i<nbLineMatrice; i++)
free(matriceMines[i]);
free(matriceMines);
//--------------------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment