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
  • melissa
  • variant
  • yanis
4 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 (83)
Showing
with 1203 additions and 287 deletions
......@@ -32,3 +32,11 @@ fabric.properties
.idea/caches/build_file_checksums.ser
.gradle/
*.class
bin/
.gradle
*.bin
*.lock
.gradle/8.10.2/executionHistory/executionHistory.bin
build/
\ No newline at end of file
.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.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import view.FirefighterGrid?>
<HBox styleClass="background" stylesheets="@DarkTheme.css"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="controller.Controller">
<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>
diagrams/model.png

77.1 KiB

package app;
import java.io.IOException;
import java.net.URL;
import controller.Controller;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
......@@ -7,21 +10,23 @@ import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
public class SimulatorApplication extends javafx.application.Application {
private static final String VIEW_RESOURCE_PATH = "/view/view.fxml";
private static final String APP_NAME = "Firefighter simulator";
private static final int ROW_COUNT = 20;
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;
private static final int ROW_COUNT = 40;
private static final int COLUMN_COUNT = 40;
private static final int BOX_WIDTH = 15;
private static final int BOX_HEIGHT = 15;
public static final int INITIAL_FIRE_COUNT = 8;
public static final int INITIAL_FIREFIGHTER_COUNT = 6;
public static final int INITIAL_MOTORIZED_FIREFIGHTER_COUNT = 8;
public static final int INITIAL_CLOUD_COUNT = 20;
public static final int INITIAL_MOUNTAIN_COUNT= 20;
public static final int TURNS_FOR_SPAWNING_AIRTANKER = 10;
private Stage primaryStage;
private Parent view;
private void initializePrimaryStage(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle(APP_NAME);
......@@ -31,7 +36,7 @@ public class SimulatorApplication extends javafx.application.Application {
}
@Override
public void start(Stage primaryStage) throws IOException {
public void start(@SuppressWarnings("exports") Stage primaryStage) throws IOException {
initializePrimaryStage(primaryStage);
initializeView();
showScene();
......@@ -44,7 +49,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_MOTORIZED_FIREFIGHTER_COUNT, INITIAL_CLOUD_COUNT, INITIAL_MOUNTAIN_COUNT, TURNS_FOR_SPAWNING_AIRTANKER);
}
private void showScene() {
......
package controller;
import static java.util.Objects.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
......@@ -13,17 +20,20 @@ import javafx.scene.control.ToggleGroup;
import javafx.util.Duration;
import javafx.util.Pair;
import model.Board;
import model.ModelElement;
import model.FirefighterBoard;
import model.Entity;
import model.EntityFactory;
import model.Square;
import model.firefighterscenario.Cloud;
import model.firefighterscenario.Fire;
import model.firefighterscenario.FireFighter;
import model.firefighterscenario.FireFighterScenario;
import model.firefighterscenario.MotorizedFireFighter;
import model.firefighterscenario.Mountain;
import model.firefighterscenario.Rockery;
import util.Position;
import view.Grid;
import view.ViewElement;
import java.util.ArrayList;
import java.util.List;
import static java.util.Objects.requireNonNull;
public class Controller {
public static final int PERIOD_IN_MILLISECONDS = 50;
......@@ -40,7 +50,7 @@ public class Controller {
@FXML
private Grid<ViewElement> grid;
private Timeline timeline;
private Board<List<ModelElement>> board;
private Board<Square> board;
@FXML
private void initialize() {
......@@ -54,15 +64,15 @@ public class Controller {
pauseToggleButton.setSelected(true);
}
private void setModel(FirefighterBoard firefighterBoard) {
this.board = requireNonNull(firefighterBoard, "firefighter.model is null");
private void setModel(Board<Square> board) {
this.board = requireNonNull(board, "board is null");
}
private void updateBoard() {
List<Position> updatedPositions = board.updateToNextGeneration();
List<Pair<Position, ViewElement>> updatedSquares = new ArrayList<>();
for (Position updatedPosition : updatedPositions) {
List<ModelElement> squareState = board.getState(updatedPosition);
Square squareState = board.getStates(updatedPosition);
ViewElement viewElement = getViewElement(squareState);
updatedSquares.add(new Pair<>(updatedPosition, viewElement));
}
......@@ -76,25 +86,28 @@ public class Controller {
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)));
viewElements[row][column] = getViewElement(board.getStates(new Position(row, column)));
grid.repaint(viewElements);
updateGenerationLabel(board.stepNumber());
}
private ViewElement getViewElement(List<ModelElement> squareState) {
if(squareState.contains(ModelElement.FIREFIGHTER)){
return ViewElement.FIREFIGHTER;
private ViewElement getViewElement(Square square) {
if (!square.getEntities().isEmpty()) {
for (Entity entity : square.getEntities()) {
if (entity instanceof Cloud) {
return new ViewElement(((Cloud) entity).getImage());
}else{
}
// Ajoutez ici des cas similaires pour d'autres entités comme Fire, FireFighter, etc.
}
if (squareState.contains(ModelElement.FIRE)){
return ViewElement.FIRE;
}
return ViewElement.EMPTY;
return new ViewElement(square.getViewColor());
}
private void initializeTimeline() {
Duration duration = new Duration(Controller.PERIOD_IN_MILLISECONDS);
EventHandler<ActionEvent> eventHandler =
event -> updateBoard();
EventHandler<ActionEvent> eventHandler = event -> updateBoard();
KeyFrame keyFrame = new KeyFrame(duration, eventHandler);
timeline = new Timeline(keyFrame);
timeline.setCycleCount(Animation.INDEFINITE);
......@@ -124,9 +137,20 @@ 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 initialMotorizedFirefightersCount, int initialcloudCount, int initialmountaincount, int turnsForSpawningAirTanker) {
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount));
Board<Square> model = new FireFighterScenario(columnCount, rowCount);
Map<EntityFactory, Integer> entityCounts = new HashMap<EntityFactory, Integer>();
entityCounts.put((pos, b) -> new Fire(pos), initialFireCount);
entityCounts.put((pos, b) -> new FireFighter(pos,b), initialFirefighterCount);
entityCounts.put((pos, b) -> new MotorizedFireFighter(pos, b), initialMotorizedFirefightersCount);
entityCounts.put((pos, b) -> new Cloud(pos, b), initialcloudCount);
entityCounts.put((pos, b) -> new Mountain(pos), initialmountaincount);
entityCounts.put((pos, b) -> new Rockery(pos), 3);
model.placeInitialEntities(entityCounts);
this.setModel(model);
repaintGrid();
}
......
package model;
import util.Position;
import java.util.List;
import java.util.Map;
import util.Position;
/**
* This interface represents a generic board for modeling various state-based systems.
* This interface represents a generic board for modeling various state-based
* systems.
*
* @param <S> The type of state represented on the board.
*/
......@@ -17,7 +19,7 @@ public interface Board<S> {
* @param position The position on the board for which to retrieve the state.
* @return The state at the specified position.
*/
S getState(Position position);
S getStates(Position position);
/**
* Set the state of a specific position on the board to the specified state.
......@@ -25,7 +27,7 @@ public interface Board<S> {
* @param state The state to set for the given position.
* @param position The position on the board for which to set the state.
*/
void setState(S state, Position position);
void setSquare(S square);
/**
* Get the number of rows in the board.
......@@ -61,5 +63,27 @@ public interface Board<S> {
* @return The current step number or generation.
*/
int stepNumber();
}
public int getStepNumber();
// Le booléen replaceState permet de forcer le remplacement des cases vides
public void setSquare(S square, boolean replaceStates);
public boolean doesPositionExist(Position position);
public void clearCaseFrom(Entity entity, Position position);
public Position getNearestEntity(Position fromPos, Class<?> entityType);
public boolean doesSquareContainEntity(Position squarePos, Class<?> entityType);
public void addEntityAtSquare(Entity entity, Position position);
public void placeInitialEntities(Map<EntityFactory, Integer> entityCounts);
//Return if the position is completely free
public boolean isPositionEmpty(Position position);
//Return if the position is available for the specified priority
public boolean isPositionFree(Position position, int priority);
}
package model;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.paint.Color;
import util.Position;
public class EmptySquare implements Entity {
private Position position;
private final Color viewColor = Color.WHITE;
private int age;
private final int priotity = -1;
public EmptySquare(Position p) {
this.position = p;
this.age = -999;
}
public EmptySquare(Position p, int age) {
this.position = p;
this.age = age;
}
@Override
public List<Position> nextTurn(Board<Square> board) {
return new ArrayList<Position>();
}
@Override
public Position getPosition() {
return position;
}
@Override
public void setPosition(Position p) {
this.position = p;
}
public Color getViewColor() {
return this.viewColor;
}
@Override
public int getAge() {
return this.age;
}
@Override
public void incrementAge() {
age = age + 1;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public int getPriority(){
return this.priotity;
}
}
package model;
import java.util.List;
import javafx.scene.paint.Color;
import util.Position;
public interface Entity {
/**
* Exécute un tour de jeu, en vérifiant les cases adjacentes pour des instances de Fire.
*
* @param b Le plateau de jeu contenant des entités.
* @return Une liste de positions affectées durant le tour (que la vue doit mettre à jour).
*/
public List<Position> nextTurn(Board<Square> board);
public Position getPosition();
public void setPosition(Position p);
public int getAge();
public void setAge(int age);
public void incrementAge();
public Color getViewColor();
public int getPriority();
}
package model;
import util.Position;
public interface EntityFactory {
Entity create(Position position, Board<Square> board);
}
package model;
import util.Matrix;
import util.Position;
public abstract class EntityScenario implements Scenario{
public void initScenario(Matrix<Square> matrix){
for(int x = 0; x < matrix.getRows(); x++){
for(int y = 0; y < matrix.getColumns(); y++){
Square s = new Square(new Position(x, y), new EmptySquare(new Position(x,y)));
matrix.set(x,y, s);
}
}
}
}
package model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import util.Direction;
import util.Position;
public class EntitySpawner {
private final Board<Square> board;
private final Random random = new Random();
public EntitySpawner(Board<Square> board) {
this.board = board;
}
public void spawnEntities(Map<EntityFactory, Integer> entityCounts) {
Map<EntityFactory, Integer> counts = new HashMap<>();
for (EntityFactory factory : entityCounts.keySet()) {
counts.put(factory, 0);
}
int totalEntitiesToPlace = entityCounts.values().stream().mapToInt(Integer::intValue).sum();
int totalEntitiesPlaced = 0;
int chance = 5;
List<Position> positions = generateAllPositions();
while (totalEntitiesPlaced < totalEntitiesToPlace) {
Collections.shuffle(positions);
for (Position pos : positions) {
if (board.getStates(pos).isEmpty()) {
for (EntityFactory factory : entityCounts.keySet()) {
int desiredCount = entityCounts.get(factory);
int currentCount = counts.get(factory);
if (currentCount < desiredCount && random.nextInt(100) < chance) {
Entity entity = factory.create(pos, board);
board.setSquare(new Square(pos, entity));
counts.put(factory, currentCount + 1);
totalEntitiesPlaced++;
if (totalEntitiesPlaced == totalEntitiesToPlace) {
return;
}
break; // Move to the next position
}
}
}
}
// Increase chance after each full traversal
chance = Math.min(chance + 5, 100);
}
}
private List<Position> generateAllPositions() {
List<Position> positions = new ArrayList<>();
for (int x = 0; x < board.rowCount(); x++) {
for (int y = 0; y < board.columnCount(); y++) {
positions.add(new Position(x, y));
}
}
return positions;
}
public static void generateEntitiesInLine(Board<Square> board, Position anchor, EntityFactory entityFactory) {
int xIncrement = 0;
int yIncrement = 0;
// Determine increments based on which coordinate is zero
if (anchor.x() == 0 && anchor.y() >= 0) {
// Starting from the left edge (x == 0), increment x to move right
xIncrement = 1;
} else if (anchor.y() == 0 && anchor.x() >= 0) {
// Starting from the top edge (y == 0), increment y to move down
yIncrement = 1;
} else {
// If neither x nor y is 0, cannot determine direction
throw new IllegalArgumentException("Anchor position must have x or y equal to 0");
}
int x = anchor.x();
int y = anchor.y();
// Continue until we reach the edge of the board
while (board.doesPositionExist(new Position(x, y))) {
Position pos = new Position(x, y);
// Create a new entity for each position
Entity entity = entityFactory.create(pos, board);
entity.setPosition(pos); // Set the position if not already set in the factory
board.addEntityAtSquare(entity, pos);
x += xIncrement;
y += yIncrement;
}
}
public static List<Position> generateEntitiesInRandomLine(Board<Square> board, Position anchor, int maxStepsInDirection, int minimumRoadLength) {
Random random = new Random();
List<Position> path = new ArrayList<>();
int x = anchor.x();
int y = anchor.y();
// Toutes les directions possibles
List<Direction> allDirections = Arrays.asList(Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST);
// Choisir une direction initiale aléatoire
Direction initialDirection = allDirections.get(random.nextInt(allDirections.size()));
path.add(new Position(x, y)); // Ajouter la position de l'ancre au chemin
int roadLength = 1;
// Déterminer la direction interdite (opposée à la direction initiale)
Direction forbiddenDirection = getOppositeDirection(initialDirection);
// Initialiser la direction courante
Direction currentDirection = initialDirection;
int stepsInCurrentDirection = 0;
// Ensemble des directions définitivement exclues (direction initiale)
Set<Direction> permanentlyExcludedDirections = new HashSet<>();
permanentlyExcludedDirections.add(initialDirection);
// Ensemble des directions temporairement exclues (initialement vide)
Set<Direction> temporarilyExcludedDirections = new HashSet<>();
while (true) {
// Calculer la prochaine position dans la direction courante
int nextX = x + getXIncrement(currentDirection);
int nextY = y + getYIncrement(currentDirection);
Position nextPos = new Position(nextX, nextY);
if (board.doesPositionExist(nextPos)) {
// Ajouter la position au chemin
path.add(nextPos);
x = nextX;
y = nextY;
roadLength++;
stepsInCurrentDirection++;
} else {
// La position dans la direction courante est invalide
if (roadLength < minimumRoadLength) {
// Exclure temporairement la direction courante
temporarilyExcludedDirections.add(currentDirection);
// Choisir une nouvelle direction valide
Direction newDirection = chooseNewDirection(allDirections, currentDirection, forbiddenDirection,
permanentlyExcludedDirections, temporarilyExcludedDirections, board, x, y, random);
if (newDirection == null) {
// Aucune direction valide disponible pour atteindre la longueur minimale
break;
}
// Mettre à jour la direction courante
currentDirection = newDirection;
forbiddenDirection = getOppositeDirection(currentDirection);
stepsInCurrentDirection = 0;
continue; // Recommencer avec la nouvelle direction
} else {
// La longueur minimale est atteinte, arrêter la génération
break;
}
}
// Vérifier s'il est temps de changer de direction
if (stepsInCurrentDirection >= maxStepsInDirection) {
// Choisir une nouvelle direction
Direction newDirection = chooseNewDirection(allDirections, currentDirection, forbiddenDirection,
permanentlyExcludedDirections, temporarilyExcludedDirections, board, x, y, random);
if (newDirection == null) {
// Aucune direction valide disponible
break;
}
// Mettre à jour la direction courante
currentDirection = newDirection;
forbiddenDirection = getOppositeDirection(currentDirection);
stepsInCurrentDirection = 0;
}
}
return path; // Retourner la liste des positions formant le serpent
}
/**
* Choisit une nouvelle direction valide en tenant compte des exclusions permanentes et temporaires.
*
* @param allDirections Toutes les directions possibles.
* @param currentDirection La direction actuelle.
* @param forbiddenDirection La direction opposée à la direction actuelle (interdite).
* @param permanentlyExcludedDirections Les directions définitivement exclues.
* @param temporarilyExcludedDirections Les directions temporairement exclues.
* @param board Le plateau de jeu.
* @param x La coordonnée X actuelle.
* @param y La coordonnée Y actuelle.
* @param random Une instance de Random pour le choix aléatoire.
* @return La nouvelle direction choisie ou null si aucune direction valide n'est disponible.
*/
private static Direction chooseNewDirection(List<Direction> allDirections, Direction currentDirection, Direction forbiddenDirection,
Set<Direction> permanentlyExcludedDirections, Set<Direction> temporarilyExcludedDirections,
Board<Square> board, int x, int y, Random random) {
// Créer une liste de directions valides en excluant :
// - La direction actuelle
// - La direction interdite (opposée à la direction actuelle)
// - Les directions définitivement exclues
// - Les directions temporairement exclues
List<Direction> validDirections = new ArrayList<>(allDirections);
validDirections.remove(currentDirection);
validDirections.remove(forbiddenDirection);
validDirections.removeAll(permanentlyExcludedDirections);
validDirections.removeAll(temporarilyExcludedDirections);
// Filtrer les directions qui permettent de continuer le chemin
validDirections.removeIf(dir -> !board.doesPositionExist(new Position(x + getXIncrement(dir), y + getYIncrement(dir))));
if (validDirections.isEmpty()) {
// Aucune direction valide disponible
return null;
}
// Choisir une nouvelle direction aléatoirement
return validDirections.get(random.nextInt(validDirections.size()));
}
private static int getXIncrement(Direction direction) {
switch (direction) {
case NORTH: return -1;
case SOUTH: return 1;
default: return 0;
}
}
private static int getYIncrement(Direction direction) {
switch (direction) {
case EAST: return 1;
case WEST: return -1;
default: return 0;
}
}
private static Direction getOppositeDirection(Direction direction) {
switch (direction) {
case NORTH: return Direction.SOUTH;
case SOUTH: return Direction.NORTH;
case EAST: return Direction.WEST;
case WEST: return Direction.EAST;
default: throw new IllegalArgumentException("Direction non supportée : " + direction);
}
}
}
package model;
import util.Position;
import java.util.*;
public class FirefighterBoard implements Board<List<ModelElement>> {
private final int columnCount;
private final int rowCount;
private final int initialFireCount;
private final int initialFirefighterCount;
private final TargetStrategy targetStrategy = new TargetStrategy();
private List<Position> firefighterPositions;
private Set<Position> firePositions;
private Map<Position, List<Position>> neighbors = new HashMap();
private final Position[][] positions;
private int step = 0;
private final Random randomGenerator = new Random();
public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) {
this.columnCount = columnCount;
this.rowCount = rowCount;
this.positions = new Position[rowCount][columnCount];
for (int column = 0; column < columnCount; column++)
for (int row = 0; row < rowCount; row++)
positions[row][column] = new Position(row, column);
for (int column = 0; column < columnCount; column++)
for (int row = 0; row < rowCount; row++) {
List<Position> list = new ArrayList<>();
if (row > 0) list.add(positions[row - 1][column]);
if (column > 0) list.add(positions[row][column - 1]);
if (row < rowCount - 1) list.add(positions[row + 1][column]);
if (column < columnCount - 1) list.add(positions[row][column + 1]);
neighbors.put(positions[row][column], list);
}
this.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount;
initializeElements();
}
public void initializeElements() {
firefighterPositions = new ArrayList<>();
firePositions = new HashSet<>();
for (int index = 0; index < initialFireCount; index++)
firePositions.add(randomPosition());
for (int index = 0; index < initialFirefighterCount; index++)
firefighterPositions.add(randomPosition());
}
private Position randomPosition() {
return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount));
}
@Override
public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions)
if (firefighterPosition.equals(position))
result.add(ModelElement.FIREFIGHTER);
if (firePositions.contains(position))
result.add(ModelElement.FIRE);
return result;
}
@Override
public int rowCount() {
return rowCount;
}
@Override
public int columnCount() {
return columnCount;
}
public List<Position> updateToNextGeneration() {
List<Position> modifiedPositions = updateFirefighters();
modifiedPositions.addAll(updateFires());
step++;
return modifiedPositions;
}
private List<Position> updateFires() {
List<Position> modifiedPositions = new ArrayList<>();
if (step % 2 == 0) {
List<Position> newFirePositions = new ArrayList<>();
for (Position fire : firePositions) {
newFirePositions.addAll(neighbors.get(fire));
}
firePositions.addAll(newFirePositions);
modifiedPositions.addAll(newFirePositions);
}
return modifiedPositions;
}
@Override
public int stepNumber() {
return step;
}
private List<Position> updateFirefighters() {
List<Position> modifiedPosition = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition =
targetStrategy.neighborClosestToFire(firefighterPosition,
firePositions, neighbors);
firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition);
modifiedPosition.add(firefighterPosition);
modifiedPosition.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors.get(newFirefighterPosition).stream()
.filter(firePositions::contains).toList();
for (Position firePosition : neighborFirePositions)
extinguish(firePosition);
modifiedPosition.addAll(neighborFirePositions);
}
firefighterPositions = firefighterNewPositions;
return modifiedPosition;
}
@Override
public void reset() {
step = 0;
initializeElements();
}
private void extinguish(Position position) {
firePositions.remove(position);
}
@Override
public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position);
for (; ; ) {
if (!firefighterPositions.remove(position)) break;
}
for (ModelElement element : state) {
switch (element) {
case FIRE -> firePositions.add(position);
case FIREFIGHTER -> firefighterPositions.add(position);
}
}
}
}
\ No newline at end of file
package model;
public enum ModelElement {
FIREFIGHTER, FIRE
}
package model;
import javafx.scene.paint.Color;
import util.Position;
import java.util.List;
public class Road implements Entity{
private int age;
private Position position;
private final Color viewColor = Color.GREY;
private final int priority = 0;
@Override
public List<Position> nextTurn(Board<Square> board) {
return List.of();
}
@Override
public Position getPosition() {
return null;
}
@Override
public void setPosition(Position p) {
}
@Override
public int getAge() {
return 0;
}
@Override
public void setAge(int age) {
}
@Override
public void incrementAge() {
}
@Override
public Color getViewColor() {
return null;
}
@Override
public int getPriority() {
return 0;
}
}
package model;
import util.Matrix;
public interface Scenario {
public void initScenario(Matrix<Square> matrix);
}
\ No newline at end of file
package model;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.paint.Color;
import util.Position;
public class Square {
private List<Entity> entities;
private Position position;
public Square(Position position){
this.entities = new ArrayList<Entity>();
this.position = position;
}
public Square(Position position, Entity entity){
this.entities = new ArrayList<Entity>();
this.entities.add(entity);
this.position = position;
}
public List<Entity> getEntities(){
return this.entities;
}
public Position getPosition(){
return this.position;
}
public void addEntity(Entity entity){
entities.add(entity);
}
public void removeEntity(Entity entity){
entities.remove(entity);
}
public void setEntities(List<Entity> entities){
this.entities = entities;
}
public boolean isEmpty(){
return entities.isEmpty() ||(entities.size() == 1 && entities.get(0) instanceof EmptySquare);
}
public int getMinimalAge(){
int minimalAge = 0;
for(Entity e : entities){
if(e.getAge() < minimalAge){
minimalAge = e.getAge();
}
}
return minimalAge;
}
public int getMaxAge(){
int maxAge = 0;
for(Entity e : entities){
if(e.getAge() > maxAge){
maxAge = e.getAge();
}
}
return maxAge;
}
public void incrementAllAges(){
for(Entity e : entities){
e.incrementAge();
}
}
public Color getViewColor() {
if (entities.isEmpty()) {
return Color.WHITE;
} else {
int maxPriority = -1; // Assuming priorities are non-negative
Color c = Color.WHITE;
// Iterate over entities to find the one with the highest priority
for (Entity e : entities) {
// Check for null entities if necessary
if (e != null && e.getPriority() > maxPriority) {
maxPriority = e.getPriority();
c = e.getViewColor();
}
}
return c;
}
}
}
package model.firefighterscenario;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javafx.scene.paint.Color;
import model.Board;
import model.Entity;
import model.Square;
import util.Direction;
import util.Position;
import util.PositionUtil;
public class AirTanker implements Entity{
private final Color viewColor = Color.GOLD;
private Direction direction; // Direction in which the AirTanker moves
private int age;
private Position position;
private int priority = 3;
public AirTanker(Position position, Board<Square> b, int age) {
this.age = age;
this.position = position;
determineDirection(b);
extinguish(position,b);
}
@Override
public int getPriority() {
return this.priority;
}
private void determineDirection(Board<Square> b) {
int rowCount = b.rowCount();
int columnCount = b.columnCount();
if (position.x() == 0) {
direction = Direction.EAST; // Move east if on left edge
} else if (position.x() == columnCount - 1) {
direction = Direction.WEST; // Move west if on right edge
} else if (position.y() == 0) {
direction = Direction.SOUTH; // Move south if on top edge
} else if (position.y() == rowCount - 1) {
direction = Direction.NORTH; // Move north if on bottom edge
} else {
// Not on an edge; default to moving east or choose a random direction
direction = Direction.EAST;
}
}
@Override
public List<Position> nextTurn(Board<Square> b) {
List<Position> positions = new ArrayList<>();
// Move in the determined direction
Position nextPos = getNextPosition(position, direction, b);
if (nextPos == null || !b.doesPositionExist(nextPos)) {
// Reached the edge; remove AirTanker from the board
b.clearCaseFrom(this, position);
positions.add(position);
return positions;
} else {
// Extinguish fires within a 3-square radius
positions.addAll(extinguish(position, b));
// Move to the next position
b.clearCaseFrom(this, position);
positions.add(new Position(position.x(), position.y())); // Old position
this.position = nextPos;
b.addEntityAtSquare(this, nextPos);
positions.add(nextPos); // New position
return positions;
}
}
protected List<Position> extinguish(Position position, Board<Square> b){
List<Position> positions = new ArrayList<Position>();
List<Position> positionsInRange = PositionUtil.getPositionsInRadius(position, 3, b);
for (Position p : positionsInRange) {
if (b.doesSquareContainEntity(p, Fire.class)) {
b.getStates(p).getEntities().removeIf(e -> e instanceof Fire);
positions.add(p); // Add position where fire was extinguished
}
}
return positions;
}
private Position getNextPosition(Position currentPosition, Direction direction, Board<Square> b) {
int x = currentPosition.x();
int y = currentPosition.y();
switch (direction) {
case NORTH:
y -= 1;
break;
case SOUTH:
y += 1;
break;
case EAST:
x += 1;
break;
case WEST:
x -= 1;
break;
default:
break; // Only handling cardinal directions
}
Position newPos = new Position(x, y);
if (b.doesPositionExist(newPos)) {
return newPos;
} else {
return null; // Reached the edge
}
}
public Direction getDirection(){
return this.direction;
}
@Override
public void setPosition(Position p) {
this.position = p;
}
@Override
public Position getPosition() {
return this.position;
}
@Override
public Color getViewColor() {
return this.viewColor;
}
@Override
public int getAge() {
return this.age;
}
@Override
public void incrementAge() {
this.age++;
}
@Override
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true; // Check if same object
if (obj == null || getClass() != obj.getClass()) return false; // Check for null and class match
AirTanker airTanker = (AirTanker) obj; // Cast to Fire
return age == airTanker.age && position.equals(airTanker.position); // Compare age and position
}
@Override
public int hashCode() {
return Objects.hash(position, age);
}
}
package model.firefighterscenario;
import java.util.List;
import java.util.Random;
import javafx.scene.paint.Color;
import model.Board;
import model.EmptySquare;
import model.Entity;
import model.Square;
import util.Position;
import util.PositionUtil;
public class Cloud implements Entity{
private int age;
private Position position;
private final Color viewColor = Color.GRAY;
private final int priority = 2;
public Cloud(Position position, Board<Square> b){
this.age = 0;
this.position = position;
}
public Cloud(Position position, Board<Square> b, int age){
this.age = age;
this.position = position;
}
@Override
public List<Position> nextTurn(Board<Square> b) {
List<Position> adjacentPositions = PositionUtil.generateAdjacentPositions(position, b);
adjacentPositions.removeIf( p -> b.doesSquareContainEntity(p, Cloud.class));
adjacentPositions.removeIf( p -> b.doesSquareContainEntity(p, Mountain.class));
// Filtrer pour obtenir uniquement les positions qui ne contiennent pas de pompier
adjacentPositions.removeIf(p -> b.doesSquareContainEntity(p, FireFighter.class));
// Choisir une position aléatoire parmi les mouvements possibles
if(adjacentPositions.size() < 0){
return List.of();
}
Position next_position = adjacentPositions.get(new Random().nextInt(adjacentPositions.size()));
// Si la nouvelle position contient un feu, éteindre le feu
if (b.getStates(next_position).getEntities().stream().anyMatch(element -> element instanceof Fire) ){
extinguish(next_position, b);
}
Position old_position = this.position;
this.position = next_position;
b.addEntityAtSquare(this, next_position);
b.clearCaseFrom(this, old_position);
return List.of(old_position, this.position);
}
private Position extinguish(Position p, Board<Square> b) {
b.getStates(p).getEntities().removeIf(element -> element instanceof Fire);
List<Entity> entities = b.getStates(p).getEntities();
for (Entity e : entities) {
if (e instanceof EmptySquare) {
e.setAge(b.stepNumber() + 1);
}
}
return p;
}
@Override
public Position getPosition() {
return this.position;
}
@Override
public void setPosition(Position p) {
this.position = p;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age) {
this.age =age;
}
@Override
public void incrementAge() {
this.age += 1;
}
@Override
public Color getViewColor() {
return this.viewColor;
}
@Override
public int getPriority(){
return this.priority;
}
public javafx.scene.image.Image getImage() {
return new javafx.scene.image.Image(getClass().getResource("/icons/img.png").toExternalForm());
}
}