diff --git a/src/backend/Grid.java b/src/backend/Grid.java index 99bf246..17ef5ac 100644 --- a/src/backend/Grid.java +++ b/src/backend/Grid.java @@ -1,17 +1,35 @@ package backend; import java.util.Random; +/** + * The Grid class represents a 2D grid of integers. + */ public class Grid { - private int width; - private int height; - private int[][] grid; - private int rando; + private final int width; + private final int height; + private final int[][] grid; + private final Random rand = new Random(); + + /** + * Constructs a Grid with the specified width and height. + * + * @param width the width of the grid + * @param height the height of the grid + */ public Grid(int width, int height) { this.width = width; this.height = height; this.grid = new int[height][width]; } + /** + * Sets the value at the specified position in the grid. + * + * @param x the x-coordinate + * @param y the y-coordinate + * @param value the value to set + * @throws IndexOutOfBoundsException if the position is out of range + */ public void setValue(int x, int y, int value) { if (x >= 0 && x < width && y >= 0 && y < height) { grid[y][x] = value; @@ -20,6 +38,14 @@ public class Grid { } } + /** + * Gets the value at the specified position in the grid. + * + * @param x the x-coordinate + * @param y the y-coordinate + * @return the value at the specified position + * @throws IndexOutOfBoundsException if the position is out of range + */ public int getValue(int x, int y) { if (x >= 0 && x < width && y >= 0 && y < height) { return grid[y][x]; @@ -27,15 +53,35 @@ public class Grid { throw new IndexOutOfBoundsException("Grid position out of range"); } } - + + /** + * Fills the grid with random values based on the given randomness parameter. + * + * @param randomness a float value between 0 and 1 indicating the probability + * of a cell being set to 1 + */ public void fillRandom(float randomness) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { - int rando = Random.nextInt(100); - int r= (int) randomness*100; - if(rando(); fieldSurviveValues = new ArrayList(); - //TODO : add missing attribute initialization - - - - //Default rule : Survive always, birth never - for(int i =0; i<9; i++) { - fieldSurviveValues.add(i); - } + // Conway's Game of Life rules: + // Survive with 2 or 3 neighbors + fieldSurviveValues.add(2); + fieldSurviveValues.add(3); + + // Birth with exactly 3 neighbors + fieldBirthValues.add(3); } @@ -118,7 +117,61 @@ public class Simulator extends Thread { * and the count is in the birth list, * then the cell becomes alive */ - + Grid nextGrid = new Grid(LINE_NUM,COL_NUM); + for (int x = 0; x < getWidth(); x++) { + for (int y = 0; y < getHeight(); y++) { + int actualState = getCell(x,y); + int futureState =0; + // high life + if(actualState==0) { + for (int i = 1; i<=5; i++) { + if(fieldBirthValues.contains(countNeighbors(x,y,i))) { + futureState = i; + } + } + } else { + if(fieldSurviveValues.contains(countNeighbors(x,y,actualState))) { + futureState = actualState; + } + } + + nextGrid.setValue(x,y,futureState); + } + } + maingrid = nextGrid; + } + + private int countNeighbors(int x, int y, int state) { + int nbNeighbors = 0; + + for (int i = x - 1; i <= x + 1; i++) { + for (int j = y - 1; j <= y + 1; j++) { + int x_looped = i; + int y_looped = j; + + if (loopingBorder) { + if (x_looped < 0) { + x_looped = getWidth() - 1; + } else if (x_looped >= getWidth()) { + x_looped = 0; + } + if (y_looped < 0) { + y_looped = getHeight() - 1; + } else if (y_looped >= getHeight()) { + y_looped = 0; + } + } + + if (x_looped >= 0 && x_looped < getWidth() && y_looped >= 0 && y_looped < getHeight() && (x_looped != x || y_looped != y)) { + if (state == maingrid.getValue(x_looped, y_looped)) { + nbNeighbors++; + } + } + } + } + + System.out.println(nbNeighbors); + return nbNeighbors; } /* @@ -205,7 +258,18 @@ public class Simulator extends Thread { */ public ArrayList getSaveState() { //TODO : complete method with proper return - return null; + ArrayList saveState = new ArrayList<>(); + for (int y = 0; y < LINE_NUM; y++) { + StringBuilder rowBuilder = new StringBuilder(); + for (int x = 0; x < COL_NUM; x++) { + if (x > 0) { + rowBuilder.append(";"); + } + rowBuilder.append(maingrid.getValue(x, y)); + } + saveState.add(rowBuilder.toString()); + } + return saveState; } /** * @@ -261,16 +325,17 @@ public class Simulator extends Thread { public boolean isLoopingBorder() { //TODO : complete method with proper return - return false; + return loopingBorder; } public void toggleLoopingBorder() { //TODO : complete method - + loopingBorder = !loopingBorder; } public void setLoopDelay(int delay) { //TODO : complete method + loopDelay = delay; } public void toggleClickAction() { @@ -292,8 +357,29 @@ public class Simulator extends Thread { */ public ArrayList getRule() { //TODO : complete method with proper return - - return null; + + StringBuilder birthValues = new StringBuilder(); + StringBuilder surviveValues = new StringBuilder(); + + for (int value : fieldBirthValues) { + if (birthValues.length() > 0) { + birthValues.append(";"); + } + birthValues.append(value); + } + + for (int value : fieldSurviveValues) { + if (surviveValues.length() > 0) { + surviveValues.append(";"); + } + surviveValues.append(value); + } + + ArrayList result = new ArrayList<>(); + result.add(birthValues.toString()); + result.add(surviveValues.toString()); + + return result; } public void loadRule(ArrayList lines) { @@ -302,7 +388,8 @@ public class Simulator extends Thread { return; } //TODO : remove previous rule (=emptying lists) - + fieldSurviveValues.clear(); + fieldBirthValues.clear(); String surviveLine = lines.get(0); String birthLine = lines.get(1); @@ -311,20 +398,22 @@ public class Simulator extends Thread { String elem = surviveElements[x]; int value = Integer.parseInt(elem); //TODO : add value to possible survive values - + fieldSurviveValues.add(value); } String[] birthElements = birthLine.split(";"); for(int x=0; x getAgentsSave() { //TODO : Same idea as the other save method, but for agents - return null; + + + return null; } public void loadAgents(ArrayList stringArray) { @@ -339,7 +428,7 @@ public class Simulator extends Thread { public String clickActionName() { // TODO : initially return "sheep" or "cell" // depending on clickActionFlag - return ""; + if (clickActionFlag) return "cell"; else return "sheep"; } }