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

dsfsdfsd

parent 5d451f57
No related branches found
No related tags found
No related merge requests found
Pipeline #24840 passed
Showing
with 45 additions and 106 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -33,7 +33,7 @@ ...@@ -33,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>
......
File deleted
File deleted
No preview for this file type
...@@ -27,7 +27,7 @@ import static java.util.Objects.requireNonNull; ...@@ -27,7 +27,7 @@ import static java.util.Objects.requireNonNull;
public class Controller { public class Controller {
public static final int PERIOD_IN_MILLISECONDS = 500; public static final int PERIOD_IN_MILLISECONDS = 50;
@FXML @FXML
public Button restartButton; public Button restartButton;
@FXML @FXML
...@@ -151,10 +151,10 @@ public class Controller { ...@@ -151,10 +151,10 @@ public class Controller {
int rowCount, int initialFireCount, int initialFirefighterCount) { int rowCount, int initialFireCount, int initialFirefighterCount) {
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight); grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
List<item> items= new ArrayList<>(); List<item> items= new ArrayList<>();
List<item> list= new ArrayList<>(); items.add(new Fire(initialFireCount));
list.add(new Fire(initialFireCount)); items.add(new FireFighter(initialFirefighterCount));
list.add(new FireFighter(initialFirefighterCount,b));
Fboard b=new Fboard(columnCount,rowCount,items); Fboard b=new Fboard(columnCount,rowCount,items);
b.initializeElements();
this.setModel(b); this.setModel(b);
repaintGrid(); repaintGrid();
} }
......
...@@ -5,12 +5,11 @@ import util.Position; ...@@ -5,12 +5,11 @@ import util.Position;
import java.util.List; import java.util.List;
public abstract class Extinguisher implements item { public abstract class Extinguisher implements item {
final int ID=1;
public abstract List<Position> update(); public abstract List<Position> update(Fboard board);
public abstract void extinguish(Position position); public abstract void extinguish(Position position,Fboard board);
public abstract void setState();
public abstract ModelElement getState(); public abstract ModelElement getState();
public abstract void initialize(); public abstract void initialize();
......
...@@ -11,7 +11,7 @@ public class Fboard { ...@@ -11,7 +11,7 @@ public class Fboard {
protected static int rowCount; protected static int rowCount;
private int step = 0; private int step = 0;
private List<item> items; private final List<item> items;
public Fboard(int columnCount, int rowCount,List<item> item) { public Fboard(int columnCount, int rowCount,List<item> item) {
this.items=item; this.items=item;
...@@ -32,12 +32,12 @@ public class Fboard { ...@@ -32,12 +32,12 @@ public class Fboard {
List<Position> result = new ArrayList<>(); List<Position> result = new ArrayList<>();
for(item item : items){ for(item item : items){
if(item.getID()==1){ if(item.getID()==1){
result.addAll(item.update()); result.addAll(item.update(this));
} }
} }
for(item item : items){ for(item item : items){
if(item.getID()==0 && step % 2 ==0){ if(item.getID()==0 && step % 2 ==0){
result.addAll(item.update()); result.addAll(item.update(this));
} }
} }
step++; step++;
...@@ -63,27 +63,18 @@ public class Fboard { ...@@ -63,27 +63,18 @@ public class Fboard {
} }
} }
} }
public int getStep(){
return step;
}
public int getColumnCount(){
return this.columnCount;
}
public int getRowCount(){
return this.rowCount;
}
public Fire getFire() { public Fire getFire() {
Fire x = null; Fire x = null;
for (item e : items) { for (item e : items) {
if (e.getID() == 0) { if (e.getID() == 0) {
x = (Fire) e; x = (Fire) e;
break;
} }
} }
return x; return x;
} }
public static List<Position> neighbors(Position position) { public List<Position> neighbors(Position position) {
List<Position> list = new ArrayList<>(); List<Position> list = new ArrayList<>();
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column())); if (position.row() > 0) list.add(new Position(position.row() - 1, position.column()));
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1)); if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1));
...@@ -92,10 +83,10 @@ public class Fboard { ...@@ -92,10 +83,10 @@ public class Fboard {
return list; return list;
} }
public static Position neighborClosestToFire(Position position) { public Position neighborClosestToFire(Position position) {
Set<Position> seen = new HashSet<>(); Set<Position> seen = new HashSet<>();
HashMap<Position, Position> firstMove = new HashMap<>(); HashMap<Position, Position> firstMove = new HashMap<>();
Queue<Position> toVisit = new LinkedList<>(neighbors(position)); //Queue is initialised with the neighbors of position Queue<Position> toVisit = new LinkedList<>(this.neighbors(position)); //Queue is initialised with the neighbors of position
for (Position initialMove : toVisit) for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove); firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) { while (!toVisit.isEmpty()) {
...@@ -115,12 +106,12 @@ public class Fboard { ...@@ -115,12 +106,12 @@ public class Fboard {
public List<ModelElement> getState(Position position){ public List<ModelElement> getState(Position position){
List<ModelElement> result = new ArrayList<>(); List<ModelElement> result = new ArrayList<>();
for (item e :items) { for (item e :items) {
if (e.getPositions().equals(position) && e.getID()==1){ if (e.getPositions().contains(position) && e.getID() == 1) {
result.add(e.getState()); result.add(e.getState());break;
} }
if (e.getPositions().contains(position) && e.getID() == 0)
result.add(getFire().getState());break;
} }
if(getFire().containsFire(position))
result.add(getFire().getState());
return result; return result;
} }
......
...@@ -10,31 +10,29 @@ public class Fire implements item{ ...@@ -10,31 +10,29 @@ public class Fire implements item{
private final int initialFireCount; private final int initialFireCount;
private List<Position> firePositions; private List<Position> firePositions;
private ModelElement state; private final ModelElement state;
private final Random randomGenerator = new Random(); private final Random randomGenerator = new Random();
private final int ID=0; private final int ID=0;
public Fire(int initialFireCount) { public Fire(int initialFireCount) {
this.initialFireCount = initialFireCount;this.state=ModelElement.FIRE; this.initialFireCount = initialFireCount;
this.state=ModelElement.FIRE;
} }
public List<Position> update() { public List<Position> update(Fboard board) {
List<Position> result = new ArrayList<>();
List<Position> newFirePositions = new ArrayList<>(); List<Position> newFirePositions = new ArrayList<>();
for (Position fire : firePositions) { for (Position fire : firePositions) {
newFirePositions.addAll(Fboard.neighbors(fire)); newFirePositions.addAll(board.neighbors(fire));
} }
firePositions.addAll(newFirePositions); firePositions.addAll(newFirePositions);
result.addAll(newFirePositions); return newFirePositions;
return result;
} }
public ModelElement getState() { public ModelElement getState() {
return state; return this.state;
} }
public int getID() { public int getID() {
...@@ -61,11 +59,4 @@ public class Fire implements item{ ...@@ -61,11 +59,4 @@ public class Fire implements item{
} }
public boolean containsFire(Position position) {
if (firePositions.contains(position)) {
return true;
}
else return false;
}
} }
...@@ -4,31 +4,36 @@ import util.Position; ...@@ -4,31 +4,36 @@ import util.Position;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import model.ModelElement;
public class FireFighter extends Extinguisher { public class FireFighter extends Extinguisher {
private ModelElement state; private final ModelElement state;
private List<Position> firefighterPositions; private List<Position> firefighterPositions;
private final Random randomGenerator = new Random(); private final Random randomGenerator = new Random();
private final int initialFirefighterCount; private final int initialFirefighterCount;
private int ID=1;
public FireFighter(int initialFirefighterCount){ public FireFighter(int initialFirefighterCount){
this.initialFirefighterCount=initialFirefighterCount; this.initialFirefighterCount=initialFirefighterCount;
this.state=ModelElement.FIREFIGHTER;
} }
public List<Position> update() { public List<Position> update(Fboard board) {
List<Position> result = new ArrayList<>(); List<Position> result = new ArrayList<>();
List<Position> firefighterNewPositions = new ArrayList<>(); List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) { for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = Fboard.neighborClosestToFire(firefighterPosition); Position newFirefighterPosition = board.neighborClosestToFire(firefighterPosition);
firefighterNewPositions.add(newFirefighterPosition); firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition); extinguish(newFirefighterPosition,board);
result.add(firefighterPosition); result.add(firefighterPosition);
result.add(newFirefighterPosition); result.add(newFirefighterPosition);
List<Position> neighborFirePositions = Fboard.neighbors(newFirefighterPosition).stream() List<Position> neighborFirePositions = board.neighbors(newFirefighterPosition).stream()
.filter(board.getFire().getPositions()::contains).toList(); //this code create a list filtred with only the fire positions that are close to the newfirefighter position .filter(board.getFire().getPositions()::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 for(Position firePosition : neighborFirePositions) //and exist in the firepositions list also
extinguish(firePosition); extinguish(firePosition,board);
result.addAll(neighborFirePositions); result.addAll(neighborFirePositions);
} }
firefighterPositions = firefighterNewPositions; firefighterPositions = firefighterNewPositions;
...@@ -36,22 +41,18 @@ public class FireFighter extends Extinguisher { ...@@ -36,22 +41,18 @@ public class FireFighter extends Extinguisher {
} }
public void extinguish(Position position) { public void extinguish(Position position,Fboard board) {
this.board.getFire().extinguish(position); board.getFire().extinguish(position);
}
public void setState() {
state= ModelElement.FIREFIGHTER;
} }
public ModelElement getState() { public ModelElement getState() {
return state; return this.state;
} }
@Override // implementi liha pattern visiteur
public int getID() { public int getID() {
return this.ID; return this.ID;
} }
......
package newmodel;
import model.ModelElement;
import util.Position;
import java.util.List;
public class MotoFireFighter extends Extinguisher {
@Override
public List<Position> update() {
return null;
}
@Override
public void extinguish(Position position) {
}
@Override
public void setState() {
}
@Override
public ModelElement getState() {
return null;
}
@Override
public int getID() {
return 0;
}
@Override
public void initialize() {
}
@Override
public List<Position> getPositions() {
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment