diff --git a/src/main/java/app/SimulatorApplication.java b/src/main/java/app/SimulatorApplication.java index 0b2081cdd9186f46454e0c82ac0bce3ce81f5769..0e3d3359f2042e1a03608d7b0d26fd01ef9bd7d4 100644 --- a/src/main/java/app/SimulatorApplication.java +++ b/src/main/java/app/SimulatorApplication.java @@ -13,8 +13,8 @@ import javafx.stage.Stage; public class SimulatorApplication extends javafx.application.Application { private static final String VIEW_RESOURCE_PATH = "/view/view.fxml"; private static final String APP_NAME = "Firefighter simulator"; - private static final int ROW_COUNT = 22; - private static final int COLUMN_COUNT = 22; + private static final int ROW_COUNT = 20; + private static final int COLUMN_COUNT = 20; private static final int BOX_WIDTH = 25; private static final int BOX_HEIGHT = 25; public static final int INITIAL_FIRE_COUNT = 3; diff --git a/src/main/java/model/Board.java b/src/main/java/model/Board.java index b0e448c8012bfe706f1a8d8dcf8b642a1aec6955..1768662163cfe1cf207b57a534f51aee105b8f7f 100644 --- a/src/main/java/model/Board.java +++ b/src/main/java/model/Board.java @@ -78,7 +78,11 @@ public interface Board<S> { public void addEntityAtSquare(Entity entity, Position position); + //Return if the position is completely free public boolean isPositionEmpty(Position position); - public void addEntityAtSquare(Entity entity, Position position, boolean replaceStates); + //Return if the position is available for the specified priority + public boolean isPositionFree(Position position, int priority); + + } diff --git a/src/main/java/model/EmptyEntity.java b/src/main/java/model/EmptyEntity.java deleted file mode 100644 index 69ce92772341497e5da5e6605d9bbff936996bcd..0000000000000000000000000000000000000000 --- a/src/main/java/model/EmptyEntity.java +++ /dev/null @@ -1,62 +0,0 @@ -package model; - -import java.util.ArrayList; -import java.util.List; - -import javafx.scene.paint.Color; -import util.Position; - -public class EmptyEntity implements Entity{ - - private final int priority=3; - private Position position; - private int age; - public EmptyEntity(Position position){ - this.position = position; - this.age = 0; - } - public EmptyEntity(Position position, int age){ - this.position = position; - this.age = age; - } - @Override - public List<Position> nextTurn(Board<Square> board) { - return new ArrayList<Position>(); - } - - @Override - public Position getPosition() { - return this.position; - } - - @Override - public void setPosition(Position p) { - this.position = p; - } - - @Override - public int getAge() { - return age; - } - - @Override - public void setAge(int age){ - this.age = age; - } - - @Override - public void incrementAge() { - this.age = age + 1; - } - - - - @Override - public Color getViewColor() { - return Color.WHITE; - } - - @Override - public int getPriority(){return this.priority;} - -} \ No newline at end of file diff --git a/src/main/java/model/EmptySquare.java b/src/main/java/model/EmptySquare.java index aa7c5dee8d7e942ccf67ca63824c71531e237947..a196fe418145e94a1cf48bca393158abe603cf95 100644 --- a/src/main/java/model/EmptySquare.java +++ b/src/main/java/model/EmptySquare.java @@ -11,7 +11,7 @@ public class EmptySquare implements Entity { private Position position; private final Color viewColor = Color.WHITE; private int age; - private final int priotity = 0; + private final int priotity = -1; public EmptySquare(Position p) { this.position = p; diff --git a/src/main/java/model/EntityScenario.java b/src/main/java/model/EntityScenario.java index e6c9bd3554e3ac84accb7243441663d196e44be9..fdbedaac570216fd1e5fb9626d1f0fb78b17c1b7 100644 --- a/src/main/java/model/EntityScenario.java +++ b/src/main/java/model/EntityScenario.java @@ -7,7 +7,7 @@ public abstract class EntityScenario implements Scenario{ public void initScenario(Matrix<Square> matrix){ for(int x = 0; x < matrix.getRows(); x++){ for(int y = 0; y < matrix.getColumns(); y++){ - Square s = new Square(new Position(x, y), new EmptyEntity(new Position(x,y))); + Square s = new Square(new Position(x, y), new EmptySquare(new Position(x,y))); matrix.set(x,y, s); } } diff --git a/src/main/java/model/FireFighterScenario.java b/src/main/java/model/FireFighterScenario.java index 58d37043dbde2935604c9453caffb18a7f8f0b4a..bce13d353a6b7b5bc13f93b9cac25f00d577ed5b 100644 --- a/src/main/java/model/FireFighterScenario.java +++ b/src/main/java/model/FireFighterScenario.java @@ -44,14 +44,14 @@ public class FireFighterScenario extends EntityScenario implements Board<Square> positions.add(new Position(x, y)); } } - setSquare(new Square(new Position(0,0), new MotorizedFireFighter(new Position(0,0), this))); + //setSquare(new Square(new Position(0,0), new MotorizedFireFighter(new Position(0,0), this))); while (fireCount < initialFireCount || fireFighterCount < initialFireFightersCount || cloudCount < intialCloudCount) { Collections.shuffle(positions); // Mélange les positions pour un parcours aléatoire for (Position pos : positions) { if (getStates(pos).isEmpty()) { if (fireCount < initialFireCount && random.nextInt(100) < chance) { - setSquare(new Square(pos, new Fire(pos, this, 1))); + setSquare(new Square(pos, new Fire(pos, 0))); fireCount++; if (fireCount == initialFireCount && fireFighterCount == initialFireFightersCount && cloudCount == initialCloudCount && moutainCount == initialMoutainCount) { return; @@ -117,21 +117,11 @@ public class FireFighterScenario extends EntityScenario implements Board<Square> } public void addEntityAtSquare(Entity entity, Position position) { - if (!(getStates(position).isEmpty())) { - return; - } if (doesPositionExist(position)) { matrix.get(position.x(), position.y()).addEntity(entity); } } - public void addEntityAtSquare(Entity entity, Position position, boolean replaceStates) { - if (!(getStates(position).isEmpty()) && !replaceStates) { - return; - } - matrix.get(position.x(), position.y()).addEntity(entity); - ; - } public int rowCount() { return matrix.getRows(); @@ -149,29 +139,77 @@ public class FireFighterScenario extends EntityScenario implements Board<Square> public List<Position> updateToNextGeneration() { ArrayList<Position> changedPositions = new ArrayList<>(); Iterator<Square> iterator = matrix.iterator(); - while (iterator.hasNext()) { - Square s = iterator.next(); - if (s.isEmpty()) - continue; - if (s.getMaxAge() == 0) { - s.incrementAllAges(); - continue; - } - if (s.getMaxAge() == step + 1) { - continue; - } - List<Entity> entities = new ArrayList<>(s.getEntities()); - for (Entity e : entities) { - e.incrementAge(); - changedPositions.addAll(e.nextTurn(this)); - } + while (iterator.hasNext()) { + Square s = iterator.next(); + if (s.isEmpty()) + continue; + if (s.getMaxAge() == 0) { + s.incrementAllAges(); + continue; + } + if (s.getMaxAge() == step + 1) { + continue; + } + List<Entity> entities = new ArrayList<>(s.getEntities()); + for (Entity e : entities) { + if(e.getAge() >= stepNumber()-1){ + continue; + } + e.incrementAge(); + changedPositions.addAll(e.nextTurn(this)); + } } + + // Increment the step counter this.step = this.step + 1; - // matrix.displayMatrix(); + + // Check if it's time to spawn an AirTanker + if (this.step % 8 == 0) { + // Spawn an AirTanker at a random edge position + spawnAirTanker(changedPositions); + } + return changedPositions; +} + +// Helper method to spawn an AirTanker +private void spawnAirTanker(List<Position> changedPositions) { + System.out.println("Spawning AirTanker"); + Random rand = new Random(); + int edge = rand.nextInt(4); // 0: top, 1: bottom, 2: left, 3: right + Position position = null; + + if (edge == 0) { // Top edge (x == 0) + int y = rand.nextInt(columnCount()-1); + position = new Position(0, y); + } else if (edge == 1) { // Bottom edge (x == rowCount() - 1) + int y = rand.nextInt(columnCount()); + position = new Position(rowCount() - 1, y); + } else if (edge == 2) { // Left edge (y == 0) + int x = rand.nextInt(rowCount()-1); + position = new Position(x, 0); + } else if (edge == 3) { // Right edge (y == columnCount() - 1) + int x = rand.nextInt(rowCount()-1); + position = new Position(x, columnCount() -1); + } else { + // This else block is technically not necessary because edge will always be between 0 and 3 + throw new IllegalStateException("Unexpected edge value: " + edge); } + System.out.println("Position: " + position.toString()); + // Create a new AirTanker + AirTanker airTanker = new AirTanker(position, this); + System.out.println(" direction : " + airTanker.getDirection()); + // Add the AirTanker to the board + addEntityAtSquare(airTanker, position); + + // Record the changed position + changedPositions.add(position); +} + + + public Position getNearestEntity(Position fromPos, Class<?> entityType) { int rows = matrix.getRows(); int cols = matrix.getColumns(); @@ -228,4 +266,15 @@ public class FireFighterScenario extends EntityScenario implements Board<Square> public boolean isPositionEmpty(Position position) { return getStates(position).isEmpty(); } + + @Override + public boolean isPositionFree(Position position, int priority) { + List<Entity> entities = matrix.get(position.x(), position.y()).getEntities(); + for(Entity e : entities){ + if(e.getPriority() == priority){ + return false; + } + } + return true; + } } diff --git a/src/main/java/model/Square.java b/src/main/java/model/Square.java index 779884b2442587710d96ef24ee0c867502f0f369..50c9a839cefa34a4bea0da061346f9e44e8bfeeb 100644 --- a/src/main/java/model/Square.java +++ b/src/main/java/model/Square.java @@ -40,7 +40,7 @@ public class Square { } public boolean isEmpty(){ - return entities.isEmpty() ||(entities.size() == 1 && entities.get(0) instanceof EmptyEntity); + return entities.isEmpty() ||(entities.size() == 1 && entities.get(0) instanceof EmptySquare); } public int getMinimalAge(){ @@ -69,31 +69,25 @@ public class Square { } } - public Color getViewColor(){ + public Color getViewColor() { if (entities.isEmpty()) { return Color.WHITE; } else { - int sumRed = 0, sumGreen = 0, sumBlue = 0; + int maxPriority = -1; // Assuming priorities are non-negative + Color c = Color.WHITE; + + // Iterate over entities to find the one with the highest priority for (Entity e : entities) { - Color color = e.getViewColor(); - if (color != null) { - if(sumRed == 255 & sumGreen == 255 & sumBlue == 255){ - continue; - } - sumRed += color.getRed(); - sumGreen += color.getGreen(); - sumBlue += color.getBlue(); + // Check for null entities if necessary + if (e != null && e.getPriority() > maxPriority) { + maxPriority = e.getPriority(); + c = e.getViewColor(); } } - int count = entities.size(); - sumRed /= count; - sumGreen /= count; - sumBlue /= count; - Color color = new Color((double)(sumRed),(double)(sumGreen),(double)(sumBlue), 1.0); - return color; - + return c; } } + }