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
}
}