Skip to content
Snippets Groups Projects
Commit ba6f7943 authored by LABOUREL Arnaud's avatar LABOUREL Arnaud
Browse files

added generation number on the GUI

parent 64c3380f
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ import javafx.event.ActionEvent; ...@@ -7,6 +7,7 @@ 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;
...@@ -14,7 +15,7 @@ import javafx.util.Pair; ...@@ -14,7 +15,7 @@ import javafx.util.Pair;
import firefighter.model.Board; import firefighter.model.Board;
import firefighter.model.ModelElement; import firefighter.model.ModelElement;
import firefighter.model.FirefighterBoard; import firefighter.model.FirefighterBoard;
import firefighter.model.Position; import firefighter.util.Position;
import firefighter.view.FirefighterGrid; import firefighter.view.FirefighterGrid;
import firefighter.view.ViewElement; import firefighter.view.ViewElement;
...@@ -31,6 +32,8 @@ public class Controller { ...@@ -31,6 +32,8 @@ 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;
...@@ -64,6 +67,7 @@ public class Controller { ...@@ -64,6 +67,7 @@ public class Controller {
updatedSquares.add(new Pair<>(updatedPosition, viewElement)); updatedSquares.add(new Pair<>(updatedPosition, viewElement));
} }
grid.repaint(updatedSquares); grid.repaint(updatedSquares);
updateLabel(board.stepNumber());
} }
private void repaintBoard(){ private void repaintBoard(){
...@@ -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);
updateLabel(board.stepNumber());
} }
private ViewElement getViewElement(List<ModelElement> squareState) { private ViewElement getViewElement(List<ModelElement> squareState) {
...@@ -129,4 +134,8 @@ public class Controller { ...@@ -129,4 +134,8 @@ public class Controller {
this.pause(); this.pause();
updateBoard(); updateBoard();
} }
private void updateLabel(int value){
generationNumberLabel.setText(Integer.toString(value));
}
} }
\ No newline at end of file
package firefighter.model; package firefighter.model;
import firefighter.util.Position;
import java.util.List; import java.util.List;
/**
* This interface represents a generic board for modeling various state-based systems.
*
* @param <S> The type of state represented on the board.
*/
public interface Board<S> { public interface Board<S> {
/**
* Get the state of the board at a specific position.
*
* @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 getState(Position position);
/**
* Get the number of rows in the board.
*
* @return The number of rows in the board.
*/
int rowCount(); int rowCount();
/**
* Get the number of columns in the board.
*
* @return The number of columns in the board.
*/
int columnCount(); 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(); List<Position> updateToNextGeneration();
/**
* Reset the board to its initial state.
*/
void reset(); 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 firefighter.model;
import firefighter.util.Position;
import java.util.*; import java.util.*;
...@@ -76,6 +78,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -76,6 +78,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
} }
@Override
public int stepNumber() {
return step;
}
private List<Position> activateFirefighters() { private List<Position> activateFirefighters() {
List<Position> result = new ArrayList<>(); List<Position> result = new ArrayList<>();
firefighterNewPositions = new ArrayList<>(); firefighterNewPositions = new ArrayList<>();
...@@ -96,6 +103,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -96,6 +103,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
@Override @Override
public void reset() { public void reset() {
step = 0;
initializeElements(); initializeElements();
} }
......
package firefighter.model; package firefighter.util;
public record Position(int row, int column) { public record Position(int row, int column) {
......
...@@ -3,7 +3,7 @@ package firefighter.view; ...@@ -3,7 +3,7 @@ package firefighter.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 firefighter.util.Position;
import java.util.List; import java.util.List;
......
package firefighter.view; package firefighter.view;
import javafx.util.Pair; import javafx.util.Pair;
import firefighter.model.Position; import firefighter.util.Position;
import java.util.List; 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> { public interface Grid<E> {
void repaint(List<Pair<Position,E>> elements);
/**
* 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); void repaint(E[][] elements);
/**
* Get the number of columns in the grid.
*
* @return The number of columns in the grid.
*/
int getColumnCount(); int getColumnCount();
/**
* Get the number of rows in the grid.
*
* @return The number of rows in the grid.
*/
int getRowCount(); int getRowCount();
} }
...@@ -6,10 +6,20 @@ ...@@ -6,10 +6,20 @@
<?import firefighter.view.FirefighterGrid?> <?import firefighter.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="firefighter.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"/>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment