Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Z
Zaid-Sarah_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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ZAID Sarah
Zaid-Sarah_tp3
Commits
85bd0c3c
Commit
85bd0c3c
authored
4 years ago
by
zaid
Browse files
Options
Downloads
Patches
Plain Diff
version finale
parent
adb13dc8
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Grid.java
+105
-10
105 additions, 10 deletions
Grid.java
with
105 additions
and
10 deletions
Grid.java
+
105
−
10
View file @
85bd0c3c
import
java.util.Arrays
;
import
java.util.*
;
import
java.util.Iterator
;
import
java.util.concurrent.Callable
;
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>.
...
@@ -97,30 +95,113 @@ public class Grid implements Iterable<Cell> {
...
@@ -97,30 +95,113 @@ public class Grid implements Iterable<Cell> {
goToNextState
(
calculateNextStates
());
goToNextState
(
calculateNextStates
());
}
}
private
boolean
[][]
calculateNextStates
()
{
private
boolean
[][]
calculateNextStates
()
{
return
null
;
boolean
[][]
MyMatrice
=
new
boolean
[
numberOfRows
][
numberOfColumns
];
for
(
int
i
=
0
;
i
<
numberOfRows
;
i
++){
for
(
int
j
=
0
;
j
<
numberOfColumns
;
j
++){
MyMatrice
[
i
][
j
]=
calculateNextState
(
i
,
j
);
}
}
}
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
,
Cell
cell
)
{
return
MyMatrice
;
}
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
)
{
int
alive
=
countAliveNeighbours
(
rowIndex
,
columnIndex
);
Cell
c
=
getCell
(
rowIndex
,
columnIndex
);
if
(
c
.
isAlive
()){
if
((
alive
==
2
)
||
(
alive
==
3
)){
return
true
;
}
else
{
return
false
;
return
false
;
}
}
}
else
{
if
(
alive
==
3
){
return
true
;
}
else
{
return
false
;
}
}
}
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
0
;
List
<
Cell
>
neighbours
=
getNeighbours
(
rowIndex
,
columnIndex
);
int
count
=
0
;
for
(
Cell
i:
neighbours
)
{
if
(
i
.
isAlive
())
{
count
++;
}
}
return
count
;
}
}
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
null
;
List
<
Cell
>
voisines
=
new
ArrayList
<>();
Cell
c
=
getCell
(
rowIndex
,
columnIndex
);
voisines
.
add
(
c
);
Cell
v1
=
getCell
(
rowIndex
-
1
,
columnIndex
-
1
);
voisines
.
add
(
v1
);
Cell
v2
=
getCell
(
rowIndex
-
1
,
columnIndex
);
voisines
.
add
(
v2
);
Cell
v3
=
getCell
(
rowIndex
-
1
,
columnIndex
+
1
);
voisines
.
add
(
v3
);
Cell
v4
=
getCell
(
rowIndex
,
columnIndex
-
1
);
voisines
.
add
(
v4
);
Cell
v5
=
getCell
(
rowIndex
,
columnIndex
+
1
);
voisines
.
add
(
v5
);
Cell
v6
=
getCell
(
rowIndex
+
1
,
columnIndex
-
1
);
voisines
.
add
(
v6
);
Cell
v7
=
getCell
(
rowIndex
+
1
,
columnIndex
);
voisines
.
add
(
v7
);
Cell
v8
=
getCell
(
rowIndex
+
1
,
columnIndex
+
1
);
voisines
.
add
(
v8
);
return
voisines
;
}
}
private
void
goToNextState
(
boolean
[][]
nextState
)
{
private
void
goToNextState
(
boolean
[][]
nextState
)
{
nextState
=
calculateNextStates
();
for
(
int
i
=
0
;
i
<
numberOfRows
;
i
++){
for
(
int
j
=
0
;
j
<
numberOfColumns
;
j
++){
if
(
nextState
[
i
][
j
]==
true
){
cells
[
i
][
j
].
setAlive
();
}
else
{
cells
[
i
][
j
].
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
(
int
i
=
0
;
i
<
numberOfRows
;
i
++){
for
(
int
j
=
0
;
j
<
numberOfColumns
;
j
++){
cells
[
i
][
j
].
setDead
();
}
}
}
}
/**
/**
...
@@ -131,6 +212,20 @@ public class Grid implements Iterable<Cell> {
...
@@ -131,6 +212,20 @@ public class Grid implements Iterable<Cell> {
*/
*/
void
randomGeneration
(
Random
random
)
{
void
randomGeneration
(
Random
random
)
{
boolean
b
;
for
(
int
i
=
0
;
i
<
numberOfRows
;
i
++){
for
(
int
j
=
0
;
j
<
numberOfColumns
;
j
++){
b
=
random
.
nextBoolean
();
if
(
b
){
cells
[
i
][
j
].
setAlive
();
}
else
{
cells
[
i
][
j
].
setDead
();
}
}
}
}
}
}
}
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