A complete chess game built with vanilla JavaScript. Play against a friend or challenge the AI with three difficulty levels - perfect for both casual players and chess enthusiasts!
classic-chess-js/
├── pieces/ # All chess piece images
│ ├── white_pawn.png
│ ├── black_king.png
│ └── ... (12 total)
├── assets/ # Additional assets
│ └── screenshot.png # Game preview
├── index.html # Main game interface
├── styles.css # All visual styling
├── script.js # Game logic (2000+ lines)
├── README.md # This document
└── LICENSE # MIT License
Game Modes:
- 👥 Local 2-player
- 🤖 AI opponent (Easy/Medium/Hard)
Full Chess Rules:
- ♜ Castling (both kingside and queenside)
- ♟ En passant captures
- ♛ Pawn promotion
- ⚔️ Check/checkmate detection
- 🏳️ Stalemate recognition
Player Tools:
- 💡 Smart move hints
- ↩️ Unlimited undo
- ⏱️ Adjustable timers (3/5/10/15 min)
- 🔄 Instant restart
-
Download the game
git clone https://github.com/sagarbudhwani/classic-chess-js.git
-
Open in browser
- Simply double-click
index.html - Works in Chrome, Firefox, Edge (no server needed)
- Simply double-click
-
Start playing!
- Choose game mode and settings
- Drag pieces or click to move
| Level | Algorithm | Speed | Skill |
|---|---|---|---|
| Easy | Random valid moves | Instant | Beginner |
| Medium | 1-ply greedy search | Fast | Intermediate |
| Hard | 3-ply minimax | 1-2 sec delay | Advanced |
// Sample AI evaluation (from script.js)
function evaluatePosition() {
// Values pieces: pawn=1, knight/bishop=3, rook=5, queen=9
// Considers: king safety, piece activity, pawn structure
}Tricky Parts I Solved:
-
Castling Logic
Had to track if king/rook moved while ensuring no squares are under attack -
En Passant
Implemented special state tracking after pawn double-moves -
Check Detection
Optimized by checking attack paths to king instead of full board scan
You Might Notice:
- The AI pauses slightly on Hard difficulty (better simulation!)
- Mobile touch works but desktop is smoother
- Promotion always goes to queen (simpler UI)
- Use hints when learning (lightbulb button)
- Right-click cancels piece selection
- Enable timer for tournament-style games
MIT © Sagar Budhwani - Free to use, modify, and share! See LICENSE.