Skip to content

cnkang/sudoku

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1,206 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-Size Sudoku Challenge (v3.0.0)

Project Description

A modern, full-featured Sudoku game supporting 4×4, 6×6, and 9×9 grids, built with Next.js 16 and React 19. Features intelligent puzzle generation, child-friendly design, and comprehensive accessibility. Comprehensively optimized across design, performance, security, and code quality with 16% bundle size reduction, WCAG AAA accessibility, and distinctive geometric design system. Achieves 87.5% test coverage with 70+ validated correctness properties.

Features

Comprehensive Optimization Highlights

This application has undergone comprehensive optimization across four key areas, transforming it from functional to unforgettable while maintaining 87.5% test coverage and WCAG AAA accessibility:

🎨 Design System Transformation

  • Custom Typography: Professional font system with Space Grotesk, Inter Variable, and JetBrains Mono
  • Geometric Sunset Palette: Distinctive warm colors (coral, amber, teal, indigo) with validated 7:1 contrast ratios
  • Delightful Animations: 60fps CSS animations (ripple, burst, shake, reveal, confetti) with reduced-motion support
  • Build-Time Validation: Automated contrast ratio checking prevents accessibility regressions

⚡ Performance Optimization

  • 16% Bundle Reduction: 250KB → 210KB through tree-shaking, code splitting, and direct imports
  • 25-40% Faster Operations: Parallelized async operations eliminate request waterfalls
  • Two-Tier Caching: React.cache() + LRU with 80%+ hit rate for puzzle generation
  • Optimized Re-renders: React.memo, useEvent pattern, and stable callbacks minimize unnecessary updates

🔒 Security Hardening

  • Defense in Depth: CSP with nonce, comprehensive security headers (HSTS, X-Frame-Options, etc.)
  • Input Validation: Zod schemas for all API inputs with XSS/injection prevention
  • API Protection: Rate limiting, origin validation, CSRF tokens, request size limits
  • Zero Vulnerabilities: Automated pnpm audit with Dependabot for continuous security

✅ Code Quality & Testing

  • TypeScript Strict Mode: No any types, explicit return types, discriminated unions
  • 70+ Property Tests: Correctness properties validated with fast-check for design, performance, security, and accessibility
  • Comprehensive Coverage: 87.5% test coverage maintained across all optimizations
  • Progressive Enhancement: Modern features with fallbacks for older browsers

Core Gameplay

  • Multi-Size Grid System: Support for 4×4 (beginner), 6×6 (intermediate), and 9×9 (traditional) grids
  • Adaptive Difficulty: 4×4 (3-5 levels), 6×6 (5-7 levels), 9×9 (10 levels) with intelligent puzzle generation
  • Seamless Grid Switching: Smooth transitions between grid sizes with state preservation
  • Real-time Timer: Track your solving time with pause/resume functionality
  • Solution Validation: Instant feedback on puzzle completion

Game Controls

  • Hint System: Smart hints with usage tracking and strategic suggestions
  • Undo/Redo: Full move history with unlimited undo capability
  • Reset Game: Generate new puzzles with cooldown protection
  • Pause/Resume: Pause timer and hide grid for breaks
  • Grid Size Selector: Switch between 4×4, 6×6, and 9×9 grids
  • Difficulty Selector: Adaptive difficulty ranges per grid size

Design System (v3.0)

  • Custom Typography System: Space Grotesk for display, Inter Variable for body, JetBrains Mono for grid numbers
  • Font Loading Optimization: next/font with font-display: swap, CLS < 0.1, system font fallbacks
  • Geometric Sunset Palette: Coral, amber, teal, indigo primaries with warm cream backgrounds
  • WCAG AAA Compliance: All color combinations maintain 7:1 contrast ratios (validated at build time)
  • Delightful Animations: Ripple effects, particle bursts, gentle shakes, geometric reveals, confetti explosions
  • 60fps Performance: CSS-only animations with GPU acceleration
  • Reduced Motion Support: Respects prefers-reduced-motion for accessibility
  • Geometric Decorative Elements: Circles, triangles, squares with mesh gradients
  • Asymmetric Layout: Diagonal flow composition with Container Queries

Performance Optimizations

Bundle Optimization

  • 16% Bundle Size Reduction: From 250KB to 210KB gzipped through comprehensive optimization
  • Tree-Shaking: Direct imports (no barrel files), lodash-es for ES modules
  • Code Splitting: React.lazy() for heavy components (> 50KB), route-based splitting
  • Font Optimization: next/font with automatic subsetting and preloading

