A modern, professional implementation of the classic 2048 puzzle game built with React, TypeScript, and Tailwind CSS. Features smooth animations, responsive design, and clean modular architecture.
- Responsive Design: Works on desktop, tablet, and mobile
- Multiple Grid Sizes: 2ร2 to 6ร6 grids
- Multi-Input Support: Keyboard, touch gestures, and mouse controls
- Score Persistence: Best scores saved automatically
- Professional Codebase: TypeScript, modular architecture, reusable components
src/
โโโ components/ # React UI components
โ โโโ ui/ # Reusable components (Button, Select)
โ โโโ game/ # Game-specific components
โโโ hooks/ # Custom React hooks
โโโ services/ # Business logic (Storage, TileRegistry)
โโโ utils/ # Pure functions (gameLogic, tileUtils)
โโโ constants/ # Configuration and theme
โโโ types/ # TypeScript definitions
- Service Layer:
StorageService,TileRegistryServicefor data management - Custom Hooks:
useGameBoardfor state,useSwipeGesturesfor input - Component Composition: Reusable UI components with clear separation
- Type Safety: Comprehensive TypeScript coverage
- Stable Tile IDs: TileRegistryService maintains component identity for smooth CSS transitions
- Position Calculations: Dynamic sizing for responsive gameplay across devices
- Performance: Memoized components prevent unnecessary re-renders
- Centralized State: Single
useGameBoardhook manages all game logic - Immutable Updates: Pure functions ensure predictable state changes
- Type Safety: Comprehensive TypeScript interfaces and strict typing
- Multi-Platform: Keyboard (Arrow/WASD), touch gestures, mouse clicks
- Gesture Detection: Sophisticated swipe detection with configurable thresholds
- Accessibility: Full keyboard navigation and screen reader support
- Node.js 16+ (Download)
- Modern browser (Chrome, Firefox, Safari, Edge)
# Clone and install
git clone <repository-url>
cd 2048-game
npm install
# Start development server
npm start
# Game opens at http://localhost:3000
# Build for production
npm run buildCombine tiles with the same number to reach 2048. Continue playing for higher scores!
- Desktop: Arrow keys, WASD, or click direction buttons
- Mobile: Swipe gestures or tap direction buttons
- Move: All tiles slide until they hit the edge or another tile
- Merge: Identical tiles combine into one with double the value
- Score: Each merge adds the new tile's value to your score
- New Tiles: A new tile (2 or 4) appears after each move
- Win: Create a 2048 tile (can continue for higher scores)
- Game Over: No more moves possible
- 2ร2: Quick games for beginners
- 3ร3: Moderate challenge
- 4ร4: Classic experience (default)
- 5ร5: Strategic gameplay
- 6ร6: Maximum challenge
- Keep highest tile in a corner
- Build tiles in one direction
- Plan moves ahead
- Focus on larger combinations
Customize game settings in src/constants/game.ts:
export const GAME_CONFIG = {
DEFAULT_SIZE: 4, // Starting grid size
WINNING_TILE: 2048, // Target tile value
ANIMATION_DURATION: 300, // Animation speed (ms)
} as const;
export const THEME_COLORS = {
background: "#faf8ef",
boardBackground: "#bbada0",
// ... more colors
} as const;The modular architecture makes extending the game straightforward:
- Services: Add new services for features like audio or analytics
- Hooks: Create custom hooks for new input methods or game modes
- Components: Build reusable UI components following existing patterns
- Utils: Add pure functions for game logic extensions
- Unit Tests: Test utilities and services in isolation
- Component Tests: Test React components with proper mocking
- Integration Tests: Test complete user workflows
- Components use
React.memofor optimization - State updates are immutable and efficient
- Bundle analysis available with
webpack-bundle-analyzer
- Undo/Redo System: Move history with undo functionality
- Game Statistics: Detailed analytics (moves, time, efficiency)
- Multiple Themes: Dark mode, high contrast, custom color schemes
- Sound Effects: Audio feedback for moves, merges, and achievements
- Multiplayer Support: Real-time competitive gameplay
- Progressive Web App: Offline support and native app experience
Game Won't Start
npm cache clean --force
rm -rf node_modules package-lock.json
npm install && npm startTypeScript Errors
npm install typescript@latest
# Restart TypeScript service in VS Code: Ctrl+Shift+P โ "TypeScript: Restart TS Server"Animation Issues
- Check if
prefers-reduced-motionis enabled in browser settings - Try reducing grid size for better performance
Mobile Touch Issues
- Ensure you're swiping on the game board area, not UI elements
- Adjust
thresholdvalue inuseSwipeGestures.tsif needed
- Chrome: 88+ โ
- Firefox: 85+ โ
- Safari: 14+ โ
- Edge: 88+ โ
- Mobile: iOS 14+, Android 8+ โ
-
Fork & Clone
git clone https://github.com/yourusername/2048-game.git cd 2048-game -
Install & Develop
npm install git checkout -b feature/your-feature-name # Make changes, add tests -
Submit
npm run type-check && npm run lint git commit -m "Add amazing feature" git push origin feature/your-feature-name # Create Pull Request
- Use TypeScript for all new code
- Follow existing patterns and naming conventions
- Add tests for new features
- Keep components small and focused
MIT License - see LICENSE file for details.
What this means:
- โ Commercial use, modification, distribution allowed
- โ No warranty or liability provided
- Original 2048: Gabriele Cirulli
- Technologies: React, TypeScript, Tailwind CSS, Framer Motion
- Community: React, TypeScript, and open source communities
Made with โค๏ธ for developers and gamers
If you found this helpful, please โญ the repository!