Newer
Older
package model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import util.Position;
Yanis O
committed
import util.PositionUtil;
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
public class EntitySpawner {
private final Board<Square> board;
private final Random random = new Random();
public EntitySpawner(Board<Square> board) {
this.board = board;
}
public void spawnEntities(Map<EntityFactory, Integer> entityCounts) {
Map<EntityFactory, Integer> counts = new HashMap<>();
for (EntityFactory factory : entityCounts.keySet()) {
counts.put(factory, 0);
}
int totalEntitiesToPlace = entityCounts.values().stream().mapToInt(Integer::intValue).sum();
int totalEntitiesPlaced = 0;
int chance = 5;
List<Position> positions = generateAllPositions();
while (totalEntitiesPlaced < totalEntitiesToPlace) {
Collections.shuffle(positions);
for (Position pos : positions) {
if (board.getStates(pos).isEmpty()) {
for (EntityFactory factory : entityCounts.keySet()) {
int desiredCount = entityCounts.get(factory);
int currentCount = counts.get(factory);
if (currentCount < desiredCount && random.nextInt(100) < chance) {
Entity entity = factory.create(pos, board);
board.setSquare(new Square(pos, entity));
counts.put(factory, currentCount + 1);
totalEntitiesPlaced++;
if (totalEntitiesPlaced == totalEntitiesToPlace) {
return;
}
break; // Move to the next position
}
}
}
}
// Increase chance after each full traversal
chance = Math.min(chance + 5, 100);
}
}
private List<Position> generateAllPositions() {
List<Position> positions = new ArrayList<>();
for (int x = 0; x < board.rowCount(); x++) {
for (int y = 0; y < board.columnCount(); y++) {
positions.add(new Position(x, y));
}
}
return positions;
}
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
public static void generateEntitiesInLine(Board<Square> board, Position anchor, EntityFactory entityFactory) {
int xIncrement = 0;
int yIncrement = 0;
// Determine increments based on which coordinate is zero
if (anchor.x() == 0 && anchor.y() >= 0) {
// Starting from the left edge (x == 0), increment x to move right
xIncrement = 1;
} else if (anchor.y() == 0 && anchor.x() >= 0) {
// Starting from the top edge (y == 0), increment y to move down
yIncrement = 1;
} else {
// If neither x nor y is 0, cannot determine direction
throw new IllegalArgumentException("Anchor position must have x or y equal to 0");
}
int x = anchor.x();
int y = anchor.y();
// Continue until we reach the edge of the board
while (board.doesPositionExist(new Position(x, y))) {
Position pos = new Position(x, y);
// Create a new entity for each position
Entity entity = entityFactory.create(pos, board);
entity.setPosition(pos); // Set the position if not already set in the factory
board.addEntityAtSquare(entity, pos);
x += xIncrement;
y += yIncrement;
}
}
public static List<Position> generateEntitiesInRandomLine(Board<Square> board, Position anchor, int maxStepsInDirection, int minimumRoadLength) {
Random random = new Random();
List<Position> path = new ArrayList<>();
int x = anchor.x();
int y = anchor.y();
// Toutes les directions possibles
List<Direction> allDirections = Arrays.asList(Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST);
// Choisir une direction initiale aléatoire
Direction initialDirection = allDirections.get(random.nextInt(allDirections.size()));
path.add(new Position(x, y)); // Ajouter la position de l'ancre au chemin
int roadLength = 1;
// Déterminer la direction interdite (opposée à la direction initiale)
Yanis O
committed
Direction forbiddenDirection = PositionUtil.getOppositeDirection(initialDirection);
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
// Initialiser la direction courante
Direction currentDirection = initialDirection;
int stepsInCurrentDirection = 0;
// Ensemble des directions définitivement exclues (direction initiale)
Set<Direction> permanentlyExcludedDirections = new HashSet<>();
permanentlyExcludedDirections.add(initialDirection);
// Ensemble des directions temporairement exclues (initialement vide)
Set<Direction> temporarilyExcludedDirections = new HashSet<>();
while (true) {
// Calculer la prochaine position dans la direction courante
int nextX = x + getXIncrement(currentDirection);
int nextY = y + getYIncrement(currentDirection);
Position nextPos = new Position(nextX, nextY);
if (board.doesPositionExist(nextPos)) {
// Ajouter la position au chemin
path.add(nextPos);
x = nextX;
y = nextY;
roadLength++;
stepsInCurrentDirection++;
} else {
// La position dans la direction courante est invalide
if (roadLength < minimumRoadLength) {
// Exclure temporairement la direction courante
temporarilyExcludedDirections.add(currentDirection);
// Choisir une nouvelle direction valide
Direction newDirection = chooseNewDirection(allDirections, currentDirection, forbiddenDirection,
permanentlyExcludedDirections, temporarilyExcludedDirections, board, x, y, random);
if (newDirection == null) {
// Aucune direction valide disponible pour atteindre la longueur minimale
break;
}
// Mettre à jour la direction courante
currentDirection = newDirection;
Yanis O
committed
forbiddenDirection = PositionUtil.getOppositeDirection(currentDirection);
stepsInCurrentDirection = 0;
continue; // Recommencer avec la nouvelle direction
} else {
// La longueur minimale est atteinte, arrêter la génération
break;
}
}
// Vérifier s'il est temps de changer de direction
if (stepsInCurrentDirection >= maxStepsInDirection) {
// Choisir une nouvelle direction
Direction newDirection = chooseNewDirection(allDirections, currentDirection, forbiddenDirection,
permanentlyExcludedDirections, temporarilyExcludedDirections, board, x, y, random);
if (newDirection == null) {
// Aucune direction valide disponible
break;
}
// Mettre à jour la direction courante
currentDirection = newDirection;
Yanis O
committed
forbiddenDirection = PositionUtil.getOppositeDirection(currentDirection);
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
stepsInCurrentDirection = 0;
}
}
return path; // Retourner la liste des positions formant le serpent
}
/**
* Choisit une nouvelle direction valide en tenant compte des exclusions permanentes et temporaires.
*
* @param allDirections Toutes les directions possibles.
* @param currentDirection La direction actuelle.
* @param forbiddenDirection La direction opposée à la direction actuelle (interdite).
* @param permanentlyExcludedDirections Les directions définitivement exclues.
* @param temporarilyExcludedDirections Les directions temporairement exclues.
* @param board Le plateau de jeu.
* @param x La coordonnée X actuelle.
* @param y La coordonnée Y actuelle.
* @param random Une instance de Random pour le choix aléatoire.
* @return La nouvelle direction choisie ou null si aucune direction valide n'est disponible.
*/
private static Direction chooseNewDirection(List<Direction> allDirections, Direction currentDirection, Direction forbiddenDirection,
Set<Direction> permanentlyExcludedDirections, Set<Direction> temporarilyExcludedDirections,
Board<Square> board, int x, int y, Random random) {
// Créer une liste de directions valides en excluant :
// - La direction actuelle
// - La direction interdite (opposée à la direction actuelle)
// - Les directions définitivement exclues
// - Les directions temporairement exclues
List<Direction> validDirections = new ArrayList<>(allDirections);
validDirections.remove(currentDirection);
validDirections.remove(forbiddenDirection);
validDirections.removeAll(permanentlyExcludedDirections);
validDirections.removeAll(temporarilyExcludedDirections);
// Filtrer les directions qui permettent de continuer le chemin
validDirections.removeIf(dir -> !board.doesPositionExist(new Position(x + getXIncrement(dir), y + getYIncrement(dir))));
if (validDirections.isEmpty()) {
// Aucune direction valide disponible
return null;
}
// Choisir une nouvelle direction aléatoirement
return validDirections.get(random.nextInt(validDirections.size()));
}
private static int getXIncrement(Direction direction) {
switch (direction) {
case NORTH: return -1;
case SOUTH: return 1;
default: return 0;
}
}
private static int getYIncrement(Direction direction) {
switch (direction) {
case EAST: return 1;
case WEST: return -1;
default: return 0;
}
}
Yanis O
committed