Runtime Performance

  • 25-40% Faster Grid Changes: Parallelized async operations with Promise.all()
  • Two-Tier Caching: React.cache() for per-request + LRU with TTL for cross-request
  • Cache Hit Rate: > 80% for puzzle generation with 30-second TTL
  • React.memo Optimization: Minimized re-renders for SudokuGrid and Timer components
  • Stable Callbacks: useEvent pattern for callbacks that don't trigger re-renders
  • Passive Event Listeners: Smooth scrolling and touch with passive: true
  • Debounced/Throttled Handlers: Optimized scroll and touch event processing
  • Intersection Observer: Efficient visibility detection replacing scroll listeners

Server Optimization

  • Per-Request Deduplication: React.cache() prevents duplicate work within requests
  • Cross-Request Caching: LRU cache with automatic eviction for memory efficiency
  • Edge Runtime: API routes optimized for edge deployment
  • Request Deduplication: Duplicate API calls within 5s window share results

Security Hardening

Security Headers

  • Content Security Policy: CSP with nonce for inline scripts/styles, violation reporting
  • X-Frame-Options: DENY to prevent clickjacking attacks
  • Strict-Transport-Security: HSTS with 1-year max-age and includeSubDomains
  • X-Content-Type-Options: nosniff to prevent MIME type sniffing
  • Referrer-Policy: strict-origin-when-cross-origin for privacy
  • Permissions-Policy: Restricted browser features (camera, microphone, geolocation)

Input Validation & Sanitization

  • Zod Schema Validation: Type-safe validation for all API inputs
  • Input Sanitization: XSS prevention through HTML entity escaping
  • Length Limits: DoS prevention with maximum input sizes
  • LocalStorage Validation: Structure and version validation with corruption recovery
  • Malformed Request Rejection: 400 status codes with sanitized error messages

API Security

  • Rate Limiting: Per-endpoint limits with 429 responses and Retry-After headers
  • Origin Validation: Request origin checking against allowed domains
  • CORS Configuration: Strict allowed origins, methods, and headers
  • Request Size Limits: Maximum body size enforcement (1MB default)
  • CSRF Protection: Token-based protection for state-changing operations
  • HTTPS-Only: All API communication enforced over HTTPS
  • Error Sanitization: Stack traces removed from client responses
  • Security Event Logging: Comprehensive logging for monitoring and alerting

Dependency Security

  • Zero Critical Vulnerabilities: Automated pnpm audit in CI pipeline
  • Dependabot Integration: Automated dependency updates with security patches
  • Version Pinning: Exact versions in package.json for reproducible builds
  • Lockfile Integrity: pnpm-lock.yaml verification in CI

Technical Features

  • Server-side Puzzle Generation: Advanced DLX algorithm for unique, solvable puzzles
  • Intelligent Caching: Two-tier caching with React.cache() and LRU with TTL
  • Mobile-first Design: Fully responsive with touch optimization and haptic feedback
  • Comprehensive Testing: 87.5% test coverage with unit, integration, property-based, and E2E tests
  • Property-Based Testing: 70+ correctness properties validated with fast-check
  • Type Safety: Full TypeScript strict mode with no any types
  • Progressive Enhancement: Modern CSS with @supports queries and fallbacks
  • Error Boundaries: Graceful error handling with user progress preservation
  • Retry Logic: Exponential backoff for failed API requests
  • Accessibility Excellence: WCAG AAA compliance with keyboard navigation and screen reader support

Prerequisites

  • Node.js 24.14.1 (see .nvmrc)
  • pnpm 10.29.2+

Installation

corepack enable
corepack use pnpm@10.29.2
pnpm install

Development

# Start development server
pnpm dev

# Build for production
pnpm build

# Start production server
pnpm start

Testing

Test Coverage: 87.5% (Maintained through comprehensive optimizations)

# Run all tests
pnpm test

# Run with coverage report
pnpm test:coverage

# Run property-based tests
pnpm test:pbt

# Run E2E tests
pnpm test:e2e

Test Categories

  • Component Tests: 40+ tests for UI components with React Testing Library
  • Hook Tests: Comprehensive state management and custom hook validation
  • API Tests: 31+ tests with caching, rate limiting, and security validation
  • Property-Based Tests: 70+ correctness properties validated with fast-check
    • Design system properties (contrast, fonts, animations, reduced-motion)
    • Performance properties (bundle size, lazy loading, caching, event listeners)
    • Security properties (CSP, input validation, rate limiting, CSRF protection)
    • Accessibility properties (keyboard navigation, ARIA labels, focus indicators)
    • Error handling properties (retry logic, error boundaries, progress preservation)
  • Responsive Tests: 20+ tests for mobile-first design and touch optimization
  • Utility Tests: Comprehensive validation and error handling coverage
  • E2E Tests: Playwright integration with multi-grid size scenarios
  • Accessibility Tests: WCAG AAA compliance validation with axe-core

