Commit 95f2f797 authored by Erwan's avatar Erwan

Ajout de l'initialisation de la grille (affichage console)

parent f20f7939
#include "raylib.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
struct celluleDemineur{
bool hasMine;
int nearbyMine;
};
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");
}
}
void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMines, int nbLigneMatrice, int nbColonneMatrice){
bool linePrev = (line-1 >= 0), colPrev = (column-1 >= 0);
bool lineNext = (line+1 < nbLigneMatrice), colNext = (column+1 < nbColonneMatrice);
if(colPrev){matriceMines[line][column-1]->nearbyMine++;}
if(colNext){matriceMines[line][column+1]->nearbyMine++;}
if(linePrev){matriceMines[line-1][column]->nearbyMine++;}
if(lineNext){matriceMines[line+1][column]->nearbyMine++;}
if(line%2 == 0){
if(linePrev && colNext){matriceMines[line-1][column+1]->nearbyMine++;}
if(lineNext && colNext){matriceMines[line+1][column+1]->nearbyMine++;}
}else{
if(linePrev && colPrev){matriceMines[line-1][column-1]->nearbyMine++;}
if(lineNext && colPrev){matriceMines[line+1][column-1]->nearbyMine++;}
}
}
void generationMines(int nbMines, int nbLigneMatrice, int nbColonneMatrice, CelluleDemineur** matriceMines){
srand(time(NULL));
for(int i=0; i<nbMines; i++){
int lineRand = rand()%nbLigneMatrice;
int colRand = rand()%nbColonneMatrice;
if(!containsMine(lineRand, colRand, matriceMines)){
matriceMines[lineRand][colRand]->hasMine = true;
matriceMines[lineRand][colRand]->nearbyMine = -9;
incrementationVoisinMine(lineRand, colRand, matriceMines, nbLigneMatrice, nbColonneMatrice);
}
}
}
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
//CREER MATRICE DES MINES
int nbLigneMatrice = 10, nbColonneMatrice = 10, nbMines = 50;
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++){
for(int j=0; j<nbColonneMatrice; j++){
matriceMines[i][j] = malloc(sizeof(struct celluleDemineur));
matriceMines[i][j]->hasMine = false;
matriceMines[i][j]->nearbyMine = 0;
}
}
//GENERATION DES MINES
generationMines(nbMines, nbLigneMatrice, nbColonneMatrice, matriceMines);
afficherGrilleConsole(nbLigneMatrice, nbColonneMatrice, matriceMines);
InitWindow(screenWidth, screenHeight, "HexaSweeper");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
// TODO: Update your variables here
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
\ No newline at end of file
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