diff --git a/src/main/java/controller/Controller.java b/src/main/java/controller/Controller.java index 2a60897c6eb8ba847cb8589840c16a0f175ce0a3..462ef3977a771b05a193d875aa4f506f6350243a 100644 --- a/src/main/java/controller/Controller.java +++ b/src/main/java/controller/Controller.java @@ -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)); } diff --git a/src/main/java/model/FirefighterBoard.java b/src/main/java/model/FirefighterBoard.java index d6f62db3cf210f8afe0b4c14222588f07d226dd2..b7a3ba54f50ca201bed93a27ed191d81b8c10a85 100644 --- a/src/main/java/model/FirefighterBoard.java +++ b/src/main/java/model/FirefighterBoard.java @@ -25,7 +25,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { } // Intialise parametres(firefighterPositions,firePositions into random positions,firefighterPositions into random positions) - public void initializeElements() { + public void initializeElements() { firefighterPositions = new ArrayList<>(); firePositions = new HashSet<>(); for (int index = 0; index < initialFireCount; index++) @@ -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); diff --git a/src/main/java/view/FirefighterGrid.java b/src/main/java/view/FirefighterGrid.java index cadad96e44aca3fc275a0fd9d0ee4f2105210f7d..f48762c7f1b6b89132613bd726a8c719a101966f 100644 --- a/src/main/java/view/FirefighterGrid.java +++ b/src/main/java/view/FirefighterGrid.java @@ -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); }