From 5cafa9ad0ac773b92a2b0911fad3d3144887928c Mon Sep 17 00:00:00 2001 From: julie Date: Sun, 2 Jun 2024 12:35:22 +0200 Subject: [PATCH] final --- src/backend/Cell.java | 21 +++++++++ src/backend/Gride.java | 98 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 src/backend/Cell.java create mode 100644 src/backend/Gride.java diff --git a/src/backend/Cell.java b/src/backend/Cell.java new file mode 100644 index 0000000..834ef59 --- /dev/null +++ b/src/backend/Cell.java @@ -0,0 +1,21 @@ +package backend; + +// Cell in grid creation +public class Cell { + private int value; + + // Constructor + public Cell(int value) { + this.value = value; + } + + // obtain value of the cell + public int getValue() { + return value; + } + + // Method to set value of the cell + public void setValue(int value) { + this.value = value; + } +} diff --git a/src/backend/Gride.java b/src/backend/Gride.java new file mode 100644 index 0000000..5aecc23 --- /dev/null +++ b/src/backend/Gride.java @@ -0,0 +1,98 @@ +package backend; + +import java.util.ArrayList; + +// Class representing the grid +public class Gride { + // Attributes + private int height; // Height - grid + private int width; // Width - grid + private ArrayList> gride; // 2D array for the grid + private Simulator simulator; + + // Constructor + public Gride(int height, int width, Simulator tempSimulator) { + this.height = height; // Set height + this.width = width; // Set width + this.simulator = tempSimulator; // Set simulator object + + // Create grid + gride = new ArrayList<>(height); + + for (int i = 0; i < height; i++) { + this.gride.add(i, new ArrayList()); + for (int j = 0; j < width; j++) { + this.gride.get(i).add(new Cell(0)); // Initialize cells with value 0 + } + } + } + + // Method height of grid + public int getheight() { + return this.height; + } + + // Method width of grid + public int getwidth() { + return this.width; + } + + // Method lopping border + public boolean isLoopingBorder() { + return simulator.isLoopingBorder(); + } + + // Method to get cell at specified coordinates + public Cell getCell(int row,int column) { + return gride.get(row).get(column); + } + + // Method to set cell at specified coordinates + public void setCell(int row, int column, Cell cell){ + this.gride.get(row).set(column, cell); + } + + // Count surrounding cells + public int countAround(int row, int column) { + int count = 0; + boolean loopingBorder = isLoopingBorder(); + + for (int i = row - 1; i <= row + 1; i++) { + for (int j = column - 1; j <= column + 1; j++) { + if (i == row && j == column) { + continue; + } + + int x = i; + int y = j; + + if (loopingBorder) { + x = (x + width) % width; + y = (y + height) % height; + } else { + if (x < 0 || x >= width || y < 0 || y >= height) { + continue; + } + } + + count += this.getCell(x, y).getValue(); + } + } + + return count; // Return total count + } + + // creation of a grid with specified density + public void setRandom(double density) { + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + double random = Math.random(); // Get random number + if (random < density) { + gride.get(i).get(j).setValue(1); // set cell value to 1 if is less than density + } else { + gride.get(i).get(j).setValue(0); // else set cell value to 0 + } + } + } + } +}