public void updateRandomLocation() {
int numRows = grid.length;
int numCols = grid[0].length;
int row = (int) (Math.random() * numRows);
int col = (int) (Math.random() * numCols);
Material currentMaterial = grid[row][col];
switch (currentMaterial) {
case ERASER:
break;
case METAL:
break;
case WATER:
updateWaterBehavior(row, col, numRows, numCols);
break;
case SAND:
updateSandBehavior(row, col, numRows, numCols);
break;
case ACID:
updateAcidBehavior(row, col, numRows, numCols);
break;
case SMOKE:
updateSmokeBehavior(row, col, numRows, numCols);
break;
}
}
private void updateWaterBehavior(int row, int col, int numRows, int numCols) {
if (row < numRows - 1) {
Material below = grid[row + 1][col];
if (below == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row + 1][col] = Material.WATER;
} else {
boolean updated = updateWaterMoveSideways(row, col, numRows, numCols, below);
if (!updated) {
updateWaterPile(row, col, numRows, numCols);
}
}
} else {
updateWaterPile(row, col, numRows, numCols);
}
}
private boolean updateWaterMoveSideways(int row, int col, int numRows, int numCols, Material materialBelow) {
int[] directions = {-1, 1};
for (int direction : directions) {
if (col + direction >= 0 && col + direction < numCols) {
Material side = grid[row][col + direction];
if (side == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row][col + direction] = Material.WATER;
return true;
} else if (side == materialBelow) {
if (Math.random() < 0.2) {
grid[row][col] = side;
grid[row][col + direction] = Material.WATER;
return true;
}
}
}
}
return false;
}
private void updateWaterPile(int row, int col, int numRows, int numCols) {
grid[row][col] = Material.WATER;
for (int i = row - 1; i >= 0; i--) {
if (grid[i][col] == Material.ERASER) {
grid[i][col] = Material.WATER;
grid[i + 1][col] = Material.ERASER;
} else {
break;
}
}
}
private void updateSandBehavior(int row, int col, int numRows, int numCols) {
if (row < numRows - 1) {
Material below = grid[row + 1][col];
if (below == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row + 1][col] = Material.SAND;
} else {
boolean updated = updateSandMoveSideways(row, col, numRows, numCols, below);
if (!updated) {
updateSandPile(row, col, numRows, numCols);
}
}
} else {
updateSandPile(row, col, numRows, numCols);
}
}
private boolean updateSandMoveSideways(int row, int col, int numRows, int numCols, Material materialBelow) {
int direction = (int) (Math.random() * 3) - 1; // -1, 0, or 1
if (direction != 0 && col + direction >= 0 && col + direction < numCols) {
Material side = grid[row][col + direction];
if (side == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row][col + direction] = Material.SAND;
return true;
} else if (side == materialBelow) {
grid[row][col] = side;
grid[row + 1][col] = Material.SAND;
return true;
}
}
return false;
}
private void updateSandPile(int row, int col, int numRows, int numCols) {
grid[row][col] = Material.SAND;
for (int i = row - 1; i >= 0; i--) {
if (grid[i][col] == Material.ERASER) {
grid[i][col] = Material.SAND;
grid[i + 1][col] = Material.ERASER;
} else {
break;
}
}
}
private void updateAcidBehavior(int row, int col, int numRows, int numCols) {
if (row < numRows - 1 && grid[row + 1][col] == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row + 1][col] = Material.ACID;
} else {
boolean updated = updateAcidSideways(row, col, numRows, numCols);
if (!updated) {
detectAcidAndWater(row, col, numRows, numCols);
}
}
corrodeMetal(row, col, numRows, numCols);
}
private boolean updateAcidSideways(int row, int col, int numRows, int numCols) {
int[] directions = {-1, 1};
for (int direction : directions) {
if (col + direction >= 0 && col + direction < numCols && grid[row][col + direction] == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row][col + direction] = Material.ACID;
return true;
}
}
return false;
}
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.ERASER;
break;
}
}
}
}
private void corrodeMetal(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.ACID;
}
}
}
}
private void updateSmokeBehavior(int row, int col, int numRows, int numCols) {
if (row > 0) {
Material above = grid[row - 1][col];
if (above == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row - 1][col] = Material.SMOKE;
} else {
updateSmokeSideways(row, col, numRows, numCols, above);
}
}
}
private void updateSmokeSideways(int row, int col, int numRows, int numCols, Material materialAbove) {
int[] directions = {-1, 1};
for (int direction : directions) {
if (col + direction >= 0 && col + direction < numCols && grid[row][col + direction] == Material.ERASER) {
grid[row][col] = Material.ERASER;
grid[row][col + direction] = Material.SMOKE;
} else if (grid[row][col + direction] == materialAbove) {
grid[row][col] = materialAbove;
grid[row][col + direction] = Material.SMOKE;
}
}
}
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 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; // -1, 0, or 1
if (col + direction >= 0 && col + direction < numCols && canMoveTo(row, col + direction, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row, col + direction);
}
}
}
private void updateAcidBehavior(int row, int col, int numRows, int numCols) {
// Acid corrosion and interaction with water behavior remains the same
corrodeMetal(row, col, numRows, numCols);
detectAcidAndWater(row, col, numRows, numCols);
// Updated acid movement behavior
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; // -1, 0, or 1
if (col + direction >= 0 && col + direction < numCols && canMoveTo(row, col + direction, numRows, numCols, Material.ERASER)) {
moveMaterial(row, col, row, col + direction);
}
}
}
private boolean canMoveTo(int newRow, int newCol, int numRows, int numCols, Material target) {
return newRow >= 0 && newRow < numRows && newCol >= 0 && newCol < numCols && grid[newRow][newCol] == target;
}
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;
}