Skip to content
Snippets Groups Projects
Commit 31daecc4 authored by Goik Martin's avatar Goik Martin
Browse files

Favouring two dimensional array

parent 570b5834
No related branches found
No related tags found
No related merge requests found
......@@ -13,8 +13,8 @@ public class LifeWorld extends World {
private static int initialLivingPercentage = 25;
private static final int
width = 50,
static private final int
width = 80,
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[] getCellsNeighbours(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];
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment