Skip to content
Snippets Groups Projects
Commit 093a9ecc authored by MAAZOUZ Ilyas's avatar MAAZOUZ Ilyas
Browse files

hhh

parent 90ed7a22
No related branches found
No related tags found
No related merge requests found
Pipeline #22600 passed
......@@ -26,7 +26,7 @@ import static java.util.Objects.requireNonNull;
public class Controller {
public static final int PERIOD_IN_MILLISECONDS = 50;
public static final int PERIOD_IN_MILLISECONDS = 500;
@FXML
public Button restartButton;
@FXML
......@@ -42,34 +42,48 @@ public class Controller {
private Timeline timeline;
private Board<List<ModelElement>> board;
//this function initialise the frame of the execution of updateboard function and adds the play&pause buttons
@FXML
private void initialize() {
initializePlayAndPauseToggleButtons();
initializeTimeline();
}
// function that add the play and pause button
private void initializePlayAndPauseToggleButtons() {
ToggleGroup toggleGroup = new PersistentToggleGroup();
toggleGroup.getToggles().addAll(playToggleButton, pauseToggleButton);
pauseToggleButton.setSelected(true);
}
//this function checks if our model of the class firefighterboard is not null and raises an exception if so
// and affect the model to board
private void setModel(FirefighterBoard firefighterBoard) {
this.board = requireNonNull(firefighterBoard, "firefighter.model is null");
}
//this function is a combination of multiple functions that re update the board elements get the new positions and colors of new elements
//and then repaint the new board and increment the generation label
private void updateBoard(){
List<Position> updatedPositions = board.updateToNextGeneration();
//this function calls both update firefighters and updatefire functions so they can be updated to their correponding
// new position and sum up the results and returns them also it increments the step variable
List<Pair<Position, ViewElement>> updatedSquares = new ArrayList<>();
for(Position updatedPosition : updatedPositions){
List<ModelElement> squareState = board.getState(updatedPosition);
// Function that return the state (Fire or Firefighter) of a certain position
ViewElement viewElement = getViewElement(squareState);
// Function that return the state (Fire or Firefighter or empty) of an element
updatedSquares.add(new Pair<>(updatedPosition, viewElement));
}
grid.repaint(updatedSquares);
updateGenerationLabel(board.stepNumber());
}
//this function takes all the states from the model then clears the actual the grid and repaints everything from the positions to the
//grid lines and update the number of generation label also
private void repaintGrid(){
int columnCount = board.columnCount();
int rowCount = board.rowCount();
......@@ -81,6 +95,7 @@ public class Controller {
updateGenerationLabel(board.stepNumber());
}
//this function check a modelelement and returns if this model is a firefighter or fire or empty
private ViewElement getViewElement(List<ModelElement> squareState) {
if(squareState.contains(ModelElement.FIREFIGHTER)){
return ViewElement.FIREFIGHTER;
......@@ -91,6 +106,7 @@ public class Controller {
return ViewElement.EMPTY;
}
//this function set the action to updateboard for each duration of the frame and looping for infinite amount of time
private void initializeTimeline() {
Duration duration = new Duration(Controller.PERIOD_IN_MILLISECONDS);
EventHandler<ActionEvent> eventHandler =
......@@ -100,22 +116,28 @@ public class Controller {
timeline.setCycleCount(Animation.INDEFINITE);
}
// this function causes to play any ongoing animation or sequences
public void play() {
timeline.play();
}
// this function causes to stop any ongoing animation or sequences
public void pause() {
timeline.pause();
}
//this function execute the pause function while toggling the button
public void pauseToggleButtonAction() {
this.pause();
}
//this function execute the play function while toggling the button
public void playToggleButtonAction() {
this.play();
}
//this function does a pause to the animation and sequences then does a rest to the parametres and repaint the board based on the new
//parametres that are random positions for fire and firefighters
public void restartButtonAction() {
this.pause();
board.reset();
......@@ -123,6 +145,7 @@ public class Controller {
repaintGrid();
}
//this function initialise all parametres of the games including the firefighter model and the grids
public void initialize(int squareWidth, int squareHeight, int columnCount,
int rowCount, int initialFireCount, int initialFirefighterCount) {
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
......@@ -130,11 +153,12 @@ public class Controller {
repaintGrid();
}
//this function causes the sequences and animnation to stop then does a signle update to the board
public void oneStepButtonAction() {
this.pause();
updateBoard();
}
// Function that updates the value of variable generatioNumberLAbel
private void updateGenerationLabel(int value){
generationNumberLabel.setText(Integer.toString(value));
}
......
......@@ -126,6 +126,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
}
//Function that returns a list of positions neighbors of a certain 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()));
......@@ -161,7 +162,9 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
return position;
}
//Update the state of the fire and firefighters positions
@Override
public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position);
......
......@@ -9,7 +9,7 @@ import java.util.List;
public class FirefighterGrid extends Canvas implements Grid<ViewElement>{
//this fucnction paint a certain position passed into the arguments with corresponding element color
//this function paint a certain position passed into the arguments with corresponding element color
private void paintElementAtPosition(ViewElement element, Position position) {
paintSquare(position.row(), position.column(), element.color);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment