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

Road corrected (connex)

parent d4e925f7
No related branches found
No related tags found
No related merge requests found
Pipeline #41694 failed
package model;
public interface ConnexElement extends Element {
public ElementFactory getFactory();
}
......@@ -6,7 +6,6 @@ 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);
}
......@@ -31,14 +31,15 @@ public class FFBoard implements Board<List<ModelElement>> {
FFUpdater= new FFUpdater();
FFboardFiller filler=new FFboardFiller();
filler.fillBoard(boardData,new int[]{
1,//roads
20,//forests
10,//rocks
30,//mountains
5,//firefighter
2,//motorized firefighter
2,//clouds
30,//mountains
10,//roads
20,//forests
10,//fires
10//rocks
});
}
......
......@@ -5,19 +5,25 @@ import view.ViewElement;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class FFboardFiller {
Random random=new Random();
public void fillBoard(BoardData boardData,int[] counter){
List<Position> positions= new ArrayList<>(List.copyOf(boardData.getNeighbors().keySet()));
for(ViewElement ve:ViewElement.values()){
if (ve!=ViewElement.EMPTY)
while(counter[ve.ordinal()]>0) {
Element element;
Position p;
do {
Position p=new Position(random.nextInt(boardData.getRowCount()), random.nextInt(boardData.getColumnCount()));
p=new Position(random.nextInt(boardData.getRowCount()), random.nextInt(boardData.getColumnCount()));
element= ve.instanciate(boardData,p);
}while (!boardData.addElement(element));
}while (!boardData.addElement(element) && positions.contains(element.getPosition()));
positions.remove(p);
counter[ve.ordinal()]--;
}
}
......
......@@ -11,13 +11,6 @@ public class FireFactory implements ElementFactory{
static{
fireDictionary.put(ModelElement.FOREST,ModelElement.SLOWFIRE);
}
public ModelElement getNewType(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()){
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();
for(Map.Entry<ModelElement,ModelElement> entry: fireDictionary.entrySet()){
......
......@@ -2,9 +2,15 @@ package model;
import util.Position;
public class Road extends Land implements Element {
public class Road extends Land implements Element,ConnexElement {
public static ElementFactory factory=new RoadFactory();
public Road(Position position) {
super(position);
type=ModelElement.ROAD;
}
@Override
public ElementFactory getFactory() {
return null;
}
}
package model;
import util.Position;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class RoadFactory implements ElementFactory{
Random random = new Random();
public Element getNewElement(BoardData boardData, Position position){
int[][] dir={{0,1},{0,-1},{1,0},{-1,0}};
int[] currentDirection1,currentDirection2;
do{
currentDirection1 = dir[random.nextInt(4)];
currentDirection2 = dir[random.nextInt(4)];
}while(currentDirection1==currentDirection2);
for (int i = position.row(),j= position.column();
i < boardData.getRowCount() && i >= 0 && j < boardData.getColumnCount() && j >= 0 ;
i+=currentDirection1[0],j+=currentDirection1[1]) {
System.out.println(i+","+j);
boardData.addElement(new Road(new Position(i,j)));
}
for (int i = position.row(),j= position.column();
i < boardData.getRowCount() && i >= 0 && j < boardData.getColumnCount() && j >= 0 ;
i+=currentDirection2[0],j+=currentDirection2[1]) {
boardData.addElement(new Road(new Position(i,j)));
}
return new Road(position);
}
}
......@@ -7,15 +7,14 @@ import util.Position;
import java.lang.reflect.InvocationTargetException;
public enum ViewElement {
FIREFIGHTER(Color.BLUE, StandardFireFighter.class),
ROAD(Color.BLACK, Road.class, Road.factory),
FOREST(Color.DARKGREEN, Forest.class),
ROCK(Color.GREEN, Rock.class),
MOUNTAIN(Color.DARKGRAY, Mountain.class),
FIREFIGHTER(Color.LIGHTBLUE, StandardFireFighter.class),
MOTORIZEDFIREFIGHTER(Color.DARKBLUE, MotorizedFireFighter.class),
CLOUD(Color.GRAY, Cloud.class),
MOUNTAIN(Color.BROWN, Mountain.class),
ROAD(Color.BLACK, Road.class),
FOREST(Color.GREEN, Forest.class),
CLOUD(Color.LIGHTGRAY, Cloud.class),
FIRE(Color.RED,Fire.class,Fire.factory),
ROCK(Color.DARKGOLDENROD, Rock.class),
EMPTY(Color.WHITE, null);
final Color color;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment