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
  • 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
  • main
  • variant
2 results
Show changes
Commits on Source (12)
Showing
with 494 additions and 65 deletions
...@@ -17,9 +17,7 @@ repositories { ...@@ -17,9 +17,7 @@ repositories {
} }
dependencies { dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher") { 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.jupiter:junit-jupiter-engine") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.assertj:assertj-core:3.24.2") testImplementation("org.assertj:assertj-core:3.24.2")
testImplementation("org.junit.jupiter:junit-jupiter:5.10.0") testImplementation("org.junit.jupiter:junit-jupiter:5.10.0")
...@@ -30,11 +28,7 @@ test { ...@@ -30,11 +28,7 @@ test {
} }
application { application {
mainClass.set("firefighter.app.SimulatorMain") mainClass.set("app.SimulatorMain")
} }
tasks {
shadowJar {
exclude("module-info.class")
}
}
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists 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 zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
package firefighter.app; package app;
import firefighter.controller.Controller; import controller.Controller;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
...@@ -11,12 +11,12 @@ import java.io.IOException; ...@@ -11,12 +11,12 @@ import java.io.IOException;
import java.net.URL; import java.net.URL;
public class SimulatorApplication extends javafx.application.Application { 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 String APP_NAME = "Firefighter simulator";
private static final int ROW_COUNT = 20; private static final int ROW_COUNT = 20;
private static final int COLUMN_COUNT = 20; private static final int COLUMN_COUNT = 20;
private static final int SQUARE_WIDTH = 50; private static final int BOX_WIDTH = 50;
private static final int SQUARE_HEIGHT = 50; private static final int BOX_HEIGHT = 50;
public static final int INITIAL_FIRE_COUNT = 3; public static final int INITIAL_FIRE_COUNT = 3;
public static final int INITIAL_FIREFIGHTER_COUNT = 6; public static final int INITIAL_FIREFIGHTER_COUNT = 6;
...@@ -26,7 +26,7 @@ public class SimulatorApplication extends javafx.application.Application { ...@@ -26,7 +26,7 @@ public class SimulatorApplication extends javafx.application.Application {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
this.primaryStage.setTitle(APP_NAME); this.primaryStage.setTitle(APP_NAME);
this.primaryStage.setOnCloseRequest(event -> Platform.exit()); this.primaryStage.setOnCloseRequest(event -> Platform.exit());
this.primaryStage.setResizable(false); this.primaryStage.setResizable(true);
this.primaryStage.sizeToScene(); this.primaryStage.sizeToScene();
} }
...@@ -43,7 +43,7 @@ public class SimulatorApplication extends javafx.application.Application { ...@@ -43,7 +43,7 @@ public class SimulatorApplication extends javafx.application.Application {
loader.setLocation(location); loader.setLocation(location);
view = loader.load(); view = loader.load();
Controller controller = loader.getController(); 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); INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT);
} }
......
package firefighter.app; package app;
public class SimulatorMain { public class SimulatorMain {
public static void main(String[] args){ public static void main(String[] args){
......
package firefighter.controller; package controller;
import javafx.animation.Animation; import javafx.animation.Animation;
import javafx.animation.KeyFrame; import javafx.animation.KeyFrame;
...@@ -7,16 +7,17 @@ import javafx.event.ActionEvent; ...@@ -7,16 +7,17 @@ import javafx.event.ActionEvent;
import javafx.event.EventHandler; import javafx.event.EventHandler;
import javafx.fxml.FXML; import javafx.fxml.FXML;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton; import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup; import javafx.scene.control.ToggleGroup;
import javafx.util.Duration; import javafx.util.Duration;
import javafx.util.Pair; import javafx.util.Pair;
import firefighter.model.Board; import model.Board;
import firefighter.model.ModelElement; import model.ModelElement;
import firefighter.model.FirefighterBoard; import model.FirefighterBoard;
import firefighter.model.Position; import util.Position;
import firefighter.view.FirefighterGrid; import view.Grid;
import firefighter.view.ViewElement; import view.ViewElement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -31,11 +32,13 @@ public class Controller { ...@@ -31,11 +32,13 @@ public class Controller {
@FXML @FXML
public Button oneStepButton; public Button oneStepButton;
@FXML @FXML
public Label generationNumberLabel;
@FXML
private ToggleButton pauseToggleButton; private ToggleButton pauseToggleButton;
@FXML @FXML
private ToggleButton playToggleButton; private ToggleButton playToggleButton;
@FXML @FXML
private FirefighterGrid grid; private Grid<ViewElement> grid;
private Timeline timeline; private Timeline timeline;
private Board<List<ModelElement>> board; private Board<List<ModelElement>> board;
...@@ -64,9 +67,10 @@ public class Controller { ...@@ -64,9 +67,10 @@ public class Controller {
updatedSquares.add(new Pair<>(updatedPosition, viewElement)); updatedSquares.add(new Pair<>(updatedPosition, viewElement));
} }
grid.repaint(updatedSquares); grid.repaint(updatedSquares);
updateGenerationLabel(board.stepNumber());
} }
private void repaintBoard(){ private void repaintGrid(){
int columnCount = board.columnCount(); int columnCount = board.columnCount();
int rowCount = board.rowCount(); int rowCount = board.rowCount();
ViewElement[][] viewElements = new ViewElement[rowCount][columnCount]; ViewElement[][] viewElements = new ViewElement[rowCount][columnCount];
...@@ -74,6 +78,7 @@ public class Controller { ...@@ -74,6 +78,7 @@ public class Controller {
for(int row = 0; row < rowCount; row++) for(int row = 0; row < rowCount; row++)
viewElements[row][column] = getViewElement(board.getState(new Position(row, column))); viewElements[row][column] = getViewElement(board.getState(new Position(row, column)));
grid.repaint(viewElements); grid.repaint(viewElements);
updateGenerationLabel(board.stepNumber());
} }
private ViewElement getViewElement(List<ModelElement> squareState) { private ViewElement getViewElement(List<ModelElement> squareState) {
...@@ -115,18 +120,22 @@ public class Controller { ...@@ -115,18 +120,22 @@ public class Controller {
this.pause(); this.pause();
board.reset(); board.reset();
pauseToggleButton.setSelected(true); pauseToggleButton.setSelected(true);
repaintBoard(); repaintGrid();
} }
public void initialize(int squareWidth, int squareHeight, int columnCount, public void initialize(int squareWidth, int squareHeight, int columnCount,
int rowCount, int initialFireCount, int initialFirefighterCount) { int rowCount, int initialFireCount, int initialFirefighterCount) {
grid.initialize(squareWidth, squareHeight, columnCount, rowCount); grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount)); this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount));
repaintBoard(); repaintGrid();
} }
public void oneStepButtonAction() { public void oneStepButtonAction() {
this.pause(); this.pause();
updateBoard(); updateBoard();
} }
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.collections.ListChangeListener.Change;
import javafx.scene.control.Toggle; import javafx.scene.control.Toggle;
......
package firefighter.model;
import java.util.List;
public interface Board<S> {
S getState(Position position);
int rowCount();
int columnCount();
List<Position> updateToNextGeneration();
void reset();
}
package firefighter.view;
import javafx.util.Pair;
import firefighter.model.Position;
import java.util.List;
public interface Grid<E> {
void repaint(List<Pair<Position,E>> elements);
void repaint(E[][] elements);
int getColumnCount();
int getRowCount();
}
package model;
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> {
/**
* Get the state of the board at a specific position.
*
* @param position The position on the board for which to retrieve the state.
* @return The state at the specified position.
*/
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.
*
* @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 firefighter.model; package model;
import util.Position;
import java.util.*; import java.util.*;
...@@ -8,14 +10,30 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -8,14 +10,30 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private final int rowCount; private final int rowCount;
private final int initialFireCount; private final int initialFireCount;
private final int initialFirefighterCount; private final int initialFirefighterCount;
List<Position> firefighterPositions; private final TargetStrategy targetStrategy = new TargetStrategy();
Set<Position> firePositions; private List<Position> firefighterPositions;
List<Position> firefighterNewPositions; private Set<Position> firePositions;
int step = 0; 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) { public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) {
this.columnCount = columnCount; this.columnCount = columnCount;
this.rowCount = rowCount; 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.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount; this.initialFirefighterCount = initialFirefighterCount;
initializeElements(); initializeElements();
...@@ -31,7 +49,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -31,7 +49,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
private Position randomPosition() { private Position randomPosition() {
return new Position((int) (Math.random() * rowCount), (int) (Math.random() * columnCount)); return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount));
} }
@Override @Override
...@@ -56,46 +74,55 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -56,46 +74,55 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
public List<Position> updateToNextGeneration() { public List<Position> updateToNextGeneration() {
List<Position> result = activateFirefighters(); List<Position> modifiedPositions = updateFirefighters();
result.addAll(activateFires()); modifiedPositions.addAll(updateFires());
step++; step++;
return result; return modifiedPositions;
} }
private List<Position> activateFires() { private List<Position> updateFires() {
List<Position> result = new ArrayList<>(); List<Position> modifiedPositions = new ArrayList<>();
if (step % 2 == 0) { if (step % 2 == 0) {
List<Position> newFirePositions = new ArrayList<>(); List<Position> newFirePositions = new ArrayList<>();
for (Position fire : firePositions) { for (Position fire : firePositions) {
newFirePositions.addAll(neighbors(fire)); newFirePositions.addAll(neighbors.get(fire));
} }
firePositions.addAll(newFirePositions); firePositions.addAll(newFirePositions);
result.addAll(newFirePositions); modifiedPositions.addAll(newFirePositions);
}
return modifiedPositions;
} }
return result;
@Override
public int stepNumber() {
return step;
} }
private List<Position> activateFirefighters() { private List<Position> updateFirefighters() {
List<Position> result = new ArrayList<>(); List<Position> modifiedPosition = new ArrayList<>();
firefighterNewPositions = new ArrayList<>(); List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) { for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition); Position newFirefighterPosition =
result.add(firefighterPosition); targetStrategy.neighborClosestToFire(firefighterPosition,
firePositions, neighbors);
firefighterNewPositions.add(newFirefighterPosition); firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition); extinguish(newFirefighterPosition);
result.add(newFirefighterPosition); modifiedPosition.add(firefighterPosition);
List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream().filter(firePositions::contains).toList(); modifiedPosition.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors.get(newFirefighterPosition).stream()
.filter(firePositions::contains).toList();
for (Position firePosition : neighborFirePositions) for (Position firePosition : neighborFirePositions)
extinguish(firePosition); extinguish(firePosition);
result.addAll(neighborFirePositions); modifiedPosition.addAll(neighborFirePositions);
} }
firefighterPositions = firefighterNewPositions; firefighterPositions = firefighterNewPositions;
return result; return modifiedPosition;
} }
@Override @Override
public void reset() { public void reset() {
step = 0;
initializeElements(); initializeElements();
} }
...@@ -103,32 +130,18 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -103,32 +130,18 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
firePositions.remove(position); firePositions.remove(position);
} }
private List<Position> neighbors(Position position) {
List<Position> list = new ArrayList<>(); @Override
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column())); public void setState(List<ModelElement> state, Position position) {
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1)); firePositions.remove(position);
if (position.row() < rowCount - 1) list.add(new Position(position.row() + 1, position.column())); for (; ; ) {
if (position.column() < columnCount - 1) list.add(new Position(position.row(), position.column() + 1)); if (!firefighterPositions.remove(position)) break;
return list; }
} for (ModelElement element : state) {
switch (element) {
private Position neighborClosestToFire(Position position) { case FIRE -> firePositions.add(position);
Set<Position> seen = new HashSet<>(); case FIREFIGHTER -> firefighterPositions.add(position);
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;
} }
} }
\ No newline at end of file
package firefighter.model; package model;
public enum ModelElement { public enum ModelElement {
FIREFIGHTER, FIRE FIREFIGHTER, FIRE
......
...@@ -2,7 +2,7 @@ module firefighter { ...@@ -2,7 +2,7 @@ module firefighter {
requires javafx.controls; requires javafx.controls;
requires javafx.fxml; requires javafx.fxml;
requires javafx.graphics; requires javafx.graphics;
opens firefighter.controller to javafx.fxml; opens controller to javafx.fxml;
exports firefighter.app; exports app;
opens firefighter.app to javafx.fxml; opens app to javafx.fxml;
} }
package firefighter.model; package util;
public record Position(int row, int column) { 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.canvas.Canvas;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
import javafx.util.Pair; import javafx.util.Pair;
import firefighter.model.Position; import util.Position;
import java.util.List; import java.util.List;
public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
private void paintElementAtPosition(ViewElement element, Position position) { private void paintElementAtPosition(ViewElement element, Position position) {
paintSquare(position.row(), position.column(), element.color); paintBox(position.row(), position.column(), element.color);
} }
private int boxWidth;
private int squareWidth; private int boxHeight;
private int squareHeight;
private int columnCount; private int columnCount;
private int rowCount; private int rowCount;
@Override @Override
public void repaint(List<Pair<Position, ViewElement>> positionedElements) { 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){ for(Pair<Position, ViewElement> pair : positionedElements){
paintElementAtPosition(pair.getValue(), pair.getKey()); paintElementAtPosition(pair.getValue(), pair.getKey());
} }
paintLines();
} }
@Override @Override
public void repaint(ViewElement[][] elements) { 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 column = 0; column < columnCount; column++)
for(int row = 0; row < rowCount; row++){ for(int row = 0; row < rowCount; row++){
paintElementAtPosition(elements[row][column], new Position(row, column)); paintElementAtPosition(elements[row][column], new Position(row, column));
} }
paintLines();
} }
public int getColumnCount() { public int columnCount() {
return columnCount; return columnCount;
} }
public int getRowCount() { public int rowCount() {
return rowCount; return rowCount;
} }
public FirefighterGrid(){ @Override
} public void setDimensions(int columnCount, int rowCount, int boxWidth, int boxHeight) {
this.boxWidth = boxWidth;
public void initialize(int squareWidth, this.boxHeight = boxHeight;
int squareHeight,
int columnCount,
int rowCount) {
this.squareWidth = squareWidth;
this.squareHeight = squareHeight;
this.columnCount = columnCount; this.columnCount = columnCount;
this.rowCount = rowCount; this.rowCount = rowCount;
super.setWidth(boxWidth * columnCount);
super.setHeight(boxHeight * rowCount);
} }
private void paintLines(){ private void paintLines(){
paintHorizontalLines();
paintVerticalLines();
}
private void paintVerticalLines() {
for(int column = 0; column < columnCount; column++) 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++) 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().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 view;
import javafx.util.Pair;
import util.Position;
import java.util.List;
/**
* This interface represents a generic grid structure for displaying two-dimensional data.
*
* @param <E> The type of elements stored in the grid.
*/
public interface Grid<E> {
/**
* Repaint the grid with a list of elements, each associated with their respective positions.
*
* @param elements A list of pairs, each containing a position and the element to be displayed at that position.
*/
void repaint(List<Pair<Position, E>> elements);
/**
* Repaint the grid with a two-dimensional array of elements. The array's dimensions should match
* the row and column count of the grid.
*
* @param elements A two-dimensional array of elements to be displayed on the grid.
*/
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 columnCount();
/**
* Get the number of rows in the grid.
*
* @return The number of rows in the grid.
*/
int rowCount();
}
package firefighter.view; package view;
import javafx.scene.paint.Color; import javafx.scene.paint.Color;
......
...@@ -3,13 +3,23 @@ ...@@ -3,13 +3,23 @@
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?> <?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import firefighter.view.FirefighterGrid?> <?import view.FirefighterGrid?>
<?import javafx.scene.control.ToggleButton?> <?import javafx.scene.control.ToggleButton?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.Label?>
<HBox styleClass="background" stylesheets="@DarkTheme.css" <HBox styleClass="background" stylesheets="@DarkTheme.css"
xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml"
fx:controller="firefighter.controller.Controller"> fx:controller="controller.Controller">
<VBox> <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" <Button fx:id="restartButton" maxHeight="-Infinity" maxWidth="-Infinity"
mnemonicParsing="false" onAction="#restartButtonAction" prefHeight="24.0" prefWidth="200.0" mnemonicParsing="false" onAction="#restartButtonAction" prefHeight="24.0" prefWidth="200.0"
text="Restart"/> text="Restart"/>
...@@ -23,7 +33,7 @@ ...@@ -23,7 +33,7 @@
mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0" mnemonicParsing="false" onAction="#pauseToggleButtonAction" prefHeight="24.0"
prefWidth="200.0" styleClass="button" text="Pause"/> prefWidth="200.0" styleClass="button" text="Pause"/>
</VBox> </VBox>
<FirefighterGrid fx:id="grid" width="1000.0" height="1000.0" <FirefighterGrid fx:id="grid"
xmlns="http://javafx.com/javafx" xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"> xmlns:fx="http://javafx.com/fxml">
</FirefighterGrid> </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);
}
}