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 (11)
Showing
with 403 additions and 50 deletions
......@@ -17,9 +17,7 @@ repositories {
}
dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher") {
because("Only needed to run tests in a version of IntelliJ IDEA that bundles older versions")
}
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.assertj:assertj-core:3.24.2")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
......@@ -30,11 +28,7 @@ test {
}
application {
mainClass.set("firefighter.app.SimulatorMain")
mainClass.set("app.SimulatorMain")
}
tasks {
shadowJar {
exclude("module-info.class")
}
}
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
package firefighter.app;
package app;
import firefighter.controller.Controller;
import controller.Controller;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
......@@ -11,12 +11,12 @@ import java.io.IOException;
import java.net.URL;
public class SimulatorApplication extends javafx.application.Application {
private static final String VIEW_RESOURCE_PATH = "/firefighter/view/view.fxml";
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 SQUARE_WIDTH = 50;
private static final int SQUARE_HEIGHT = 50;
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;
......@@ -26,7 +26,7 @@ public class SimulatorApplication extends javafx.application.Application {
this.primaryStage = primaryStage;
this.primaryStage.setTitle(APP_NAME);
this.primaryStage.setOnCloseRequest(event -> Platform.exit());
this.primaryStage.setResizable(false);
this.primaryStage.setResizable(true);
this.primaryStage.sizeToScene();
}
......@@ -43,7 +43,7 @@ public class SimulatorApplication extends javafx.application.Application {
loader.setLocation(location);
view = loader.load();
Controller controller = loader.getController();
controller.initialize(SQUARE_WIDTH, SQUARE_HEIGHT, COLUMN_COUNT, ROW_COUNT,
controller.initialize(BOX_WIDTH, BOX_HEIGHT, COLUMN_COUNT, ROW_COUNT,
INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT);
}
......
package firefighter.app;
package app;
public class SimulatorMain {
public static void main(String[] args){
......
package firefighter.controller;
package controller;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
......@@ -12,12 +12,12 @@ import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.util.Duration;
import javafx.util.Pair;
import firefighter.model.Board;
import firefighter.model.ModelElement;
import firefighter.model.FirefighterBoard;
import firefighter.util.Position;
import firefighter.view.FirefighterGrid;
import firefighter.view.ViewElement;
import model.Board;
import model.ModelElement;
import model.FirefighterBoard;
import util.Position;
import view.Grid;
import view.ViewElement;
import java.util.ArrayList;
import java.util.List;
......@@ -38,7 +38,7 @@ public class Controller {
@FXML
private ToggleButton playToggleButton;
@FXML
private FirefighterGrid grid;
private Grid<ViewElement> grid;
private Timeline timeline;
private Board<List<ModelElement>> board;
......@@ -67,10 +67,10 @@ public class Controller {
updatedSquares.add(new Pair<>(updatedPosition, viewElement));
}
grid.repaint(updatedSquares);
updateLabel(board.stepNumber());
updateGenerationLabel(board.stepNumber());
}
private void repaintBoard(){
private void repaintGrid(){
int columnCount = board.columnCount();
int rowCount = board.rowCount();
ViewElement[][] viewElements = new ViewElement[rowCount][columnCount];
......@@ -78,7 +78,7 @@ public class Controller {
for(int row = 0; row < rowCount; row++)
viewElements[row][column] = getViewElement(board.getState(new Position(row, column)));
grid.repaint(viewElements);
updateLabel(board.stepNumber());
updateGenerationLabel(board.stepNumber());
}
private ViewElement getViewElement(List<ModelElement> squareState) {
......@@ -120,14 +120,14 @@ public class Controller {
this.pause();
board.reset();
pauseToggleButton.setSelected(true);
repaintBoard();
repaintGrid();
}
public void initialize(int squareWidth, int squareHeight, int columnCount,
int rowCount, int initialFireCount, int initialFirefighterCount) {
grid.initialize(squareWidth, squareHeight, columnCount, rowCount);
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount));
repaintBoard();
repaintGrid();
}
public void oneStepButtonAction() {
......@@ -135,7 +135,7 @@ public class Controller {
updateBoard();
}
private void updateLabel(int value){
private void updateGenerationLabel(int value){
generationNumberLabel.setText(Integer.toString(value));
}
}
\ No newline at end of file
package firefighter.controller;
package controller;
import javafx.collections.ListChangeListener.Change;
import javafx.scene.control.Toggle;
......
package firefighter.model;
package model;
import firefighter.util.Position;
import util.Position;
import java.util.List;
......@@ -19,6 +19,14 @@ public interface Board<S> {
*/
S getState(Position position);
/**
* Set the state of a specific position on the board to the specified state.
*
* @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);
/**
* Get the number of rows in the board.
*
......
package firefighter.model;
package model;
import firefighter.util.Position;
import util.Position;
import java.util.*;
......@@ -10,14 +10,30 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private final int rowCount;
private final int initialFireCount;
private final int initialFirefighterCount;
List<Position> firefighterPositions;
Set<Position> firePositions;
List<Position> firefighterNewPositions;
int step = 0;
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();
......@@ -33,7 +49,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
private Position randomPosition() {
return new Position((int) (Math.random() * rowCount), (int) (Math.random() * columnCount));
return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount));
}
@Override
......@@ -58,23 +74,23 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
public List<Position> updateToNextGeneration() {
List<Position> result = activateFirefighters();
result.addAll(activateFires());
List<Position> modifiedPositions = updateFirefighters();
modifiedPositions.addAll(updateFires());
step++;
return result;
return modifiedPositions;
}
private List<Position> activateFires() {
List<Position> result = new ArrayList<>();
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(fire));
newFirePositions.addAll(neighbors.get(fire));
}
firePositions.addAll(newFirePositions);
result.addAll(newFirePositions);
modifiedPositions.addAll(newFirePositions);
}
return result;
return modifiedPositions;
}
......@@ -83,22 +99,25 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
return step;
}
private List<Position> activateFirefighters() {
List<Position> result = new ArrayList<>();
firefighterNewPositions = new ArrayList<>();
private List<Position> updateFirefighters() {
List<Position> modifiedPosition = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
result.add(firefighterPosition);
Position newFirefighterPosition =
targetStrategy.neighborClosestToFire(firefighterPosition,
firePositions, neighbors);
firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition);
result.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream().filter(firePositions::contains).toList();
modifiedPosition.add(firefighterPosition);
modifiedPosition.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors.get(newFirefighterPosition).stream()
.filter(firePositions::contains).toList();
for (Position firePosition : neighborFirePositions)
extinguish(firePosition);
result.addAll(neighborFirePositions);
modifiedPosition.addAll(neighborFirePositions);
}
firefighterPositions = firefighterNewPositions;
return result;
return modifiedPosition;
}
@Override
......@@ -111,32 +130,18 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
firePositions.remove(position);
}
private List<Position> neighbors(Position position) {
List<Position> list = new ArrayList<>();
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column()));
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1));
if (position.row() < rowCount - 1) list.add(new Position(position.row() + 1, position.column()));
if (position.column() < columnCount - 1) list.add(new Position(position.row(), position.column() + 1));
return list;
}
private Position neighborClosestToFire(Position position) {
Set<Position> seen = new HashSet<>();
HashMap<Position, Position> firstMove = new HashMap<>();
Queue<Position> toVisit = new LinkedList<>(neighbors(position));
for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (firePositions.contains(current))
return firstMove.get(current);
for (Position adjacent : neighbors(current)) {
if (seen.contains(adjacent)) continue;
toVisit.add(adjacent);
seen.add(adjacent);
firstMove.put(adjacent, firstMove.get(current));
}
}
return 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 firefighter.model;
package model;
public enum ModelElement {
FIREFIGHTER, FIRE
......
......@@ -2,7 +2,7 @@ module firefighter {
requires javafx.controls;
requires javafx.fxml;
requires javafx.graphics;
opens firefighter.controller to javafx.fxml;
exports firefighter.app;
opens firefighter.app to javafx.fxml;
opens controller to javafx.fxml;
exports app;
opens app to javafx.fxml;
}
package firefighter.util;
package util;
public record Position(int row, int column) {
......
package model;
import util.Position;
import java.util.*;
public class TargetStrategy {
/**
* @param position current position.
* @param targets positions that are targeted.
* @return the position next to the current position that is on the path to the closest target.
*/
Position neighborClosestToFire(Position position, Collection<Position> targets,
Map<Position,List<Position>>neighbors) {
Set<Position> seen = new HashSet<Position>();
HashMap<Position, Position> firstMove = new HashMap<Position, Position>();
Queue<Position> toVisit = new LinkedList<Position>(neighbors.get(position));
for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (targets.contains(current))
return firstMove.get(current);
for (Position adjacent : neighbors.get(current)) {
if (seen.contains(adjacent)) continue;
toVisit.add(adjacent);
seen.add(adjacent);
firstMove.put(adjacent, firstMove.get(current));
}
}
return position;
}
}
\ No newline at end of file
package firefighter.view;
package view;
import javafx.scene.canvas.Canvas;
import javafx.scene.paint.Color;
import javafx.util.Pair;
import firefighter.util.Position;
import util.Position;
import java.util.List;
public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
private void paintElementAtPosition(ViewElement element, Position position) {
paintSquare(position.row(), position.column(), element.color);
paintBox(position.row(), position.column(), element.color);
}
private int squareWidth;
private int squareHeight;
private int boxWidth;
private int boxHeight;
private int columnCount;
private int rowCount;
@Override
public void repaint(List<Pair<Position, ViewElement>> positionedElements) {
clear(positionedElements);
paint(positionedElements);
paintLines();
}
private void clear(List<Pair<Position, ViewElement>> positionedElements) {
for (Pair<Position, ViewElement> positionElement : positionedElements) {
Position position = positionElement.getKey();
clearBox(position.row(), position.column());
}
}
private void paint(List<Pair<Position, ViewElement>> positionedElements) {
for(Pair<Position, ViewElement> pair : positionedElements){
paintElementAtPosition(pair.getValue(), pair.getKey());
}
paintLines();
}
@Override
public void repaint(ViewElement[][] elements) {
clear();
paint(elements);
paintLines();
}
private void clear() {
getGraphicsContext2D().clearRect(0,0,getWidth(), getHeight());
}
private void paint(ViewElement[][] elements) {
for(int column = 0; column < columnCount; column++)
for(int row = 0; row < rowCount; row++){
paintElementAtPosition(elements[row][column], new Position(row, column));
}
paintLines();
}
public int getColumnCount() {
public int columnCount() {
return columnCount;
}
public int getRowCount() {
public int rowCount() {
return rowCount;
}
public FirefighterGrid(){
}
public void initialize(int squareWidth,
int squareHeight,
int columnCount,
int rowCount) {
this.squareWidth = squareWidth;
this.squareHeight = squareHeight;
@Override
public void setDimensions(int columnCount, int rowCount, int boxWidth, int boxHeight) {
this.boxWidth = boxWidth;
this.boxHeight = boxHeight;
this.columnCount = columnCount;
this.rowCount = rowCount;
super.setWidth(boxWidth * columnCount);
super.setHeight(boxHeight * rowCount);
}
private void paintLines(){
paintHorizontalLines();
paintVerticalLines();
}
private void paintVerticalLines() {
for(int column = 0; column < columnCount; column++)
getGraphicsContext2D().strokeLine(0, column*squareHeight, getWidth(), column*squareWidth);
getGraphicsContext2D().strokeLine(column * boxWidth, 0,column * boxWidth, getHeight());
}
private void paintHorizontalLines() {
for(int row = 0; row < rowCount; row++)
getGraphicsContext2D().strokeLine(row*squareHeight, 0,row*squareHeight, getHeight());
getGraphicsContext2D().strokeLine(0, row * boxHeight, getWidth(), row * boxHeight);
}
private void paintSquare(int row, int column, Color color){
private void paintBox(int row, int column, Color color){
getGraphicsContext2D().setFill(color);
getGraphicsContext2D().fillRect(row*squareHeight,column*squareWidth,squareHeight,squareWidth);
getGraphicsContext2D().fillRect(column * boxWidth,row * boxHeight, boxWidth, boxHeight);
}
private void clearBox(int row, int column){
getGraphicsContext2D().clearRect(column * boxWidth,row * boxHeight, boxWidth, boxHeight);
}
}
\ No newline at end of file
package firefighter.view;
package view;
import javafx.util.Pair;
import firefighter.util.Position;
import util.Position;
import java.util.List;
......@@ -27,18 +27,29 @@ public interface Grid<E> {
*/
void repaint(E[][] elements);
/**
* Set the dimensions of the grid to the specified number of columns, number of rows, square width,
* and square height.
*
* @param columnCount The new number of columns in the grid.
* @param rowCount The new number of rows in the grid.
* @param squareWidth The width of each square within the grid.
* @param squareHeight The height of each square within the grid.
*/
void setDimensions(int columnCount, int rowCount, int squareWidth, int squareHeight);
/**
* Get the number of columns in the grid.
*
* @return The number of columns in the grid.
*/
int getColumnCount();
int columnCount();
/**
* Get the number of rows in the grid.
*
* @return The number of rows in the grid.
*/
int getRowCount();
int rowCount();
}
package firefighter.view;
package view;
import javafx.scene.paint.Color;
......
......@@ -3,14 +3,14 @@
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import firefighter.view.FirefighterGrid?>
<?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="firefighter.controller.Controller">
fx:controller="controller.Controller">
<VBox>
<Separator maxHeight="-Infinity" maxWidth="-Infinity"
prefHeight="24.0" prefWidth="200.0"/>
......@@ -33,7 +33,7 @@
mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0"
prefWidth="200.0" styleClass="button" text="Pause"/>
</VBox>
<FirefighterGrid fx:id="grid" width="1000.0" height="1000.0"
<FirefighterGrid fx:id="grid"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml">
</FirefighterGrid>
......
package model;
import org.junit.jupiter.api.Test;
import util.Position;
import java.util.List;
import static org.assertj.core.api.Assertions.*;
public class FirefighterBoardTest {
@Test
void testColumnCount(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3);
assertThat(board.columnCount()).isEqualTo(20);
}
@Test
void testRowCount(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3);
assertThat(board.rowCount()).isEqualTo(10);
}
@Test
void testStepNumber(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 1, 3);
for(int index = 0; index < 10; index++){
assertThat(board.stepNumber()).isEqualTo(index);
board.updateToNextGeneration();
}
assertThat(board.stepNumber()).isEqualTo(10);
}
@Test
void testGetState_afterSet(){
Board<List<ModelElement>> board = new FirefighterBoard(20, 10, 0, 0);
Position position = new Position(1,2);
assertThat(board.getState(position)).isEmpty();
board.setState(List.of(ModelElement.FIRE), position);
assertThat(board.getState(position)).containsExactly(ModelElement.FIRE);
}
}
package view;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class FirefighterGridTest {
@Test
void testColumnCount(){
Grid<ViewElement> grid = new FirefighterGrid();
grid.setDimensions(20,10,10,10);
assertThat(grid.columnCount()).isEqualTo(20);
}
@Test
void testRowCount(){
Grid<ViewElement> grid = new FirefighterGrid();
grid.setDimensions(20,10,10,10);
assertThat(grid.rowCount()).isEqualTo(10);
}
}