OOP_D8_Project/src/backend/Gride.java

104 lines
2.7 KiB
Java

package backend;
import java.util.ArrayList;
public class Gride {
private int height;
private int width;
private ArrayList<ArrayList<Cell>> gride;
private Simulator simulator;
public Gride(int height, int width, Simulator tempSimulator) {
this.height = height;
this.width = width;
this.simulator = tempSimulator;
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));
}
}
}
public int getheight() {
return this.height;
}
public int getwidth() {
return this.width;
}
public boolean isLoopingBorder() {
return simulator.isLoopingBorder();
}
public Cell getCell(int row,int column) {
return gride.get(row).get(column);
}
public void setCell(int row, int column, Cell cell){
this.gride.get(row).set(column, cell);
}
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;
}
//TODO : set agent (x y agent) load an agent to coordinates x,y
//TODO : set random (density) create a random gride of determined density
public void setRandom(double density) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
double random = Math.random();
if (random < density) {
gride.get(i).get(j).setValue(1);
} else {
gride.get(i).get(j).setValue(0);
}
}
}
System.out.println("Created a random field");
}
public void serialPrint(){
for (int i = 0; i < height; i++) {
System.out.print("\n");
for (int j = 0; j < width; j++) {
System.out.print(this.getCell(i, j).getValue() +" ");
}
}
}
}