Commit 3da7dc9f authored by Erwan MERLY's avatar Erwan MERLY

Delete minesweeper.c (moved to .src/)

parent 84f51268
#include "raylib.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define SIZE_OF_HEX 25
struct celluleDemineur{
int line, column;
int nearbyMine;
bool isMine;
bool isPressed;
bool isFlagged;
bool isNeareast;
Color couleurCase;
Vector2 pos;
};
typedef struct celluleDemineur * CelluleDemineur;
void initGrille(int nbLineMatrice, int nbColumnMatrice, CelluleDemineur** matriceMines);
void generationMines(int nbMines, int nbLineMatrice, int nbColumnMatrice, CelluleDemineur** matriceMines);
CelluleDemineur* recupererVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice);
void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice);
bool revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLineMatrice, int nbColumnMatrice);
bool revelerVoisins(int line, int column, CelluleDemineur** matriceMines, int nbLineMatrice, int nbColumnMatrice);
float distanceEntrePoints(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};
}
}
}
}
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 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 < nbLineMatrice), colNext = (column+1 < nbColumnMatrice);
if(colPrev){tabVoisins[0] = matriceMines[line][column-1];}
if(colNext){tabVoisins[1] = matriceMines[line][column+1];}
if(linePrev){tabVoisins[2] = matriceMines[line-1][column];}
if(lineNext){tabVoisins[3] = matriceMines[line+1][column];}
if(line%2 == 0){
if(linePrev && colNext){tabVoisins[4] = matriceMines[line-1][column+1];}
if(lineNext && colNext){tabVoisins[5] = matriceMines[line+1][column+1];}
}else{
if(linePrev && colPrev){tabVoisins[4] = matriceMines[line-1][column-1];}
if(lineNext && colPrev){tabVoisins[5] = matriceMines[line+1][column-1];}
}
return tabVoisins;
}
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){
voisins[i]->nearbyMine++;
}
}
free(voisins);
}
bool revelerCase(CelluleDemineur** matriceMines, int line, int column, int nbLineMatrice, int nbColumnMatrice){
bool res = false;
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, nbLineMatrice, nbColumnMatrice);
}else if(matriceMines[line][column]->isMine){
matriceMines[line][column]->couleurCase = MAROON;
res = true;
}else{
matriceMines[line][column]->couleurCase = LIGHTGRAY;
}
}
return res;
}
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<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 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
//--------------------------------------------------------------------------------------
const int screenWidth = 1280;
const int screenHeight = 800;
InitWindow(screenWidth, screenHeight, "HexaSweeper - Erwan MERLY");
SetTargetFPS(60);
srand(time(NULL));
int nbLineMatrice = 17, nbColumnMatrice = 21, nbMines = 80;
int curCaseX = 0, curCaseY = 0, flaggedMines = 0;
bool lose = false, win = false, onTile = false;
float timeEllapsed = 0;
//CREATION DE LA MATRICE DES MINES
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
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
onTile = false;
if(!win && !lose){
timeEllapsed += GetFrameTime();
//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;
}
}
}
//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]->isFlagged = !matriceMines[curCaseX][curCaseY]->isFlagged;
if(matriceMines[curCaseX][curCaseY]->isFlagged){
matriceMines[curCaseX][curCaseY]->couleurCase = GREEN;
flaggedMines++;
}
else{
matriceMines[curCaseX][curCaseY]->couleurCase = DARKGRAY;
flaggedMines--;
}
}
}
if(IsMouseButtonPressed(MOUSE_MIDDLE_BUTTON) && onTile){
if(matriceMines[curCaseX][curCaseY]->isPressed){
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(nbLineMatrice, nbColumnMatrice, matriceMines);
generationMines(nbMines, nbLineMatrice, nbColumnMatrice, matriceMines);
}
}
//TEST LA CONDITION DE VICTOIRE
if(flaggedMines == nbMines){
int trueFlagged = 0;
for(int i=0; i<nbLineMatrice; i++){
for(int j=0; j<nbColumnMatrice; j++){
if(matriceMines[i][j]->isMine && matriceMines[i][j]->isFlagged) trueFlagged++;
}
}
if(trueFlagged == nbMines) win = true;
}
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
//AFFICHAGE DE LA GRILLE
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));
if(matriceMines[i][j]->isPressed && matriceMines[i][j]->nearbyMine>0)
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);
}
}
//AFFICHAGE DE L'INTERFACE
DrawRectangle(0, 725, 1280, 75, Fade(LIGHTGRAY, 0.5f));
DrawText(TextFormat("MINE FLAGGED: %d/%d", flaggedMines, nbMines), 50, 750, 35, GRAY);
DrawText(TextFormat("Time: %d s", (int)timeEllapsed), 1030, 750, 35, GRAY);
//AFFICHAGE CONTROLES ET REGLES
DrawText("CONTROLS", 1005, 50, 35, DARKGRAY);
DrawText("LEFT CLICK - REVEAL TILE", 930, 100, 18, LIGHTGRAY);
DrawText("RIGHT CLICK - FLAG TILE", 930, 130, 18, LIGHTGRAY);
DrawText("MIDDLE CLICK - REVEAL NEIGHBOURS", 930, 160, 18, LIGHTGRAY);
DrawText("RULES", 1005, 210, 35, DARKGRAY);
DrawText("NUMBER INDICATE MINES AROUND", 930, 260, 18, LIGHTGRAY);
DrawText("FLAG ALL THE MINES TO WIN", 930, 290, 18, LIGHTGRAY);
DrawText("TILES", 1005, 340, 35, DARKGRAY);
DrawText("DARK GRAY - NON REVEALED", 930, 390, 18, LIGHTGRAY);
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<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;
}
}
}
if(win) DrawText("YOU WIN!", 500, 465, 35, LIME);
DrawText("PRESS R TO REPLAY", 500, 495, 30, DARKGRAY);
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
for(int i=0; i<nbLineMatrice; i++)
free(matriceMines[i]);
free(matriceMines);
//--------------------------------------------------------------------------------------
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