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
Hide 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,11 +4,19 @@
public
class
Cell
{
private
boolean
isAlive
;
private
String
color
=
"Red"
;
public
void
setColor
(
String
hisColor
)
{
color
=
hisColor
;
}
public
String
getColor
()
{
return
color
;
}
public
Cell
(){
this
.
isAlive
=
false
;
this
.
isAlive
=
false
;
}
/**
* Determines whether this {@link Cell} is alive or not.
*
...
...
@@ -36,7 +44,7 @@ public class Cell {
*/
public
void
setAlive
()
{
this
.
isAlive
=
true
;
this
.
isAlive
=
true
;
}
/**
...
...
@@ -46,7 +54,7 @@ public class Cell {
*/
public
void
setDead
()
{
this
.
isAlive
=
false
;
this
.
isAlive
=
false
;
}
...
...
@@ -55,24 +63,24 @@ public class Cell {
*/
public
void
toggleState
()
{
if
(
this
.
isAlive
)
this
.
isAlive
=
false
;
else
this
.
isAlive
=
true
;
if
(
this
.
isAlive
)
this
.
isAlive
=
false
;
else
this
.
isAlive
=
true
;
}
public
boolean
isAliveInNextState
(
int
numberOfAliveNeighbours
)
{
if
(
isAlive
()){
if
(
numberOfAliveNeighbours
==
2
||
numberOfAliveNeighbours
==
3
)
return
true
;
else
return
false
;
}
else
{
if
(
numberOfAliveNeighbours
==
3
)
return
true
;
else
return
false
;
}
if
(
isAlive
()){
if
(
numberOfAliveNeighbours
==
2
||
numberOfAliveNeighbours
==
3
)
return
true
;
else
return
false
;
}
else
{
if
(
numberOfAliveNeighbours
==
3
)
return
true
;
else
return
false
;
}
}
}
This diff is collapsed.
Click to expand it.
GameOfLife.java
+
11
−
11
View file @
e0901c67
...
...
@@ -16,8 +16,8 @@ public class GameOfLife {
* @throws NullPointerException if {@code grid} is {@code null}
*/
public
GameOfLife
(
Grid
grid
)
{
// this.grid = requireNonNull(grid, "grid is null");
this
.
grid
=
grid
;
// this.grid = requireNonNull(grid, "grid is null");
this
.
grid
=
grid
;
grid
.
randomGeneration
(
random
);
}
...
...
@@ -26,7 +26,7 @@ public class GameOfLife {
*/
public
void
next
()
{
grid
.
nextGeneration
();
generationNumber
++;
generationNumber
++;
}
...
...
@@ -52,19 +52,19 @@ public class GameOfLife {
* Plays the game.
*/
public
void
play
(
int
maxGenerations
)
{
for
(
int
generation
=
0
;
generation
<
maxGenerations
;
generation
++){
this
.
next
();
System
.
out
.
println
(
"generation : "
+
generation
);
}
for
(
int
generation
=
0
;
generation
<
maxGenerations
;
generation
++){
this
.
next
();
System
.
out
.
println
(
"generation : "
+
generation
);
}
}
/**
* Pauses the game.
*/
public
void
pause
()
{
// timeline.pause();
// timeline.pause();
}
/**
...
...
@@ -73,7 +73,7 @@ public class GameOfLife {
public
void
clear
()
{
pause
();
grid
.
clear
();
generationNumber
=
0
;
generationNumber
=
0
;
}
/**
...
...
This diff is collapsed.
Click to expand it.
GameOfLifeGUI.java
+
28
−
21
View file @
e0901c67
...
...
@@ -10,39 +10,46 @@ public class GameOfLifeGUI extends JFrame {
private
GridLayout
gridLayout
;
private
JPanel
gridPanel
;
private
JFrame
frame
;
public
GameOfLifeGUI
(
Grid
g
)
{
this
.
numberOfRows
=
g
.
getNumberOfRows
();
this
.
numberOfColumns
=
g
.
getNumberOfColumns
();
gridLayout
=
new
GridLayout
(
numberOfRows
,
numberOfColumns
);
this
.
numberOfRows
=
g
.
getNumberOfRows
();
this
.
numberOfColumns
=
g
.
getNumberOfColumns
();
gridLayout
=
new
GridLayout
(
numberOfRows
,
numberOfColumns
);
gridPanel
=
new
JPanel
(
gridLayout
);
labelGrid
=
new
JLabel
[
numberOfRows
][
numberOfColumns
];
labelGrid
=
new
JLabel
[
numberOfRows
][
numberOfColumns
];
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
);
else
labelGrid
[
x
][
y
].
setForeground
(
Color
.
white
);
gridPanel
.
add
(
labelGrid
[
x
][
y
]);
}
labelGrid
[
x
][
y
]
=
new
JLabel
(
"*"
);
JLabel
label
;
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
labelGrid
[
x
][
y
].
setForeground
(
Color
.
red
);
else
labelGrid
[
x
][
y
].
setForeground
(
Color
.
white
);
gridPanel
.
add
(
labelGrid
[
x
][
y
]);
}
frame
=
new
JFrame
(
"Game of Life"
);
frame
.
setDefaultCloseOperation
(
WindowConstants
.
EXIT_ON_CLOSE
);
frame
.
setContentPane
(
gridPanel
);
frame
.
setSize
(
squareSize
*
numberOfRows
,
squareSize
*
numberOfColumns
);
frame
.
setLocationByPlatform
(
true
);
frame
.
setSize
(
squareSize
*
numberOfRows
,
squareSize
*
numberOfColumns
);
frame
.
setLocationByPlatform
(
true
);
frame
.
setVisible
(
true
);
}
public
void
update
(
Grid
g
){
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
())
label
.
setForeground
(
Color
.
red
);
else
label
.
setForeground
(
Color
.
white
);
}
JLabel
label
=
labelGrid
[
x
][
y
];
if
(
g
.
getCell
(
x
,
y
).
isAlive
())
{
if
((
g
.
getCell
(
x
,
y
).
getColor
()).
equals
(
"Red"
))
{
label
.
setForeground
(
Color
.
red
);
}
else
{
label
.
setForeground
(
Color
.
blue
);
}
}
else
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.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>.
...
...
@@ -98,29 +95,94 @@ public class Grid implements Iterable<Cell> {
}
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
)
{
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
)
{
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
)
{
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
)
{
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.
*/
void
clear
()
{
for
(
Iterator
<
Cell
>
it
=
this
.
iterator
();
it
.
hasNext
();
)
{
Cell
cell
=
it
.
next
();
cell
.
setDead
();
}
}
/**
...
...
@@ -129,8 +191,25 @@ 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
)
{
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
...
...
@@ -4,22 +4,22 @@ import javax.swing.*;
public
class
Main
{
public
static
void
main
(
String
args
[])
throws
IOException
{
int
NUMBER_OF_ROWS
=
64
;
int
NUMBER_OF_COLUMNS
=
64
;
GameOfLife
gameOfLife
=
new
GameOfLife
(
new
Grid
(
NUMBER_OF_ROWS
,
NUMBER_OF_COLUMNS
));
GameOfLifeGUI
gui
=
new
GameOfLifeGUI
(
gameOfLife
.
getGrid
());
int
NUMBER_OF_ROWS
=
64
;
int
NUMBER_OF_COLUMNS
=
64
;
for
(
int
generation
=
0
;
generation
<
1000
;
generation
++){
try
{
Thread
.
sleep
(
100
);
}
catch
(
InterruptedException
ie
)
{
GameOfLife
gameOfLife
=
new
GameOfLife
(
new
Grid
(
NUMBER_OF_ROWS
,
NUMBER_OF_COLUMNS
));
GameOfLifeGUI
gui
=
new
GameOfLifeGUI
(
gameOfLife
.
getGrid
());
for
(
int
generation
=
0
;
generation
<
1000
;
generation
++){
try
{
Thread
.
sleep
(
100
);
}
catch
(
InterruptedException
ie
)
{
}
gameOfLife
.
next
();
gui
.
update
(
gameOfLife
.
getGrid
());
}
}
gameOfLife
.
next
();
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