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

debbuged the factories,

added FFboardFiller to initialize the board with custom values
parent 711e8905
No related branches found
No related tags found
No related merge requests found
Pipeline #41202 passed
package model;
import util.Position;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FireFactory implements ElementFactory{
static Map<ModelElement,ModelElement> fireDictionary=new HashMap<>();
static{
fireDictionary.put(ModelElement.ROCK,ModelElement.SLOWFIRE);
}
public ModelElement getNewType(BoardData boardData, Position position){
List<ModelElement> s = boardData.getCell(position).Content.stream().map(x -> x.getType()).toList();
System.out.println("getnewtype");
for(Map.Entry<ModelElement,ModelElement> entry: fireDictionary.entrySet()){
if (s.contains(entry.getKey())) return entry.getValue();
}
return ModelElement.FIRE;
}
public Element getNewElement(BoardData boardData, Position position){
List<ModelElement> s = boardData.getCell(position).Content.stream().map(x -> x.getType()).toList();
System.out.println("getnewtype");
for(Map.Entry<ModelElement,ModelElement> entry: fireDictionary.entrySet()){
if (s.contains(entry.getKey())) return entry.getValue().instanciate(position);
}
return new StandardFireFighter(position);
}
}
......@@ -37,4 +37,9 @@ public List<Position> updateSelf(BoardData boardData) {
}
return modifPositions;
}
@Override
public Behavior getBehavior() {
return behavior;
}
}
......@@ -37,6 +37,15 @@ public class FireFighterBehavior extends FFBehavior implements TangibleBehavior<
return obstacles;
}
@Override
public Boolean isLegal(BoardData boardData, Position position) {
for (Element e:boardData.getCell(position).Content) {
for(ModelElement o:getObstacles())
if (e.getType()==o) return false;
}
return true;
}
public void extinguish(BoardData boardData, Position position){
List<Position> l=new ArrayList<>(legalNeighbors(boardData,position));
l.add(position);
......@@ -52,10 +61,7 @@ public class FireFighterBehavior extends FFBehavior implements TangibleBehavior<
public List<Position> legalNeighbors(BoardData boardData,Position position) {
List<Position> neighbors=new ArrayList<>(boardData.getNeighbor(position));
for (Position p:boardData.getNeighbor(position)) {
for (Element e:boardData.getCell(p).Content) {
for(ModelElement o:getObstacles())
if (e.getType()==o) neighbors.remove(p);
}
if (!isLegal(boardData,p)) neighbors.remove(p);
}
return neighbors;
}
......
......@@ -20,4 +20,8 @@ public abstract class Land implements Element ,Printable{
return type;
}
@Override
public Behavior getBehavior() {
return null;
}
}
......@@ -6,4 +6,5 @@ import java.util.List;
public interface TangibleBehavior<E> extends Behavior {
public List<E> getObstacles();
public Boolean isLegal(BoardData boardData,Position position);
}
package model;
import util.Position;
import java.util.List;
import java.util.Map;
public interface ZoneDependent {
public ElementFactory getFactory();
}
......@@ -2,13 +2,54 @@ package view;
import javafx.scene.paint.Color;
import model.*;
import util.Position;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public enum ViewElement {
FIREFIGHTER(Color.BLUE, StandardFireFighter.class), MOTORIZEDFIREFIGHTER(Color.DARKBLUE, MotorizedFireFighter.class), FIRE(Color.RED,Fire.class),CLOUD(Color.GRAY, Cloud.class) ,MOUNTAIN(Color.BROWN, Mountain.class),ROAD(Color.BLACK, Road.class),ROCK(Color.LIGHTYELLOW, Rock.class), EMPTY(Color.WHITE, null);
FIREFIGHTER(Color.BLUE, StandardFireFighter.class),
MOTORIZEDFIREFIGHTER(Color.DARKBLUE, MotorizedFireFighter.class),
CLOUD(Color.GRAY, Cloud.class) ,
MOUNTAIN(Color.BROWN, Mountain.class),
ROAD(Color.BLACK, Road.class),
ROCK(Color.LIGHTYELLOW, Rock.class),
FIRE(Color.RED,Fire.class,Fire.factory),
EMPTY(Color.WHITE, null);
final Color color;
public final Class<?> c;
final ElementFactory elementFactory;
ViewElement(Color color, Class<?> c) {
this.color = color;
this.c = c;
this.elementFactory=null;
}
ViewElement(Color color, Class<?> c,ElementFactory elementFactory) {
this.color = color;
this.c = c;
this.elementFactory=elementFactory;
}
public Element instanciate(BoardData boardData,Position position){
Element element;
Class<?>[] arg2=new Class[]{BoardData.class,Position.class};
Class<?>[] arg1=new Class[]{Position.class};
if (elementFactory!=null) {
element=elementFactory.getNewElement(boardData,position);
}
else {
try {
element=(Element)this.c.getDeclaredConstructor(arg1).newInstance(position);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
return element;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment