MazeGeneration/Cell.pde

52 lines
1.7 KiB
Plaintext

class Cell {
boolean state; // Indicator for being either "alive" (=true;=path) or "dead" (=false;=wall)
int x, y; // Remember what 2D-Coordinates each Cell has
Cell(int i, int j) {
state = false;
x = i;
y = j;
}
ArrayList<Cell> getNeighbors() {
ArrayList<Cell> neighbors = new ArrayList<Cell>();
// Go through Grid at 2-Cell-Steps
for (int nx = -2; nx <= 2; nx += 2) {
for (int ny = -2; ny <= 2; ny += 2) {
boolean isCurrent = (nx == 0 && ny == 0); // Don't set Current as Neighbor
boolean isDiagonal = (abs(nx) == abs(ny)); // Don't set Diagonals as Neighbors
boolean isOutsideGrid = false; // Don't set Cells outside of Grid as Neighbors
if (x + nx < 0) { isOutsideGrid = true; } // Outside left side of Screen
if (x + nx >= cellsW) { isOutsideGrid = true; } // Outside right side of Screen
if (y + ny < 0) { isOutsideGrid = true; } // Outside top side of Screen
if (y + ny >= cellsH) { isOutsideGrid = true; } // Outside bottom side of Screen
if (!isCurrent && !isDiagonal && !isOutsideGrid) {
Cell c = getCell(Cells, x + nx, y + ny);
if (!c.state) {
neighbors.add(c);
}
}
}
}
return neighbors;
}
Cell getBetween(Cell other) {
// Get Cell in between current and "other"
int xDiff = x - other.x, yDiff = y - other.y;
Cell c = getCell(Cells, x - xDiff/2, y - yDiff/2);
return c;
}
}
Cell getCell(ArrayList<Cell> c, int i, int j) {
// translate 2D coordinates into 1D array
// x coordinate + (y coordinate * width of row) = 1D Array index
int index = i + j * cellsW;
return c.get(index);
}