Skip to content
Snippets Groups Projects
Commit 5f57314d authored by BACHTARZI Imed eddine's avatar BACHTARZI Imed eddine
Browse files

added MotorizedFireFighter

used the Factory Method through ElementFactory
parent 709906a3
No related branches found
No related tags found
No related merge requests found
Pipeline #41012 passed
Showing
with 64 additions and 11 deletions
File added
File added
File added
File added
No preview for this file type
package model;
import util.Position;
public class ElementFactory {
//this is using the factoryMethod design pattern (I'm writing this it just in case )
public static Element createElement(BoardData boardData,ModelElement modelElement, Position position){
Element e=modelElement.instanciate(position);
boardData.addElement(e);
return e;
}
}
......@@ -33,10 +33,12 @@ public class FFBoard implements Board<List<ModelElement>> {
boardData.addElement(new StandardFire(randomPosition()));
for (int index = 0; index < initialFirefighterCount; index++)
boardData.addElement(new StandardFireFighter(randomPosition()));
for (int index = 0; index < 2; index++)
boardData.addElement(new MotorizedFireFighter(randomPosition()));
for (int index = 0; index < 10; index++)
boardData.addElement(new Cloud(randomPosition()));
for (int index = 0; index < 50; index++)
boardData.addElement(new Mountain(randomPosition()));
boardData.addElement(new Road(randomPosition()));
}
......
......@@ -56,10 +56,13 @@ public class FFBoardData implements BoardData{
return neighbors.get(position);
}
public void addElement(Element element){
if(element!=null)
{
elementList.get(element.getType().ordinal()).add(element);
getCell(element.getPosition()).Content.add(element);
FFUpdater.modifiedPositions.add(element.getPosition());
}
}
public void removeElement(Element element){
FFUpdater.modifiedPositions.add(element.getPosition());
elementList.get(element.getType().ordinal()).remove(element);
......
......@@ -12,12 +12,14 @@ public abstract class Fire implements Element,Updatable{
ModelElement type;
Position position;
static Behavior behavior=new FireBehavior();
Map<ModelElement,ModelElement> FireDictionary=new HashMap<>(); //.put(MAISON,FEUMAISON)
Map<ModelElement,ModelElement> fireDictionary=new HashMap<>();
//.put(MAISON,FEUMAISON)
public Fire(int delay, ModelElement type, Position position) {
this.delay = delay;
this.type = type;
this.position = position;
fireDictionary.put(ModelElement.MOUNTAIN,null);
}
@Override
public Position getPosition() {
......@@ -29,7 +31,7 @@ public abstract class Fire implements Element,Updatable{
}
private Fire getNewFireByType(BoardData boardData,Position position){
List<ModelElement> s = boardData.getCell(position).Content.stream().map(x -> x.getType()).toList();
for(Map.Entry<ModelElement,ModelElement> entry: FireDictionary.entrySet()){
for(Map.Entry<ModelElement,ModelElement> entry: fireDictionary.entrySet()){
if (s.contains(entry.getKey())) return (Fire) entry.getValue().instanciate(position);
}
return new StandardFire(position);
......@@ -38,8 +40,7 @@ public abstract class Fire implements Element,Updatable{
public List<Position> updateSelf(BoardData boardData) {
List<Position> positions=behavior.update(boardData,this);
for (Position p:positions) {
boardData.addElement(new StandardFire(p));
boardData.addElement(getNewFireByType(boardData,p));
}
return positions;
}
......
......@@ -8,7 +8,7 @@ import java.util.Map;
import java.util.Set;
public class FireBehavior extends FFBehavior /*implements TangibleBehavior<ModelElement>*/ {
private List<ModelElement> obstacles=List.of(ModelElement.MOUNTAIN);
private List<ModelElement> obstacles=List.of(ModelElement.MOUNTAIN,ModelElement.ROAD);
@Override
public List<Position> update(BoardData boardData,Element element) {
......
......@@ -10,9 +10,11 @@ public enum ModelElement {
FIREFIGHTER(StandardFireFighter.class),
MOTORIZEDFIREFIGHTER(MotorizedFireFighter.class),
FIRE(StandardFire.class),
CLOUD(Cloud.class),
MOUNTAIN(Mountain.class);
MOUNTAIN(Mountain.class),
ROAD(Road.class);
public final Class<?> c;
......
package model;
import util.Position;
public class MotorizedFireFighter extends FireFighter implements Updatable{
public MotorizedFireFighter(Position position) {
super(2, ModelElement.MOTORIZEDFIREFIGHTER, position);
}
}
package model;
import util.Position;
public class Road implements Element {
Position position;
ModelElement type;
public Road(Position position) {
this.position = position;
type=ModelElement.ROAD;
}
@Override
public Position getPosition() {
return position;
}
@Override
public ModelElement getType() {
return type;
}
}
......@@ -3,7 +3,7 @@ package view;
import javafx.scene.paint.Color;
public enum ViewElement {
FIREFIGHTER(Color.BLUE), FIRE(Color.RED),CLOUD(Color.GRAY) ,MOUNTAIN(Color.BROWN) , EMPTY(Color.WHITE);
FIREFIGHTER(Color.BLUE), MOTORIZEDFIREFIGHTER(Color.DARKBLUE), FIRE(Color.RED),CLOUD(Color.GRAY) ,MOUNTAIN(Color.BROWN),ROAD(Color.BLACK), EMPTY(Color.WHITE);
final Color color;
ViewElement(Color color) {
this.color = color;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment