Skip to content

DIPESHGOEL27/Dots_and_Boxes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

21 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ Dots & Boxes

CI TypeScript React Node.js Socket.io License

A production-grade implementation of the classic Dots and Boxes strategy game featuring real-time online multiplayer, AI opponents with multiple difficulty levels, and a polished dark-themed UI โ€” built with TypeScript end-to-end.

๐ŸŽฏ Live Demo

Play Now โ†’

โœจ Features

๐ŸŽฒ Game Modes

Mode Description
๐Ÿ‘ฅ Local Multiplayer 2โ€“4 players on the same device, hot-seat style
๐Ÿค– vs AI Play against AI with Easy, Medium, or Hard difficulty
๐ŸŒ Online Multiplayer Real-time rooms via Socket.io with shareable invite links

๐Ÿค– AI Opponent

  • Easy โ€” Random move selection
  • Medium โ€” Greedy strategy: completes boxes, avoids giving away 3-sided boxes
  • Hard โ€” Minimax with alpha-beta pruning (depth 6, full search for โ‰ค12 remaining moves)

๐ŸŽจ Player Customization

  • Custom player names (persisted in localStorage)
  • 4 color presets
  • 12 avatar emojis

๐Ÿ† Game Features

  • 3ร—3 to 10ร—10 grid sizes
  • Automatic box completion detection & scoring
  • Turn indicator with active player glow animation
  • Game-over overlay with ranked leaderboard & confetti
  • Procedural sound effects (Web Audio API โ€” no external files)
  • Shareable room links for online play (/game/online/:roomId)
  • Disconnect handling with 30-second reconnection window
  • Mute toggle (persisted in localStorage)

๐Ÿ—๏ธ Architecture

dots_and_boxes/
โ”œโ”€โ”€ shared/              # Shared TypeScript package
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ types.ts     # Type definitions (GameState, events, etc.)
โ”‚       โ”œโ”€โ”€ constants.ts # Grid sizes, colors, timing constants
โ”‚       โ”œโ”€โ”€ gameLogic.ts # Core game logic (applyMove, findCompletedBoxes, etc.)
โ”‚       โ””โ”€โ”€ gameLogic.test.ts  # 52 unit tests
โ”‚
โ”œโ”€โ”€ backend/             # Node.js + Express + Socket.io
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ index.ts     # Server entry, health check, graceful shutdown
โ”‚       โ”œโ”€โ”€ socket/
โ”‚       โ”‚   โ”œโ”€โ”€ handlers.ts    # Socket event handlers with turn validation
โ”‚       โ”‚   โ””โ”€โ”€ validation.ts  # Input validation (payloads, line coords)
โ”‚       โ”œโ”€โ”€ game/
โ”‚       โ”‚   โ””โ”€โ”€ roomManager.ts # Room CRUD, TTL cleanup, disconnections
โ”‚       โ”œโ”€โ”€ middleware/
โ”‚       โ”‚   โ””โ”€โ”€ rateLimiter.ts # Per-socket sliding window rate limiting
โ”‚       โ””โ”€โ”€ utils/
โ”‚           โ””โ”€โ”€ logger.ts      # Structured logging (pino)
โ”‚
โ”œโ”€โ”€ frontend/            # React 19 + TypeScript (CRA)
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ App.tsx      # Router (react-router-dom v6)
โ”‚       โ”œโ”€โ”€ routes/
โ”‚       โ”‚   โ”œโ”€โ”€ Home.tsx       # Lobby with player customization
โ”‚       โ”‚   โ””โ”€โ”€ Game.tsx       # Route wrapper parsing URL params
โ”‚       โ”œโ”€โ”€ components/GameBoard/
โ”‚       โ”‚   โ”œโ”€โ”€ GameBoard.tsx  # Main orchestrator (local/AI/online)
โ”‚       โ”‚   โ”œโ”€โ”€ Board.tsx      # Pure rendering (dots, lines, boxes)
โ”‚       โ”‚   โ”œโ”€โ”€ Scoreboard.tsx # Player scores with turn glow
โ”‚       โ”‚   โ”œโ”€โ”€ WaitingRoom.tsx# Pre-game lobby with copy link
โ”‚       โ”‚   โ””โ”€โ”€ GameOver.tsx   # Results, leaderboard, confetti
โ”‚       โ”œโ”€โ”€ hooks/
โ”‚       โ”‚   โ”œโ”€โ”€ useSocket.ts   # Socket.io connection lifecycle
โ”‚       โ”‚   โ”œโ”€โ”€ useGameState.ts# Local game state via shared logic
โ”‚       โ”‚   โ””โ”€โ”€ useAI.ts       # AI strategies (easy/medium/hard)
โ”‚       โ””โ”€โ”€ utils/
โ”‚           โ””โ”€โ”€ sounds.ts      # Procedural Web Audio API sounds
โ”‚
โ””โ”€โ”€ .github/workflows/
    โ””โ”€โ”€ ci.yml           # CI pipeline (build, test, typecheck)

