Skip to content
Snippets Groups Projects
Commit 588a2b48 authored by COUETOUX Basile's avatar COUETOUX Basile
Browse files

finish extract

parent 647d95cd
Branches
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ public class SimulatorApplication extends javafx.application.Application { ...@@ -26,7 +26,7 @@ public class SimulatorApplication extends javafx.application.Application {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
this.primaryStage.setTitle(APP_NAME); this.primaryStage.setTitle(APP_NAME);
this.primaryStage.setOnCloseRequest(event -> Platform.exit()); this.primaryStage.setOnCloseRequest(event -> Platform.exit());
this.primaryStage.setResizable(false); this.primaryStage.setResizable(true);
this.primaryStage.sizeToScene(); this.primaryStage.sizeToScene();
} }
......
...@@ -10,15 +10,30 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -10,15 +10,30 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
private final int rowCount; private final int rowCount;
private final int initialFireCount; private final int initialFireCount;
private final int initialFirefighterCount; private final int initialFirefighterCount;
private final TargetStrategy targetStrategy = new TargetStrategy(this); private final TargetStrategy targetStrategy = new TargetStrategy();
private List<Position> firefighterPositions; private List<Position> firefighterPositions;
private Set<Position> firePositions; private Set<Position> firePositions;
private Map<Position, List<Position>> neighbors = new HashMap();
private final Position[][] positions;
private int step = 0; private int step = 0;
private final Random randomGenerator = new Random(); private final Random randomGenerator = new Random();
public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) { public FirefighterBoard(int columnCount, int rowCount, int initialFireCount, int initialFirefighterCount) {
this.columnCount = columnCount; this.columnCount = columnCount;
this.rowCount = rowCount; this.rowCount = rowCount;
this.positions = new Position[rowCount][columnCount];
for (int column = 0; column < columnCount; column++)
for (int row = 0; row < rowCount; row++)
positions[row][column] = new Position(row, column);
for (int column = 0; column < columnCount; column++)
for (int row = 0; row < rowCount; row++) {
List<Position> list = new ArrayList<>();
if (row > 0) list.add(positions[row - 1][column]);
if (column > 0) list.add(positions[row][column - 1]);
if (row < rowCount - 1) list.add(positions[row + 1][column]);
if (column < columnCount - 1) list.add(positions[row][column + 1]);
neighbors.put(positions[row][column], list);
}
this.initialFireCount = initialFireCount; this.initialFireCount = initialFireCount;
this.initialFirefighterCount = initialFirefighterCount; this.initialFirefighterCount = initialFirefighterCount;
initializeElements(); initializeElements();
...@@ -40,10 +55,10 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -40,10 +55,10 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
@Override @Override
public List<ModelElement> getState(Position position) { public List<ModelElement> getState(Position position) {
List<ModelElement> result = new ArrayList<>(); List<ModelElement> result = new ArrayList<>();
for(Position firefighterPosition : firefighterPositions) for (Position firefighterPosition : firefighterPositions)
if (firefighterPosition.equals(position)) if (firefighterPosition.equals(position))
result.add(ModelElement.FIREFIGHTER); result.add(ModelElement.FIREFIGHTER);
if(firePositions.contains(position)) if (firePositions.contains(position))
result.add(ModelElement.FIRE); result.add(ModelElement.FIRE);
return result; return result;
} }
...@@ -70,7 +85,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -70,7 +85,7 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
if (step % 2 == 0) { if (step % 2 == 0) {
List<Position> newFirePositions = new ArrayList<>(); List<Position> newFirePositions = new ArrayList<>();
for (Position fire : firePositions) { for (Position fire : firePositions) {
newFirePositions.addAll(neighbors(fire)); newFirePositions.addAll(neighbors.get(fire));
} }
firePositions.addAll(newFirePositions); firePositions.addAll(newFirePositions);
modifiedPositions.addAll(newFirePositions); modifiedPositions.addAll(newFirePositions);
...@@ -89,14 +104,15 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -89,14 +104,15 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
List<Position> firefighterNewPositions = new ArrayList<>(); List<Position> firefighterNewPositions = new ArrayList<>();
for (Position firefighterPosition : firefighterPositions) { for (Position firefighterPosition : firefighterPositions) {
Position newFirefighterPosition = Position newFirefighterPosition =
targetStrategy.neighborClosestToFire(firefighterPosition, firePositions); targetStrategy.neighborClosestToFire(firefighterPosition,
firePositions, neighbors);
firefighterNewPositions.add(newFirefighterPosition); firefighterNewPositions.add(newFirefighterPosition);
extinguish(newFirefighterPosition); extinguish(newFirefighterPosition);
modifiedPosition.add(firefighterPosition); modifiedPosition.add(firefighterPosition);
modifiedPosition.add(newFirefighterPosition); modifiedPosition.add(newFirefighterPosition);
List<Position> neighborFirePositions = neighbors(newFirefighterPosition).stream() List<Position> neighborFirePositions = neighbors.get(newFirefighterPosition).stream()
.filter(firePositions::contains).toList(); .filter(firePositions::contains).toList();
for(Position firePosition : neighborFirePositions) for (Position firePosition : neighborFirePositions)
extinguish(firePosition); extinguish(firePosition);
modifiedPosition.addAll(neighborFirePositions); modifiedPosition.addAll(neighborFirePositions);
} }
...@@ -114,29 +130,18 @@ public class FirefighterBoard implements Board<List<ModelElement>> { ...@@ -114,29 +130,18 @@ public class FirefighterBoard implements Board<List<ModelElement>> {
firePositions.remove(position); firePositions.remove(position);
} }
public List<Position> neighbors(Position position) {
List<Position> list = new ArrayList<>();
if (position.row() > 0) list.add(new Position(position.row() - 1, position.column()));
if (position.column() > 0) list.add(new Position(position.row(), position.column() - 1));
if (position.row() < rowCount - 1) list.add(new Position(position.row() + 1, position.column()));
if (position.column() < columnCount - 1) list.add(new Position(position.row(), position.column() + 1));
return list;
}
@Override @Override
public void setState(List<ModelElement> state, Position position) { public void setState(List<ModelElement> state, Position position) {
firePositions.remove(position); firePositions.remove(position);
for (;;) { for (; ; ) {
if (!firefighterPositions.remove(position)) break; if (!firefighterPositions.remove(position)) break;
} }
for(ModelElement element : state){ for (ModelElement element : state) {
switch (element){ switch (element) {
case FIRE -> firePositions.add(position); case FIRE -> firePositions.add(position);
case FIREFIGHTER -> firefighterPositions.add(position); case FIREFIGHTER -> firefighterPositions.add(position);
} }
} }
} }
public List<Position> getFirePositions() {
return firefighterPositions; }
} }
\ No newline at end of file
...@@ -5,28 +5,25 @@ import util.Position; ...@@ -5,28 +5,25 @@ import util.Position;
import java.util.*; import java.util.*;
public class TargetStrategy { public class TargetStrategy {
private final FirefighterBoard firefighterBoard;
public TargetStrategy(FirefighterBoard firefighterBoard) {
this.firefighterBoard = firefighterBoard;
}
/** /**
* @param position current position. * @param position current position.
* @param targets positions that are targeted. * @param targets positions that are targeted.
* @return the position next to the current position that is on the path to the closest target. * @return the position next to the current position that is on the path to the closest target.
*/ */
Position neighborClosestToFire(Position position, Collection<Position> targets) { Position neighborClosestToFire(Position position, Collection<Position> targets,
Map<Position,List<Position>>neighbors) {
Set<Position> seen = new HashSet<Position>(); Set<Position> seen = new HashSet<Position>();
HashMap<Position, Position> firstMove = new HashMap<Position, Position>(); HashMap<Position, Position> firstMove = new HashMap<Position, Position>();
Queue<Position> toVisit = new LinkedList<Position>(firefighterBoard.neighbors(position)); Queue<Position> toVisit = new LinkedList<Position>(neighbors.get(position));
for (Position initialMove : toVisit) for (Position initialMove : toVisit)
firstMove.put(initialMove, initialMove); firstMove.put(initialMove, initialMove);
while (!toVisit.isEmpty()) { while (!toVisit.isEmpty()) {
Position current = toVisit.poll(); Position current = toVisit.poll();
if (targets.contains(current)) if (targets.contains(current))
return firstMove.get(current); return firstMove.get(current);
for (Position adjacent : firefighterBoard.neighbors(current)) { for (Position adjacent : neighbors.get(current)) {
if (seen.contains(adjacent)) continue; if (seen.contains(adjacent)) continue;
toVisit.add(adjacent); toVisit.add(adjacent);
seen.add(adjacent); seen.add(adjacent);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment