Amélioration génération labyrinthes: chemins denses partout, réduction des grands espaces vides

This commit is contained in:
2025-11-28 18:17:36 +01:00
parent 754d4a3162
commit 4d4f51d66c

140
game.js
View File

@ -559,6 +559,7 @@ function nextLevel() {
console.log('Labyrinthe copié, maze.length:', maze.length); console.log('Labyrinthe copié, maze.length:', maze.length);
fillEmptySpaces();
randomizeMaze(); randomizeMaze();
countDots(); countDots();
@ -606,12 +607,102 @@ function nextLevel() {
}); });
} }
function fillEmptySpaces() {
console.log('Remplissage de tous les espaces vides avec des chemins');
// Première passe : remplir tous les empty
for (let y = 1; y < ROWS - 1; y++) {
for (let x = 1; x < COLS - 1; x++) {
if (maze[y][x] === EMPTY) {
maze[y][x] = DOT;
}
}
}
// Détecter et remplir les grandes zones vides
for (let iteration = 0; iteration < 6; iteration++) {
for (let y = 1; y < ROWS - 1; y++) {
for (let x = 1; x < COLS - 1; x++) {
if (maze[y][x] === WALL) {
const neighbors = [
maze[y-1][x], maze[y+1][x], maze[y][x-1], maze[y][x+1]
];
const emptyCount = neighbors.filter(c => c === EMPTY).length;
const dotCount = neighbors.filter(c => c === DOT).length;
const wallCount = neighbors.filter(c => c === WALL).length;
// Si entouré d'espaces vides ou de beaucoup de chemins, transformer en chemin
if (emptyCount >= 1) {
maze[y][x] = DOT;
} else if (wallCount <= 1 && dotCount >= 2) {
if (Math.random() < 0.9) {
maze[y][x] = DOT;
}
} else if (wallCount === 0) {
maze[y][x] = DOT;
}
} else if (maze[y][x] === EMPTY) {
maze[y][x] = DOT;
}
}
}
// Détecter les grandes zones vides (empty) et les remplir
for (let y = 2; y < ROWS - 2; y++) {
for (let x = 2; x < COLS - 2; x++) {
if (maze[y][x] === EMPTY) {
let emptyArea = 0;
const visited = new Set();
const stack = [[x, y]];
while (stack.length > 0 && emptyArea < 20) {
const [cx, cy] = stack.pop();
const key = `${cy},${cx}`;
if (visited.has(key)) continue;
visited.add(key);
if (maze[cy][cx] === EMPTY) {
emptyArea++;
if (cy > 1) stack.push([cx, cy-1]);
if (cy < ROWS-2) stack.push([cx, cy+1]);
if (cx > 1) stack.push([cx-1, cy]);
if (cx < COLS-2) stack.push([cx+1, cy]);
}
}
if (emptyArea >= 5) {
for (const key of visited) {
const [cy, cx] = key.split(',').map(Number);
if (maze[cy][cx] === EMPTY) {
maze[cy][cx] = DOT;
}
}
}
}
}
}
}
// Créer une petite cavité au centre seulement
const centerX = Math.floor(COLS / 2);
const centerY = Math.floor(ROWS / 2);
if (maze[centerY] && maze[centerY][centerX] !== WALL) {
maze[centerY][centerX] = EMPTY;
}
console.log('Tous les grands espaces réduits, chemins denses créés');
}
function randomizeMaze() { function randomizeMaze() {
const modificationRate = 0.1; const modificationRate = 0.05;
const changes = Math.floor(ROWS * COLS * modificationRate); const changes = Math.floor(ROWS * COLS * modificationRate);
console.log('Modification aléatoire du labyrinthe,', changes, 'changements'); console.log('Modification aléatoire du labyrinthe,', changes, 'changements');
const centerX = Math.floor(COLS / 2);
const centerY = Math.floor(ROWS / 2);
for (let i = 0; i < changes; i++) { for (let i = 0; i < changes; i++) {
const x = Math.floor(Math.random() * COLS); const x = Math.floor(Math.random() * COLS);
const y = Math.floor(Math.random() * ROWS); const y = Math.floor(Math.random() * ROWS);
@ -620,42 +711,67 @@ function randomizeMaze() {
continue; continue;
} }
const distX = Math.abs(x - centerX);
const distY = Math.abs(y - centerY);
if (distX <= 1 && distY <= 1) {
continue;
}
const currentCell = maze[y][x]; const currentCell = maze[y][x];
if (currentCell === WALL) { if (currentCell === WALL) {
if (Math.random() < 0.9) { if (Math.random() < 0.8) {
maze[y][x] = DOT; maze[y][x] = DOT;
} }
} else if (currentCell === EMPTY) { } else if (currentCell === EMPTY) {
maze[y][x] = DOT; maze[y][x] = DOT;
} else if (currentCell === DOT) { }
if (Math.random() < 0.05) { }
maze[y][x] = WALL;
// Remplir tous les empty restants
for (let y = 1; y < ROWS - 1; y++) {
for (let x = 1; x < COLS - 1; x++) {
const distX = Math.abs(x - centerX);
const distY = Math.abs(y - centerY);
if (distX > 1 || distY > 1) {
if (maze[y][x] === EMPTY) {
maze[y][x] = DOT;
}
} }
} }
} }
// Transformer les murs isolés en chemins
for (let iteration = 0; iteration < 3; iteration++) {
for (let y = 1; y < ROWS - 1; y++) { for (let y = 1; y < ROWS - 1; y++) {
for (let x = 1; x < COLS - 1; x++) { for (let x = 1; x < COLS - 1; x++) {
if (maze[y][x] === EMPTY) { const distX = Math.abs(x - centerX);
maze[y][x] = DOT; const distY = Math.abs(y - centerY);
} else if (maze[y][x] === WALL) {
if (distX <= 1 && distY <= 1) {
continue;
}
if (maze[y][x] === WALL) {
const neighbors = [ const neighbors = [
maze[y-1][x], maze[y+1][x], maze[y][x-1], maze[y][x+1] maze[y-1][x], maze[y+1][x], maze[y][x-1], maze[y][x+1]
]; ];
const wallCount = neighbors.filter(c => c === WALL).length; const wallCount = neighbors.filter(c => c === WALL).length;
const emptyCount = neighbors.filter(c => c === EMPTY).length; const dotCount = neighbors.filter(c => c === DOT).length;
if (wallCount === 0 && Math.random() < 0.7) { if (wallCount <= 1 && dotCount >= 2 && Math.random() < 0.85) {
maze[y][x] = DOT; maze[y][x] = DOT;
} else if (emptyCount >= 2 && Math.random() < 0.9) { } else if (wallCount === 0) {
maze[y][x] = DOT; maze[y][x] = DOT;
} }
} }
} }
} }
}
console.log('Labyrinthe modifié aléatoirement, tous les trous remplis'); console.log('Labyrinthe modifié avec chemins denses, grands espaces réduits');
} }
function placeBonuses() { function placeBonuses() {