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
Compare revisions
master to 14f3e6854c8d79c42fcd8484429d10367750ad79
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
a19028956/tp3
Select target project
No results found
14f3e6854c8d79c42fcd8484429d10367750ad79
Select Git revision
Branches
master
undefined
2 results
Swap
Target
das.s/tp3
Select target project
das.s/tp3
f19002502/tp3
r17010960/tp3
l19004806/tp3
y19010055/tp3
o18034026/tp3
z18029613/tp3
p19021289/tp3
d19027596/tp3
f18010428/tp3
f19003868/tp3
c19022214/tp3
c19017929/tp3
m16014784/tp3
a19028956/tp3
c19026071/tp3
h18008908/tp3
17 results
master
Select Git revision
Branches
master
1 result
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (7)
Replace Cell.java
· ad5e7fc5
AZZOUG Lydia
authored
4 years ago
ad5e7fc5
Replace GameOfLife.java
· 19cf26f8
AZZOUG Lydia
authored
4 years ago
19cf26f8
Replace GameOfLifeGUI.java
· bb19284a
AZZOUG Lydia
authored
4 years ago
bb19284a
Replace Grid.java
· bcd0d31c
AZZOUG Lydia
authored
4 years ago
bcd0d31c
Replace GridIterator.java
· 4e856b7e
AZZOUG Lydia
authored
4 years ago
4e856b7e
Replace Main.java
· 90fdf03e
AZZOUG Lydia
authored
4 years ago
90fdf03e
Replace Main.java
· 14f3e685
AZZOUG Lydia
authored
4 years ago
14f3e685
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
Cell.java
+30
-30
30 additions, 30 deletions
Cell.java
GameOfLife.java
+0
-1
0 additions, 1 deletion
GameOfLife.java
GameOfLifeGUI.java
+38
-23
38 additions, 23 deletions
GameOfLifeGUI.java
Grid.java
+92
-16
92 additions, 16 deletions
Grid.java
GridIterator.java
+4
-3
4 additions, 3 deletions
GridIterator.java
with
164 additions
and
73 deletions
Cell.java
View file @
14f3e685
/**
* {@link Cell} instances represent the cells of <i>The Game of Life</i>.
*/
public
class
Cell
{
private
boolean
isAlive
;
private
boolean
isRed
;
public
Cell
(){
this
.
isAlive
=
false
;
}
public
void
setisRed
(
boolean
bool
){
this
.
isRed
=
bool
;
}
public
boolean
isRed
(){
return
this
.
isRed
;
}
/**
* Determines whether this {@link Cell} is alive or not.
*
* @return {@code true} if this {@link Cell} is alive and {@code false} otherwise
*/
public
boolean
isAlive
()
{
return
this
.
isAlive
;
}
...
...
@@ -24,7 +30,6 @@ public class Cell {
*
* @return {@code true} if this {@link Cell} is dead and {@code false} otherwise
*/
public
boolean
isDead
()
{
return
!
this
.
isAlive
;
}
...
...
@@ -32,9 +37,7 @@ public class Cell {
/**
* Sets the state of this {@link Cell} to alive.
*
* @param cellState the new state of this {@link Cell}
*/
public
void
setAlive
()
{
this
.
isAlive
=
true
;
}
...
...
@@ -42,18 +45,14 @@ public class Cell {
/**
* Sets the state of this {@link Cell} to dead.
*
* @param cellState the new state of this {@link Cell}
*/
public
void
setDead
()
{
this
.
isAlive
=
false
;
}
/**
* Change the state of this {@link Cell} from ALIVE to DEAD or from DEAD to ALIVE.
*/
public
void
toggleState
()
{
if
(
this
.
isAlive
)
this
.
isAlive
=
false
;
...
...
@@ -63,7 +62,7 @@ public class Cell {
public
boolean
isAliveInNextState
(
int
numberOfAliveNeighbours
)
{
if
(
isAlive
()){
if
(
numberOfAliveNeighbours
==
2
||
numberOfAliveNeighbours
==
3
)
if
(
(
numberOfAliveNeighbours
==
2
)
||
(
numberOfAliveNeighbours
==
3
)
)
return
true
;
else
return
false
;
...
...
@@ -76,3 +75,4 @@ public class Cell {
}
}
}
This diff is collapsed.
Click to expand it.
GameOfLife.java
View file @
14f3e685
...
...
@@ -47,7 +47,6 @@ public class GameOfLife {
public
Grid
getGrid
()
{
return
grid
;
}
/**
* Plays the game.
*/
...
...
This diff is collapsed.
Click to expand it.
GameOfLifeGUI.java
View file @
14f3e685
...
...
@@ -11,22 +11,31 @@ public class GameOfLifeGUI extends JFrame {
private
JPanel
gridPanel
;
private
JFrame
frame
;
public
GameOfLifeGUI
(
Grid
g
)
{
this
.
numberOfRows
=
g
.
getNumberOfRows
();
this
.
numberOfColumns
=
g
.
getNumberOfColumns
();
gridLayout
=
new
GridLayout
(
numberOfRows
,
numberOfColumns
);
gridPanel
=
new
JPanel
(
gridLayout
);
labelGrid
=
new
JLabel
[
numberOfRows
][
numberOfColumns
];
for
(
int
x
=
0
;
x
<
numberOfColumns
;
x
++)
for
(
int
x
=
0
;
x
<
numberOfColumns
;
x
++){
for
(
int
y
=
0
;
y
<
numberOfRows
;
y
++){
labelGrid
[
x
][
y
]
=
new
JLabel
(
"*"
);
JLabel
label
;
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
labelGrid
[
x
][
y
].
setForeground
(
Color
.
red
);
JLabel
label
=
labelGrid
[
x
][
y
];
Cell
cell
=
g
.
getCell
(
x
,
y
);
if
(
cell
.
isAlive
())
{
if
(
cell
.
isRed
())
label
.
setForeground
(
Color
.
red
);
else
labelGrid
[
x
][
y
].
setForeground
(
Color
.
white
);
label
.
setForeground
(
Color
.
blue
);
}
else
{
label
.
setForeground
(
Color
.
white
);
}
gridPanel
.
add
(
labelGrid
[
x
][
y
]);
}
}
frame
=
new
JFrame
(
"Game of Life"
);
frame
.
setDefaultCloseOperation
(
WindowConstants
.
EXIT_ON_CLOSE
);
frame
.
setContentPane
(
gridPanel
);
...
...
@@ -36,13 +45,19 @@ public class GameOfLifeGUI extends JFrame {
}
public
void
update
(
Grid
g
){
for
(
int
x
=
0
;
x
<
numberOfColumns
;
x
++)
for
(
int
x
=
0
;
x
<
numberOfColumns
;
x
++)
{
for
(
int
y
=
0
;
y
<
numberOfRows
;
y
++)
{
JLabel
label
=
labelGrid
[
x
][
y
];
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
Cell
cell
=
g
.
getCell
(
x
,
y
);
if
(
cell
.
isAlive
())
{
if
(
cell
.
isRed
())
label
.
setForeground
(
Color
.
red
);
else
label
.
setForeground
(
Color
.
blue
);
}
else
{
label
.
setForeground
(
Color
.
white
);
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Grid.java
View file @
14f3e685
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
();
}
}
/**
...
...
@@ -131,6 +182,31 @@ public class Grid implements Iterable<Cell> {
*/
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.
GridIterator.java
View file @
14f3e685
...
...
@@ -8,11 +8,12 @@ public class GridIterator implements Iterator<Cell> {
private
int
columnIndex
;
private
Grid
grid
;
GridIterator
(
Grid
grid
)
{
this
.
rowIndex
=
0
;
this
.
columnIndex
=
0
;
this
.
grid
=
grid
;
}
this
.
grid
=
grid
;
}
@Override
public
boolean
hasNext
()
{
...
...
This diff is collapsed.
Click to expand it.