From 31daecc447c37d3a5e0d01e013b1f18e4ff6c454 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Sat, 20 Dec 2014 16:36:43 +0100 Subject: [PATCH] Favouring two dimensional array --- Sd1/P/life/V3/src/main/java/LifeWorld.java | 56 ++++++++-------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/Sd1/P/life/V3/src/main/java/LifeWorld.java b/Sd1/P/life/V3/src/main/java/LifeWorld.java index 09664f4f3..57244ed1d 100755 --- a/Sd1/P/life/V3/src/main/java/LifeWorld.java +++ b/Sd1/P/life/V3/src/main/java/LifeWorld.java @@ -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]; } } } -- GitLab