Skip to content

About

Dots & Boxes engine and AI playground – seven AIs from random to chain control, with an interactive demo.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

dots-and-boxes

CI

A Dots & Boxes playground: a framework-free TypeScript rules engine with nine AIs, plus a minimal web demo. It started as a university project and now lives here as a small, well-tested library of game AI techniques.

Play the demo — play against the computer, watch two AIs face off, or run strength simulations.

The game

Players take turns drawing lines between adjacent dots on a grid. Completing the fourth side of a box claims it and grants another turn. When all boxes are claimed, the most boxes wins. Simple rules, surprisingly deep strategy: the endgame revolves around chains (forced capture sequences) and control (who is forced to open them first).

The AIs

AI Idea in one line
Random Uniformly random legal move. The baseline.
Greedy Always completes a box when possible, otherwise random.
Smart Greedy Completes boxes, otherwise avoids handing any to the opponent.
Minimax Chain analysis with a depth-limited minimax search (ported from the original).
Alpha-Beta Game-tree search with alpha-beta pruning, a transposition table and quiescence.
Chain Control Search plus endgame control theory: exact small endgames and loony sacrifices.
Monte Carlo (MCTS) UCT tree search over heuristic playouts, with exact small endgames.
Deepening Iterative deepening: searches until its second runs out.
Q-Learning Learned linear weights over board features; no search at play time.

Indicative strength on 3×3 (npm run benchmark -w dots-and-boxes-ai, round-robin, 2 games per pairing):

AI W L ms/move
deepening 14 2 319.4
chain-control 12 4 14.4
alpha-beta 11 5 14.2
mcts 10 6 22.3
smart-greedy 8 8 0.0
q-learning 8 8 0.0
minimax 5 11 0.0
greedy 4 12 0.0
random 0 16 0.0

Quickstart

Requires Node.js 20 or newer.

git clone https://github.com/nbozidarevic/dots-and-boxes.git
cd dots-and-boxes
npm install
npm run dev        # demo at http://localhost:5173
npm run test       # all unit tests (154 total)
npm run build      # production demo build
npm run benchmark -w dots-and-boxes-ai

Using the library

The engine has zero dependencies and ships ESM, CJS and TypeScript declarations. (Published as dots-and-boxes-ai — see Publishing below.)

import {
  chainControlAI,
  createGame,
  getWinner,
  isGameOver,
  playMove,
  randomAI,
} from 'dots-and-boxes-ai'

let state = createGame(5, 5)
while (!isGameOver(state)) {
  const move = (state.currentPlayer === 0 ? chainControlAI : randomAI)(state)
  state = playMove(state, move!)
}
console.log('winner:', getWinner(state), 'scores:', state.scores)

States are immutable snapshots, so every position is trivially replayable, serializable (serializeState / deserializeState) and React-friendly. Custom AIs implement one function type:

type AI = (state: GameState, rng?: () => number) => Move | null

Pass a createSeededRng(seed) source for fully deterministic games. See packages/core/README.md for the full API reference.

Project layout

apps/demo      Vite + React demo (English, minimal design, GitHub Pages deploy)
packages/core  Rules engine, AI opponents, board component, benchmark (120 tests)

Root scripts: dev, build, test, lint (oxlint), format (prettier), typecheck. CI runs all of them; pushing to master redeploys the demo.

Publishing (npm)

packages/core publishes as dots-and-boxes-ai (prepack builds ESM/CJS/declarations, verified against ESM, CJS and nodenext TypeScript consumers). Releases go out automatically with provenance via the publish workflow:

  1. One-time npmjs.com setup: package Settings → Trusted Publisher → this repo and the publish workflow.
  2. Bump the version in packages/core/package.json, commit and push.
  3. Push a tag v + version (e.g. git tag v0.2.0 && git push origin v0.2.0); publishing starts automatically. The workflow refuses to run when the tag and version disagree.

License

MIT — see LICENSE.

About

Dots & Boxes engine and AI playground – seven AIs from random to chain control, with an interactive demo.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages