Newer
Older
import java.util.List;
import java.util.Map;
import java.util.Random;
import app.SimulatorApplication;
import model.firefighterscenario.Fire;
import model.firefighterscenario.Mountain;
import model.firefighterscenario.Road;
import model.firefighterscenario.Rockery;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import util.PathGenerator;
import util.Position;
import util.PositionUtil;
public class Scenario implements Board<Square>{
private Matrix<Square> matrix;
protected int step;
protected int turnsToSpawnAirTanker;
private Map<EntityFactory, Integer> initialMap;
public Scenario(int columns, int rows, Map<EntityFactory, Integer> initialMap) {
this.matrix = new Matrix<Square>(columns, rows);
initScenario(matrix);
this.turnsToSpawnAirTanker = SimulatorApplication.TURNS_FOR_SPAWNING_AIRTANKER;
this.step = 0;
this.initialMap = initialMap;
placeInitialEntities(initialMap);
}
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 EmptySquare(new Position(x,y)));
matrix.set(x,y, s);
}
}
}
protected Matrix<Square> getMatrix(){
return this.matrix;
}
public void placeInitialEntities(Map<EntityFactory, Integer> initialMap) {
EntitySpawner spawner = new EntitySpawner(this);
spawner.spawnEntities(initialMap);
generateRoads();
}
public Square getStates(Position position) {
if (position.x() > matrix.size() || position.y() > matrix.size()) {
throw new IllegalArgumentException(
"The position x:" + position.x() + " y:" + position.y() + " is out of the board.");
}
return matrix.get(position.x(), position.y());
}
public void setSquare(Square square) {
Position position = square.getPosition();
if (!(getStates(position).isEmpty())) {
return;
}
if (doesPositionExist(position)) {
matrix.set(position.x(), position.y(), square);
}
}
public void setSquare(Square square, boolean replaceStates) {
Position position = square.getPosition();
if (!(getStates(position).isEmpty()) && !replaceStates) {
return;
}
matrix.set(position.x(), position.y(), square);
}
public void addEntityAtSquare(Entity entity, Position position) {
if (doesPositionExist(position)) {
matrix.get(position.x(), position.y()).addEntity(entity);
}
}
public int rowCount() {
return matrix.getRows();
}
public int columnCount() {
return matrix.getColumns();
}
@Override
public void clearCaseFrom(Entity entity, Position position) {
matrix.get(position.x(), position.y()).getEntities().removeIf(element -> element.equals(entity));
}
public Position getNearestEntity(Position fromPos, Class<?> entityType) {
int rows = matrix.getRows();
int cols = matrix.getColumns();
Position nearestPosition = fromPos;
// Définir la distance maximale possible
int maxDistance = rows + cols;
// Parcourir les distances croissantes à partir de 1
for (int distance = 1; distance < maxDistance; distance++) {
List<Position> positionsAtDistance = PositionUtil.getPositionsAtManhattanDistance(fromPos, distance, rows, cols);
for (Position currentPos : positionsAtDistance) {
Square currentSquare = matrix.get(currentPos.x(), currentPos.y());
for (Entity currentEntity : currentSquare.getEntities()) {
if (entityType.isInstance(currentEntity)) {
// Dès qu'une entité est trouvée à cette distance, elle est la plus proche
// possible
return currentPos;
}
}
}
}
return nearestPosition; // Retourne null si aucune entité n'est trouvée
}
public void reset() {
step = 0;
matrix.clear();
initScenario(matrix);
placeInitialEntities(initialMap);
}
public int stepNumber() {
return this.step;
}
@Override
public boolean doesPositionExist(Position position) {
return matrix.validateIndex(position);
}
@Override
public int getStepNumber() {
return step;
}
@Override
public boolean doesSquareContainEntity(Position squarePos, Class<?> entityType) {
return getStates(squarePos).getEntities().stream().anyMatch(entityType::isInstance);
}
@Override
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;
}
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);
}
}