diff --git a/src/main/java/model/FirefighterBoard.java b/src/main/java/model/FirefighterBoard.java index 3251952ea90ec5ea59fc3a3630f3d2a4e4e96f01..d6f62db3cf210f8afe0b4c14222588f07d226dd2 100644 --- a/src/main/java/model/FirefighterBoard.java +++ b/src/main/java/model/FirefighterBoard.java @@ -15,6 +15,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { private int step = 0; private final Random randomGenerator = new Random(); + // FireFighterBoard Class Constructor public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) { this.columnCount = columnCount; this.rowCount = rowCount; @@ -23,6 +24,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { initializeElements(); } + // Intialise parametres(firefighterPositions,firePositions into random positions,firefighterPositions into random positions) public void initializeElements() { firefighterPositions = new ArrayList<>(); firePositions = new HashSet<>(); @@ -32,10 +34,12 @@ public class FirefighterBoard implements Board<List<ModelElement>> { firefighterPositions.add(randomPosition()); } + // Function that returns (Random(<Rowcount,<columnCount) private Position randomPosition() { return new Position(randomGenerator.nextInt(rowCount), randomGenerator.nextInt(columnCount)); } + // Function that return a state (Fire or Firefighter) of a certain position @Override public List<ModelElement> getState(Position position) { List<ModelElement> result = new ArrayList<>(); @@ -57,6 +61,9 @@ public class FirefighterBoard implements Board<List<ModelElement>> { return columnCount; } + + //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 public List<Position> updateToNextGeneration() { List<Position> result = updateFirefighters(); result.addAll(updateFires()); @@ -64,6 +71,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { return result; } + // this function add fire in the neighbor of fires in every 2 steps and update the list of fires private List<Position> updateFires() { List<Position> result = new ArrayList<>(); if (step % 2 == 0) { @@ -77,12 +85,15 @@ public class FirefighterBoard implements Board<List<ModelElement>> { return result; } - + // show the number of steps @Override public int stepNumber() { return step; } + //result contient FirefighterPosition , newfigherfighterposition ,neighborfirepositions + //this function update the firefighter position to the nearest fire position and extinguish fire from this psoition and all the neighbor positions to this fire + private List<Position> updateFirefighters() { List<Position> result = new ArrayList<>(); List<Position> firefighterNewPositions = new ArrayList<>(); @@ -93,8 +104,8 @@ public class FirefighterBoard implements Board<List<ModelElement>> { result.add(firefighterPosition); result.add(newFirefighterPosition); List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream() - .filter(firePositions::contains).toList(); - for(Position firePosition : neighborFirePositions) + .filter(firePositions::contains).toList(); //this code create a list filtred with only the fire positions that are close to the newfirefighter position + for(Position firePosition : neighborFirePositions) //and exist in the firepositions list also extinguish(firePosition); result.addAll(neighborFirePositions); } @@ -102,16 +113,19 @@ public class FirefighterBoard implements Board<List<ModelElement>> { return result; } + // function that calls the initialise elements @Override public void reset() { step = 0; initializeElements(); } + // Function that removes fire from a certain position private void extinguish(Position position) { firePositions.remove(position); } + //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())); @@ -121,26 +135,33 @@ public class FirefighterBoard implements Board<List<ModelElement>> { 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)); + //HashMAp is a data structure that store elements in a key-value + //Set is a data structure that containts a collection of unique elements with no duplicata + //Queue is a FIFO structure + + + //This function searches for the nearest fire to the position passed in arguments and returns this position + private Position neighborClosestToFire(Position position) { + Set<Position> seen = new HashSet<>(); + HashMap<Position, Position> firstMove = new HashMap<>(); + Queue<Position> toVisit = new LinkedList<>(neighbors(position)); //Queue is initialised with the neighbors of 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; } - 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/model/ModelElement.java b/src/main/java/model/ModelElement.java index 759eee5e54c3a39472d8f7defbbbe6a2b67b8f00..2b2122b19bf0480928820f9e0ba4ebb5b73fd230 100644 --- a/src/main/java/model/ModelElement.java +++ b/src/main/java/model/ModelElement.java @@ -1,5 +1,6 @@ package model; +//this class represents two types of constant Firefighter or fire public enum ModelElement { FIREFIGHTER, FIRE } diff --git a/src/main/java/view/FirefighterGrid.java b/src/main/java/view/FirefighterGrid.java index c1b2274637f93e4a2482faa9d2a5559211b31dc6..cadad96e44aca3fc275a0fd9d0ee4f2105210f7d 100644 --- a/src/main/java/view/FirefighterGrid.java +++ b/src/main/java/view/FirefighterGrid.java @@ -9,6 +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 private void paintElementAtPosition(ViewElement element, Position position) { paintSquare(position.row(), position.column(), element.color); } @@ -17,6 +18,8 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ private int columnCount; private int rowCount; +//this function clears the colors of the postions passed in the arguments and repaint the positions with the corresponding + //colors of the elements then print the lines of the board @Override public void repaint(List<Pair<Position, ViewElement>> positionedElements) { clear(positionedElements); @@ -24,19 +27,21 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ paintLines(); } +//this function clears the colors of the positions passed in the arguments private void clear(List<Pair<Position, ViewElement>> positionedElements) { for (Pair<Position, ViewElement> positionElement : positionedElements) { Position position = positionElement.getKey(); clearSquare(position.row(), position.column()); } } - + //this function combines the element with it's position and paint it with the corresponding color of the elements private void paint(List<Pair<Position, ViewElement>> positionedElements) { for(Pair<Position, ViewElement> pair : positionedElements){ paintElementAtPosition(pair.getValue(), pair.getKey()); } } + //this function repaints everything from the lines to the positions @Override public void repaint(ViewElement[][] elements) { clear(); @@ -44,10 +49,13 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ paintLines(); } + //this function clears all the colors in the board for a fresh frame private void clear() { getGraphicsContext2D().clearRect(0,0,getWidth(), getHeight()); } + //this function call another function to paint all the positions of the board with the corresponding color of each item fire or firefighter + // in the list passed in the arguments private void paint(ViewElement[][] elements) { for(int column = 0; column < columnCount; column++) for(int row = 0; row < rowCount; row++){ @@ -63,6 +71,7 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ return rowCount; } + //This Function initialise all the parameteres(int columnCount, int rowCount, int squareWidth, int squareHeight) @Override public void setDimensions(int columnCount, int rowCount, int squareWidth, int squareHeight) { this.squareWidth = squareWidth; @@ -75,22 +84,23 @@ public class FirefighterGrid extends Canvas implements Grid<ViewElement>{ paintHorizontalLines(); paintVerticalLines(); } - +//this function draws the vertical lines private void paintVerticalLines() { for(int column = 0; column < columnCount; column++) getGraphicsContext2D().strokeLine(column*squareWidth, 0,column*squareWidth, getHeight()); } - +//this function draws the horizontal lines private void paintHorizontalLines() { for(int row = 0; row < rowCount; row++) getGraphicsContext2D().strokeLine(0, row*squareHeight, getWidth(), row*squareHeight); } + //this function color each case in the board with corresponding elments fire or firefighter private void paintSquare(int row, int column, Color color){ getGraphicsContext2D().setFill(color); getGraphicsContext2D().fillRect(row*squareHeight,column*squareWidth,squareHeight,squareWidth); } - + //this function clear the color in each case in the board passed in the arguments private void clearSquare(int row, int column){ getGraphicsContext2D().clearRect(row*squareHeight,column*squareWidth,squareHeight,squareWidth); } diff --git a/src/main/java/view/ViewElement.java b/src/main/java/view/ViewElement.java index ffb76112e1af543df5af41fa906082ef11be9967..bd847977fc254483e0e255a17586cfb6733a023a 100644 --- a/src/main/java/view/ViewElement.java +++ b/src/main/java/view/ViewElement.java @@ -2,6 +2,7 @@ package view; import javafx.scene.paint.Color; +//this class declare three constants with each constants is related to a specified color like fire to red public enum ViewElement { FIREFIGHTER(Color.BLUE), FIRE(Color.RED), EMPTY(Color.WHITE); final Color color;