Skip to content
Snippets Groups Projects
Commit 2e6fc89a authored by AREZKI Celia's avatar AREZKI Celia
Browse files

Add a spreadFire method to prevent fire from spreading across mountains.

parent 1d51f176
No related branches found
No related tags found
No related merge requests found
......@@ -4,4 +4,5 @@ public class SimulatorMain {
public static void main(String[] args){
SimulatorApplication.main(args);
}
}
package model;
import util.Position;
import view.ViewElement;
import java.util.*;
......@@ -37,6 +38,46 @@ public class FireManager implements FireBehavior, FireProperties{
}
return newFirePositions;
}
public void spreadFire(ViewElement[][] grid) {
int rows = grid.length;
int columns = grid[0].length;
ViewElement[][] newGrid = new ViewElement[rows][columns];
for (int i = 0; i < rows; i++) {
System.arraycopy(grid[i], 0, newGrid[i], 0, columns);
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (grid[i][j] == ViewElement.FIRE) {
// Tenter de propager le feu sur les voisins
for (Position neighbor : getNeighbors(i, j, rows, columns)) {
if (grid[neighbor.row()][neighbor.column()] == ViewElement.EMPTY) {
newGrid[neighbor.row()][neighbor.column()] = ViewElement.FIRE;
}
// Empêcher la propagation sur les montagnes
if (grid[neighbor.row()][neighbor.column()] == ViewElement.MOUNTAIN) {
newGrid[neighbor.row()][neighbor.column()] = ViewElement.MOUNTAIN;
}
}
}
}
}
// Mise à jour de la grille
for (int i = 0; i < rows; i++) {
System.arraycopy(newGrid[i], 0, grid[i], 0, columns);
}
}
private List<Position> getNeighbors(int row, int column, int rows, int columns) {
List<Position> neighbors = new ArrayList<>();
if (row > 0) neighbors.add(new Position(row - 1, column));
if (row < rows - 1) neighbors.add(new Position(row + 1, column));
if (column > 0) neighbors.add(new Position(row, column - 1));
if (column < columns - 1) neighbors.add(new Position(row, column + 1));
return neighbors;
}
// Extinguish a fire at a specific position
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment