Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor 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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
31daecc4
Commit
31daecc4
authored
10 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Favouring two dimensional array
parent
570b5834
No related branches found
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
Sd1/P/life/V3/src/main/java/LifeWorld.java
+19
-37
19 additions, 37 deletions
Sd1/P/life/V3/src/main/java/LifeWorld.java
with
19 additions
and
37 deletions
Sd1/P/life/V3/src/main/java/LifeWorld.java
+
19
−
37
View file @
31daecc4
...
...
@@ -13,8 +13,8 @@ public class LifeWorld extends World {
private
static
int
initialLivingPercentage
=
25
;
private
static
final
int
width
=
5
0
,
static
private
final
int
width
=
8
0
,
height
=
50
;
public
static
final
int
cellSize
=
10
;
// Minimum of 3 pixel in width here yields a (cellSize-2)x(cellSize-2) pixel visible cell
...
...
@@ -34,7 +34,7 @@ public class LifeWorld extends World {
}
private
final
GreenfootImage
gridImage
;
private
final
Cell
[]
cells
;
private
final
Cell
[]
[]
cells
;
/**
* The probability for a newly born cell
...
...
@@ -56,13 +56,13 @@ public class LifeWorld extends World {
super
(
width
,
height
,
cellSize
);
gridImage
=
getBackground
();
drawGrid
();
cells
=
createCells
();
// Create an initialize array all cells.
cells
=
new
Cell
[
width
][
height
];
// Create cells array container.
createCells
();
// Having created all cells we may now determine each cell's neighbors.
// Having created all cells we may now determine
and set
each cell's
set of
neighbors.
for
(
int
x
=
0
;
x
<
width
;
x
++)
{
for
(
int
y
=
0
;
y
<
height
;
y
++)
{
final
Cell
centerCell
=
cells
[
getCellIndex
(
x
,
y
)];
centerCell
.
setNeighbors
(
getCellsNeighbours
(
x
,
y
));
cells
[
x
][
y
].
setNeighbors
(
getCellNeighbours
(
x
,
y
));
}
}
}
...
...
@@ -73,8 +73,10 @@ public class LifeWorld extends World {
* calling any actor's {@link Actor#act()} methods methods.
*/
public
void
act
()
{
for
(
final
Cell
c
:
cells
)
{
c
.
saveCurrentToPreviousState
();
for
(
int
x
=
0
;
x
<
width
;
x
++)
{
for
(
int
y
=
0
;
y
<
height
;
y
++)
{
cells
[
x
][
y
].
saveCurrentToPreviousState
();
}
}
}
...
...
@@ -108,41 +110,21 @@ public class LifeWorld extends World {
// (height-1) __________..._____
}
/**
* Turn (x|y) coordinates into linear cell array index
* values ranging from 0 to (width * height - 1).
*
* Consider a simplified example of an array having width = 4 and
* height = 3:
*
* {(0|0) (1|0) (2|0) (3|0)} --> Linear array Index values {0, 1, 2, 3}
* {(0|1) (1|1) (2|1) (4|1)} --> Linear array Index values {4, 5, 6, 7}
* {(0|2) (1|2) (2|2) (4|2)} --> Linear array Index values {8, 9, 10, 11}
*
* @param x horizontal position in cell coordinates
* @param y vertical position in cell coordinates
* @return The linear array index.
*/
private
int
getCellIndex
(
int
x
,
int
y
)
{
return
x
+
y
*
width
;
}
/** Create the array of cells and randomly turn some of them on
* according to {@link #setInitialLivingPercentage(int)}.
*/
private
Cell
[]
createCells
()
{
final
Cell
[]
cellsToReturn
=
// Variable name has to be different from LifeWorld.cells
new
Cell
[
width
*
height
];
// in order to initialize final variable.
private
void
createCells
()
{
for
(
int
x
=
0
;
x
<
width
;
x
++)
{
for
(
int
y
=
0
;
y
<
height
;
y
++)
{
final
Cell
c
=
new
Cell
(
cellSize
,
// Create a cell having a fixed probability
getRandomLifeOrDeadState
());
// to be either dead or alive.
cellsToReturn
[
getCellIndex
(
x
,
y
)]
=
c
;
// A separate array allows for easier access than via
getRandomLifeOrDeadState
());
// to be either dead or alive.
cells
[
x
][
y
]
=
c
;
// A separate array allows for easier access than via
// Greenfoot's own "world" object accounting mechanism.
addObject
(
c
,
x
,
y
);
// Add to Greenfoot world.
addObject
(
c
,
x
,
y
);
// Add to Greenfoot world
as well
.
}
}
return
cellsToReturn
;
// Will be assigned to variable cells
}
/**
...
...
@@ -163,7 +145,7 @@ public class LifeWorld extends World {
* @param y The given cell's y-position
* @return The array of all neighboring cells.
*/
private
Cell
[]
getCell
s
Neighbours
(
final
int
x
,
final
int
y
)
{
private
Cell
[]
getCellNeighbours
(
final
int
x
,
final
int
y
)
{
// Rectangular limits corresponding to the 9 different boundary
// related situations being described in the above Javadoc.
//
...
...
@@ -179,7 +161,7 @@ public class LifeWorld extends World {
for
(
int
dx
=
xMin
;
dx
<=
xMax
;
dx
++)
{
for
(
int
dy
=
yMin
;
dy
<=
yMax
;
dy
++)
{
if
(
dx
!=
x
||
dy
!=
y
)
{
// Exclude center cell
neighbors
[
neighborIndexCount
++]
=
cells
[
getCellIndex
(
dx
,
dy
)
];
neighbors
[
neighborIndexCount
++]
=
cells
[
dx
][
dy
];
}
}
}
...
...
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