Skip to content

top-5/klondike

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

19 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿƒ Klondike Solitaire

Modern TypeScript Edition โ€“ React + Vite

Deploy to GitHub Pages License

A modern, type-safe implementation of Classic Klondike Solitaire, built with React, TypeScript, and Vite, featuring beautiful spritesheet-based card graphics, custom mouse-driven drag & drop, auto-move functionality, and full move validation logic.

๐ŸŽฎ Play Now on GitHub Pages

Klondike Solitaire Screenshot

โœจ Features

  • โ™ ๏ธ Classic Klondike Rules โ€“ Traditional solitaire gameplay
  • ๐ŸŽจ Beautiful Card Graphics โ€“ High-quality spritesheet rendering with subtle animations
  • ๐Ÿ–ฑ๏ธ Custom Drag & Drop โ€“ Precise mouse-driven card movement with real-time visual feedback
  • ๐ŸŽฏ Multi-Card Stack Dragging โ€“ Drag entire card sequences smoothly with proper offset
  • ๐Ÿš€ Auto-Move to Foundation โ€“ Double-click cards to automatically send them to foundations
  • โœจ Flying Animation โ€“ Cards fly to their destination with beautiful arc motion
  • โœ… Move Validation โ€“ Only valid moves allowed
  • ๐Ÿ”„ Stock Recycling โ€“ Draw through deck multiple times
  • ๐Ÿ† Win Detection โ€“ Automatic celebration on completion
  • โฑ๏ธ Timer & Move Counter โ€“ Track your performance
  • ๐Ÿ“ฑ Responsive Design โ€“ Works on desktop, tablet, and mobile
  • ๐Ÿ”’ Crypto RNG โ€“ Cryptographically secure card shuffling
  • ๐Ÿงช Vitest Coverage โ€“ Fast, comprehensive tests
  • ๐Ÿงฉ TypeScript 5.7+ โ€“ Strict type checking
  • ๐Ÿ’… ESLint + Prettier โ€“ Consistent code style

๐Ÿš€ Quick Start

# Install dependencies
npm install

# Start dev server (auto-opens in browser)
npm run dev

# Build production bundle
npm run build

# Preview production build
npm run preview

# Run tests
npm test

Open http://localhost:10010/ to play.

๐Ÿ› ๏ธ Development

PowerShell Helpers

.\dev.ps1 start   # Start development mode
.\dev.ps1 stop    # Stop dev server
.\dev.ps1 status  # Check server status

Build & Lint

npm run build
npm run lint
npm run format

Testing

npm run test
npm run test:watch
npm run test:coverage

๐Ÿงฉ Technology Stack

Layer Technology Purpose
UI React 18 Game interface
Graphics Spritesheet Card rendering via canvas extraction
Drag System Custom Mouse Events Precise positioning & multi-card stacks
Build Vite 5 Lightning-fast dev + build
Language TypeScript 5.7 Type-safe logic
Tests Vitest 2.1 Unit testing
Quality ESLint + Prettier Formatting and linting
Runtime Node.js 18+ ES module environment

๐Ÿ“ Project Structure

cardserver/
โ”œโ”€โ”€ index.html
โ”œโ”€โ”€ vite.config.ts
โ”œโ”€โ”€ vitest.config.ts
โ”œโ”€โ”€ dev.ps1
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ main.tsx          # React entry point
โ”‚   โ”œโ”€โ”€ App.tsx           # Main game component
โ”‚   โ”œโ”€โ”€ App.css           # Styling
โ”‚   โ”œโ”€โ”€ types.ts          # Type definitions
โ”‚   โ”œโ”€โ”€ cardSprites.ts    # Spritesheet loader
โ”‚   โ””โ”€โ”€ gameLogic.ts      # Klondike rules engine
โ”œโ”€โ”€ public/
โ”‚   โ”œโ”€โ”€ deck.png          # Card spritesheet (4x13 grid)
โ”‚   โ”œโ”€โ”€ back.jpg          # Card back image
โ”‚   โ””โ”€โ”€ screenshot.png    # Game screenshot
โ”œโ”€โ”€ test/
โ”‚   โ””โ”€โ”€ *.test.ts         # Unit tests
โ””โ”€โ”€ .github/
    โ””โ”€โ”€ instructions.md

๐Ÿงฎ Game Rules (Classic Klondike Solitaire)

๐ŸŽฏ Objective

Move all 52 cards into four Foundations, building each suit from Ace to King.

๐Ÿงฑ Layout

  • Deck: 52 standard cards (no jokers)
  • Suits: โ™  Spades, โ™ฅ Hearts, โ™ฆ Diamonds, โ™ฃ Clubs
  • Colors: Red (โ™ฅโ™ฆ) / Black (โ™ โ™ฃ)
  • Tableau: 7 columns (1โ€“7 cards each)
  • Stock: Remaining 24 cards (face-down)
  • Waste: Discard pile (face-up)
  • Foundations: 4 piles, one per suit (build Aโ†’K)

Initial Deal:

Column Cards Face-up Face-down
1 1 1 0
2 2 1 1
3 3 1 2
4 4 1 3
5 5 1 4
6 6 1 5
7 7 1 6

โ™ป๏ธ Gameplay Rules

Stock โ†’ Waste

  • Draw 1 card from Stock onto Waste (classic mode)
  • When Stock is empty, flip the Waste back to form a new Stock (preserve order)

Waste โ†’ Tableau or Foundation

  • Move top Waste card to Foundation if next in suit order
  • Or move to Tableau if opposite color and one rank lower