## Performance Metrics

### Core Web Vitals (Production)
- **LCP (Largest Contentful Paint)**: < 2.5s on mobile 3G networks
- **FID (First Input Delay)**: < 100ms for all user interactions
- **CLS (Cumulative Layout Shift)**: < 0.1 during page load and font loading

### Optimization Results
- **Bundle Size**: 210KB gzipped (16% reduction from 250KB baseline)
- **Grid Size Change Latency**: 25-40% improvement through async parallelization
- **Cache Hit Rate**: > 80% for puzzle generation with two-tier caching
- **Animation Performance**: Consistent 60fps with GPU acceleration
- **Font Loading CLS**: < 0.1 with optimized fallbacks and font-display: swap
- **Re-render Optimization**: Minimized unnecessary renders with React.memo and useEvent

### Lighthouse Scores (Mobile)
- **Performance**: 95+ (optimized bundle, caching, lazy loading)
- **Accessibility**: 100 (WCAG AAA compliant with 7:1 contrast)
- **Best Practices**: 100 (security headers, HTTPS, modern standards)
- **SEO**: 100 (semantic HTML, meta tags, structured data)

### Build Metrics
- **TypeScript Strict Mode**: Zero any types, explicit return types
- **Zero Critical Vulnerabilities**: Automated pnpm audit in CI
- **Test Coverage**: 87.5% with 70+ property-based tests
- **Bundle Size Budget**: < 15% increase enforced in CI pipeline

## Code Quality

```bash
# Check code quality
pnpm quality

# Fix linting and formatting
pnpm quality:fix

SonarCloud Integration

This project is configured for SonarCloud Automatic Analysis for continuous code quality monitoring.

Project Structure

src/
├── app/                    # Next.js App Router
│   ├── api/solveSudoku/   # Puzzle generation API
│   ├── __tests__/         # Page component tests
│   ├── globals.css        # Global styles
│   ├── page.styles.ts     # Page-specific styles
│   └── page.tsx           # Main game page
├── components/            # React components
│   ├── __tests__/         # Component tests
│   ├── SudokuGrid.tsx     # Interactive game grid
│   ├── SudokuGrid.styles.ts # Grid component styles
│   ├── GameControls.tsx   # Game control buttons
│   ├── GameControls.styles.ts # Controls component styles
│   ├── Timer.tsx          # Game timer
│   └── DifficultySelector.tsx
├── hooks/                 # Custom React hooks
│   ├── __tests__/         # Hook tests
│   └── useGameState.ts    # Game state management
├── utils/                 # Utility functions
│   ├── __tests__/         # Utility tests
│   ├── hints.ts           # Hint generation logic
│   ├── apiCache.ts        # API caching
│   └── stats.ts           # Game statistics
├── test-utils/            # Testing utilities
└── types/                 # TypeScript definitions

API Endpoints

Puzzle Generation

  • POST /api/solveSudoku?difficulty=1-10&gridSize=9 - Generate 9×9 puzzle
  • POST /api/solveSudoku?difficulty=1-7&gridSize=6 - Generate 6×6 puzzle
  • POST /api/solveSudoku?difficulty=1-5&gridSize=4 - Generate 4×4 puzzle
  • POST /api/solveSudoku?difficulty=5&force=true - Force new puzzle (bypass cache)

Security Features

  • Rate limiting: 100 requests/minute per endpoint
  • Origin validation against allowed domains
  • Request size limits (1MB maximum)
  • CSRF protection for state-changing operations
  • Input validation with Zod schemas

Documentation

Technical Guides

Project Documentation

Dependencies

Runtime

  • next (16.1.6) - React framework with App Router and Turbopack
  • react (19.2.4) - UI library with React Compiler optimizations
  • react-dom (19.2.4) - React DOM renderer
  • fast-sudoku-solver (3.0.0) - Advanced DLX algorithm for puzzle generation
  • zod (4.3.6) - TypeScript-first schema validation

Development

  • typescript (5.9.3) - Type safety with strict mode
  • vitest (4.0.18) - Fast testing framework with coverage
  • @biomejs/biome (2.4.4) - Fast linter and formatter
  • playwright (1.58.2) - E2E testing framework
  • fast-check (4.5.3) - Property-based testing library
  • husky (9.1.7) - Git hooks for quality gates

License

MIT License - see LICENSE file for details.

About

A Sudoku game application built with Next.js and React, featuring an interactive grid for users to solve puzzles.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Contributors