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

73
TP2/EX1/lights.html Normal file
View File

@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Lights Out</title>
<style>
.grid-container {
display: grid;
grid-template-columns: repeat(5, 50px);
gap: 5px;
margin: 20px;
}
.light {
width: 50px;
height: 50px;
background-color: #ddd;
border: 1px solid #999;
cursor: pointer;
}
.light.on {
background-color: yellow;
}
</style>
</head>
<body>
<h1>Lights Out</h1>
<div class="grid-container" id="game-grid"></div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const gridSize = 5;
const gameGrid = document.getElementById('game-grid');
// Créer la grille de lumières
for (let i = 0; i < gridSize * gridSize; i++) {
const light = document.createElement('div');
light.classList.add('light');
light.dataset.id = i;
gameGrid.appendChild(light);
}
const lights = document.querySelectorAll('.light');
gameGrid.addEventListener('click', event => {
const target = event.target;
if (!target.classList.contains('light')) return;
const id = parseInt(target.dataset.id);
const row = Math.floor(id / gridSize);
const col = id % gridSize;
// Fonction pour basculer une lumière
const toggleLight = (index) => {
if (index >= 0 && index < lights.length) {
lights[index].classList.toggle('on');
}
};
// Basculer la lumière cliquée
toggleLight(id);
// Basculer les voisins
if (col > 0) toggleLight(id - 1); // Gauche
if (col < gridSize - 1) toggleLight(id + 1); // Droite
if (row > 0) toggleLight(id - gridSize); // Haut
if (row < gridSize - 1) toggleLight(id + gridSize); // Bas
});
});
</script>
</body>
</html>