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
Branches
No related tags found
No related merge requests found
Pipeline #41202 passed
Showing
with 97 additions and 74 deletions
File added
No preview for this file type
File added
No preview for this file type
File added
File added
File added
File added
File added
No preview for this file type
......@@ -8,12 +8,13 @@ import java.util.Map;
public interface BoardData {
public List<List<Element>> getElements();
public Cell<Element> getCell(Position position);
public void addElement(Element element);
public Boolean addElement(Element element);
public void removeElement(Element element);
public Map<Position, List<Position>> getNeighbors();
public List<Position> getNeighbor(Position position);
public int getStep();
public void setStep(int step);
public void initialize();
public int getColumnCount();
public int getRowCount();
}
......@@ -22,6 +22,12 @@ public Position getPosition() {
public ModelElement getType() {
return type;
}
@Override
public Behavior getBehavior() {
return behavior;
}
@Override
public List<Position> updateSelf(BoardData boardData) {
List<Position> positions=behavior.update(boardData,this);
......
......@@ -5,5 +5,6 @@ import util.Position;
public interface Element {
Position getPosition();
ModelElement getType();
Behavior getBehavior();
}
......@@ -2,10 +2,11 @@ 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){
return modelElement.instanciate(position);
}
import java.util.List;
import java.util.Map;
public interface ElementFactory {
public ModelElement getNewType(BoardData boardData, Position position);
public Element getNewElement(BoardData boardData, Position position);
}
......@@ -29,17 +29,15 @@ public class FFBoard implements Board<List<ModelElement>> {
public void initializeElements() {
boardData.initialize();
FFUpdater= new FFUpdater();
for (int index = 0; index < initialFireCount; index++)
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 < 1000; index++)
boardData.addElement(new Rock(randomPosition()));
FFboardFiller filler=new FFboardFiller();
filler.fillBoard(boardData,new int[]{
5,//firefighter
2,//motorized firefighter
3,//clouds
10,//mountains
5,//roads
10,//rocks
10});//fires
}
private Position randomPosition() {
......
......@@ -9,7 +9,7 @@ public class FFBoardData implements BoardData{
private List<List<Element>> elementList;
private Map<Position, List<Position>> neighbors = new HashMap<Position, List<Position>>();
private int step;
private int columnCount,rowCount;
public int columnCount,rowCount;
private List<List<Cell<Element>>> cells;
public FFBoardData(int columnCount, int rowCount) {
this.columnCount=columnCount;
......@@ -43,6 +43,16 @@ public class FFBoardData implements BoardData{
}
}
@Override
public int getColumnCount() {
return columnCount;
}
@Override
public int getRowCount() {
return rowCount;
}
@Override
public List<List<Element>> getElements() {
......@@ -66,13 +76,17 @@ public class FFBoardData implements BoardData{
public List<Position> getNeighbor(Position position) {
return neighbors.get(position);
}
public void addElement(Element element){
if(element!=null)
{
public Boolean addElement(Element element){
if(element==null){ return false;}
if (element.getBehavior()!=null)
if (element.getBehavior() instanceof TangibleBehavior<?>)
if (!((TangibleBehavior)element.getBehavior()).isLegal(this,element.getPosition()))
return false;
System.out.println(element.getType());
elementList.get(element.getType().ordinal()).add(element);
getCell(element.getPosition()).Content.add(element);
FFUpdater.modifiedPositions.add(element.getPosition());
}
return true;
}
public void removeElement(Element element){
FFUpdater.modifiedPositions.add(element.getPosition());
......
......@@ -18,7 +18,6 @@ public class FFUpdater implements Updater {
updatables.clear();
modifiedPositions=new ArrayList<Position>();
for (ModelElement modelElement: ModelElement.values()) {
System.out.println(modelElement+" "+modelElement.isUpdatable());
if (modelElement.isUpdatable())
for (Element e:boardData.getElements().get(modelElement.ordinal())) {
updatables.add((Updatable) e);
......
package model;
import util.Position;
import view.ViewElement;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Random;
public class FFboardFiller {
Random random=new Random();
public void fillBoard(BoardData boardData,int[] counter){
for(ViewElement ve:ViewElement.values()){
if (ve!=ViewElement.EMPTY)
while(counter[ve.ordinal()]>0) {
Element element;
do {
Position p=new Position(random.nextInt(boardData.getRowCount()), random.nextInt(boardData.getColumnCount()));
element= ve.instanciate(boardData,p);
}while (!boardData.addElement(element));
counter[ve.ordinal()]--;
}
}
}
}
......@@ -7,22 +7,20 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public abstract class Fire implements Element,Updatable,Printable{
public abstract class Fire implements Element,Updatable,Printable,ZoneDependent{
int counter;
int delay;
ModelElement type;
Position position;
static Behavior behavior=new FireBehavior();
static Map<ModelElement,ModelElement> fireDictionary=new HashMap<>();
//.put(MAISON,FEUMAISON)
public static FireFactory factory=new FireFactory();
public Fire(int delay, ModelElement type, Position position) {
this.counter=0;
this.delay = delay;
this.type = type;
this.position = position;
fireDictionary.put(ModelElement.ROCK,ModelElement.SLOWFIRE);
fireDictionary.put(ModelElement.MOUNTAIN,null);
}
@Override
public Position getPosition() {
......@@ -32,21 +30,24 @@ public abstract class Fire implements Element,Updatable,Printable{
public ModelElement getType() {
return type;
}
public static 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()){
System.out.println(entry.toString()+" "+s.contains(entry.getKey())+" "+entry.getValue());
if (s.contains(entry.getKey())) return (Fire) entry.getValue().instanciate(position);
}
return new StandardFire(position);
}
@Override
public List<Position> updateSelf(BoardData boardData) {
counter++;
List<Position> positions=behavior.update(boardData,this);
for (Position p:positions) {
boardData.addElement(getNewFireByType(boardData,p));
boardData.addElement(getFactory().getNewElement(boardData,p));
}
return positions;
}
@Override
public ElementFactory getFactory() {
return factory;
}
@Override
public Behavior getBehavior() {
return behavior;
}
}
......@@ -22,11 +22,7 @@ public class FireBehavior extends FFBehavior implements TangibleBehavior<ModelE
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) {
if (e instanceof Fire) neighbors.remove(p);
for(ModelElement o:getObstacles())
if (e.getType()==o) neighbors.remove(p);
}
if (!isLegal(boardData,p)) neighbors.remove(p);
}
return neighbors;
}
......@@ -40,38 +36,19 @@ public class FireBehavior extends FFBehavior implements TangibleBehavior<ModelE
return neighbors;
}
@Override
public List<ModelElement> getObstacles() {
return obstacles;
}
/*@Override
public List<Position> getNext(BoardData boardData) {
List<Position> firePositions = boardData.getPositions().get(element.ordinal());
List<Position> newFirePositions = new ArrayList<>();
if (boardData.getStep() % 2 == 0) {
for (Position fire : firePositions) {
for (Position neighbor:legalNeighbors(boardData).get(fire)) {
if (!newFirePositions.contains(neighbor))
newFirePositions.add(neighbor);
}
}
}
return newFirePositions;
}
@Override
public List<ModelElement> getObstacles() {
return obstacles;
}
@Override
public Map<Position, List<Position>> legalNeighbors(BoardData boardData) {
Map<Position,List<Position>> neigbors= super.legalNeighbors(boardData);
for (Map.Entry<Position,List<Position>> entry: neigbors.entrySet()) {
for (ModelElement e:getObstacles()) {
entry.getValue().removeAll(boardData.getPositions().get(e.ordinal()));
public Boolean isLegal(BoardData boardData,Position position) {
System.out.println("is legal");
for (Element e:boardData.getCell(position).Content) {
if (e instanceof Fire) return false;
for(ModelElement o:getObstacles())
if (e.getType()==o) return false;
}
return true;
}
return neigbors;
}*/
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment