|
| 1 | +import React, { useState, useEffect, useCallback } from 'react'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import { ArrowLeftIcon, BrainIcon } from '@phosphor-icons/react'; // Using BrainIcon for memory game |
| 4 | +import colors from '../../config/colors'; |
| 5 | +import useSeo from '../../hooks/useSeo'; |
| 6 | +import '../../styles/MemoryGamePage.css'; |
| 7 | + |
| 8 | +const cardValues = ['🍎', '🍌', '🍒', '🍇', '🍋', '🍊', '🍓', '🍉']; // Example card values |
| 9 | + |
| 10 | +const MemoryGamePage = () => { |
| 11 | + useSeo({ |
| 12 | + title: 'Memory Game | Fezcodex', |
| 13 | + description: 'A classic memory game to test your concentration.', |
| 14 | + keywords: ['Fezcodex', 'memory game', 'match pairs', 'brain game', 'concentration'], |
| 15 | + ogTitle: 'Memory Game | Fezcodex', |
| 16 | + ogDescription: 'A classic memory game to test your concentration.', |
| 17 | + ogImage: 'https://fezcode.github.io/logo512.png', |
| 18 | + twitterCard: 'summary_large_image', |
| 19 | + twitterTitle: 'Memory Game | Fezcodex', |
| 20 | + twitterDescription: 'A classic memory game to test your concentration.', |
| 21 | + twitterImage: 'https://fezcode.github.io/logo512.png' |
| 22 | + }); |
| 23 | + |
| 24 | + const [cards, setCards] = useState([]); |
| 25 | + const [flippedCards, setFlippedCards] = useState([]); |
| 26 | + const [matchesFound, setMatchesFound] = useState(0); |
| 27 | + const [moves, setMoves] = useState(0); |
| 28 | + const [gameOver, setGameOver] = useState(false); |
| 29 | + const [gameStarted, setGameStarted] = useState(false); |
| 30 | + |
| 31 | + const initializeGame = useCallback(() => { |
| 32 | + const shuffledCards = shuffleCards(cardValues); |
| 33 | + setCards(shuffledCards); |
| 34 | + setFlippedCards([]); |
| 35 | + setMatchesFound(0); |
| 36 | + setMoves(0); |
| 37 | + setGameOver(false); |
| 38 | + setGameStarted(false); // Game starts when "Play Game" is clicked |
| 39 | + }, []); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + initializeGame(); |
| 43 | + }, [initializeGame]); |
| 44 | + |
| 45 | + const shuffleCards = (values) => { |
| 46 | + let id = 0; |
| 47 | + const initialCards = [...values, ...values].map(value => ({ |
| 48 | + id: id++, |
| 49 | + value, |
| 50 | + isFlipped: false, |
| 51 | + isMatched: false, |
| 52 | + })); |
| 53 | + |
| 54 | + // Fisher-Yates shuffle algorithm |
| 55 | + for (let i = initialCards.length - 1; i > 0; i--) { |
| 56 | + const j = Math.floor(Math.random() * (i + 1)); |
| 57 | + [initialCards[i], initialCards[j]] = [initialCards[j], initialCards[i]]; |
| 58 | + } |
| 59 | + return initialCards; |
| 60 | + }; |
| 61 | + |
| 62 | + const handleCardClick = (clickedCard) => { |
| 63 | + if (!gameStarted || gameOver || clickedCard.isFlipped || clickedCard.isMatched || flippedCards.length === 2) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + const newCards = cards.map(card => |
| 68 | + card.id === clickedCard.id ? { ...card, isFlipped: true } : card |
| 69 | + ); |
| 70 | + setCards(newCards); |
| 71 | + setFlippedCards(prev => [...prev, clickedCard.id]); |
| 72 | + |
| 73 | + if (flippedCards.length === 1) { |
| 74 | + setMoves(prev => prev + 1); |
| 75 | + const firstCard = cards.find(card => card.id === flippedCards[0]); |
| 76 | + const secondCard = clickedCard; |
| 77 | + |
| 78 | + if (firstCard.value === secondCard.value) { |
| 79 | + // Match found |
| 80 | + setTimeout(() => { |
| 81 | + const matchedCards = newCards.map(card => |
| 82 | + card.id === firstCard.id || card.id === secondCard.id ? { ...card, isMatched: true } : card |
| 83 | + ); |
| 84 | + setCards(matchedCards); |
| 85 | + setMatchesFound(prev => prev + 1); |
| 86 | + setFlippedCards([]); |
| 87 | + }, 700); |
| 88 | + } else { |
| 89 | + // No match, flip back |
| 90 | + setTimeout(() => { |
| 91 | + const flippedBackCards = newCards.map(card => |
| 92 | + card.id === firstCard.id || card.id === secondCard.id ? { ...card, isFlipped: false } : card |
| 93 | + ); |
| 94 | + setCards(flippedBackCards); |
| 95 | + setFlippedCards([]); |
| 96 | + }, 1000); |
| 97 | + } |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + useEffect(() => { |
| 102 | + if (matchesFound === cardValues.length) { |
| 103 | + setGameOver(true); |
| 104 | + } |
| 105 | + }, [matchesFound]); |
| 106 | + |
| 107 | + const cardStyle = { |
| 108 | + backgroundColor: colors['app-alpha-10'], |
| 109 | + borderColor: colors['app-alpha-50'], |
| 110 | + color: colors.app, |
| 111 | + }; |
| 112 | + |
| 113 | + return ( |
| 114 | + <div className="py-16 sm:py-24"> |
| 115 | + <div className="mx-auto max-w-7xl px-6 lg:px-8 text-gray-300"> |
| 116 | + <Link |
| 117 | + to="/apps" |
| 118 | + className="text-article hover:underline flex items-center justify-center gap-2 text-lg mb-4" |
| 119 | + > |
| 120 | + <ArrowLeftIcon size={24} /> Back to Apps |
| 121 | + </Link> |
| 122 | + <h1 className="text-4xl font-bold tracking-tight sm:text-6xl mb-4 flex items-center"> |
| 123 | + <span className="codex-color">fc</span> |
| 124 | + <span className="separator-color">::</span> |
| 125 | + <span className="apps-color">apps</span> |
| 126 | + <span className="separator-color">::</span> |
| 127 | + <span className="single-app-color">mg</span> |
| 128 | + </h1> |
| 129 | + <hr className="border-gray-700" /> |
| 130 | + <div className="flex justify-center items-center mt-16"> |
| 131 | + <div |
| 132 | + className="group bg-transparent border rounded-lg shadow-2xl p-6 flex flex-col justify-between relative transform overflow-hidden h-full w-full max-w-4xl" |
| 133 | + style={cardStyle} |
| 134 | + > |
| 135 | + <div |
| 136 | + className="absolute top-0 left-0 w-full h-full opacity-10" |
| 137 | + style={{ |
| 138 | + backgroundImage: |
| 139 | + 'radial-gradient(circle, white 1px, transparent 1px)', |
| 140 | + backgroundSize: '10px 10px', |
| 141 | + }} |
| 142 | + ></div> |
| 143 | + <div className="relative z-10 p-1"> |
| 144 | + <h1 className="text-3xl font-arvo font-normal mb-4 text-app flex items-center gap-2"> |
| 145 | + <BrainIcon size={32} /> Memory Game |
| 146 | + </h1> |
| 147 | + <hr className="border-gray-700 mb-4" /> |
| 148 | + |
| 149 | + <div className="memory-game-area"> |
| 150 | + <div className="game-info mb-4 text-xl font-bold"> |
| 151 | + Moves: {moves} | Matches: {matchesFound}/{cardValues.length} |
| 152 | + </div> |
| 153 | + |
| 154 | + {gameOver && ( |
| 155 | + <div className="game-over-message text-center text-3xl font-bold mb-4"> |
| 156 | + You Won! |
| 157 | + <button |
| 158 | + onClick={initializeGame} |
| 159 | + 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" |
| 160 | + style={{ |
| 161 | + backgroundColor: 'rgba(0, 0, 0, 0.2)', |
| 162 | + color: cardStyle.color, |
| 163 | + borderColor: cardStyle.borderColor, |
| 164 | + border: '1px solid', |
| 165 | + }} |
| 166 | + > |
| 167 | + Play Again |
| 168 | + </button> |
| 169 | + </div> |
| 170 | + )} |
| 171 | + |
| 172 | + {!gameStarted && ( |
| 173 | + <div className="start-game-message text-center text-3xl font-bold mb-4"> |
| 174 | + Click "Play Game" to Start! |
| 175 | + <button |
| 176 | + onClick={() => setGameStarted(true)} |
| 177 | + 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" |
| 178 | + style={{ |
| 179 | + backgroundColor: 'rgba(0, 0, 0, 0.2)', |
| 180 | + color: cardStyle.color, |
| 181 | + borderColor: cardStyle.borderColor, |
| 182 | + border: '1px solid', |
| 183 | + }} |
| 184 | + > |
| 185 | + Play Game |
| 186 | + </button> |
| 187 | + </div> |
| 188 | + )} |
| 189 | + |
| 190 | + <div className={`cards-grid ${!gameStarted || gameOver ? 'blurred' : ''}`}> |
| 191 | + {cards.map(card => ( |
| 192 | + <div |
| 193 | + key={card.id} |
| 194 | + className={`card ${card.isFlipped || card.isMatched ? 'flipped' : ''} ${card.isMatched ? 'matched' : ''}`} |
| 195 | + onClick={() => handleCardClick(card)} |
| 196 | + > |
| 197 | + <div className="card-inner"> |
| 198 | + <div className="card-face card-back"></div> |
| 199 | + <div className="card-face card-front"> |
| 200 | + {card.value} |
| 201 | + </div> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + ))} |
| 205 | + </div> |
| 206 | + </div> |
| 207 | + </div> |
| 208 | + </div> |
| 209 | + </div> |
| 210 | + </div> |
| 211 | + </div> |
| 212 | + ); |
| 213 | +}; |
| 214 | + |
| 215 | +export default MemoryGamePage; |
0 commit comments