Select Git revision
FireProperties.java
Forked from
COUETOUX Basile / FirefighterStarter
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
FireFighterScenario.java 4.44 KiB
package model.firefighterscenario;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import model.Board;
import model.Entity;
import model.EntityFactory;
import model.Model;
import model.Road;
import model.Scenario;
import model.Square;
import util.PathGenerator;
import util.Position;
public class FireFighterScenario extends Scenario implements Model{
public FireFighterScenario(int columns, int rows, Map<EntityFactory, Integer> initialMap) {
super(columns, rows, initialMap);
generateRoads();
}
public List<Position> updateToNextGeneration() {
ArrayList<Position> changedPositions = new ArrayList<>();
Iterator<Square> iterator = getMatrix().iterator();
List<Entity> updatedEntities = new ArrayList<Entity>();
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(updatedEntities.contains(e))continue;
if (e.getAge() >= stepNumber() - 1) {
continue;
}
e.incrementAge();
updatedEntities.add(e);
changedPositions.addAll(e.nextTurn(this));
}
}
// Increment the step counter
this.step = this.step + 1;
// Check if it's time to spawn an AirTanker
if (this.step % this.turnsToSpawnAirTanker == 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) {
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);
}
// Create a new AirTanker
AirTanker airTanker = new AirTanker(position, this, getStepNumber());
// Add the AirTanker to the board
addEntityAtSquare(airTanker, position);
// Record the changed position
changedPositions.add(position);
}
private void generateRoads() {
if(columnCount() < 10 || rowCount() < 10){
return;
}
Random random = new Random();
// Get board dimensions
int rowCount = rowCount(); // Number of rows (vertical axis)
int columnCount = columnCount(); // Number of columns (horizontal axis)
// Decide randomly whether to set x or y to 0
boolean setXToZero = random.nextBoolean();
int x = 0;
int y = 0;
if (setXToZero) {
// x is set to 0, y is random within column bounds
x = 0;
y = random.nextInt(columnCount);
} else {
// y is set to 0, x is random within row bounds
x = random.nextInt(rowCount);
y = 0;
}
Position startPosition = new Position(x, y);
PathGenerator pathGenerator = new PathGenerator(this, startPosition, 4, columnCount());
// Call generateEntitiesInLine to place the roads
List<Position> snake = pathGenerator.generate();
for(Position p : snake){
List<Entity> entitiesAtSquare = List.copyOf(getStates(p).getEntities());
for(Entity e: entitiesAtSquare){
if(e instanceof Mountain || e instanceof Rockery || e instanceof Fire){
clearCaseFrom(e, e.getPosition());
}
}
addEntityAtSquare(new Road(p), p);
}
}
@Override
public Board<Square> getBoard() {
return this;
}
public void reset() {
step = 0;
super.getMatrix().clear();
initScenario(super.getMatrix());
placeInitialEntities(initialMap);
generateRoads();
}
}