This commit is contained in:
2025-08-28 14:34:18 +02:00
commit 5ff4c53a10
29 changed files with 1705 additions and 0 deletions

58
TP1/EX1/Ant.js Normal file
View File

@@ -0,0 +1,58 @@
class Ant {
constructor(grid, startX, startY) {
this.grid = grid;
this.x = startX;
this.y = startY;
this.direction = 0; // 0: N, 90: E, 180: S, 270: W
this.colors = { white: 0, black: 1 };
}
// Effectue un pas de la fourmi
step() {
const currentColor = this.grid[this.y][this.x];
if (currentColor === this.colors.white) {
// Case blanche : la repeindre en noir et tourner à droite
this.grid[this.y][this.x] = this.colors.black;
this.direction = (this.direction + 90) % 360;
} else {
// Case noire : la repeindre en blanc et tourner à gauche
this.grid[this.y][this.x] = this.colors.white;
this.direction = (this.direction - 90 + 360) % 360;
}
// Déplacer la fourmi d'une case dans sa nouvelle direction
switch (this.direction) {
case 0:
this.y--;
break; // Nord
case 90:
this.x++;
break; // Est
case 180:
this.y++;
break; // Sud
case 270:
this.x--;
break; // Ouest
}
// Gérer les bords de la grille (déplacement cyclique)
const gridSize = this.grid.length;
this.x = (this.x + gridSize) % gridSize;
this.y = (this.y + gridSize) % gridSize;
}
// Réinitialise la fourmi et la grille
reset() {
const gridSize = this.grid.length;
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
this.grid[y][x] = this.colors.white;
}
}
this.x = Math.floor(gridSize / 2);
this.y = Math.floor(gridSize / 2);
this.direction = 90; // Réinitialise la direction vers l'est
}
}

40
TP1/EX1/app.js Normal file
View File

@@ -0,0 +1,40 @@
import Ant from './Ant.js';
// Supposons que vous avez une fonction render pour l'affichage
// Par exemple : function render(grid) { ... }
let ant;
let grid;
const gridSize = 50;
const intervalId;
function setup() {
grid = createGrid(gridSize);
ant = new Ant(grid, Math.floor(gridSize / 2), Math.floor(gridSize / 2));
document.getElementById('resetBtn').addEventListener('click', resetSimulation);
startSimulation();
}
function createGrid(size) {
const newGrid = [];
for (let i = 0; i < size; i++) {
newGrid[i] = new Array(size).fill(0);
}
return newGrid;
}
function startSimulation() {
intervalId = setInterval(() => {
ant.step();
render(grid); // Appelez votre fonction de rendu ici
}, 100);
}
function resetSimulation() {
clearInterval(intervalId);
ant.reset();
render(grid);
startSimulation();
}
// Assurez-vous d'appeler setup() au chargement de la page
window.onload = setup;