Download script.js from waste1234/chroma-memory-mastermind: direct link, hf CLI and curl.
- Browser
- Download file 3.91 kB
-
https://huggingface.co/spaces/waste1234/chroma-memory-mastermind/resolve/main/script.js
- Command line
-
hf download hf://spaces/waste1234/chroma-memory-mastermind/script.js
-
curl -L -o script.js https://huggingface.co/spaces/waste1234/chroma-memory-mastermind/resolve/main/script.js
3.91 kB
| document.addEventListener('DOMContentLoaded', () => { | |
| const buttons = document.querySelectorAll('game-button'); | |
| const startBtn = document.getElementById('startBtn'); | |
| const strictBtn = document.getElementById('strictBtn'); | |
| const scoreDisplay = document.querySelector('score-display'); | |
| let sequence = []; | |
| let playerSequence = []; | |
| let level = 0; | |
| let strictMode = false; | |
| let gameActive = false; | |
| // Sound effects | |
| const sounds = { | |
| red: new Audio('https://www.soundjay.com/buttons/sounds/button-09.mp3'), | |
| blue: new Audio('https://www.soundjay.com/buttons/sounds/button-10.mp3'), | |
| yellow: new Audio('https://www.soundjay.com/buttons/sounds/button-21.mp3'), | |
| green: new Audio('https://www.soundjay.com/buttons/sounds/button-22.mp3'), | |
| error: new Audio('https://www.soundjay.com/buttons/sounds/button-08.mp3') | |
| }; | |
| // Initialize game | |
| function initGame() { | |
| sequence = []; | |
| playerSequence = []; | |
| level = 0; | |
| gameActive = true; | |
| scoreDisplay.setAttribute('score', level); | |
| generateSequence(); | |
| playSequence(); | |
| } | |
| // Generate random sequence | |
| function generateSequence() { | |
| const colors = ['red', 'blue', 'yellow', 'green', 'purple', 'pink', 'indigo', 'teal']; | |
| sequence.push(colors[Math.floor(Math.random() * 4)]); | |
| } | |
| // Play the current sequence | |
| function playSequence() { | |
| let i = 0; | |
| const interval = setInterval(() => { | |
| if (i >= sequence.length) { | |
| clearInterval(interval); | |
| return; | |
| } | |
| const color = sequence[i]; | |
| const button = document.querySelector(`game-button[color="${color}"]`); | |
| button.flash(); | |
| sounds[color].play(); | |
| i++; | |
| }, 800); | |
| } | |
| // Handle player input | |
| function handlePlayerInput(color) { | |
| if (!gameActive) return; | |
| playerSequence.push(color); | |
| sounds[color].play(); | |
| const currentIndex = playerSequence.length - 1; | |
| if (playerSequence[currentIndex] !== sequence[currentIndex]) { | |
| // Wrong sequence | |
| sounds.error.play(); | |
| scoreDisplay.setAttribute('status', 'error'); | |
| document.body.classList.add('game-over'); | |
| setTimeout(() => { | |
| document.body.classList.remove('game-over'); | |
| scoreDisplay.setAttribute('status', ''); | |
| if (strictMode) { | |
| initGame(); // Restart in strict mode | |
| } else { | |
| playerSequence = []; | |
| playSequence(); // Replay sequence | |
| } | |
| }, 1000); | |
| return; | |
| } | |
| // Correct sequence so far | |
| if (playerSequence.length === sequence.length) { | |
| // Completed the level | |
| level++; | |
| scoreDisplay.setAttribute('score', level); | |
| scoreDisplay.setAttribute('status', 'success'); | |
| setTimeout(() => { | |
| scoreDisplay.setAttribute('status', ''); | |
| playerSequence = []; | |
| generateSequence(); | |
| setTimeout(playSequence, 1000); | |
| }, 1000); | |
| } | |
| } | |
| // Event listeners | |
| buttons.forEach(button => { | |
| button.addEventListener('click', () => { | |
| if (!gameActive || playerSequence.length >= sequence.length) return; | |
| button.flash(); | |
| handlePlayerInput(button.getAttribute('color')); | |
| }); | |
| }); | |
| startBtn.addEventListener('click', initGame); | |
| strictBtn.addEventListener('click', () => { | |
| strictMode = !strictMode; | |
| strictBtn.classList.toggle('bg-indigo-600', strictMode); | |
| strictBtn.classList.toggle('bg-gray-700', !strictMode); | |
| }); | |
| }); |