|
| 1 | +import React, { useState, useEffect, useRef } from "react"; |
| 2 | +import "../../styles/pages/games/SimonSays.css"; |
| 3 | + |
| 4 | +export const SimonSays = () => { |
| 5 | + const [sequence, setSequence] = useState([]); |
| 6 | + const [userSequence, setUserSequence] = useState([]); |
| 7 | + const [isPlaying, setIsPlaying] = useState(false); |
| 8 | + const [isUserTurn, setIsUserTurn] = useState(false); |
| 9 | + const [level, setLevel] = useState(0); |
| 10 | + const [gameOver, setGameOver] = useState(false); |
| 11 | + const [activeButton, setActiveButton] = useState(null); |
| 12 | + const [message, setMessage] = useState("Press Start to Play!"); |
| 13 | + |
| 14 | + const colors = ["red", "blue", "green", "yellow"]; |
| 15 | + const audioContextRef = useRef(null); |
| 16 | + |
| 17 | + // Initialize audio context |
| 18 | + useEffect(() => { |
| 19 | + audioContextRef.current = new (window.AudioContext || window.webkitAudioContext)(); |
| 20 | + return () => { |
| 21 | + if (audioContextRef.current) { |
| 22 | + audioContextRef.current.close(); |
| 23 | + } |
| 24 | + }; |
| 25 | + }, []); |
| 26 | + |
| 27 | + // Play sound for each color |
| 28 | + const playSound = (color) => { |
| 29 | + if (!audioContextRef.current) return; |
| 30 | + |
| 31 | + const frequencies = { |
| 32 | + red: 329.63, // E4 |
| 33 | + blue: 261.63, // C4 |
| 34 | + green: 392.00, // G4 |
| 35 | + yellow: 440.00 // A4 |
| 36 | + }; |
| 37 | + |
| 38 | + const oscillator = audioContextRef.current.createOscillator(); |
| 39 | + const gainNode = audioContextRef.current.createGain(); |
| 40 | + |
| 41 | + oscillator.connect(gainNode); |
| 42 | + gainNode.connect(audioContextRef.current.destination); |
| 43 | + |
| 44 | + oscillator.frequency.value = frequencies[color]; |
| 45 | + oscillator.type = "sine"; |
| 46 | + |
| 47 | + gainNode.gain.setValueAtTime(0.3, audioContextRef.current.currentTime); |
| 48 | + gainNode.gain.exponentialRampToValueAtTime(0.01, audioContextRef.current.currentTime + 0.3); |
| 49 | + |
| 50 | + oscillator.start(audioContextRef.current.currentTime); |
| 51 | + oscillator.stop(audioContextRef.current.currentTime + 0.3); |
| 52 | + }; |
| 53 | + |
| 54 | + // Flash button animation |
| 55 | + const flashButton = (color, duration = 500) => { |
| 56 | + return new Promise((resolve) => { |
| 57 | + setActiveButton(color); |
| 58 | + playSound(color); |
| 59 | + setTimeout(() => { |
| 60 | + setActiveButton(null); |
| 61 | + setTimeout(resolve, 200); |
| 62 | + }, duration); |
| 63 | + }); |
| 64 | + }; |
| 65 | + |
| 66 | + // Play the sequence |
| 67 | + const playSequence = async (seq) => { |
| 68 | + setIsUserTurn(false); |
| 69 | + setMessage("Watch the sequence..."); |
| 70 | + |
| 71 | + for (let color of seq) { |
| 72 | + await flashButton(color); |
| 73 | + } |
| 74 | + |
| 75 | + setIsUserTurn(true); |
| 76 | + setMessage("Your turn! Repeat the sequence."); |
| 77 | + }; |
| 78 | + |
| 79 | + // Start new game |
| 80 | + const startGame = () => { |
| 81 | + setGameOver(false); |
| 82 | + setLevel(1); |
| 83 | + setUserSequence([]); |
| 84 | + const newSequence = [colors[Math.floor(Math.random() * 4)]]; |
| 85 | + setSequence(newSequence); |
| 86 | + setIsPlaying(true); |
| 87 | + playSequence(newSequence); |
| 88 | + }; |
| 89 | + |
| 90 | + // Handle user button click |
| 91 | + const handleColorClick = async (color) => { |
| 92 | + if (!isUserTurn || gameOver || !isPlaying) return; |
| 93 | + |
| 94 | + await flashButton(color, 300); |
| 95 | + |
| 96 | + const newUserSequence = [...userSequence, color]; |
| 97 | + setUserSequence(newUserSequence); |
| 98 | + |
| 99 | + // Check if the user's input matches the sequence so far |
| 100 | + const currentIndex = newUserSequence.length - 1; |
| 101 | + |
| 102 | + if (newUserSequence[currentIndex] !== sequence[currentIndex]) { |
| 103 | + // Wrong color - game over |
| 104 | + setGameOver(true); |
| 105 | + setIsPlaying(false); |
| 106 | + setIsUserTurn(false); |
| 107 | + setMessage(`Game Over! You reached level ${level}. Press Start to try again.`); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + // Check if user completed the full sequence |
| 112 | + if (newUserSequence.length === sequence.length) { |
| 113 | + // User successfully completed the sequence |
| 114 | + setUserSequence([]); |
| 115 | + setLevel(level + 1); |
| 116 | + setIsUserTurn(false); |
| 117 | + setMessage("Great! Get ready for the next level..."); |
| 118 | + |
| 119 | + // Add new color to sequence after a delay |
| 120 | + setTimeout(() => { |
| 121 | + const newColor = colors[Math.floor(Math.random() * 4)]; |
| 122 | + const newSequence = [...sequence, newColor]; |
| 123 | + setSequence(newSequence); |
| 124 | + playSequence(newSequence); |
| 125 | + }, 1000); |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + // Reset game |
| 130 | + const resetGame = () => { |
| 131 | + setSequence([]); |
| 132 | + setUserSequence([]); |
| 133 | + setIsPlaying(false); |
| 134 | + setIsUserTurn(false); |
| 135 | + setLevel(0); |
| 136 | + setGameOver(false); |
| 137 | + setActiveButton(null); |
| 138 | + setMessage("Press Start to Play!"); |
| 139 | + }; |
| 140 | + |
| 141 | + return ( |
| 142 | + <div className="simon-says-container"> |
| 143 | + <h1>Simon Says</h1> |
| 144 | + <div className="game-info"> |
| 145 | + <p className="level">Level: {level}</p> |
| 146 | + <p className="message">{message}</p> |
| 147 | + </div> |
| 148 | + |
| 149 | + <div className="simon-board"> |
| 150 | + {colors.map((color) => ( |
| 151 | + <button |
| 152 | + key={color} |
| 153 | + className={`simon-button ${color} ${activeButton === color ? "active" : ""}`} |
| 154 | + onClick={() => handleColorClick(color)} |
| 155 | + disabled={!isUserTurn} |
| 156 | + /> |
| 157 | + ))} |
| 158 | + </div> |
| 159 | + |
| 160 | + <div className="controls"> |
| 161 | + {!isPlaying ? ( |
| 162 | + <button className="control-btn start-btn" onClick={startGame}> |
| 163 | + {gameOver ? "Play Again" : "Start Game"} |
| 164 | + </button> |
| 165 | + ) : ( |
| 166 | + <button className="control-btn reset-btn" onClick={resetGame}> |
| 167 | + Reset |
| 168 | + </button> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + ); |
| 173 | +}; |
0 commit comments