Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MLOD_ProjectC
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Erwan MERLY
MLOD_ProjectC
Commits
03239074
Commit
03239074
authored
Oct 30, 2020
by
Erwan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Version fonctionnel et gestion de la mémoire
parent
8e9651ba
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
38 deletions
+37
-38
minesweeper.c
minesweeper.c
+37
-38
No files found.
minesweeper.c
View file @
03239074
...
...
@@ -4,7 +4,7 @@
#include <stdbool.h>
#include <math.h>
#define SIZE_OF_HEX 2
0
#define SIZE_OF_HEX 2
5
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
=
8
00
;
const
int
screenHeight
=
6
00
;
const
int
screenWidth
=
16
00
;
const
int
screenHeight
=
9
00
;
srand
(
time
(
NULL
));
InitWindow
(
screenWidth
,
screenHeight
,
"HexaSweeper"
);
//CREER MATRICE DES MINES
int
nbLigneMatrice
=
1
6
,
nbColonneMatrice
=
21
,
nbMines
=
60
;
int
nbLigneMatrice
=
1
0
,
nbColonneMatrice
=
10
,
nbMines
=
1
;
CelluleDemineur
**
matriceMines
=
malloc
(
nbLigneMatrice
*
sizeof
(
CelluleDemineur
*
));
for
(
int
i
=
0
;
i
<
nb
Colon
neMatrice
;
i
++
)
matriceMines
[
i
]
=
malloc
(
nbColonneMatrice
*
sizeof
(
CelluleDemineur
));
for
(
int
i
=
0
;
i
<
nb
Lig
neMatrice
;
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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment