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
Loading items

Target

Select target project
  • l22015417/firefighterstarter
  • 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
24 results
Select Git revision
Loading items
Show changes
Showing
with 881 additions and 74 deletions
.background {
-fx-background-color: #1d1d1d;
}
.label {
-fx-font-size: 11pt;
-fx-font-family: "Segoe UI Semibold";
-fx-text-fill: white;
-fx-opacity: 0.6;
}
.label-bright {
-fx-font-size: 11pt;
-fx-font-family: "Segoe UI Semibold";
-fx-text-fill: white;
-fx-opacity: 1;
}
.label-header {
-fx-font-size: 32pt;
-fx-font-family: "Segoe UI Light";
-fx-text-fill: white;
-fx-opacity: 1;
}
.table-view {
-fx-base: #1d1d1d;
-fx-control-inner-background: #1d1d1d;
-fx-background-color: #1d1d1d;
-fx-table-cell-border-color: transparent;
-fx-table-header-border-color: transparent;
-fx-padding: 5;
}
.table-view .column-header-background {
-fx-background-color: transparent;
}
.table-view .column-header, .table-view .filler {
-fx-border-width: 0 0 1 0;
-fx-background-color: transparent;
-fx-border-color:
transparent
transparent
derive(-fx-base, 80%)
transparent;
-fx-border-insets: 0 10 1 0;
}
.table-view .column-header .label {
-fx-font-size: 20pt;
-fx-font-family: "Segoe UI Light";
-fx-text-fill: white;
-fx-alignment: center-left;
-fx-opacity: 1;
}
.table-view:focused .table-row-cell:filled:focused:selected {
-fx-background-color: -fx-focus-color;
}
.split-pane:horizontal > .split-pane-divider {
-fx-border-color: transparent #1d1d1d transparent #1d1d1d;
-fx-background-color: transparent, derive(#1d1d1d,20%);
}
.split-pane {
-fx-padding: 1 0 0 0;
}
.menu-bar {
-fx-background-color: derive(#1d1d1d,20%);
}
.context-menu {
-fx-background-color: derive(#1d1d1d,50%);
}
.menu-bar .label {
-fx-font-size: 14pt;
-fx-font-family: "Segoe UI Light";
-fx-text-fill: white;
-fx-opacity: 0.9;
}
.menu .left-container {
-fx-background-color: black;
}
.text-field {
-fx-font-size: 12pt;
-fx-font-family: "Segoe UI Semibold";
}
/*
* Metro style Push Button
* Author: Pedro Duque Vieira
* http://pixelduke.wordpress.com/2012/10/23/jmetro-windows-8-controls-on-java/
*/
.button {
-fx-padding: 5 22 5 22;
-fx-border-color: #e2e2e2;
-fx-border-width: 2;
-fx-background-radius: 0;
-fx-background-color: #1d1d1d;
-fx-font-family: "Segoe UI", Helvetica, Arial, sans-serif;
-fx-font-size: 11pt;
-fx-text-fill: #d8d8d8;
-fx-background-insets: 0 0 0 0, 0, 1, 2;
}
.button:hover {
-fx-background-color: #3a3a3a;
}
.button:pressed, .button:default:hover:pressed {
-fx-background-color: white;
-fx-text-fill: #1d1d1d;
}
.button:focused {
-fx-border-color: white, white;
-fx-border-width: 1, 1;
-fx-border-style: solid;
-fx-border-radius: 0, 0;
-fx-border-insets: 1 1 1 1, 0;
}
.button:disabled, .button:default:disabled {
-fx-opacity: 0.4;
-fx-background-color: #1d1d1d;
-fx-text-fill: white;
}
.button:default {
-fx-background-color: -fx-focus-color;
-fx-text-fill: #ffffff;
}
.button:default:hover {
-fx-background-color: derive(-fx-focus-color,30%);
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import view.FirefighterGrid?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Label?>
<HBox styleClass="background" stylesheets="@DarkTheme.css"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="controller.Controller" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity">
<VBox>
<Separator maxHeight="-Infinity" maxWidth="-Infinity"
prefHeight="24.0" prefWidth="200.0"/>
<Label maxHeight="-Infinity" maxWidth="-Infinity" alignment="CENTER" prefHeight="24.0" prefWidth="200.0"
text="Generation number"/>
<Label fx:id="generationNumberLabel" alignment="CENTER" contentDisplay="TEXT_ONLY"
maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="24.0" prefWidth="200.0"/>
<Separator maxHeight="-Infinity" maxWidth="-Infinity"
prefHeight="24.0" prefWidth="200.0"/>
<Button fx:id="restartButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#restartButtonAction" prefHeight="24.0" prefWidth="200.0"
text="Restart"/>
<Button fx:id="oneStepButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#oneStepButtonAction" prefHeight="24.0" prefWidth="200.0"
text="One step"/>
<ToggleButton fx:id="playToggleButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#playToggleButtonAction" prefHeight="24.0"
prefWidth="200.0" styleClass="button" text="Play"/>
<ToggleButton fx:id="pauseToggleButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0"
prefWidth="200.0" styleClass="button" text="Pause"/>
</VBox>
<FirefighterGrid fx:id="grid"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml">
</FirefighterGrid>
</HBox>
File added
......@@ -17,8 +17,11 @@ public class SimulatorApplication extends javafx.application.Application {
private static final int COLUMN_COUNT = 20;
private static final int BOX_WIDTH = 50;
private static final int BOX_HEIGHT = 50;
public static final int INITIAL_FIRE_COUNT = 3;
public static final int INITIAL_FIREFIGHTER_COUNT = 6;
public static final int INITIAL_FIRE_COUNT = 10;
public static final int INITIAL_FIREFIGHTER_COUNT = 3;
public static final int INITIAL_CLOUD_COUNT = 3;
public static final int INITIAL_MOTORIZED_COUNT = 3;
public static final int INITIAL_ROCKY_COUNT = 3;
private Stage primaryStage;
private Parent view;
......@@ -44,7 +47,7 @@ public class SimulatorApplication extends javafx.application.Application {
view = loader.load();
Controller controller = loader.getController();
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,INITIAL_MOTORIZED_COUNT,INITIAL_ROCKY_COUNT);
}
private void showScene() {
......
......@@ -4,4 +4,5 @@ public class SimulatorMain {
public static void main(String[] args){
SimulatorApplication.main(args);
}
}
......@@ -12,9 +12,7 @@ import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.util.Duration;
import javafx.util.Pair;
import model.Board;
import model.ModelElement;
import model.FirefighterBoard;
import model.*;
import util.Position;
import view.Grid;
import view.ViewElement;
......@@ -40,7 +38,7 @@ public class Controller {
@FXML
private Grid<ViewElement> grid;
private Timeline timeline;
private Board<List<ModelElement>> board;
private FirefighterBoard board;
@FXML
private void initialize() {
......@@ -59,7 +57,7 @@ public class Controller {
}
private void updateBoard(){
List<Position> updatedPositions = board.updateToNextGeneration();
List<Position> updatedPositions = board.getBehavior().updateToNextGeneration();
List<Pair<Position, ViewElement>> updatedSquares = new ArrayList<>();
for(Position updatedPosition : updatedPositions){
List<ModelElement> squareState = board.getState(updatedPosition);
......@@ -67,30 +65,52 @@ public class Controller {
updatedSquares.add(new Pair<>(updatedPosition, viewElement));
}
grid.repaint(updatedSquares);
updateGenerationLabel(board.stepNumber());
updateGenerationLabel(board.getBehavior().stepNumber());
}
private void repaintGrid(){
int columnCount = board.columnCount();
int rowCount = board.rowCount();
int columnCount = board.getProperties().columnCount();
int rowCount = board.getProperties().rowCount();
ViewElement[][] viewElements = new ViewElement[rowCount][columnCount];
for(int column = 0; column < columnCount; column++)
for(int row = 0; row < rowCount; row++)
viewElements[row][column] = getViewElement(board.getState(new Position(row, column)));
grid.repaint(viewElements);
updateGenerationLabel(board.stepNumber());
updateGenerationLabel(board.getBehavior().stepNumber());
}
private ViewElement getViewElement(List<ModelElement> squareState) {
if(squareState.contains(ModelElement.FIREFIGHTER)){
for (ModelElement element : squareState) {
if (element instanceof FireFighter) {
return ViewElement.FIREFIGHTER;
}
if (squareState.contains(ModelElement.FIRE)){
if (element instanceof Fire) {
return ViewElement.FIRE;
}
if (element instanceof Cloud) {
return ViewElement.CLOUD;
}
if (element instanceof MotorizedFireFighter)
{
return ViewElement.MOTORIZED;
}
if(element instanceof Mountain)
{
return ViewElement.MOUNTAIN;
}
if (element instanceof Road)
{
return ViewElement.ROAD;
}
if (element instanceof Rocky)
{
return ViewElement.ROCKY;
}
}
return ViewElement.EMPTY;
}
private void initializeTimeline() {
Duration duration = new Duration(Controller.PERIOD_IN_MILLISECONDS);
EventHandler<ActionEvent> eventHandler =
......@@ -124,9 +144,9 @@ public class Controller {
}
public void initialize(int squareWidth, int squareHeight, int columnCount,
int rowCount, int initialFireCount, int initialFirefighterCount) {
int rowCount, int initialFireCount, int initialFirefighterCount,int initialCloud,int initialMotorized,int initialRocky) {
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount));
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount,initialCloud,initialMotorized,initialRocky));
repaintGrid();
}
......
......@@ -4,62 +4,23 @@ import util.Position;
import java.util.List;
/**
* This interface represents a generic board for modeling various state-based systems.
*
* @param <S> The type of state represented on the board.
*/
public interface Board<S> {
public interface Board<S> {
/**
* Get the state of the board at a specific position.
* Retourne l'état des éléments situés à une position spécifique du tableau.
*
* @param position The position on the board for which to retrieve the state.
* @return The state at the specified position.
* @param position La position sur le tableau pour laquelle récupérer l'état.
* @return L'état des éléments à la position donnée.
*/
S getState(Position position);
S getState(Position position); // État des éléments sur une position donnée.
/**
* Set the state of a specific position on the board to the specified state.
* Définit l'état des éléments à une position spécifique du tableau.
*
* @param state The state to set for the given position.
* @param position The position on the board for which to set the state.
* @param state L'état à définir pour la position donnée.
* @param position La position sur le tableau pour laquelle définir l'état.
*/
void setState(S state, Position position);
/**
* Get the number of rows in the board.
*
* @return The number of rows in the board.
*/
int rowCount();
/**
* Get the number of columns in the board.
*
* @return The number of columns in the board.
*/
int columnCount();
/**
* Update the board to its next generation or state. This method may modify the
* internal state of the board and return a list of positions that have changed
* during the update.
*
* @return A list of positions that have changed during the update.
*/
List<Position> updateToNextGeneration();
/**
* Reset the board to its initial state.
*/
void reset();
/**
* Get the current step number or generation of the board.
*
* @return The current step number or generation.
*/
int stepNumber();
}
package model;
import util.Position;
import java.util.List;
public interface BoardBehavior<S> {
/**
* Get the current step number or generation of the board.
*
* @return The current step number or generation.
*/
int stepNumber();
/**
* Update the board to its next generation or state. This method may modify the
* internal state of the board and return a list of positions that have changed
* during the update.
*
* @return A list of positions that have changed during the update.
*/
List<Position> updateToNextGeneration();
/**
* Reset the board to its initial state.
*/
void reset();
}
package model;
import util.Position;
public abstract class BoardElement implements ModelElement{
protected Position position;
public BoardElement(Position position) {
this.position = position;
}
@Override
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public abstract String getType(); // Ajout de la méthode abstraite
public abstract String toString();
}
package model;
import util.Position;
import util.TargetStrategy;
import java.util.*;
public class BoardFireFighterBehavior implements BoardBehavior{
private static TargetStrategy targetStrategy = new TargetStrategy();
private static Map<Position, List<Position>> neighbors;
private static List<Position> rocky;
private ElementFactory<Rocky> rockyFactory;
private static Map<Position, Terrain> terrainMap = new HashMap<>();
private static int step;
static Movements fireMovements ;
static Movements fireFighterMovements;
static Movements cloudMovements;
static Movements motorizedMovements;
public BoardFireFighterBehavior(Map<Position, List<Position>> neighbors, ElementFactory<Fire> fireFactory ,ElementFactory<FireFighter> firefighterFactory,
ElementFactory<Cloud> cloudFactory,ElementFactory<MotorizedFireFighter> motorizedFactory,ElementFactory<Rocky> rockyFactory) {
this.step=0;
this.neighbors = neighbors;
this.rockyFactory=rockyFactory;
fireMovements=new FireMovements(fireFactory);
fireFighterMovements=new FireFighterMovements(firefighterFactory);
cloudMovements=new CloudMovements(cloudFactory);
motorizedMovements=new MotorizedMovements(motorizedFactory);
}
public void initializeElements(int rowCount, int columnCount) {
fireMovements.initializeElement(rowCount,columnCount);
fireFighterMovements.initializeElement(rowCount,columnCount);
cloudMovements.initializeElement(rowCount,columnCount);
// Pompiers motorisés
motorizedMovements.initializeElement(rowCount,columnCount);
// Rocky
rocky = new ArrayList<>();
List<Rocky> rockies = rockyFactory.createElements(rowCount, columnCount);
for (Rocky rockyElement : rockies) {
rocky.add(rockyElement.getPosition());
}
generateMountainBlocks(rowCount, columnCount);
generateRoads(rowCount,columnCount);
}
public static void extinguish(Position position) {
fireMovements.getPositions().remove(position);
// Si la position est une montagne, elle cesse d'être une montagne
if (rocky.contains(position)) {
rocky.remove(position);
terrainMap.remove(position); // Optionnel, si vous utilisez terrainMap pour gérer les types de terrain
System.out.println("Une montagne a brûlé à la position : " + position);
}
}
public Set<Position> getFirePositions() {
return (Set<Position>) fireMovements.getPositions();
}
public void incrementStep() {
step++;
}
@Override
public int stepNumber() {
return step;
}
@Override
public List<Position> updateToNextGeneration() {
List<Position> modifiedPositions = fireFighterMovements.updateElements();
modifiedPositions.addAll(fireMovements.updateElements());
modifiedPositions.addAll(cloudMovements.updateElements());
modifiedPositions.addAll(motorizedMovements.updateElements());
incrementStep();
return modifiedPositions;
}
@Override
public void reset() {
this.step=0;
// Vider toutes les positions stockées
fireMovements.getPositions().clear();
fireFighterMovements.getPositions().clear();
cloudMovements.getPositions().clear();
motorizedMovements.getPositions().clear();
rocky.clear();
terrainMap.clear();
}
public Collection<Position> getCloudPositions() {
return cloudMovements.getPositions();
}
public List<Position> getMotorizedFighters() {
return (List<Position>) motorizedMovements.getPositions();
}
private void generateMountainBlocks(int rowCount, int columnCount) {
Random random = new Random();
int maxMountains = 5; // Par exemple, générer 5 blocs de montagnes
int blockSize = 4; // Taille du bloc de montagnes (4 cases)
for (int i = 0; i < maxMountains; i++) {
// Générer un coin aléatoire pour chaque bloc de montagne (en s'assurant que les blocs ne dépassent pas la grille)
int startRow = random.nextInt(rowCount - 1); // -1 pour éviter le débordement
int startCol = random.nextInt(columnCount - 1);
// Vérifier si les cases sont libres avant d'y placer une montagne
if (canPlaceMountainBlock(startRow, startCol)) {
// Placer les montagnes sur la grille
placeMountainBlock(startRow, startCol);
}
}
}
private boolean canPlaceMountainBlock(int startRow, int startCol) {
// Vérifier si les 4 cases sont libres
for (int row = startRow; row < startRow + 2; row++) {
for (int col = startCol; col < startCol + 2; col++) {
Position pos = new Position(row, col);
if (terrainMap.containsKey(pos)) {
return false; // Une case est déjà occupée
}
}
}
return true; // Toutes les cases sont libres
}
private void placeMountainBlock(int startRow, int startCol) {
// Placer les 4 cases de montagne
for (int row = startRow; row < startRow + 2; row++) {
for (int col = startCol; col < startCol + 2; col++) {
Position pos = new Position(row, col);
terrainMap.put(pos, new Mountain(pos));
}
}
}
/**
* Éteint les feux à proximité d'une position donnée.
*
* @param firefighterPosition La position actuelle du pompier.
* @param modifiedPositions Les positions modifiées pendant ce tour.
*/
public static void extinguishNearbyFires(Position firefighterPosition, List<Position> modifiedPositions) {
List<Position> nearbyFires = getNeighbors().getOrDefault(firefighterPosition, Collections.emptyList())
.stream()
.filter(fireMovements.getPositions()::contains)
.toList();
for (Position fire : nearbyFires) {
fireMovements.getPositions().remove(fire);
modifiedPositions.add(fire);
System.out.println("Feu éteint à : " + fire);
}
}
public Map<Position, Terrain> getTerrainMap() {
return terrainMap;
}
public static boolean canMoveTo(Position position, Set<Position> firePositions, List<Position> firefighterPositions) {
// Vérifie si la position est hors des limites (par exemple, en dehors de la grille ou inaccessible)
if (!neighbors.containsKey(position)) {
return false;
}
// Si la position est occupée par un feu et que le terrain n'est pas traversable par le feu
if (firePositions.contains(position)) {
return false; // Impossible de traverser une case contenant un feu
}
// Si la position est occupée par un pompier et que le pompier ne peut pas franchir
if (firefighterPositions.contains(position)) {
return false; // Impossible de se déplacer sur la position d'un autre pompier
}
// Si la position est une montagne, aucun élément ne peut la franchir sauf les nuages
if (terrainMap.get(position) != null && !cloudMovements.getPositions().contains(position)) {
return false; // Impossible de franchir une montagne, sauf pour un nuage
}
if(rocky.contains(position))
{
return Rocky.canFirePropagate();
}
return true; // La position est traversable
}
private void generateRoads(int rowCount, int columnCount) {
Random random = new Random();
// Définir les bords de départ et d'arrivée
int startRow = random.nextInt(rowCount); // Bord gauche
int endRow = random.nextInt(rowCount); // Bord droit
int startCol = 0; // Bord gauche (colonne 0)
int endCol = columnCount - 1; // Bord droit (dernière colonne)
// Générer une route connectant ces deux points
List<Position> path = createPath(new Position(startRow, startCol), new Position(endRow, endCol));
// Placer la route dans la grille
for (Position pos : path) {
if (!terrainMap.containsKey(pos)) { // Éviter les obstacles
terrainMap.put(pos, new Road(pos));
}
}
}
/**
* Génère un chemin connectant deux positions.
*/
private List<Position> createPath(Position start, Position end) {
List<Position> path = new ArrayList<>();
int currentRow = start.row();
int currentCol = start.column();
// Algorithme simple de connexion entre les points (chemin en "zigzag")
while (currentRow != end.row() || currentCol != end.column()) {
path.add(new Position(currentRow, currentCol));
// Mouvement vertical
if (currentRow < end.row()) {
currentRow++;
} else if (currentRow > end.row()) {
currentRow--;
}
// Mouvement horizontal
if (currentCol < end.column()) {
currentCol++;
} else if (currentCol > end.column()) {
currentCol--;
}
}
// Ajouter la position finale
path.add(end);
return path;
}
public static List<Position> getRocky() {
return rocky;
}
public static int getStep() {
return step;
}
public static Map<Position, List<Position>> getNeighbors() {
return neighbors;
}
public static TargetStrategy getTargetStrategy() {
return targetStrategy;
}
public static Collection<Position> getFirefighterPositions() {
return fireFighterMovements.getPositions();
}
}
package model;
import util.Position;
import java.util.List;
import java.util.Map;
public class BoardFirefighterProperties implements BoardProperties{
private final int rowCount;
private final int columnCount;
private final Position[][] positions;
public BoardFirefighterProperties(int rowCount, int columnCount) {
this.rowCount = rowCount;
this.columnCount = columnCount;
this.positions = new Position[rowCount][columnCount];
initializePositions();
}
private void initializePositions() {
for (int row = 0; row < rowCount; row++) {
for (int column = 0; column < columnCount; column++) {
positions[row][column] = new Position(row, column);
}
}
}
public Position getPosition(int row, int column) {
return positions[row][column];
}
@Override
public int rowCount() {
return rowCount;
}
@Override
public int columnCount() {
return columnCount;
}
}
package model;
import util.Position;
import java.util.List;
/**
* Interface représentant les propriétés d'un tableau générique pour la gestion de l'état.
*
* @param <S> Le type d'état utilisé pour les éléments du tableau.
*/
public interface BoardProperties<S> {
/**
* Retourne le nombre de lignes du tableau.
*
* @return Le nombre de lignes.
*/
int rowCount(); // Nombre de lignes.
/**
* Retourne le nombre de colonnes du tableau.
*
* @return Le nombre de colonnes.
*/
int columnCount(); // Nombre de colonnes.
}
package model;
import model.BoardElement;
import util.Position;
public class Cloud extends BoardElement {
public Cloud(Position position) {
super(position);
}
@Override
public String getType() {
return "CLOUD";
}
@Override
public String toString() {
return "Cloud at " + position;
}
}
package model;
import util.Position;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class CloudFactory implements ElementFactory<Cloud>, PositionGenerator {
private final Random random;
private final int count;
public CloudFactory(Random random, int count) {
this.random = random;
this.count = count;
}
@Override
public List<Cloud> createElements(int rowCount, int columnCount) {
List<Cloud> clouds = new ArrayList<>();
for (int i = 0; i < count; i++) {
Position position = generateRandomPosition(rowCount, columnCount);
clouds.add(new Cloud(position));
}
return clouds;
}
@Override
public int getCount() {
return 0;
}
@Override
public Position generateRandomPosition(int rowCount, int columnCount) {
int row = random.nextInt(rowCount);
int col = random.nextInt(columnCount);
return new Position(row, col);
}
}
package model;
import util.Position;
import java.util.*;
import static model.BoardFireFighterBehavior.*;
public class CloudMovements implements Movements{
private List<Position> cloudPositions;
private final ElementFactory<Cloud> cloudFactory;
public CloudMovements(ElementFactory<Cloud> cloudFactory) {
this.cloudPositions = new ArrayList<>();
this.cloudFactory = cloudFactory;
}
@Override
public void initializeElement(int rowCount, int columnCount) {
// Nuages
cloudPositions = new ArrayList<>();
List<Cloud> clouds = cloudFactory.createElements(rowCount, columnCount);
for (Cloud cloud : clouds) {
cloudPositions.add(cloud.getPosition());
}
}
@Override
public List<Position> updateElements() {
List<Position> modifiedPositions = new ArrayList<>();
List<Position> newCloudPositions = new ArrayList<>();
for (Position cloudPosition : cloudPositions) {
// Déplacement aléatoire
List<Position> possibleMoves = getNeighbors().get(cloudPosition);
Position newCloudPosition = possibleMoves.get(new Random().nextInt(possibleMoves.size()));
// Vérification que le nuage ne se déplace pas vers une montagne
if (!canMoveTo(newCloudPosition, (Set<Position>) fireMovements.getPositions(), (List<Position>) fireFighterMovements.getPositions())) {
continue; // Si la position est invalide ou une montagne, le nuage reste sur place
}
newCloudPositions.add(newCloudPosition);
// Éteindre le feu à la position du nuage (le nuage mange le feu)
if (fireMovements.getPositions().contains(newCloudPosition)) {
extinguish(newCloudPosition); // Supprimer le feu
modifiedPositions.add(newCloudPosition); // Ajouter la position du feu éteint aux positions modifiées
System.out.println("Feu éteint par nuage à : " + newCloudPosition);
}
modifiedPositions.add(cloudPosition); // Ajouter l'ancienne position du nuage
modifiedPositions.add(newCloudPosition); // Ajouter la nouvelle position du nuage
}
cloudPositions = newCloudPositions;
return modifiedPositions;
}
@Override
public Collection<Position> getPositions() {
return cloudPositions;
}
}
package model;
import java.util.List;
public interface ElementFactory<T extends ModelElement> {
List<T> createElements(int rowCount, int columnCount);
int getCount();
}
package model;
public interface ElementGenerator {
public void generateElement(int rowCount, int columnCount);
}
package model;
import util.Position;
public class Fire extends BoardElement{
public Fire(Position position) {
super(position);
}
@Override
public String getType() {
return "FIRE";
}
@Override
public String toString() {
return "Fire{" +
"position=" + position +
'}';
}
}
package model;
import util.Position;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class FireFactory implements ElementFactory<Fire>,PositionGenerator{
private final Random random;
private int count;
public FireFactory(Random random, int count) {
this.random = random; // Injecte un générateur aléatoire pour une flexibilité.
this.count=count;
}
/**
* Crée une liste de feux avec des positions et intensités aléatoires.
*
* @param rowCount Nombre de lignes du plateau.
* @param columnCount Nombre de colonnes du plateau.
* @return Une liste d'objets `Fire`.
*/
public List<Fire> createElements(int rowCount, int columnCount) {
List<Fire> fires = new ArrayList<>();
for (int i = 0; i < count; i++) {
Position randomPosition = generateRandomPosition(rowCount, columnCount);
boolean add = fires.add(new Fire(randomPosition));
}
System.out.println(fires.size());
return fires;
}
/**
* Génère une position aléatoire sur le plateau.
*
* @param rowCount Nombre de lignes.
* @param columnCount Nombre de colonnes.
* @return Une position aléatoire.
*/
@Override
public Position generateRandomPosition(int rowCount, int columnCount) {
int row = random.nextInt(rowCount);
int column = random.nextInt(columnCount);
return new Position(row, column);
}
public int getCount() {
return count;
}
}
package model;
import util.Position;
public class FireFighter extends BoardElement{
public FireFighter(Position position) {
super(position);
}
@Override
public String getType() {
return "FIREFIGHTER";
}
@Override
public String toString() {
return "Firefighter at " + position;
}
}