Skip to content
Snippets Groups Projects
Commit 1de6f6f0 authored by plojolo's avatar plojolo
Browse files

Attributs elementsCount ajoutez dans les classes concerné

parent ba1ad89c
No related branches found
No related tags found
No related merge requests found
Pipeline #25753 failed
......@@ -22,6 +22,8 @@ public class SimulatorApplication extends javafx.application.Application {
public static final int INITIAL_CLOUD_COUNT = 2;
public static final int INITIAL_FIRETRUCK_COUNT = 1;
public static final int[] elementsCount = new int[]{INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT, INITIAL_CLOUD_COUNT, INITIAL_FIRETRUCK_COUNT};
private Stage primaryStage;
private Parent view;
private void initializePrimaryStage(Stage primaryStage) {
......@@ -45,8 +47,8 @@ public class SimulatorApplication extends javafx.application.Application {
loader.setLocation(location);
view = loader.load();
Controller controller = loader.getController();
controller.initialize(SQUARE_WIDTH, SQUARE_HEIGHT, COLUMN_COUNT, ROW_COUNT,
INITIAL_FIRE_COUNT, INITIAL_FIREFIGHTER_COUNT, INITIAL_CLOUD_COUNT,INITIAL_FIRETRUCK_COUNT);
controller.initialize(SQUARE_WIDTH, SQUARE_HEIGHT, COLUMN_COUNT, ROW_COUNT, elementsCount);
}
private void showScene() {
......
......@@ -130,9 +130,9 @@ public class Controller {
}
public void initialize(int squareWidth, int squareHeight, int columnCount,
int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount,int initialFireTruckCount) {
int rowCount, int[] elementsCount) {
grid.setDimensions(columnCount, rowCount, squareWidth, squareHeight);
this.setModel(new FirefighterBoard(columnCount, rowCount, initialFireCount, initialFirefighterCount, initialCloudCount, initialFireTruckCount));
this.setModel(new FirefighterBoard(columnCount, rowCount, elementsCount));
repaintGrid();
}
public void oneStepButtonAction() {
......
......@@ -9,6 +9,10 @@ public abstract class AbstractElements implements Elements {
int rowCount = 0;
int columnCount = 0;
ModelElement modelElement;
List<Position> elementsPositions;
public abstract void nextMove();
public abstract void initializeElements(int count, int rowCount, int columnCount);
......@@ -26,5 +30,15 @@ public abstract class AbstractElements implements Elements {
return list;
}
public List<Position> getElementsPositions() {
return elementsPositions;
}
public void addElement(Position position){
elementsPositions.add(position);
}
@Override
public ModelElement getModelElement() {
return modelElement;
}
}
......@@ -4,11 +4,11 @@ import util.Position;
import java.util.*;
public abstract class AbstractFireFighters extends AbstractElements{
public List<Position> firefighterPositions;
public List<Position> elementsPositions;
public Fire fire;
public AbstractFireFighters(int columnCount, int rowCount, Fire fire){
firefighterPositions = new ArrayList<>();
elementsPositions = new ArrayList<>();
this.columnCount = columnCount;
this.rowCount = rowCount;
this.fire = fire;
......@@ -25,7 +25,7 @@ public abstract class AbstractFireFighters extends AbstractElements{
while (!toVisit.isEmpty()) {
Position current = toVisit.poll();
if (fire.firePositions.contains(current)){
if (fire.elementsPositions.contains(current)){
return firstMove.get(current);}
for (Position adjacent : neighbors(current)) {
if (seen.contains(adjacent)) continue;
......@@ -40,8 +40,8 @@ public abstract class AbstractFireFighters extends AbstractElements{
public void initializeElements(int count, int rowCount, int columnCount){
firefighterPositions = new ArrayList<>();
elementsPositions = new ArrayList<>();
for(int i = 0; i < count; i++)
firefighterPositions.add(randomPosition(rowCount, columnCount));
elementsPositions.add(randomPosition(rowCount, columnCount));
}
}
......@@ -6,7 +6,7 @@ import java.util.ArrayList;
import java.util.List;
public class Cloud extends AbstractElements{
List<Position> cloudPositions;
List<Position> elementsPositions;
Fire fire;
......@@ -14,25 +14,26 @@ public class Cloud extends AbstractElements{
this.columnCount = columnCount;
this.rowCount = rowCount;
this.fire = fire;
this.modelElement = ModelElement.CLOUD;
}
@Override
public void nextMove() {
List<Position> cloudNewPositions = new ArrayList<>();
for (Position cloudPosition : cloudPositions) {
for (Position cloudPosition : elementsPositions) {
List<Position> neighborsPositions = neighbors(cloudPosition);
cloudNewPositions.add(neighborsPositions.get(randomGenerator.nextInt(neighborsPositions.size())));
fire.extinguish(cloudPosition);
}
cloudPositions = cloudNewPositions;
elementsPositions = cloudNewPositions;
}
@Override
public void initializeElements(int count, int rowCount, int columnCount) {
cloudPositions = new ArrayList<>();
elementsPositions = new ArrayList<>();
for (int i = 0; i < count; i++) {
cloudPositions.add(randomPosition(rowCount, columnCount));
elementsPositions.add(randomPosition(rowCount, columnCount));
}
System.out.println(cloudPositions);
System.out.println(elementsPositions);
}
}
package model;
import util.Position;
import java.util.List;
public interface Elements {
......@@ -8,5 +12,10 @@ public interface Elements {
void nextMove();
void initializeElements(int count, int rowCount, int columnCount);
List<Position> getElementsPositions();
void addElement(Position position);
ModelElement getModelElement();
}
......@@ -4,41 +4,42 @@ import util.Position;
import java.util.*;
public class Fire extends AbstractElements{
public List<Position> firePositions;
public List<Position> elementsPositions;
public Fire(int columnCount, int rowCount){
firePositions = new ArrayList<>();
elementsPositions = new ArrayList<>();
this.columnCount = columnCount;
this.rowCount = rowCount;
this.modelElement = ModelElement.FIRE;
}
public void nextMove(){
// propagate the fire to the adjacent cells
List<Position> newFirePositions = new ArrayList<>();
for (Position firePosition : firePositions) {
List<Position> newelementsPositions = new ArrayList<>();
for (Position firePosition : elementsPositions) {
List<Position> neighborPositions = neighbors(firePosition);
newFirePositions.addAll(neighborPositions);
newelementsPositions.addAll(neighborPositions);
}
// check if the new fire positions are not already on fire
for (Position newFirePosition : newFirePositions)
if (!firePositions.contains(newFirePosition))
firePositions.add(newFirePosition);
for (Position newFirePosition : newelementsPositions)
if (!elementsPositions.contains(newFirePosition))
elementsPositions.add(newFirePosition);
}
@Override
public void initializeElements(int count, int rowCount, int columnCount) {
firePositions = new ArrayList<>();
elementsPositions = new ArrayList<>();
for(int i = 0; i < count; i++)
firePositions.add(randomPosition(rowCount, columnCount));
elementsPositions.add(randomPosition(rowCount, columnCount));
}
public void extinguishs(List<Position> firefighterPositions){
for (Position firefighterPosition : firefighterPositions) {
List<Position> neighborFirePositions = neighbors(firefighterPosition).stream()
.filter(firePositions::contains).toList();
for(Position firePosition : neighborFirePositions)
List<Position> neighborelementsPositions = neighbors(firefighterPosition).stream()
.filter(elementsPositions::contains).toList();
for(Position firePosition : neighborelementsPositions)
extinguish(firePosition);
}
......@@ -48,6 +49,6 @@ public class Fire extends AbstractElements{
public void extinguish(Position position){
firePositions.remove(position);
elementsPositions.remove(position);
}
}
......@@ -8,17 +8,18 @@ import java.util.List;
public class FireFighterPerson extends AbstractFireFighters{
public FireFighterPerson(int columnCount, int rowCount, Fire fire) {
super(columnCount, rowCount, fire);
this.modelElement = ModelElement.FIREFIGHTER;
}
@Override
public void nextMove(){
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
for (Position firefighterPosition : elementsPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
firefighterNewPositions.add(newFirefighterPosition);
fire.extinguish(newFirefighterPosition);
}
firefighterPositions = firefighterNewPositions;
elementsPositions = firefighterNewPositions;
}
}
......@@ -8,17 +8,18 @@ import java.util.List;
public class FireTruck extends AbstractFireFighters{
public FireTruck(int columnCount, int rowCount, Fire fire) {
super(columnCount, rowCount, fire);
this.modelElement = ModelElement.FIRETRUCK;
}
public void nextMove(){
List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) {
for (Position firefighterPosition : elementsPositions) {
Position newFirefighterPosition = neighborClosestToFire(firefighterPosition);
newFirefighterPosition = neighborClosestToFire(newFirefighterPosition);
firefighterNewPositions.add(newFirefighterPosition);
fire.extinguish(newFirefighterPosition);
}
firefighterPositions = firefighterNewPositions;
elementsPositions = firefighterNewPositions;
}
}
......@@ -10,6 +10,8 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
public int columnCount;
public int rowCount;
public final Elements[] elements;
private final int initialFireCount;
private final int initialFirefighterCount;
private final int initialCloudCount;
......@@ -23,17 +25,22 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
public FireTruck fireTrucks;
public Cloud cloud;
public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount, int initialCloudCount, int initialFireTruckCount) {
public FirefighterBoard(int columnCount, int rowCount, int[] elementsCount) {
this.columnCount = columnCount;
this.rowCount = rowCount;
this.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount;
this.initialCloudCount = initialCloudCount;
this.initialFireTruckCount = initialFireTruckCount;
this.initialFireCount = elementsCount[0];
this.initialFirefighterCount = elementsCount[1];
this.initialCloudCount = elementsCount[2];
this.initialFireTruckCount = elementsCount[3];
fire = new Fire(columnCount, rowCount);
fireFighters = new FireFighterPerson(columnCount, rowCount, fire);
cloud = new Cloud(columnCount, rowCount, fire);
fireTrucks = new FireTruck(columnCount, rowCount, fire);
elements = new Elements[]{fire, fireFighters, cloud, fireTrucks};
initializeElements();
}
......@@ -48,14 +55,11 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
@Override
public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>();
if (fire.firePositions.contains(position))
result.add(ModelElement.FIRE);
if (fireFighters.firefighterPositions.contains(position))
result.add(ModelElement.FIREFIGHTER);
if (cloud.cloudPositions.contains(position))
result.add(ModelElement.CLOUD);
if (fireTrucks.firefighterPositions.contains(position))
result.add(ModelElement.FIRETRUCK);
for(Elements element : elements)
if (element.getElementsPositions().contains(position))
result.add(element.getModelElement());
return result;
}
......@@ -72,10 +76,10 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
public List<Position> updateToNextGeneration() {
List<Position> modifiedElements = new ArrayList<>();
modifiedElements.addAll(fire.firePositions);
modifiedElements.addAll(fireFighters.firefighterPositions);
modifiedElements.addAll(cloud.cloudPositions);
modifiedElements.addAll(fireTrucks.firefighterPositions);
for (Elements element : elements) {
modifiedElements.addAll(element.getElementsPositions());
}
fireFighters.nextMove();
//System.out.println(fireFighters.firefighterPositions);
......@@ -84,19 +88,18 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
cloud.nextMove();
fire.extinguishs(fireFighters.firefighterPositions);
fire.extinguishs(fireTrucks.firefighterPositions);
fire.extinguishs(cloud.cloudPositions);
fire.extinguishs(fireFighters.getElementsPositions());
fire.extinguishs(fireTrucks.getElementsPositions());
fire.extinguishs(cloud.getElementsPositions());
if (step % 2 == 0)
fire.nextMove();
step++;
modifiedElements.addAll(fire.firePositions);
modifiedElements.addAll(fireFighters.firefighterPositions);
modifiedElements.addAll(cloud.cloudPositions);
modifiedElements.addAll(fireTrucks.firefighterPositions);
for (Elements element : elements) {
modifiedElements.addAll(element.getElementsPositions());
}
return modifiedElements;
}
......@@ -116,16 +119,16 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
@Override
public void setState(List<ModelElement> state, Position position) {
fire.firePositions.remove(position);
fire.getElementsPositions().remove(position);
for (;;) {
if (!fireFighters.firefighterPositions.remove(position)) break;
if (!fireFighters.getElementsPositions().remove(position)) break;
}
for(ModelElement element : state){
switch (element){
case FIRE -> fire.firePositions.add(position);
case FIREFIGHTER -> fireFighters.firefighterPositions.add(position);
case CLOUD -> cloud.cloudPositions.add(position);
case FIRETRUCK -> fireTrucks.firefighterPositions.add(position);
case FIRE -> fire.addElement(position);
case FIREFIGHTER -> fireFighters.addElement(position);
case CLOUD -> cloud.addElement(position);
case FIRETRUCK -> fireTrucks.addElement(position);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment