diff --git a/helpers/ObstacleManager.js b/helpers/ObstacleManager.js index 5548fdf..511a8c0 100644 --- a/helpers/ObstacleManager.js +++ b/helpers/ObstacleManager.js @@ -80,4 +80,13 @@ export class ObstacleManager { // Add back the walls this.obstacles.push(...walls); } + + /** + * Update the number of obstacles and rebuild them. + * @param {number} count - New obstacle count + */ + setCount(count) { + this.count = count; + this.initializeObstacles(); + } } diff --git a/helpers/SimulationManager.js b/helpers/SimulationManager.js index 8ba9575..abb5e54 100644 --- a/helpers/SimulationManager.js +++ b/helpers/SimulationManager.js @@ -220,6 +220,29 @@ export class SimulationManager { this.obstacleManager.reset(); } + /** + * Fully reset the simulation to generation 1 with new parameters. + * @param {number} populationSize - Number of cars in the new generation. + * @param {number} obstacleCount - Number of obstacles to spawn. + */ + reset(populationSize, obstacleCount) { + this.populationSize = populationSize; + if (typeof this.obstacleManager.setCount === 'function') { + this.obstacleManager.setCount(obstacleCount); + } + + this.cars = []; + this.deadCars = []; + this.memoryQueue = []; + this.generationStats = []; + this.processedCars.clear(); + this.maxFitness = 0; + this.distanceTraveled = 0; + this.generation = 1; + + this.spawnInitialPopulation(); + } + getAliveCars() { return this.cars.filter(car => car.alive); } diff --git a/helpers/StatisticsManager.js b/helpers/StatisticsManager.js index 34c9325..b8d31d7 100644 --- a/helpers/StatisticsManager.js +++ b/helpers/StatisticsManager.js @@ -10,6 +10,14 @@ export class StatisticsManager { this.setupEventListeners(); } + /** + * Replace the underlying simulation manager instance. + * @param {SimulationManager} manager + */ + setSimulationManager(manager) { + this.simulationManager = manager; + } + setupEventListeners() { // Open modal this.statsButton.addEventListener('click', () => { diff --git a/index.html b/index.html index 6661fc3..5cb8bf3 100644 --- a/index.html +++ b/index.html @@ -7,9 +7,15 @@
- + + + + + + +