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
DURIEZ Kilian
Prog2Aix-tp3
Commits
e0901c67
Commit
e0901c67
authored
4 years ago
by
DURIEZ Kilian
Browse files
Options
Downloads
Patches
Plain Diff
tp3 fin
parent
adb13dc8
Branches
master
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
Cell.java
+28
-20
28 additions, 20 deletions
Cell.java
GameOfLife.java
+11
-11
11 additions, 11 deletions
GameOfLife.java
GameOfLifeGUI.java
+28
-21
28 additions, 21 deletions
GameOfLifeGUI.java
Grid.java
+88
-9
88 additions, 9 deletions
Grid.java
Main.java
+15
-15
15 additions, 15 deletions
Main.java
with
170 additions
and
76 deletions
Cell.java
+
28
−
20
View file @
e0901c67
...
@@ -4,6 +4,14 @@
...
@@ -4,6 +4,14 @@
public
class
Cell
{
public
class
Cell
{
private
boolean
isAlive
;
private
boolean
isAlive
;
private
String
color
=
"Red"
;
public
void
setColor
(
String
hisColor
)
{
color
=
hisColor
;
}
public
String
getColor
()
{
return
color
;
}
public
Cell
(){
public
Cell
(){
this
.
isAlive
=
false
;
this
.
isAlive
=
false
;
...
...
This diff is collapsed.
Click to expand it.
GameOfLife.java
+
11
−
11
View file @
e0901c67
This diff is collapsed.
Click to expand it.
GameOfLifeGUI.java
+
28
−
21
View file @
e0901c67
...
@@ -39,10 +39,17 @@ public class GameOfLifeGUI extends JFrame {
...
@@ -39,10 +39,17 @@ public class GameOfLifeGUI extends JFrame {
for
(
int
x
=
0
;
x
<
numberOfColumns
;
x
++)
for
(
int
x
=
0
;
x
<
numberOfColumns
;
x
++)
for
(
int
y
=
0
;
y
<
numberOfRows
;
y
++){
for
(
int
y
=
0
;
y
<
numberOfRows
;
y
++){
JLabel
label
=
labelGrid
[
x
][
y
];
JLabel
label
=
labelGrid
[
x
][
y
];
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
{
if
((
g
.
getCell
(
x
,
y
).
getColor
()).
equals
(
"Red"
))
{
label
.
setForeground
(
Color
.
red
);
label
.
setForeground
(
Color
.
red
);
}
else
{
label
.
setForeground
(
Color
.
blue
);
}
}
else
else
label
.
setForeground
(
Color
.
white
);
label
.
setForeground
(
Color
.
white
);
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Grid.java
+
88
−
9
View file @
e0901c67
import
java.util.Arrays
;
import
java.util.*
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Random
;
/**
/**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>.
* {@code Grid} instances represent the grid in <i>The Game of Life</i>.
...
@@ -98,29 +95,94 @@ public class Grid implements Iterable<Cell> {
...
@@ -98,29 +95,94 @@ public class Grid implements Iterable<Cell> {
}
}
private
boolean
[][]
calculateNextStates
()
{
private
boolean
[][]
calculateNextStates
()
{
return
null
;
boolean
[][]
nextState
=
new
boolean
[
getNumberOfRows
()][
getNumberOfColumns
()]
;
for
(
int
row
=
0
;
row
<
numberOfRows
;
row
++)
{
for
(
int
column
=
0
;
column
<
numberOfColumns
;
column
++){
nextState
[
row
][
column
]
=
this
.
calculateNextState
(
row
,
column
,
this
.
getCell
(
row
,
column
));
}
}
return
nextState
;
}
}
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
,
Cell
cell
)
{
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
,
Cell
cell
)
{
return
false
;
boolean
nextState
=
cell
.
isAliveInNextState
(
this
.
countAliveNeighbours
(
rowIndex
,
columnIndex
));
if
(
nextState
)
{
if
(
this
.
countAliveNeighbours
(
rowIndex
,
columnIndex
)
!=
2
)
{
cell
.
setColor
(
this
.
countColorNeighbours
(
rowIndex
,
columnIndex
));
}
}
return
nextState
;
}
private
String
countColorNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
String
countColorCell
;
int
Red
=
0
;
int
Blue
=
0
;
for
(
Cell
NeighboursCell
:
this
.
getNeighbours
(
rowIndex
,
columnIndex
))
{
if
(
NeighboursCell
.
isAlive
())
{
if
((
NeighboursCell
.
getColor
()).
equals
(
"Red"
))
{
Red
+=
1
;
}
else
{
Blue
+=
1
;
}
}
}
if
(
Red
>
Blue
)
{
countColorCell
=
"Red"
;
}
else
{
countColorCell
=
"Blue"
;
}
return
countColorCell
;
}
}
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
0
;
int
alive
=
0
;
for
(
Cell
NeighboursCell
:
this
.
getNeighbours
(
rowIndex
,
columnIndex
))
{
if
(
NeighboursCell
.
isAlive
())
{
alive
=
alive
+
1
;
}
}
return
alive
;
}
}
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
null
;
List
<
Cell
>
neighbours
=
new
ArrayList
<
Cell
>();
neighbours
.
add
(
this
.
getCell
(
rowIndex
-
1
,
columnIndex
-
1
));
neighbours
.
add
(
this
.
getCell
(
rowIndex
-
1
,
columnIndex
));
neighbours
.
add
(
this
.
getCell
(
rowIndex
-
1
,
columnIndex
+
1
));
neighbours
.
add
(
this
.
getCell
(
rowIndex
+
1
,
columnIndex
-
1
));
neighbours
.
add
(
this
.
getCell
(
rowIndex
+
1
,
columnIndex
));
neighbours
.
add
(
this
.
getCell
(
rowIndex
+
1
,
columnIndex
+
1
));
neighbours
.
add
(
this
.
getCell
(
rowIndex
,
columnIndex
-
1
));
neighbours
.
add
(
this
.
getCell
(
rowIndex
,
columnIndex
+
1
));
return
neighbours
;
}
}
private
void
goToNextState
(
boolean
[][]
nextState
)
{
private
void
goToNextState
(
boolean
[][]
nextState
)
{
for
(
int
row
=
0
;
row
<
numberOfRows
;
row
++)
{
for
(
int
column
=
0
;
column
<
numberOfColumns
;
column
++)
{
if
(
nextState
[
row
][
column
])
getCell
(
row
,
column
).
setAlive
();
else
getCell
(
row
,
column
).
setDead
();
}
}
}
}
/**
/**
* Sets all {@link Cell}s in this {@code Grid} as dead.
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
*/
void
clear
()
{
void
clear
()
{
for
(
Iterator
<
Cell
>
it
=
this
.
iterator
();
it
.
hasNext
();
)
{
Cell
cell
=
it
.
next
();
cell
.
setDead
();
}
}
}
/**
/**
...
@@ -131,6 +193,23 @@ public class Grid implements Iterable<Cell> {
...
@@ -131,6 +193,23 @@ public class Grid implements Iterable<Cell> {
*/
*/
void
randomGeneration
(
Random
random
)
{
void
randomGeneration
(
Random
random
)
{
for
(
Iterator
<
Cell
>
it
=
this
.
iterator
();
it
.
hasNext
();
)
{
Cell
cell
=
it
.
next
();
if
(
random
.
nextBoolean
())
{
cell
.
setAlive
();
if
(
random
.
nextBoolean
())
{
cell
.
setColor
(
"Red"
);
}
else
{
cell
.
setColor
(
"Blue"
);
}
}
else
{
cell
.
setDead
();
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Main.java
+
15
−
15
View file @
e0901c67
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