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;

63
TP1/EX2/Ant.js Normal file
View File

@@ -0,0 +1,63 @@
class Ant {
constructor(grid, startX, startY, transitionTable) {
this.grid = grid;
this.x = startX;
this.y = startY;
this.direction = 90; // 0: N, 90: E, 180: S, 270: W
this.antState = 0;
this.transitionTable = transitionTable;
this.numAntStates = transitionTable.length;
this.numTileStates = transitionTable[0].length;
}
step() {
const tileState = this.grid[this.y][this.x];
const transition = this.transitionTable[this.antState][tileState];
const [antStateChange, directionChange, tileStateChange] = transition;
// Appliquer les changements
this.antState = (this.antState + antStateChange) % this.numAntStates;
this.direction = (this.direction + directionChange + 360) % 360;
this.grid[this.y][this.x] = (tileState + tileStateChange) % this.numTileStates;
// Déplacer la fourmi
switch (this.direction) {
case 0: this.y--; break;
case 90: this.x++; break;
case 180: this.y++; break;
case 270: this.x--; break;
}
// Gérer les bords de la grille
const gridSize = this.grid.length;
this.x = (this.x + gridSize) % gridSize;
this.y = (this.y + gridSize) % gridSize;
}
reset() {
const gridSize = this.grid.length;
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
this.grid[y][x] = 0;
}
}
this.x = Math.floor(gridSize / 2);
this.y = Math.floor(gridSize / 2);
this.direction = 90;
this.antState = 0;
}
}
// Exemple d'utilisation dans app.js
// const langtonTable = [
// [[0, 90, 1], [0, -90, 0]]
// ];
// ant = new Ant(grid, Math.floor(gridSize / 2), Math.floor(gridSize / 2), langtonTable);
// Exemple de la table du TP
const tpTable = [
// Tuile 0 Tuile 1
[[1, 0, 1], [0, 90, 1]], // Fourmi 0
[[0, -90, 1], [1, 0, 1]] // Fourmi 1
];

25
TP1/EX3/array.html Normal file
View File

@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html>
<head>
<title>Array Operations</title>
</head>
<body>
<script>
const numbers = [1, 5, 2, 8, 3, 7, 4, 6];
// Utilisation de map pour créer un nouveau tableau avec chaque nombre doublé
const doubledNumbers = numbers.map(num => num * 2);
console.log("Doubled numbers:", doubledNumbers); // [2, 10, 4, 16, 6, 14, 8, 12]
// Utilisation de filter pour garder seulement les nombres pairs
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log("Even numbers:", evenNumbers); // [2, 8, 4, 6]
// Utilisation de reduce pour calculer la somme de tous les nombres
const sum = numbers.reduce((total, num) => total + num, 0);
console.log("Sum:", sum); // 36
</script>
</body>
</html>

31
TP1/EX3/minmax.html Normal file
View File

@@ -0,0 +1,31 @@
<!DOCTYPE html>
<html>
<head>
<title>Min/Max with Reduce</title>
</head>
<body>
<script>
function minmax(array) {
if (array.length === 0) {
return { min: undefined, max: undefined };
}
return array.reduce(
(acc, val) => {
return {
min: Math.min(acc.min, val),
max: Math.max(acc.max, val)
};
},
{ min: array[0], max: array[0] }
);
}
const data = [10, 5, 20, 3, 15];
const result = minmax(data);
console.log("Min/Max:", result); // { min: 3, max: 20 }
</script>
</body>
</html>

63
TP1/EX4/gameoflife.js Normal file
View File

@@ -0,0 +1,63 @@
class GameOfLife {
constructor(width, height) {
this.width = width;
this.height = height;
this.grid = new Array(height).fill(null).map(() => new Array(width).fill(0));
}
// Initialise la grille avec l'état de départ
setInitialState(initialStateString) {
this.grid = new Array(this.height).fill(null).map(() => new Array(this.width).fill(0));
const liveCells = initialStateString.split(';').map(cell => cell.split(','));
liveCells.forEach(([x, y]) => {
const cellX = parseInt(x);
const cellY = parseInt(y);
if (cellX >= 0 && cellX < this.width && cellY >= 0 && cellY < this.height) {
this.grid[cellY][cellX] = 1;
}
});
}
// Calcule le nombre de voisins vivants d'une cellule
getNumberActiveNeighbourCells(x, y) {
let count = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i === 0 && j === 0) continue;
const neighborX = x + j;
const neighborY = y + i;
if (neighborX >= 0 && neighborX < this.width && neighborY >= 0 && neighborY < this.height) {
count += this.grid[neighborY][neighborX];
}
}
}
return count;
}
// Calcule la génération suivante
computeNextGeneration() {
const nextGrid = new Array(this.height).fill(null).map(() => new Array(this.width).fill(0));
for (let y = 0; y < this.height; y++) {
for (let x = 0; x < this.width; x++) {
const liveNeighbors = this.getNumberActiveNeighbourCells(x, y);
const cellState = this.grid[y][x];
if (cellState === 1) {
// Règle 2 : Cellule vivante
if (liveNeighbors === 2 || liveNeighbors === 3) {
nextGrid[y][x] = 1;
}
} else {
// Règle 1 : Cellule morte
if (liveNeighbors === 3) {
nextGrid[y][x] = 1;
}
}
}
}
this.grid = nextGrid;
}
}