Commit 03239074 authored by Erwan's avatar Erwan

Version fonctionnel et gestion de la mémoire

parent 8e9651ba
......@@ -4,7 +4,7 @@
#include <stdbool.h>
#include <math.h>
#define SIZE_OF_HEX 20
#define SIZE_OF_HEX 25
struct celluleDemineur{
int line, column;
......@@ -17,21 +17,12 @@ struct celluleDemineur{
Color couleurCase;
};
typedef struct celluleDemineur* CelluleDemineur;
typedef struct celluleDemineur * CelluleDemineur;
bool containsMine(int line, int col, CelluleDemineur** matriceMines){
return (matriceMines[line][col]->hasMine == true);
}
void afficherGrilleConsole(int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines){
for(int i=0; i<nbLigneMatrice; i++){
for(int j=0; j<nbColonneMatrice; j++){
printf("M%d:N%d - ", matriceMines[i][j]->hasMine, matriceMines[i][j]->nearbyMine);
}
printf("\n");
}
}
CelluleDemineur* recupererVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice){
CelluleDemineur* tabVoisins = calloc(6, sizeof(struct celluleDemineur));
for(int i=0; i<6; i++){
......@@ -80,6 +71,21 @@ void revelerVoisins(int line, int column, CelluleDemineur** matriceMines, int nb
free(voisins);
}
void revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLigneMatrice, int nbColonneMatrice){
if(!matriceMines[line][column]->isFlaged && !matriceMines[line][column]->isPressed){
matriceMines[line][column]->isPressed = true;
if(matriceMines[line][column]->nearbyMine == 0){
matriceMines[line][column]->couleurCase = LIGHTGRAY;
revelerVoisins(line, column, matriceMines, nbLigneMatrice, nbColonneMatrice);
}else if(matriceMines[line][column]->nearbyMine < 0){
matriceMines[line][column]->couleurCase = MAROON;
}else{
matriceMines[line][column]->couleurCase = LIGHTGRAY;
}
}
}
void generationMines(int nbMines, int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines){
for(int i=0; i<nbMines; i++){
int lineRand = rand()%nbLigneMatrice;
......@@ -94,6 +100,8 @@ void generationMines(int nbMines, int nbLigneMatrice, int nbColonneMatrice, Cell
}
void initGrille(int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines){
int screenHeight = GetScreenHeight(), screenWidth = GetScreenWidth();
for(int i=0; i<nbLigneMatrice; i++){
for(int j=0; j<nbColonneMatrice; j++){
matriceMines[i][j] = malloc(sizeof(struct celluleDemineur));
......@@ -107,29 +115,14 @@ void initGrille(int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matr
matriceMines[i][j]->couleurCase = DARKGRAY;
if(i%2 == 0){
matriceMines[i][j]->pos = (Vector2){(1.5*j+1.75)*SIZE_OF_HEX, (1.5*i+1)*SIZE_OF_HEX};
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, (1.5*i+1)*SIZE_OF_HEX};
matriceMines[i][j]->pos = (Vector2){(1.5*j+1)*SIZE_OF_HEX + screenWidth/(nbColonneMatrice), (1.5*i+1)*SIZE_OF_HEX + screenHeight/nbLigneMatrice};
}
}
}
}
void revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLigneMatrice, int nbColonneMatrice){
if(!matriceMines[line][column]->isFlaged && !matriceMines[line][column]->isPressed){
matriceMines[line][column]->isPressed = true;
if(matriceMines[line][column]->nearbyMine == 0){
matriceMines[line][column]->couleurCase = LIGHTGRAY;
revelerVoisins(line, column, matriceMines, nbLigneMatrice, nbColonneMatrice);
}else if(matriceMines[line][column]->nearbyMine < 0){
matriceMines[line][column]->couleurCase = MAROON;
}else{
matriceMines[line][column]->couleurCase = LIGHTGRAY;
}
}
}
float getDistanceBetweenPoints(Vector2 a, Vector2 b) {
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
......@@ -138,16 +131,18 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 600;
const int screenWidth = 1600;
const int screenHeight = 900;
srand(time(NULL));
InitWindow(screenWidth, screenHeight, "HexaSweeper");
//CREER MATRICE DES MINES
int nbLigneMatrice = 16, nbColonneMatrice = 21, nbMines = 60;
int nbLigneMatrice = 10, nbColonneMatrice = 10, nbMines = 1;
CelluleDemineur** matriceMines = malloc(nbLigneMatrice * sizeof(CelluleDemineur *));
for(int i=0; i<nbColonneMatrice; i++)
matriceMines[i] = malloc(nbColonneMatrice * sizeof(CelluleDemineur));
for(int i=0; i<nbLigneMatrice; i++)
matriceMines[i] = calloc(nbLigneMatrice * nbColonneMatrice, sizeof(CelluleDemineur));
initGrille(nbLigneMatrice, nbColonneMatrice, matriceMines);
......@@ -155,10 +150,7 @@ int main(void)
generationMines(nbMines, nbLigneMatrice, nbColonneMatrice, matriceMines);
int remainingMines = nbMines, curCaseX = 0, curCaseY = 0;
InitWindow(screenWidth, screenHeight, "HexaSweeper");
bool lose = false, win = false;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
......@@ -174,7 +166,7 @@ int main(void)
if(IsMouseButtonPressed(MOUSE_RIGHT_BUTTON)){
if(!matriceMines[curCaseX][curCaseY]->isPressed){
matriceMines[curCaseX][curCaseY]->isFlaged = !matriceMines[0][0]->isFlaged;
matriceMines[curCaseX][curCaseY]->isFlaged = !matriceMines[curCaseX][curCaseY]->isFlaged;
if(matriceMines[curCaseX][curCaseY]->isFlaged){
matriceMines[curCaseX][curCaseY]->couleurCase = GREEN;
remainingMines--;
......@@ -205,6 +197,7 @@ int main(void)
}
}
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
......@@ -220,6 +213,8 @@ int main(void)
DrawText(TextFormat("%i", matriceMines[i][j]->nearbyMine), matriceMines[i][j]->pos.x - SIZE_OF_HEX/3, matriceMines[i][j]->pos.y - SIZE_OF_HEX/3, SIZE_OF_HEX*0.8, LIME);
}
}
if(lose) DrawText("GAME OVER", 0, 0, 25, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
......@@ -228,6 +223,10 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
for(int i=0; i<nbLigneMatrice; i++)
free(matriceMines[i]);
free(matriceMines);
//--------------------------------------------------------------------------------------
return 0;
......
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