|
| 1 | +const colors = ['#e57373', '#81c784', '#64b5f6', '#ffd54f', '#ba68c8', '#ff8a65']; |
| 2 | +let gridSize = 3; |
| 3 | +let sequence = []; |
| 4 | +let userSequence = []; |
| 5 | +let level = 0; |
| 6 | + |
| 7 | +document.addEventListener('DOMContentLoaded', () => { |
| 8 | + const gridContainer = document.getElementById('grid-container'); |
| 9 | + const startButton = document.getElementById('start-button'); |
| 10 | + const gridSizeSelect = document.getElementById('grid-size'); |
| 11 | + const status = document.getElementById('status'); |
| 12 | + |
| 13 | + // Start button click event |
| 14 | + startButton.addEventListener('click', startGame); |
| 15 | + |
| 16 | + // Grid size change event |
| 17 | + gridSizeSelect.addEventListener('change', (e) => { |
| 18 | + gridSize = parseInt(e.target.value); |
| 19 | + createGrid(); |
| 20 | + }); |
| 21 | + |
| 22 | + // Square click event |
| 23 | + gridContainer.addEventListener('click', (e) => { |
| 24 | + if (e.target.classList.contains('grid-square')) { |
| 25 | + handleSquareClick(e.target.dataset.index); |
| 26 | + } |
| 27 | + }); |
| 28 | + |
| 29 | + function createGrid() { |
| 30 | + gridContainer.innerHTML = ''; |
| 31 | + gridContainer.style.gridTemplateColumns = `repeat(${gridSize}, 100px)`; |
| 32 | + for (let i = 0; i < gridSize * gridSize; i++) { |
| 33 | + const square = document.createElement('div'); |
| 34 | + square.classList.add('grid-square'); |
| 35 | + square.dataset.index = i; |
| 36 | + gridContainer.appendChild(square); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + function startGame() { |
| 41 | + level = 0; |
| 42 | + sequence = []; |
| 43 | + userSequence = []; |
| 44 | + status.textContent = 'Game started!'; |
| 45 | + nextLevel(); |
| 46 | + } |
| 47 | + |
| 48 | + function nextLevel() { |
| 49 | + level++; |
| 50 | + userSequence = []; |
| 51 | + const randomSquare = Math.floor(Math.random() * gridSize * gridSize); |
| 52 | + const newColor = colors[Math.floor(Math.random() * colors.length)]; |
| 53 | + sequence.push({ index: randomSquare, color: newColor }); |
| 54 | + displaySequence(); |
| 55 | + } |
| 56 | + |
| 57 | + function displaySequence() { |
| 58 | + let i = 0; |
| 59 | + const interval = setInterval(() => { |
| 60 | + const squareData = sequence[i]; |
| 61 | + const square = document.querySelector(`[data-index='${squareData.index}']`); |
| 62 | + showColor(square, squareData.color); |
| 63 | + i++; |
| 64 | + if (i >= sequence.length) { |
| 65 | + clearInterval(interval); |
| 66 | + } |
| 67 | + }, 1000); |
| 68 | + } |
| 69 | + |
| 70 | + function showColor(square, color) { |
| 71 | + const originalColor = square.style.backgroundColor; |
| 72 | + square.style.backgroundColor = color; |
| 73 | + setTimeout(() => { |
| 74 | + square.style.backgroundColor = originalColor; |
| 75 | + }, 500); |
| 76 | + } |
| 77 | + |
| 78 | + function handleSquareClick(index) { |
| 79 | + if (userSequence.length < sequence.length) { |
| 80 | + const square = document.querySelector(`[data-index='${index}']`); |
| 81 | + const squareData = sequence[sequence.length - userSequence.length - 1]; |
| 82 | + showColor(square, squareData.color); |
| 83 | + userSequence.push({ index: parseInt(index), color: squareData.color }); |
| 84 | + |
| 85 | + // Check the sequence after each click |
| 86 | + if (!checkPartialSequence()) { |
| 87 | + status.textContent = 'Wrong! Try again.'; |
| 88 | + resetGame(); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + // If the full sequence is entered, validate it |
| 93 | + if (userSequence.length === sequence.length) { |
| 94 | + if (checkSequence()) { |
| 95 | + status.textContent = 'Correct! Next level...'; |
| 96 | + setTimeout(nextLevel, 1000); |
| 97 | + } else { |
| 98 | + status.textContent = 'Wrong! Try again.'; |
| 99 | + resetGame(); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + function checkPartialSequence() { |
| 106 | + // Check the user sequence against the reversed sequence up to the current length |
| 107 | + for (let i = 0; i < userSequence.length; i++) { |
| 108 | + if (userSequence[i].index !== sequence[sequence.length - 1 - i].index) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + function checkSequence() { |
| 116 | + // Check if the entire user sequence matches the reversed game sequence |
| 117 | + return userSequence.every((data, index) => data.index === sequence[sequence.length - 1 - index].index); |
| 118 | + } |
| 119 | + |
| 120 | + function resetGame() { |
| 121 | + sequence = []; |
| 122 | + userSequence = []; |
| 123 | + level = 0; |
| 124 | + } |
| 125 | + |
| 126 | + // Create the initial grid |
| 127 | + createGrid(); |
| 128 | +}); |
0 commit comments