Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
  • variant
2 results

Target

Select target project
  • m19023837/firefighter-starter-mansour-chadi-chahine-rami
  • r24025701/firefighterstarter
  • n24026202/firefighterstarter
  • couetoux.b/firefighterstarter
  • b23027938/firefighterstarter
  • z20039716/fire-fighter
  • a18023913/firefighterstarter
  • o22010261/firefighterstarter
  • b22015516/firefighterstarter
  • alaboure/firefighter-template
  • p21211679/firefighter-luis-parra-yanis-lounadi
  • v23014723/firefighter-project
  • k20014011/firefighter-template
  • m23022217/firefighter-template
  • p20006624/firefighter-template
  • l21221596/firefighter-template
  • s21232465/firefighter-template
  • y21224754/firefighter-template
  • w21225935/firefighter-template
  • h22023886/firefighter-template
  • b21221604/firefighter-template-boucenna-yacine-zeghar-mohamed-lamine
  • c23025119/firefighterstarter
  • b20031964/firefighterstarter
23 results
Select Git revision
  • main
  • variant
2 results
Show changes
Commits on Source (21)
Showing
with 247 additions and 73 deletions
File added
File added
...@@ -17,8 +17,10 @@ public class SimulatorApplication extends javafx.application.Application { ...@@ -17,8 +17,10 @@ public class SimulatorApplication extends javafx.application.Application {
private static final int COLUMN_COUNT = 20; private static final int COLUMN_COUNT = 20;
private static final int BOX_WIDTH = 50; private static final int BOX_WIDTH = 50;
private static final int BOX_HEIGHT = 50; private static final int BOX_HEIGHT = 50;
public static final int INITIAL_FIRE_COUNT = 3; public static final int INITIAL_FIRE_COUNT = 15;
public static final int INITIAL_FIREFIGHTER_COUNT = 6; public static final int INITIAL_FIREFIGHTER_COUNT = 10;
public static final int INITIAL_CLOUD_COUNT = 5;
private Stage primaryStage; private Stage primaryStage;
private Parent view; private Parent view;
...@@ -44,7 +46,7 @@ public class SimulatorApplication extends javafx.application.Application { ...@@ -44,7 +46,7 @@ public class SimulatorApplication extends javafx.application.Application {
view = loader.load(); view = loader.load();
Controller controller = loader.getController(); Controller controller = loader.getController();
controller.initialize(BOX_WIDTH, BOX_HEIGHT, COLUMN_COUNT, ROW_COUNT, controller.initialize(BOX_WIDTH, BOX_HEIGHT, COLUMN_COUNT, ROW_COUNT,
INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT); INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT,INITIAL_CLOUD_COUNT);
} }
private void showScene() { private void showScene() {
......
...@@ -88,9 +88,13 @@ public class Controller { ...@@ -88,9 +88,13 @@ public class Controller {
if (squareState.contains(ModelElement.FIRE)) { if (squareState.contains(ModelElement.FIRE)) {
return ViewElement.FIRE; return ViewElement.FIRE;
} }
if (squareState.contains(ModelElement.CLOUD)) { // Assurez-vous que le modèle inclut bien le nuage
return ViewElement.CLOUD;
}
return ViewElement.EMPTY; return ViewElement.EMPTY;
} }
private void initializeTimeline() { private void initializeTimeline() {
Duration duration = new Duration(Controller.PERIOD_IN_MILLISECONDS); Duration duration = new Duration(Controller.PERIOD_IN_MILLISECONDS);
EventHandler<ActionEvent> eventHandler = EventHandler<ActionEvent> eventHandler =
...@@ -124,9 +128,9 @@ public class Controller { ...@@ -124,9 +128,9 @@ public class Controller {
} }
public void initialize(int squareWidth, int squareHeight, int columnCount, public void initialize(int squareWidth, int squareHeight, int columnCount,
int rowCount, int initialFireCount, int initialFirefighterCount) { int rowCount, int initialFireCount, int initialFirefighterCount,int initialCloudCount) {
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight); grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount)); this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount,initialCloudCount));
repaintGrid(); repaintGrid();
} }
......
package model;
import util.Position;
public class Cloud {
private Position position;
public Cloud(Position position) {
this.position = position;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
}
package model;
import util.Position;
public class Fire {
private Position position;
public Fire(Position position){
this.position = position;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
}
\ No newline at end of file
package model;
import util.Position;
public class FireFighter {
private Position position;
public FireFighter(Position position){
this.position = position;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
}
\ No newline at end of file
package model; package model;
import util.Position; import util.Position;
import util.TargetStrategy;
import java.util.*; import java.util.*;
...@@ -10,59 +11,110 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -10,59 +11,110 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private final int rowCount; private final int rowCount;
private final int initialFireCount; private final int initialFireCount;
private final int initialFirefighterCount; private final int initialFirefighterCount;
private final int initialCloudCount;
private final TargetStrategy targetStrategy = new TargetStrategy(); private final TargetStrategy targetStrategy = new TargetStrategy();
private List<Position> firefighterPositions; private List<FireFighter> firefighters;
private Set<Position> firePositions; private Map<Position, Fire> fires = new HashMap<>();
private Map<Position, List<Position>> neighbors = new HashMap(); private List<Cloud> clouds;
private final Map<Position, List<Position>> neighbors = new HashMap<>();
private final Position[][] positions; private final Position[][] positions;
private int step = 0; private int step = 0;
private final Random randomGenerator = new Random(); private final Random randomGenerator = new Random();
public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) { public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount,int initialCloudCount) {
this.columnCount = columnCount; this.columnCount = columnCount;
this.rowCount = rowCount; this.rowCount = rowCount;
this.positions = new Position[rowCount][columnCount]; this.positions = new Position[rowCount][columnCount];
for (int column = 0; column < columnCount; column++)
for (int row = 0; row < rowCount; row++) initializePositionsAndNeighbors();
this.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount;
this.initialCloudCount = initialCloudCount;
initializeElements();
}
private void initializePositionsAndNeighbors() {
for (int column = 0; column < columnCount; column++) {
for (int row = 0; row < rowCount; row++) {
positions[row][column] = new Position(row, column); positions[row][column] = new Position(row, column);
for (int column = 0; column < columnCount; column++) }
}
for (int column = 0; column < columnCount; column++) {
for (int row = 0; row < rowCount; row++) { for (int row = 0; row < rowCount; row++) {
neighbors.put(positions[row][column], calculateNeighbors(row, column));
}
}
}
private List<Position> calculateNeighbors(int row, int column) {
List<Position> list = new ArrayList<>(); List<Position> list = new ArrayList<>();
if (row > 0) list.add(positions[row - 1][column]); if (row > 0) list.add(positions[row - 1][column]);
if (column > 0) list.add(positions[row][column - 1]); if (column > 0) list.add(positions[row][column - 1]);
if (row < rowCount - 1) list.add(positions[row + 1][column]); if (row < rowCount - 1) list.add(positions[row + 1][column]);
if (column < columnCount - 1) list.add(positions[row][column + 1]); if (column < columnCount - 1) list.add(positions[row][column + 1]);
neighbors.put(positions[row][column], list); return list;
}
this.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount;
initializeElements();
} }
public void initializeElements() { public void initializeElements() {
firefighterPositions = new ArrayList<>(); firefighters = new ArrayList<>();
firePositions = new HashSet<>(); clouds = new ArrayList<>();
for (int index = 0; index < initialFireCount; index++) fires.clear();
firePositions.add(randomPosition());
for (int index = 0; index < initialFirefighterCount; index++) for (int index = 0; index < initialFireCount; index++) {
firefighterPositions.add(randomPosition()); Position position = randomPosition();
fires.put(position, new Fire(position));
}
for (int index = 0; index < initialFirefighterCount; index++) {
firefighters.add(new FireFighter(randomPosition()));
}
for (int index = 0; index < initialCloudCount; index++) {
clouds.add(new Cloud(randomPosition()));
}
} }
private Position randomPosition() { private Position randomPosition() {
return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount)); return positions[randomGenerator.nextInt(rowCount)][randomGenerator.nextInt(columnCount)];
} }
@Override @Override
public List<ModelElement> getState(Position position) { public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>(); List<ModelElement> result = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions)
if (firefighterPosition.equals(position)) for (FireFighter firefighter : firefighters) {
result.add(ModelElement.FIREFIGHTER); if (firefighter.getPosition().equals(position)) result.add(ModelElement.FIREFIGHTER);
if (firePositions.contains(position)) }
result.add(ModelElement.FIRE); if (fires.containsKey(position)) result.add(ModelElement.FIRE);
for (Cloud cloud : clouds) {
if (cloud.getPosition().equals(position)) result.add(ModelElement.CLOUD);
}
return result; return result;
} }
private List<Position> updateClouds() {
List<Position> modifiedPositions = new ArrayList<>();
for (Cloud cloud : clouds) {
Position currentPosition = cloud.getPosition();
Position newPosition = randomNeighbor(currentPosition);
cloud.setPosition(newPosition);
modifiedPositions.add(currentPosition);
modifiedPositions.add(newPosition);
extinguish(newPosition);
}
return modifiedPositions;
}
private Position randomNeighbor(Position position) {
List<Position> neighborPositions = neighbors.get(position);
return neighborPositions.get(randomGenerator.nextInt(neighborPositions.size()));
}
@Override @Override
public int rowCount() { public int rowCount() {
return rowCount; return rowCount;
...@@ -75,50 +127,57 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -75,50 +127,57 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
public List<Position> updateToNextGeneration() { public List<Position> updateToNextGeneration() {
List<Position> modifiedPositions = updateFirefighters(); List<Position> modifiedPositions = updateFirefighters();
modifiedPositions.addAll(updateClouds());
modifiedPositions.addAll(updateFires()); modifiedPositions.addAll(updateFires());
step++; step++;
return modifiedPositions; return modifiedPositions;
} }
private List<Position> updateFires() { private List<Position> updateFires() {
List<Position> modifiedPositions = new ArrayList<>(); List<Position> modifiedPositions = new ArrayList<>();
if (step % 2 == 0) { if (step % 2 == 0) {
List<Position> newFirePositions = new ArrayList<>(); Set<Position> newFirePositions = new HashSet<>();
for (Position fire : firePositions) { for (Position fire : new HashSet<>(fires.keySet())) {
newFirePositions.addAll(neighbors.get(fire)); newFirePositions.addAll(neighbors.get(fire));
} }
firePositions.addAll(newFirePositions); for (Position position : newFirePositions) {
modifiedPositions.addAll(newFirePositions); if (!fires.containsKey(position)) {
fires.put(position, new Fire(position));
modifiedPositions.add(position);
}
}
} }
return modifiedPositions; return modifiedPositions;
} }
@Override @Override
public int stepNumber() { public int stepNumber() {
return step; return step;
} }
private List<Position> updateFirefighters() { private List<Position> updateFirefighters() {
List<Position> modifiedPosition = new ArrayList<>(); List<Position> modifiedPositions = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>(); for (FireFighter firefighter : firefighters) {
for (Position firefighterPosition : firefighterPositions) { Position currentPosition = firefighter.getPosition();
Position newFirefighterPosition = Position newPosition = targetStrategy.neighborClosestToFire(currentPosition, fires.keySet(), neighbors);
targetStrategy.neighborClosestToFire(firefighterPosition,
firePositions, neighbors); firefighter.setPosition(newPosition);
firefighterNewPositions.add(newFirefighterPosition); modifiedPositions.add(currentPosition);
extinguish(newFirefighterPosition); modifiedPositions.add(newPosition);
modifiedPosition.add(firefighterPosition);
modifiedPosition.add(newFirefighterPosition); extinguish(newPosition);
List<Position> neighborFirePositions = neighbors.get(newFirefighterPosition).stream()
.filter(firePositions::contains).toList(); for (Position neighbor : neighbors.get(newPosition)) {
for (Position firePosition : neighborFirePositions) extinguish(neighbor);
extinguish(firePosition); modifiedPositions.add(neighbor);
modifiedPosition.addAll(neighborFirePositions); }
}
firefighterPositions = firefighterNewPositions;
return modifiedPosition;
} }
return modifiedPositions;
}
@Override @Override
public void reset() { public void reset() {
...@@ -127,20 +186,21 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -127,20 +186,21 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
private void extinguish(Position position) { private void extinguish(Position position) {
firePositions.remove(position); if (fires.containsKey(position)) {
fires.remove(position);
}
} }
@Override @Override
public void setState(List<ModelElement> state, Position position) { public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position); extinguish(position);
for (; ; ) { firefighters.removeIf(firefighter -> firefighter.getPosition().equals(position));
if (!firefighterPositions.remove(position)) break;
}
for (ModelElement element : state) { for (ModelElement element : state) {
switch (element) { switch (element) {
case FIRE -> firePositions.add(position); case FIRE -> fires.put(position, new Fire(position));
case FIREFIGHTER -> firefighterPositions.add(position); case FIREFIGHTER -> firefighters.add(new FireFighter(position));
} }
} }
} }
......
package model; package model;
public enum ModelElement { public enum ModelElement {
FIREFIGHTER, FIRE FIREFIGHTER,
FIRE,
CLOUD;
} }
package model;
import util.Position;
public class Road {
private final Position position;
public Road(Position position){
this.position = position;
}
public Position getPosition(){
return this.position;
}
}
...@@ -4,5 +4,6 @@ module firefighter { ...@@ -4,5 +4,6 @@ module firefighter {
requires javafx.graphics; requires javafx.graphics;
opens controller to javafx.fxml; opens controller to javafx.fxml;
exports app; exports app;
exports view;
opens app to javafx.fxml; opens app to javafx.fxml;
} }
package model; package util;
import util.Position; import util.Position;
...@@ -12,7 +12,7 @@ public class TargetStrategy { ...@@ -12,7 +12,7 @@ public class TargetStrategy {
* @param targets positions that are targeted. * @param targets positions that are targeted.
* @return the position next to the current position that is on the path to the closest target. * @return the position next to the current position that is on the path to the closest target.
*/ */
Position neighborClosestToFire(Position position, Collection<Position> targets, public Position neighborClosestToFire(Position position, Collection<Position> targets,
Map<Position,List<Position>>neighbors) { Map<Position,List<Position>>neighbors) {
Set<Position> seen = new HashSet<Position>(); Set<Position> seen = new HashSet<Position>();
HashMap<Position, Position> firstMove = new HashMap<Position, Position>(); HashMap<Position, Position> firstMove = new HashMap<Position, Position>();
......
...@@ -10,8 +10,10 @@ import java.util.List; ...@@ -10,8 +10,10 @@ import java.util.List;
public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
private void paintElementAtPosition(ViewElement element, Position position) { private void paintElementAtPosition(ViewElement element, Position position) {
System.out.println("Painting " + element + " at " + position);
paintBox(position.row(), position.column(), element.color); paintBox(position.row(), position.column(), element.color);
} }
private int boxWidth; private int boxWidth;
private int boxHeight; private int boxHeight;
private int columnCount; private int columnCount;
......
...@@ -3,7 +3,7 @@ package view; ...@@ -3,7 +3,7 @@ package view;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
public enum ViewElement { public enum ViewElement {
FIREFIGHTER(Color.BLUE), FIRE(Color.RED), EMPTY(Color.WHITE); FIREFIGHTER(Color.BLUE), FIRE(Color.RED),CLOUD(Color.LIGHTSKYBLUE), EMPTY(Color.WHITE);
final Color color; final Color color;
ViewElement(Color color) { ViewElement(Color color) {
this.color = color; this.color = color;
......
...@@ -10,17 +10,17 @@ import static org.assertj.core.api.Assertions.*; ...@@ -10,17 +10,17 @@ import static org.assertj.core.api.Assertions.*;
public class FirefighterBoardTest { public class FirefighterBoardTest {
@Test @Test
void testColumnCount(){ void testColumnCount(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3,1);
assertThat(board.columnCount()).isEqualTo(20); assertThat(board.columnCount()).isEqualTo(20);
} }
@Test @Test
void testRowCount(){ void testRowCount(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3,1);
assertThat(board.rowCount()).isEqualTo(10); assertThat(board.rowCount()).isEqualTo(10);
} }
@Test @Test
void testStepNumber(){ void testStepNumber(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3); Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3,1);
for(int index = 0; index < 10; index++){ for(int index = 0; index < 10; index++){
assertThat(board.stepNumber()).isEqualTo(index); assertThat(board.stepNumber()).isEqualTo(index);
board.updateToNextGeneration(); board.updateToNextGeneration();
...@@ -29,7 +29,7 @@ public class FirefighterBoardTest { ...@@ -29,7 +29,7 @@ public class FirefighterBoardTest {
} }
@Test @Test
void testGetState_afterSet(){ void testGetState_afterSet(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 0, 0); Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 0, 0,1);
Position position = new Position(1,2); Position position = new Position(1,2);
assertThat(board.getState(position)).isEmpty(); assertThat(board.getState(position)).isEmpty();
board.setState(List.of(ModelElement.FIRE), position); board.setState(List.of(ModelElement.FIRE), position);
......