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 (7)
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
......@@ -15,8 +15,8 @@ public class SimulatorApplication extends javafx.application.Application {
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);
}
......
......@@ -10,14 +10,30 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
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();
......@@ -58,23 +74,23 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
public List<Position> updateToNextGeneration() {
List<Position> result = updateFirefighters();
result.addAll(updateFires());
List<Position> modifiedPositions = updateFirefighters();
modifiedPositions.addAll(updateFires());
step++;
return result;
return modifiedPositions;
}
private List<Position> updateFires() {
List<Position> result = new ArrayList<>();
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;
}
......@@ -84,22 +100,24 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
private List<Position> updateFirefighters() {
List<Position> result = new ArrayList<>();
List<Position> modifiedPosition = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
Position newFirefighterPosition =
targetStrategy.neighborClosestToFire(firefighterPosition,
firePositions, neighbors);
firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition);
result.add(firefighterPosition);
result.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream()
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
......@@ -112,34 +130,6 @@ 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) {
......
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
......@@ -10,19 +10,27 @@ 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());
......@@ -31,10 +39,15 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
@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++){
......@@ -51,11 +64,13 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
}
@Override
public void setDimensions(int columnCount, int rowCount, int squareWidth, int squareHeight) {
this.squareWidth = squareWidth;
this.squareHeight = squareHeight;
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(){
......@@ -65,16 +80,20 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
private void paintVerticalLines() {
for(int column = 0; column < columnCount; column++)
getGraphicsContext2D().strokeLine(column*squareWidth, 0,column*squareWidth, getHeight());
getGraphicsContext2D().strokeLine(column * boxWidth, 0,column * boxWidth, getHeight());
}
private void paintHorizontalLines() {
for(int row = 0; row < rowCount; row++)
getGraphicsContext2D().strokeLine(0, row*squareHeight, getWidth(), row*squareHeight);
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
......@@ -28,8 +28,7 @@ public interface Grid<E> {
void repaint(E[][] elements);
/**
* Set the dimensions of the grid to the specified column count, row count, square width, and square height.
* This method adjusts the dimensions of the grid to the given number of columns, number of rows, square width,
* 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.
......
......@@ -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>
......