Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Prog2Aix-tp3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FAURE Mathias
Prog2Aix-tp3
Commits
97481c2d
Commit
97481c2d
authored
4 years ago
by
Mathias
Browse files
Options
Downloads
Patches
Plain Diff
Ajout de commentaires.
parent
18d9a2da
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
Cell.java
+1
-0
1 addition, 0 deletions
Cell.java
GameOfLifeGUI.java
+3
-0
3 additions, 0 deletions
GameOfLifeGUI.java
Grid.java
+14
-4
14 additions, 4 deletions
Grid.java
with
18 additions
and
4 deletions
Cell.java
+
1
−
0
View file @
97481c2d
...
...
@@ -5,6 +5,7 @@
public
class
Cell
{
private
boolean
isAlive
;
//String qui garde la couleur de la cellule
private
String
color
=
"Red"
;
public
Cell
(){
...
...
This diff is collapsed.
Click to expand it.
GameOfLifeGUI.java
+
3
−
0
View file @
97481c2d
...
...
@@ -39,13 +39,16 @@ public class GameOfLifeGUI extends JFrame {
for
(
int
x
=
0
;
x
<
numberOfColumns
;
x
++)
for
(
int
y
=
0
;
y
<
numberOfRows
;
y
++){
JLabel
label
=
labelGrid
[
x
][
y
];
//On regarde ici si la cellule est vivante
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
{
//On fonction de l'instance qui gère la couleur on change sa couleur sur la grille
if
((
g
.
getCell
(
x
,
y
).
getColor
()).
equals
(
"Red"
))
{
label
.
setForeground
(
Color
.
red
);
}
else
{
label
.
setForeground
(
Color
.
blue
);
}
}
//sinon on l'a met blanc
else
label
.
setForeground
(
Color
.
white
);
}
...
...
This diff is collapsed.
Click to expand it.
Grid.java
+
14
−
4
View file @
97481c2d
...
...
@@ -95,7 +95,9 @@ public class Grid implements Iterable<Cell> {
}
private
boolean
[][]
calculateNextStates
()
{
//On crée une nouvelle matrice qui correspond à la prochaine grille
boolean
[][]
newState
=
new
boolean
[
getNumberOfRows
()][
getNumberOfColumns
()];
//On parcours toute la grille puis on fait appelle à la fonction calculateNextStates
for
(
int
r
=
0
;
r
<
numberOfRows
;
r
++)
{
for
(
int
c
=
0
;
c
<
numberOfColumns
;
c
++)
{
newState
[
r
][
c
]
=
this
.
calculateNextState
(
r
,
c
,
this
.
getCell
(
r
,
c
));
...
...
@@ -106,6 +108,7 @@ public class Grid implements Iterable<Cell> {
}
private
void
goToNextState
(
boolean
[][]
nextState
)
{
//On utilise la nouvelle matrice pour savoir si la cellule est vivante ou non
for
(
int
r
=
0
;
r
<
numberOfRows
;
r
++)
{
for
(
int
c
=
0
;
c
<
numberOfColumns
;
c
++)
{
if
(
nextState
[
r
][
c
])
{
...
...
@@ -119,7 +122,9 @@ public class Grid implements Iterable<Cell> {
}
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
,
Cell
cell
)
{
//On fait appelle à la méthode de Cell pour savoir si la cellule est vivante
boolean
Nextstate
=
cell
.
isAliveInNextState
(
this
.
countAliveNeighbours
(
rowIndex
,
columnIndex
));
//C'est ici aussi que l'on change la couleur de la cellule en fonction de ses voisins en appelant la méthode setColor
if
(
Nextstate
)
{
if
(
this
.
countAliveNeighbours
(
rowIndex
,
columnIndex
)
!=
2
)
{
cell
.
setColor
(
this
.
countColorNeighbours
(
rowIndex
,
columnIndex
));
...
...
@@ -130,6 +135,7 @@ public class Grid implements Iterable<Cell> {
}
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
//Ici on compte le nombre de cellules voisines vivantes
int
CounterAliveCell
=
0
;
for
(
Cell
NeighboursCell
:
this
.
getNeighbours
(
rowIndex
,
columnIndex
))
{
if
(
NeighboursCell
.
isAlive
())
{
...
...
@@ -140,9 +146,11 @@ public class Grid implements Iterable<Cell> {
}
private
String
countColorNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
//On compte ici la couleur des voisins
String
CounterColorCell
;
int
Red
=
0
;
int
Blue
=
0
;
//On compte ici la couleur des voisins
for
(
Cell
NeighboursCell
:
this
.
getNeighbours
(
rowIndex
,
columnIndex
))
{
if
(
NeighboursCell
.
isAlive
())
{
if
((
NeighboursCell
.
getColor
()).
equals
(
"Red"
))
{
...
...
@@ -153,7 +161,7 @@ public class Grid implements Iterable<Cell> {
}
}
}
//Et on fonction du nombre de couleur on change la couleur dans l'instance qui la gère
if
(
Red
>
Blue
)
{
CounterColorCell
=
"Red"
;
}
...
...
@@ -167,7 +175,9 @@ public class Grid implements Iterable<Cell> {
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
//On crée une liste de voisin
List
<
Cell
>
Neighbours
=
new
ArrayList
<>();
//On parcours les 8 voisins, en prenant compte à ne pas prendre la cellule de base, et on les ajoutes à la liste
for
(
int
r
=-
1
;
r
<=
1
;
r
++)
{
for
(
int
c
=-
1
;
c
<=
1
;
c
++)
{
if
((
r
!=
0
)
||
(
c
!=
0
))
{
...
...
@@ -185,7 +195,7 @@ public class Grid implements Iterable<Cell> {
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
void
clear
()
{
//On tue toutes les cellules
for
(
Iterator
<
Cell
>
it
=
this
.
iterator
();
it
.
hasNext
();
)
{
Cell
cell
=
it
.
next
();
cell
.
setDead
();
...
...
@@ -200,10 +210,10 @@ public class Grid implements Iterable<Cell> {
*/
void
randomGeneration
(
Random
random
)
{
//On utilise la méthode Iterator
for
(
Iterator
<
Cell
>
it
=
this
.
iterator
();
it
.
hasNext
();
)
{
Cell
cell
=
it
.
next
();
//Si le hasard a décidé que la cellule est vivante, on utilise ensuite la même méthode pour mettre sa couleur
if
(
random
.
nextBoolean
())
{
cell
.
setAlive
();
if
(
random
.
nextBoolean
())
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment