|
| 1 | +# 🎮 Memory Card Game |
| 2 | + |
| 3 | +A modern, interactive memory card game built with vanilla JavaScript, HTML5, and CSS3. Match pairs of emoji cards while tracking your moves and time! |
| 4 | + |
| 5 | +## 🎯 Game Features |
| 6 | + |
| 7 | +### Core Gameplay |
| 8 | +- **Card Matching**: Flip cards to find matching pairs |
| 9 | +- **Grid System**: Dynamic grid generation from a single HTML template |
| 10 | +- **Smart Game Logic**: Cards stay face-up when matched, flip back on mismatch |
| 11 | +- **Win Detection**: Automatic game completion detection with congratulations modal |
| 12 | + |
| 13 | +### Difficulty Levels |
| 14 | +- **Easy**: 4×4 grid (8 pairs) |
| 15 | +- **Medium**: 6×6 grid (18 pairs) |
| 16 | +- **Hard**: 8×8 grid (32 pairs) |
| 17 | + |
| 18 | +### Statistics Tracking |
| 19 | +- **Move Counter**: Tracks every pair selection |
| 20 | +- **Timer**: Real-time game duration in MM:SS format |
| 21 | +- **Pairs Counter**: Shows matched pairs vs. total pairs |
| 22 | + |
| 23 | +### Sound Effects 🔊 |
| 24 | +- **Card Flip**: Plays when flipping any card |
| 25 | +- **Match**: Celebratory sound when cards match |
| 26 | +- **Mismatch**: Feedback sound when cards don't match |
| 27 | +- **Victory**: Congratulations sound on game completion |
| 28 | + |
| 29 | +### Visual Effects |
| 30 | +- **3D Card Flip Animation**: Smooth CSS transform animations |
| 31 | +- **Hover Effects**: Interactive card hover states |
| 32 | +- **Button Press Effect**: Push-down animation on buttons |
| 33 | +- **Stats Flicker**: Animated reset indication on new game |
| 34 | + |
| 35 | +## 🎓 Assignment Requirements Met |
| 36 | + |
| 37 | +### 1. Functional Programming Concept (Chapter 11) |
| 38 | +**Location**: Lines 50-56 in `script.js` |
| 39 | +```javascript |
| 40 | +const createCardData = (numPairs) => { |
| 41 | + const selectedSymbols = CARD_SYMBOLS.slice(0, numPairs); |
| 42 | + const pairs = selectedSymbols.flatMap(symbol => [ |
| 43 | + { id: Math.random(), symbol, matched: false }, |
| 44 | + { id: Math.random(), symbol, matched: false } |
| 45 | + ]); |
| 46 | + return shuffleArray(pairs); |
| 47 | +}; |
| 48 | +``` |
| 49 | +- **Pure function** that doesn't mutate external state |
| 50 | +- Uses **higher-order functions** (`flatMap()`) |
| 51 | +- Returns new array without side effects |
| 52 | + |
| 53 | +### 2. DOM Manipulation |
| 54 | +**Location**: Lines 88-111 in `script.js` |
| 55 | +```javascript |
| 56 | +const renderGameBoard = () => { |
| 57 | + const gameBoard = document.getElementById('gameBoard'); |
| 58 | + const cardTemplate = document.getElementById('cardTemplate'); |
| 59 | + |
| 60 | + gameBoard.innerHTML = ''; |
| 61 | + gameBoard.className = `game-board ${gameState.difficulty}`; |
| 62 | + |
| 63 | + gameState.cards.forEach((cardData, index) => { |
| 64 | + const cardElement = cardTemplate.cloneNode(true); |
| 65 | + cardElement.id = `card-${index}`; |
| 66 | + cardElement.style.display = 'block'; |
| 67 | + cardElement.dataset.index = index; |
| 68 | + |
| 69 | + const cardBack = cardElement.querySelector('.card-back'); |
| 70 | + cardBack.textContent = cardData.symbol; |
| 71 | + |
| 72 | + cardElement.addEventListener('click', handleCardClick); |
| 73 | + gameBoard.appendChild(cardElement); |
| 74 | + }); |
| 75 | +}; |
| 76 | +``` |
| 77 | +- **Clones** single template element using `cloneNode()` |
| 78 | +- **Dynamically creates** multiple cards via JavaScript |
| 79 | +- Uses `querySelector()`, `appendChild()`, `innerHTML` |
| 80 | + |
| 81 | +### 3. Event Handling |
| 82 | +**Location**: Lines 166-191 in `script.js` |
| 83 | +```javascript |
| 84 | +const handleCardClick = (event) => { |
| 85 | + if (gameState.isProcessing) return; |
| 86 | + |
| 87 | + const cardElement = event.currentTarget; |
| 88 | + const index = parseInt(cardElement.dataset.index); |
| 89 | + const cardData = gameState.cards[index]; |
| 90 | + |
| 91 | + if (gameState.moves === 0) { |
| 92 | + startTimer(); |
| 93 | + } |
| 94 | + |
| 95 | + if (cardElement.classList.contains('flipped') || |
| 96 | + cardElement.classList.contains('matched')) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + cardElement.classList.add('flipped'); |
| 101 | + cardFlipSound.currentTime = 0; |
| 102 | + cardFlipSound.play().catch(e => console.log('Audio play failed:', e)); |
| 103 | + gameState.flippedCards.push({ element: cardElement, data: cardData, index }); |
| 104 | + |
| 105 | + if (gameState.flippedCards.length === 2) { |
| 106 | + gameState.moves++; |
| 107 | + updateStats(); |
| 108 | + checkForMatch(); |
| 109 | + } |
| 110 | +}; |
| 111 | +``` |
| 112 | +- Handles **click events** on cards |
| 113 | +- Uses `addEventListener()` for event binding |
| 114 | +- Event delegation pattern in difficulty buttons |
| 115 | + |
| 116 | +### 4. ES6 Features |
| 117 | +**Throughout the codebase:** |
| 118 | +- **Arrow functions**: `const handleCardClick = (event) => {...}` |
| 119 | +- **const/let**: Block-scoped variables instead of `var` |
| 120 | +- **Template literals**: `` `game-board ${gameState.difficulty}` `` |
| 121 | +- **Destructuring**: `const [card1, card2] = gameState.flippedCards;` |
| 122 | +- **Spread operator**: `...gameState` |
| 123 | +- **Default parameters**: Available in function definitions |
| 124 | + |
| 125 | +### 5. Two Enhancements Implemented |
| 126 | + |
| 127 | +#### Enhancement 1: Timer ⏱️ |
| 128 | +- Tracks game duration in seconds |
| 129 | +- Formats display as MM:SS |
| 130 | +- Starts on first move, stops on game completion |
| 131 | +- Displays in final score modal |
| 132 | + |
| 133 | +#### Enhancement 2: Difficulty Levels 🎚️ |
| 134 | +- Three difficulty options (Easy, Medium, Hard) |
| 135 | +- Different grid sizes (4×4, 6×6, 8×8) |
| 136 | +- Adjustable card pair counts |
| 137 | +- Responsive grid layouts |
| 138 | + |
| 139 | +## 🚀 How to Play |
| 140 | + |
| 141 | +1. **Select Difficulty**: Choose Easy, Medium, or Hard |
| 142 | +2. **Start Playing**: Click any card to flip it |
| 143 | +3. **Find Matches**: Click another card to find its match |
| 144 | +4. **Match Found**: Cards stay face-up with reduced opacity |
| 145 | +5. **No Match**: Cards flip back after 1 second |
| 146 | +6. **Win**: Match all pairs to see your stats! |
| 147 | +7. **New Game**: Click "New Game" to reset and play again |
| 148 | + |
| 149 | +## 🛠️ Technologies Used |
| 150 | + |
| 151 | +- **HTML5**: Semantic markup and audio elements |
| 152 | +- **CSS3**: Flexbox, Grid, animations, transforms |
| 153 | +- **JavaScript (ES6+)**: Modern syntax and features |
| 154 | +- **Web Audio API**: HTML5 Audio for sound effects |
| 155 | + |
| 156 | +## 📁 Project Structure |
| 157 | + |
| 158 | +``` |
| 159 | +MemoryGame-JS/ |
| 160 | +│ |
| 161 | +├── index.html # Main HTML file |
| 162 | +├── style.css # All styling and animations |
| 163 | +├── script.js # Game logic and functionality |
| 164 | +├── README.md # This file |
| 165 | +│ |
| 166 | +└── assets/ |
| 167 | + └── sound/ |
| 168 | + ├── cardflip.mp3 # Card flip sound |
| 169 | + ├── cardmatch.mp3 # Match success sound |
| 170 | + ├── cardfail.mp3 # Mismatch sound |
| 171 | + └── congrats.mp3 # Victory sound |
| 172 | +``` |
| 173 | + |
| 174 | +## 🎨 Key Implementation Details |
| 175 | + |
| 176 | +### Single Card Template |
| 177 | +Only **one card element** exists in HTML (`#cardTemplate`), which is cloned via JavaScript to create all game cards. |
| 178 | + |
| 179 | +### Game State Management |
| 180 | +Centralized state object tracks: |
| 181 | +- Difficulty level |
| 182 | +- Card array |
| 183 | +- Flipped cards |
| 184 | +- Matched pairs count |
| 185 | +- Moves and timer |
| 186 | +- Processing state |
| 187 | + |
| 188 | +### Responsive Design |
| 189 | +- Mobile-friendly layouts |
| 190 | +- Adaptive grid sizing |
| 191 | +- Touch-friendly card sizes |
| 192 | + |
| 193 | +## 🎵 Sound Requirements |
| 194 | + |
| 195 | +Place these MP3 files in `assets/sound/`: |
| 196 | +- `cardflip.mp3` |
| 197 | +- `cardmatch.mp3` |
| 198 | +- `cardfail.mp3` |
| 199 | +- `congrats.mp3` |
| 200 | + |
| 201 | +## 🌐 Browser Compatibility |
| 202 | + |
| 203 | +- Chrome/Edge (latest) |
| 204 | +- Firefox (latest) |
| 205 | +- Safari (latest) |
| 206 | +- Supports modern ES6+ features |
| 207 | + |
| 208 | +## 📝 License |
| 209 | + |
| 210 | +This project is for educational purposes as part of DEV 209 coursework. |
| 211 | + |
| 212 | +## 👤 Author |
| 213 | + |
| 214 | +Created for DEV 209 - Memory Game Assignment |
| 215 | + |
| 216 | +--- |
| 217 | + |
| 218 | +**Enjoy the game! 🎉** |
0 commit comments