Tableau โ†’ Tableau

  • Move sequences of descending, alternating-color cards (e.g., Red 6 on Black 7)
  • Uncovering a face-down card flips it face-up automatically
  • Empty columns can only be filled with a King (or sequence starting with King)

Foundation Building

  • Build by suit in ascending order (Aโ†’K)
  • Only Aces can start a foundation pile

๐Ÿง  Win Condition

All four Foundations complete (Aโ†’K per suit). The game ends automatically with celebration animation.

โš™๏ธ Configurable Parameters

Parameter Description Default
draw_count Cards drawn from Stock each turn 1
max_redeals Number of Stock recycles โˆž
auto_flip Auto-turn face-down when uncovered true
auto_move Auto-move valid cards to Foundation optional
scoring Vegas / Standard / None none

๐Ÿ’พ TypeScript Data Model

type Suit = 'Spades' | 'Hearts' | 'Diamonds' | 'Clubs';
type Rank = 'A' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | '10' | 'J' | 'Q' | 'K';

interface Card {
  suit: Suit;
  rank: Rank;
  faceUp: boolean;
}

interface GameState {
  stock: Card[];
  waste: Card[];
  foundations: Record<Suit, Card[]>;
  tableau: Card[][];
  moves: number;
  timer: number;
}

๐ŸŽจ Card Graphics Implementation

The game uses a spritesheet-based card rendering system for optimal performance and visual quality:

Spritesheet Layout

  • File: public/deck.png (4 rows ร— 13 columns)
  • Row mapping: 0=Clubs, 1=Hearts, 2=Spades, 3=Diamonds
  • Column mapping: 0=A, 1=2, ..., 9=10, 10=J, 11=Q, 12=K
  • Card back: public/back.jpg

Loading Process (cardSprites.ts)

  1. Load deck.png spritesheet into memory
  2. Calculate frame dimensions (width/13, height/4)
  3. Extract each card using canvas drawImage() with precise coordinates
  4. Convert to data URLs for React img elements
  5. Cache all 52 cards for instant rendering

This approach provides:

  • โœ… High-quality card graphics with smooth edges
  • โœ… Fast rendering (pre-extracted, cached data URLs)
  • โœ… Single spritesheet download (better than 52 separate images)
  • โœ… Subtle animations via CSS transforms

๐Ÿ–ฑ๏ธ Custom Drag & Drop System

The game implements a custom mouse-driven drag system (not HTML5 drag API) for precise control:

Why Custom Implementation?

  • HTML5 drag API forces semi-transparency on drag images (browser limitation)
  • Needed pixel-perfect positioning without "jump" on drag start
  • Required multi-card stack dragging with proper visual offset
  • Wanted full control over cursor states and visual feedback

How It Works

  1. Mouse Down: Calculate offset from card's top-left to click position
  2. Mouse Move: Track global mouse position, update drag overlay position
  3. Drag Overlay: Fixed-position div at mouseX - offsetX, mouseY - offsetY
  4. Original Cards: Set to opacity: 0 during drag (fully invisible, no ghosting)
  5. Drop Detection: Use elementFromPoint() to find drop zone under cursor
  6. Mouse Up: Validate move, update game state, reset cursor

Features

  • ๐ŸŽฏ Precise positioning โ€“ Card stays under cursor at click point
  • ๐Ÿ“š Multi-card stacks โ€“ Drag sequences with 25px offset per card
  • ๐Ÿ‘๏ธ Clean visuals โ€“ No ghostly images or transparency issues
  • ๐Ÿ–ฑ๏ธ Cursor feedback โ€“ grab โ†’ grabbing โ†’ normal states
  • โšก High performance โ€“ No unnecessary re-renders

๐ŸŽจ CSS Animations

/* Shimmer effect on cards */
@keyframes cardShimmer {
  0%, 100% { box-shadow: 0 2px 8px rgba(0,0,0,0.15); }
  50% { box-shadow: 0 4px 16px rgba(0,0,0,0.25); }
}

/* Flying animation for auto-move */
@keyframes flyToFoundation {
  0% { transform: scale(1) translateY(0); }
  50% { transform: scale(0.9) translateY(-30px); }
  100% { transform: scale(1) translateY(0); opacity: 0; }
}

๐Ÿš€ Deployment

This project uses GitHub Actions for automatic deployment to GitHub Pages.

Automatic Deployment

  • Every push to main branch triggers the deployment workflow
  • The workflow builds the app and publishes to gh-pages branch
  • Live site updates automatically at: https://top-5.github.io/klondike/

Manual Deployment (Optional)

npm run deploy    # Build and deploy to GitHub Pages using gh-pages

Workflow Status

Check the Actions tab to see deployment status and history.

๐Ÿงฑ Build & Deployment

npm run build     # Compile & bundle for production
npm run preview   # Local preview of production build

Everything outputs into /dist โ€” ready to serve via any static host (Netlify, Vercel, GitHub Pages, etc.).

๐Ÿงช Testing Notes

  • Run tests via Vitest for cards, moves, and win detection
  • Watch mode auto-reruns tests on file save
  • Coverage reports generated under /coverage

๐Ÿ“„ License

Non-Commercial License

This software is free for non-commercial use (personal, educational, research, AI training, etc).

Commercial use requires a separate license. Please contact @top-5 on GitHub for commercial licensing inquiries.

See LICENSE file for full terms.

ยฉ 2025 Top-5

About

Classic Solitaire - Klondike

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published