๐Ÿ› ๏ธ Tech Stack

Layer Technology
Language TypeScript (strict mode, end-to-end)
Frontend React 19, React Router v6, react-hot-toast, canvas-confetti
Backend Node.js, Express 4, Socket.io 4.8
Shared Monorepo shared package (dots-and-boxes-shared)
Testing Jest, React Testing Library (86+ tests across packages)
CI/CD GitHub Actions (Node 18 & 20 matrix)
Logging pino (structured JSON logging)
Deployment Vercel (frontend) + Render (backend)

๐Ÿ”’ Security & Reliability

  • Server-side turn validation โ€” prevents out-of-turn moves
  • Input validation โ€” all payloads validated (grid size, player info, line coordinates)
  • Rate limiting โ€” sliding window per socket (5 moves/sec, 2 rooms/min)
  • Room TTL โ€” automatic cleanup of stale rooms (1 hour)
  • Graceful shutdown โ€” SIGTERM/SIGINT handling with client notification
  • Disconnect handling โ€” 30-second reconnection window for dropped players

๐Ÿš€ Getting Started

Prerequisites

  • Node.js โ‰ฅ 18
  • npm

Installation

# Clone the repository
git clone https://github.com/DIPESHGOEL27/Dots_and_Boxes.git
cd Dots_and_Boxes

# Install & build the shared package
cd shared && npm install && npm run build && cd ..

# Install backend dependencies
cd backend && npm install && cd ..

# Install frontend dependencies
cd frontend && npm install --legacy-peer-deps && cd ..

Development

# Terminal 1: Start the backend
cd backend && npm run dev

# Terminal 2: Start the frontend
cd frontend && npm start

The frontend runs on http://localhost:3000 and the backend on http://localhost:4000.

Testing

# Run all tests
cd shared && npm test          # 52 tests
cd ../frontend && npx react-scripts test --watchAll=false  # 34 tests

Production Build

cd shared && npm run build
cd ../backend && npm run build
cd ../frontend && npx react-scripts build

๐Ÿ“ก API

Health Check

GET /health
โ†’ { status: "ok", uptime: 1234.5, rooms: 3, timestamp: "..." }

Socket.io Events

Direction Event Payload
Cโ†’S createRoom { gridSize, maxPlayers, playerInfo }
Cโ†’S joinRoom { roomId, playerInfo }
Cโ†’S startGame { roomId }
Cโ†’S makeMove { roomId, line: [x1,y1,x2,y2] }
Sโ†’C roomCreated { roomId }
Sโ†’C waitingForPlayers { players, maxPlayers, creator }
Sโ†’C startGame { state }
Sโ†’C updateGame { state }
Sโ†’C gameOver { state, winner, winnerName, isDraw }
Sโ†’C playerDisconnected { playerInfo, playerIndex, reconnectTimeout }
Sโ†’C playerReconnected { playerInfo, playerIndex }

๐Ÿ“„ License

MIT โ€” see LICENSE for details.

About

Dots and Boxes Game

Resources

License

Stars

Watchers

Forks

Contributors