Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Prog2Aix-tp3 MOHAMED
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
HAMADACHE Mohamed
Prog2Aix-tp3 MOHAMED
Commits
5e2d9612
Commit
5e2d9612
authored
4 years ago
by
HAMADACHE Mohamed
Browse files
Options
Downloads
Patches
Plain Diff
ok
parent
04a2093b
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
GameOfLife.java
+12
-12
12 additions, 12 deletions
GameOfLife.java
GameOfLifeGUI.java
+39
-26
39 additions, 26 deletions
GameOfLifeGUI.java
Grid.java
+102
-6
102 additions, 6 deletions
Grid.java
GridIterator.java
+1
-1
1 addition, 1 deletion
GridIterator.java
Main.java
+17
-16
17 additions, 16 deletions
Main.java
with
171 additions
and
61 deletions
GameOfLife.java
+
12
−
12
View file @
5e2d9612
This diff is collapsed.
Click to expand it.
GameOfLifeGUI.java
+
39
−
26
View file @
5e2d9612
...
@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame {
...
@@ -17,16 +17,23 @@ public class GameOfLifeGUI extends JFrame {
gridLayout
=
new
GridLayout
(
numberOfRows
,
numberOfColumns
);
gridLayout
=
new
GridLayout
(
numberOfRows
,
numberOfColumns
);
gridPanel
=
new
JPanel
(
gridLayout
);
gridPanel
=
new
JPanel
(
gridLayout
);
labelGrid
=
new
JLabel
[
numberOfRows
][
numberOfColumns
];
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
++)
{
for
(
int
y
=
0
;
y
<
numberOfRows
;
y
++)
{
labelGrid
[
x
][
y
]
=
new
JLabel
(
"*"
);
labelGrid
[
x
][
y
]
=
new
JLabel
(
"*"
);
JLabel
label
;
JLabel
label
;
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
{
if
(
g
.
getCell
(
x
,
y
).
isRed
())
{
labelGrid
[
x
][
y
].
setForeground
(
Color
.
red
);
labelGrid
[
x
][
y
].
setForeground
(
Color
.
red
);
else
}
else
{
labelGrid
[
x
][
y
].
setForeground
(
Color
.
blue
);
}
}
else
{
labelGrid
[
x
][
y
].
setForeground
(
Color
.
white
);
labelGrid
[
x
][
y
].
setForeground
(
Color
.
white
);
}
gridPanel
.
add
(
labelGrid
[
x
][
y
]);
gridPanel
.
add
(
labelGrid
[
x
][
y
]);
}
}
}
frame
=
new
JFrame
(
"Game of Life"
);
frame
=
new
JFrame
(
"Game of Life"
);
frame
.
setDefaultCloseOperation
(
WindowConstants
.
EXIT_ON_CLOSE
);
frame
.
setDefaultCloseOperation
(
WindowConstants
.
EXIT_ON_CLOSE
);
frame
.
setContentPane
(
gridPanel
);
frame
.
setContentPane
(
gridPanel
);
...
@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame {
...
@@ -36,13 +43,19 @@ public class GameOfLifeGUI extends JFrame {
}
}
public
void
update
(
Grid
g
)
{
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
++)
{
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
).
isBlue
())
{
label
.
setForeground
(
Color
.
blue
);
}
else
{
label
.
setForeground
(
Color
.
red
);
label
.
setForeground
(
Color
.
red
);
else
}
}
else
{
label
.
setForeground
(
Color
.
white
);
label
.
setForeground
(
Color
.
white
);
}
}
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Grid.java
+
102
−
6
View file @
5e2d9612
import
java.util.Array
s
;
import
java.util.Array
List
;
import
java.util.Iterator
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.Random
;
...
@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> {
...
@@ -98,29 +98,110 @@ public class Grid implements Iterable<Cell> {
}
}
private
boolean
[][]
calculateNextStates
()
{
private
boolean
[][]
calculateNextStates
()
{
return
null
;
boolean
[][]
states
=
new
boolean
[
numberOfRows
][
numberOfColumns
];
for
(
int
i
=
0
;
i
<
numberOfRows
;
i
++)
{
for
(
int
j
=
0
;
j
<
numberOfColumns
;
j
++)
{
states
[
i
][
j
]
=
calculateNextState
(
i
,
j
,
cells
[
i
][
j
]);
}
}
return
states
;
}
}
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
,
Cell
cell
)
{
private
boolean
calculateNextState
(
int
rowIndex
,
int
columnIndex
,
Cell
cell
)
{
return
false
;
int
neighboursAlive
=
countAliveNeighbours
(
rowIndex
,
columnIndex
);
int
blueNeighbours
=
countBlueNeighbours
(
rowIndex
,
columnIndex
);
boolean
nextStateAlive
=
cell
.
isAlive
();
if
(
cell
.
isAlive
())
{
if
(
neighboursAlive
==
2
)
{
nextStateAlive
=
true
;
}
else
if
(
neighboursAlive
==
3
)
{
nextStateAlive
=
true
;
}
else
{
cell
.
isDead
();
nextStateAlive
=
false
;
}
}
else
if
(
cell
.
isDead
())
{
if
(
neighboursAlive
==
3
)
{
cell
.
isAlive
();
nextStateAlive
=
true
;
if
(
blueNeighbours
>=
2
)
{
cell
.
setBlue
();
}
else
{
cell
.
setRed
();
}
}
else
{
nextStateAlive
=
false
;
}
}
return
nextStateAlive
;
}
}
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
private
int
countAliveNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
0
;
int
nbAlive
=
0
;
for
(
Cell
cell
:
getNeighbours
(
rowIndex
,
columnIndex
))
{
nbAlive
+=
cell
.
isAlive
()
?
1
:
0
;
}
return
nbAlive
;
}
private
int
countBlueNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
int
nbBlue
=
0
;
for
(
Cell
cell
:
getNeighbours
(
rowIndex
,
columnIndex
))
{
if
(
cell
.
isAlive
())
{
nbBlue
+=
cell
.
isBlue
()
?
1
:
0
;
}
}
return
nbBlue
;
}
}
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
private
List
<
Cell
>
getNeighbours
(
int
rowIndex
,
int
columnIndex
)
{
return
null
;
List
<
Cell
>
neighbourCells
=
new
ArrayList
<
Cell
>();
neighbourCells
.
add
(
cells
[
rowIndex
-
1
<
0
?
numberOfRows
-
1
:
rowIndex
-
1
][
columnIndex
-
1
<
0
?
numberOfColumns
-
1
:
columnIndex
-
1
]);
neighbourCells
.
add
(
cells
[
rowIndex
][
columnIndex
-
1
<
0
?
numberOfColumns
-
1
:
columnIndex
-
1
]);
neighbourCells
.
add
(
cells
[
rowIndex
+
1
>
numberOfRows
-
1
?
0
:
rowIndex
+
1
][
columnIndex
-
1
<
0
?
numberOfColumns
-
1
:
columnIndex
-
1
]);
neighbourCells
.
add
(
cells
[
rowIndex
-
1
<
0
?
numberOfRows
-
1
:
rowIndex
-
1
][
columnIndex
]);
neighbourCells
.
add
(
cells
[
rowIndex
+
1
>
numberOfRows
-
1
?
0
:
rowIndex
+
1
][
columnIndex
]);
neighbourCells
.
add
(
cells
[
rowIndex
-
1
<
0
?
numberOfRows
-
1
:
rowIndex
-
1
][
columnIndex
+
1
>
numberOfColumns
-
1
?
0
:
columnIndex
+
1
]);
neighbourCells
.
add
(
cells
[
rowIndex
][
columnIndex
+
1
>
numberOfColumns
-
1
?
0
:
columnIndex
+
1
]);
neighbourCells
.
add
(
cells
[
rowIndex
+
1
>
numberOfRows
-
1
?
0
:
rowIndex
+
1
][
columnIndex
+
1
>
numberOfColumns
-
1
?
0
:
columnIndex
+
1
]);
return
neighbourCells
;
}
}
private
void
goToNextState
(
boolean
[][]
nextState
)
{
private
void
goToNextState
(
boolean
[][]
nextState
)
{
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,21 @@ public class Grid implements Iterable<Cell> {
...
@@ -131,6 +212,21 @@ public class Grid implements Iterable<Cell> {
*/
*/
void
randomGeneration
(
Random
random
)
{
void
randomGeneration
(
Random
random
)
{
for
(
int
i
=
0
;
i
<
numberOfRows
;
i
++)
{
for
(
int
j
=
0
;
j
<
numberOfColumns
;
j
++)
{
if
(
random
.
nextBoolean
())
{
cells
[
i
][
j
].
setAlive
();
if
(
random
.
nextBoolean
())
{
cells
[
i
][
j
].
setBlue
();
}
else
{
cells
[
i
][
j
].
setRed
();
}
}
else
{
cells
[
i
][
j
].
setDead
();
}
}
}
}
}
}
}
This diff is collapsed.
Click to expand it.
GridIterator.java
+
1
−
1
View file @
5e2d9612
This diff is collapsed.
Click to expand it.
Main.java
+
17
−
16
View file @
5e2d9612
...
@@ -17,6 +17,7 @@ public class Main{
...
@@ -17,6 +17,7 @@ public class Main{
}
catch
(
InterruptedException
ie
)
{
}
catch
(
InterruptedException
ie
)
{
}
}
gameOfLife
.
next
();
gameOfLife
.
next
();
gui
.
update
(
gameOfLife
.
getGrid
());
gui
.
update
(
gameOfLife
.
getGrid
());
}
}
...
...
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