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
a1040687
Commit
a1040687
authored
Oct 30, 2020
by
Erwan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mise en place du système de sélection de tuile pour les différents clics
parent
d8c00f57
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
49 deletions
+84
-49
minesweeper.c
minesweeper.c
+84
-49
No files found.
minesweeper.c
View file @
a1040687
...
...
@@ -2,7 +2,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <
conio
.h>
#include <
math
.h>
#define SIZE_OF_HEX 20
...
...
@@ -13,6 +13,7 @@ struct celluleDemineur{
Vector2
pos
;
bool
isPressed
;
bool
isFlaged
;
bool
isNeareast
;
Color
couleurCase
;
};
...
...
@@ -68,15 +69,22 @@ void incrementationVoisinMine(int line, int column, CelluleDemineur** matriceMin
free
(
voisins
);
}
void
revelerVoisins
AZero
(
int
line
,
int
column
,
CelluleDemineur
**
matriceMines
,
int
nbLigneMatrice
,
int
nbColonneMatrice
){
void
revelerVoisins
(
int
line
,
int
column
,
CelluleDemineur
**
matriceMines
,
int
nbLigneMatrice
,
int
nbColonneMatrice
){
CelluleDemineur
*
voisins
=
recupererVoisins
(
line
,
column
,
matriceMines
,
nbLigneMatrice
,
nbColonneMatrice
);
for
(
int
i
=
0
;
i
<
6
;
i
++
){
if
(
voisins
[
i
]
!=
NULL
){
if
(
voisins
[
i
]
->
nearbyMine
==
0
&&
!
voisins
[
i
]
->
isPressed
){
voisins
[
i
]
->
isPressed
=
true
;
voisins
[
i
]
->
couleurCase
=
WHITE
;
revelerVoisinsAZero
(
voisins
[
i
]
->
line
,
voisins
[
i
]
->
column
,
matriceMines
,
nbLigneMatrice
,
nbColonneMatrice
);
voisins
[
i
]
->
couleurCase
=
LIGHTGRAY
;
revelerVoisins
(
voisins
[
i
]
->
line
,
voisins
[
i
]
->
column
,
matriceMines
,
nbLigneMatrice
,
nbColonneMatrice
);
}
else
if
(
voisins
[
i
]
->
nearbyMine
>
0
&&
!
voisins
[
i
]
->
isPressed
){
voisins
[
i
]
->
isPressed
=
true
;
voisins
[
i
]
->
couleurCase
=
LIGHTGRAY
;
}
else
if
(
voisins
[
i
]
->
nearbyMine
<
0
&&
!
voisins
[
i
]
->
isPressed
){
voisins
[
i
]
->
isPressed
=
true
;
voisins
[
i
]
->
couleurCase
=
MAROON
;
}
}
}
...
...
@@ -84,7 +92,6 @@ void revelerVoisinsAZero(int line, int column, CelluleDemineur** matriceMines, i
}
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
;
...
...
@@ -97,33 +104,19 @@ void generationMines(int nbMines, int nbLigneMatrice, int nbColonneMatrice, Cell
}
}
int
main
(
void
)
{
// Initialization
//--------------------------------------------------------------------------------------
const
int
screenWidth
=
800
;
const
int
screenHeight
=
800
;
bool
gameIsLost
=
false
;
//CREER MATRICE DES MINES
int
nbLigneMatrice
=
10
,
nbColonneMatrice
=
10
,
nbMines
=
3
;
CelluleDemineur
**
matriceMines
=
malloc
(
nbLigneMatrice
*
sizeof
(
CelluleDemineur
*
));
for
(
int
i
=
0
;
i
<
nbColonneMatrice
;
i
++
)
matriceMines
[
i
]
=
malloc
(
nbColonneMatrice
*
sizeof
(
CelluleDemineur
));
void
initGrille
(
int
nbLigneMatrice
,
int
nbColonneMatrice
,
CelluleDemineur
**
matriceMines
){
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
]
->
isFlaged
=
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
=
LIGHTGRAY
;
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
};
}
else
{
...
...
@@ -131,13 +124,50 @@ int main(void)
}
}
}
}
void
revelerCase
(
CelluleDemineur
**
matriceMines
,
int
line
,
int
column
,
int
nbLigneMatrice
,
int
nbColonneMatrice
){
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
);
}
int
main
(
void
)
{
// Initialization
//--------------------------------------------------------------------------------------
const
int
screenWidth
=
800
;
const
int
screenHeight
=
600
;
srand
(
time
(
NULL
));
//CREER MATRICE DES MINES
int
nbLigneMatrice
=
5
,
nbColonneMatrice
=
5
,
nbMines
=
2
;
CelluleDemineur
**
matriceMines
=
malloc
(
nbLigneMatrice
*
sizeof
(
CelluleDemineur
*
));
for
(
int
i
=
0
;
i
<
nbColonneMatrice
;
i
++
)
matriceMines
[
i
]
=
malloc
(
nbColonneMatrice
*
sizeof
(
CelluleDemineur
));
initGrille
(
nbLigneMatrice
,
nbColonneMatrice
,
matriceMines
);
//GENERATION DES MINES
generationMines
(
nbMines
,
nbLigneMatrice
,
nbColonneMatrice
,
matriceMines
);
afficherGrilleConsole
(
nbLigneMatrice
,
nbColonneMatrice
,
matriceMines
);
int
remainingMines
=
nbMines
;
int
remainingMines
=
nbMines
,
curCaseX
=
0
,
curCaseY
=
0
;
InitWindow
(
screenWidth
,
screenHeight
,
"HexaSweeper"
);
SetTargetFPS
(
60
);
// Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
...
...
@@ -148,31 +178,40 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
if
(
IsMouseButtonPressed
(
MOUSE_LEFT_BUTTON
)){
matriceMines
[
0
][
0
]
->
isPressed
=
true
;
if
(
matriceMines
[
0
][
0
]
->
nearbyMine
==
0
){
matriceMines
[
0
][
0
]
->
couleurCase
=
WHITE
;
revelerVoisinsAZero
(
0
,
0
,
matriceMines
,
nbLigneMatrice
,
nbColonneMatrice
);
}
else
if
(
matriceMines
[
0
][
0
]
->
nearbyMine
<
0
){
matriceMines
[
0
][
0
]
->
couleurCase
=
MAROON
;
gameIsLost
=
true
;
}
else
{
matriceMines
[
0
][
0
]
->
couleurCase
=
BEIGE
;
}
revelerCase
(
matriceMines
,
curCaseX
,
curCaseY
,
nbLigneMatrice
,
nbColonneMatrice
);
}
if
(
IsMouseButtonPressed
(
MOUSE_
MIDDLE
_BUTTON
)){
if
(
!
matriceMines
[
0
][
0
]
->
isPressed
){
matriceMines
[
0
][
0
]
->
isFlaged
=
!
matriceMines
[
0
][
0
]
->
isFlaged
;
if
(
matriceMines
[
0
][
0
]
->
isFlaged
){
matriceMines
[
0
][
0
]
->
couleurCase
=
GREEN
;
if
(
IsMouseButtonPressed
(
MOUSE_
RIGHT
_BUTTON
)){
if
(
!
matriceMines
[
curCaseX
][
curCaseY
]
->
isPressed
){
matriceMines
[
curCaseX
][
curCaseY
]
->
isFlaged
=
!
matriceMines
[
0
][
0
]
->
isFlaged
;
if
(
matriceMines
[
curCaseX
][
curCaseY
]
->
isFlaged
){
matriceMines
[
curCaseX
][
curCaseY
]
->
couleurCase
=
GREEN
;
remainingMines
--
;
}
else
{
matriceMines
[
0
][
0
]
->
couleurCase
=
LIGHT
GRAY
;
matriceMines
[
curCaseX
][
curCaseY
]
->
couleurCase
=
DARK
GRAY
;
remainingMines
++
;
}
}
}
if
(
IsMouseButtonPressed
(
MOUSE_MIDDLE_BUTTON
)){
revelerCase
(
matriceMines
,
curCaseX
,
curCaseY
,
nbLigneMatrice
,
nbColonneMatrice
);
revelerVoisins
(
curCaseX
,
curCaseY
,
matriceMines
,
nbLigneMatrice
,
nbColonneMatrice
);
}
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
;
}
}
}
// Draw
...
...
@@ -181,19 +220,15 @@ int main(void)
ClearBackground
(
RAYWHITE
);
for
(
int
i
=
0
;
i
<
nbLigneMatrice
;
i
++
){
for
(
int
j
=
0
;
j
<
nbColonneMatrice
;
j
++
){
DrawPoly
(
matriceMines
[
i
][
j
]
->
pos
,
6
,
SIZE_OF_HEX
,
0
,
GRAY
);
DrawPoly
(
matriceMines
[
i
][
j
]
->
pos
,
6
,
SIZE_OF_HEX
-
5
,
0
,
matriceMines
[
i
][
j
]
->
couleurCase
);
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
.
5
f
:
1
.
0
f
)
);
if
(
matriceMines
[
i
][
j
]
->
isPressed
&&
matriceMines
[
i
][
j
]
->
nearbyMine
>
0
)
DrawText
(
TextFormat
(
"%i"
,
matriceMines
[
i
][
j
]
->
nearbyMine
),
matriceMines
[
i
][
j
]
->
pos
.
x
,
matriceMines
[
i
][
j
]
->
pos
.
y
,
SIZE_OF_HEX
/
2
,
BROWN
);
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
);
}
}
EndDrawing
();
//----------------------------------------------------------------------------------
...
...
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