final
This commit is contained in:
parent
717eb2f842
commit
5cafa9ad0a
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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<ArrayList<Cell>> 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<Cell>());
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue