Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const grassField = document.getElementById('grassField');
+ const scoreElement = document.getElementById('score');
+ const winMessage = document.getElementById('winMessage');
+ const playAgainButton = document.getElementById('playAgain');
+
+ let score = 0;
+ const totalLuckyClovers = 4;
+
+ function initializeGame() {
+ score = 0;
+ scoreElement.textContent = '0';
+ grassField.innerHTML = '';
+ winMessage.style.display = 'none';
+
+ // Create grass tiles
+ const luckyPositions = [];
+ while (luckyPositions.length < totalLuckyClovers) {
+ const position = Math.floor(Math.random() * 36);
+ if (!luckyPositions.includes(position)) {
+ luckyPositions.push(position);
+ }
+ }
+
+ for (let i = 0; i < 36; i++) {
+ const grass = document.createElement('div');
+ grass.className = 'grass';
+ if (luckyPositions.includes(i)) {
+ grass.classList.add('lucky');
+ }
+ grass.addEventListener('click', () => checkGrass(grass));
+ grassField.appendChild(grass);
+ }
+ }
+
+ function checkGrass(grass) {
+ if (grass.classList.contains('lucky') && !grass.classList.contains('found')) {
+ grass.classList.add('found');
+ grass.textContent = '🍀';
+ score++;
+ scoreElement.textContent = score;
+
+ if (score === totalLuckyClovers) {
+ setTimeout(() => {
+ winMessage.style.display = 'block';
+ }, 500);
+ }
+ } else if (!grass.classList.contains('found')) {
+ grass.textContent = '🌿';
+ setTimeout(() => {
+ grass.textContent = '';
+ }, 500);
+ }
+ }
+
+ playAgainButton.addEventListener('click', initializeGame);
+
+ // Start the game
+ initializeGame();
+ });