private void updateWaterBehavior(int row, int col, int numRows, int numCols) {
if (row < numRows - 1 &&
canMoveTo(row + 1, col, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row + 1, col);
} else {
int direction = (int)(Math.random() * 3) - 1;
if (col + direction >= 0 && col + direction < numCols &&
canMoveTo(row, col + direction, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row, col + direction);
}
}
}
private void updateSandBehavior(int row, int col, int numRows, int numCols) {
if (row < numRows - 1 &&
canMoveTo(row + 1, col, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row + 1, col);
} else {
boolean tryLeftFirst = Math.random() < 0.5;
if (tryLeftFirst) {
if (col > 0 &&
canMoveTo(row + 1, col - 1, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row + 1, col - 1);
} else if (col < numCols - 1 &&
canMoveTo(row + 1, col + 1, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row + 1, col + 1);
}
} else {
if (col < numCols - 1 &&
canMoveTo(row + 1, col + 1, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row + 1, col + 1);
} else if (col > 0 &&
canMoveTo(row + 1, col - 1, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row + 1, col - 1);
}
}
}
}
private void updateAcidBehavior(int row, int col, int numRows, int numCols) {
detectAcidAndMetal(row, col, numRows, numCols);
detectAcidAndWater(row, col, numRows, numCols);
if (row < numRows - 1 &&
canMoveTo(row + 1, col, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row + 1, col);
} else {
int direction = (int)(Math.random() * 3) - 1;
if (col + direction >= 0 && col + direction < numCols &&
canMoveTo(row, col + direction, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row, col + direction);
}
}
}
private void updateSmokeBehavior(int row, int col, int numRows, int numCols) {
if (row > 0) {
int direction = (int)(Math.random() * 3) - 1;
if (direction == 0) {
if (canMoveTo(row - 1, col, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row - 1, col);
}
} else {
if (col + direction >= 0 && col + direction < numCols &&
canMoveTo(row - 1, col + direction, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row - 1, col + direction);
} else if (canMoveTo(row - 1, col, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row - 1, col);
}
}
}
}
private void updatePlantBehavior(int row, int col, int numRows, int numCols) {
boolean nearWater = false;
int[] directions = {-1, 0, 1};
for (int rowDelta : directions) {
for (int colDelta : directions) {
if (row + rowDelta >= 0 && row + rowDelta < numRows
&& col + colDelta >= 0 && col + colDelta < numCols) {
if (grid[row + rowDelta][col + colDelta] == Material.WATER) {
nearWater = true;
}
if (grid[row + rowDelta][col + colDelta] == Material.ACID) {
grid[row][col] = Material.ERASER;
return;
}
}
}
}
double growProbability = nearWater ? 0.0625 : 0.05;
for (int rowDelta : directions) {
for (int colDelta : directions) {
if (row + rowDelta >= 0 && row + rowDelta < numRows
&& col + colDelta >= 0 && col + colDelta < numCols) {
Material neighboringCell = grid[row + rowDelta][col + colDelta];
if (neighboringCell == Material.ERASER && Math.random() < growProbability) {
grid[row + rowDelta][col + colDelta] = Material.PLANT;
}
}
}
}
}
private boolean canMoveTo(int newRow, int newCol, int numRows, int numCols, Material material) {
return newRow >= 0 && newRow < numRows &&
newCol >= 0 && newCol < numCols && grid[newRow][newCol] == material;
}
private void moveMaterial(int fromRow, int fromCol, int toRow, int toCol) {
Material temp = grid[fromRow][fromCol];
grid[fromRow][fromCol] = grid[toRow][toCol];
grid[toRow][toCol] = temp;
}
private void detectAcidAndWater(int row, int col, int numRows, int numCols) {
int[] directions = {-1, 0, 1};
for (int rowDelta : directions) {
for (int colDelta : directions) {
if (row + rowDelta >= 0 && row + rowDelta < numRows &&
col + colDelta >= 0 && col + colDelta < numCols &&
grid[row + rowDelta][col + colDelta] == Material.WATER) {
grid[row][col] = Material.WATER;
break;
}
}
}
}
private void detectAcidAndMetal(int row, int col, int numRows, int numCols) {
int[] directions = {-1, 0, 1};
for (int rowDelta : directions) {
for (int colDelta : directions) {
if (row + rowDelta >= 0 && row + rowDelta < numRows &&
col + colDelta >= 0 && col + colDelta < numCols &&
grid[row + rowDelta][col + colDelta] == Material.METAL) {
grid[row + rowDelta][col + colDelta] = Material.SMOKE;
}
}
}
}