@@ -36,6 +36,7 @@ const MemoryGamePage = () => {
3636 const [ moves , setMoves ] = useState ( 0 ) ;
3737 const [ gameOver , setGameOver ] = useState ( false ) ;
3838 const [ gameStarted , setGameStarted ] = useState ( false ) ;
39+ const [ startTime , setStartTime ] = useState ( null ) ;
3940
4041 const initializeGame = useCallback ( ( ) => {
4142 const shuffledCards = shuffleCards ( cardValues ) ;
@@ -45,12 +46,18 @@ const MemoryGamePage = () => {
4546 setMoves ( 0 ) ;
4647 setGameOver ( false ) ;
4748 setGameStarted ( false ) ; // Game starts when "Play Game" is clicked
49+ setStartTime ( null ) ;
4850 } , [ ] ) ;
4951
5052 useEffect ( ( ) => {
5153 initializeGame ( ) ;
5254 } , [ initializeGame ] ) ;
5355
56+ const startGame = ( ) => {
57+ setGameStarted ( true ) ;
58+ setStartTime ( Date . now ( ) ) ;
59+ } ;
60+
5461 const shuffleCards = ( values ) => {
5562 let id = 0 ;
5663 const initialCards = [ ...values , ...values ] . map ( ( value ) => ( {
@@ -120,11 +127,13 @@ const MemoryGamePage = () => {
120127 useEffect ( ( ) => {
121128 if ( matchesFound === cardValues . length ) {
122129 setGameOver ( true ) ;
130+ const duration = ( Date . now ( ) - startTime ) / 1000 ;
131+ if ( duration < 45 ) unlockAchievement ( 'combo_breaker' ) ;
123132 if ( moves <= 24 ) unlockAchievement ( 'sharp_eye' ) ;
124133 if ( moves <= 18 ) unlockAchievement ( 'eidetic_memory' ) ;
125134 if ( moves <= 14 ) unlockAchievement ( 'mind_palace' ) ;
126135 }
127- } , [ matchesFound , moves , unlockAchievement ] ) ;
136+ } , [ matchesFound , moves , unlockAchievement , startTime ] ) ;
128137
129138 const cardStyle = {
130139 backgroundColor : colors [ 'app-alpha-10' ] ,
@@ -190,7 +199,7 @@ const MemoryGamePage = () => {
190199 < div className = "start-game-message text-center text-3xl font-bold mb-4" >
191200 Click "Play Game" to Start!
192201 < button
193- onClick = { ( ) => setGameStarted ( true ) }
202+ onClick = { startGame }
194203 className = "block mx-auto mt-4 px-6 py-2 rounded-md text-lg font-arvo font-normal transition-colors duration-300 ease-in-out"
195204 style = { {
196205 backgroundColor : 'rgba(0, 0, 0, 0.2)' ,
0 commit comments