Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AzzougLydia AitMouhamedSamy
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
AZZOUG Lydia
AzzougLydia AitMouhamedSamy
Commits
bcd0d31c
Commit
bcd0d31c
authored
4 years ago
by
AZZOUG Lydia
Browse files
Options
Downloads
Patches
Plain Diff
Replace Grid.java
parent
bb19284a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Grid.java
+92
-16
92 additions, 16 deletions
Grid.java
with
92 additions
and
16 deletions
Grid.java
+
92
−
16
View file @
bcd0d31c
import
java.util.Arrays
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.*
;
/**
* {@code Grid} instances represent the grid in <i>The Game of Life</i>.
*/
public
class
Grid
implements
Iterable
<
Cell
>
{
private
final
int
numberOfRows
;
...
...
@@ -86,11 +85,9 @@ public class Grid implements Iterable<Cell> {
* <p>The following rules are applied:
* <ul>
* <li>Any live {@link Cell} with fewer than two live neighbours dies, i.e. underpopulation.</li>
* <li>Any live {@link Cell} with two or three live neighbours lives on to the next
* generation.</li>
* <li>Any live {@link Cell} with two or three live neighbours lives on to the next generation.</li>
* <li>Any live {@link Cell} with more than three live neighbours dies, i.e. overpopulation.</li>
* <li>Any dead {@link Cell} with exactly three live neighbours becomes a live cell, i.e.
* reproduction.</li>
* <li>Any dead {@link Cell} with exactly three live neighbours becomes a live cell, i.e. reproduction.</li>
* </ul>
*/
void
nextGeneration
()
{
...
...
@@ -98,29 +95,83 @@ public class Grid implements Iterable<Cell> {
}
private
boolean
[][]
calculateNextStates
()
{
return
null
;
boolean
[][]
nextState
=
new
boolean
[
getNumberOfRows
()][
getNumberOfColumns
()];
for
(
int
rowIndex
=
0
;
rowIndex
<
getNumberOfRows
();
rowIndex
++){
for
(
int
columnIndex
=
0
;
columnIndex
<
getNumberOfColumns
();
columnIndex
++)
{
nextState
[
rowIndex
][
columnIndex
]
=
calculateNextState
(
rowIndex
,
columnIndex
);
}
}
return
nextState
;
}
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
,
Cell
cell
)
{
return
false
;
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
)
{
Cell
cell
=
getCell
(
rowIndex
,
columnIndex
);
int
numberOfAliveNeighbours
=
countAliveNeighbours
(
rowIndex
,
columnIndex
);
return
cell
.
isAliveInNextState
(
numberOfAliveNeighbours
);
}
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
0
;
List
<
Cell
>
neighboursList
=
getNeighbours
(
rowIndex
,
columnIndex
);
Iterator
<
Cell
>
iterator
=
neighboursList
.
iterator
();
int
counter
=
0
;
while
(
iterator
.
hasNext
())
{
if
(
iterator
.
next
().
isAlive
())
counter
++;
}
return
counter
;
}
private
boolean
aliveNeighboursMajColor
(
int
rowIndex
,
int
columnIndex
){
List
<
Cell
>
neighboursList
=
getNeighbours
(
rowIndex
,
columnIndex
);
int
count
=
countAliveNeighbours
(
rowIndex
,
columnIndex
);
Iterator
<
Cell
>
iterator
=
neighboursList
.
iterator
();
int
redCount
=
0
;
while
(
iterator
.
hasNext
())
{
Cell
cell
=
iterator
.
next
();
if
(
cell
.
isAlive
()
&&
cell
.
isRed
())
redCount
++;
}
return
(
redCount
>(
count
/
2
));
}
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
null
;
List
<
Cell
>
neighboursList
=
new
ArrayList
<
Cell
>();
neighboursList
.
add
(
getCell
(
rowIndex
,
columnIndex
+
1
));
neighboursList
.
add
(
getCell
(
rowIndex
,
columnIndex
-
1
));
neighboursList
.
add
(
getCell
(
rowIndex
+
1
,
columnIndex
));
neighboursList
.
add
(
getCell
(
rowIndex
-
1
,
columnIndex
));
neighboursList
.
add
(
getCell
(
rowIndex
+
1
,
columnIndex
+
1
));
neighboursList
.
add
(
getCell
(
rowIndex
+
1
,
columnIndex
-
1
));
neighboursList
.
add
(
getCell
(
rowIndex
-
1
,
columnIndex
+
1
));
neighboursList
.
add
(
getCell
(
rowIndex
-
1
,
columnIndex
-
1
));
return
neighboursList
;
}
private
void
goToNextState
(
boolean
[][]
nextState
)
{
for
(
int
rowIndex
=
0
;
rowIndex
<
this
.
numberOfRows
;
rowIndex
++)
{
for
(
int
columnIndex
=
0
;
columnIndex
<
this
.
numberOfColumns
;
columnIndex
++)
{
Cell
cell
=
getCell
(
rowIndex
,
columnIndex
);
if
(
nextState
[
rowIndex
][
columnIndex
])
{
if
(
cell
.
isDead
())
{
boolean
redColor
=
aliveNeighboursMajColor
(
rowIndex
,
columnIndex
);
cell
.
setisRed
(
redColor
);
}
cell
.
setAlive
();
}
else
{
cell
.
setDead
();
}
}
}
}
/**
* Sets all {@link Cell}s in this {@code Grid} as dead.
*/
void
clear
()
{
for
(
Iterator
<
Cell
>
gridIt
=
iterator
();
gridIt
.
hasNext
();
)
{
Cell
cell
=
gridIt
.
next
();
cell
.
setDead
();
}
}
/**
...
...
@@ -129,8 +180,33 @@ public class Grid implements Iterable<Cell> {
* @param random {@link Random} instance used to decide if each {@link Cell} is alive or dead
* @throws NullPointerException if {@code random} is {@code null}
*/
void
randomGeneration
(
Random
random
)
{
Iterator
<
Cell
>
gridIt
=
iterator
();
while
(
gridIt
.
hasNext
())
{
Cell
cell
=
gridIt
.
next
();
boolean
randomStat
=
random
.
nextBoolean
();
if
(
randomStat
)
{
cell
.
setAlive
();
randomStat
=
random
.
nextBoolean
();
if
(
randomStat
)
cell
.
setisRed
(
true
);
else
cell
.
setisRed
(
false
);
}
else
{
cell
.
setDead
();
}
}
// Spaceships Test
/*
Cell cell0 = getCell(1,1);
Cell cell1 = getCell(2,2);
Cell cell2 = getCell(1,3);
Cell cell3 = getCell(2,3);
Cell cell4 = getCell(3,2);
cell0.setAlive();cell1.setAlive();cell2.setAlive();cell3.setAlive();cell4.setAlive();
*/
}
}
}
\ No newline at end of file
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