Skip to content

Tic-tac-toe

The smallest complete game: a 3×3 grid, two marks, and eight lines to complete.

Also known as
Noughts and crosses
Players
2 players
Origin
Traditional
Rules file
62 lines · BGDL 0.4 draft, runs on 1.0 · no hooks
  • Perfect information
  • Grid board

The game

Players take turns placing their mark on an empty square of a 3×3 grid. The first to complete a line of three, across, down or diagonally, wins; a full board with no line is a draw.

How it's built in BGDL

  • A grid board and one placement action: choose an empty square, put your mark there.
  • The eight winning lines (three across, three down and two diagonals) are listed once and checked after every move.
  • The end of the game, win or draw, is declared in the rules rather than coded by hand.

Tested: Its whole game tree is known, so every move and every ending is checked exhaustively.

The rules file

Its top-level sections:dsl, game, boards, components, zones, functions, flow, actions, end.

Download tic-tac-toe.rules.yaml

tic-tac-toe.rules.yaml

# Tic-tac-toe (noughts and crosses). A conformance game (issue #32): its whole game tree is known, so the interpreter's
# move generation and endings can be checked exhaustively.
dsl: "0.4"

game:
  id: tic-tac-toe
  name: Tic-tac-toe
  players: { min: 2, max: 2 }
  seats:
    topology: ring
    names: [x, o]                  # seat 0 = X, who moves first

boards:
  board:
    kind: grid
    size: [3, 3]
    labels: { x: "abc", y: "123" }

components:
  mark:
    props:
      owner: seat

zones:
  cell: { kind: cell, board: board, of: mark, capacity: 1, visibility: public }

functions:
  # The eight lines: three ranks, three files and the two diagonals.
  lines:
    params: {}
    returns: "list<list<square>>"
    expr: >
      range(1, 4).select(r => range(1, 4).select(f => square(f, r)))
      .concat(range(1, 4).select(f => range(1, 4).select(r => square(f, r))))
      .concat([ range(1, 4).select(i => square(i, i)), range(1, 4).select(i => square(i, 4 - i)) ])
  wins:
    params: { s: seat }
    returns: bool
    expr: "lines().any(l => l.all(sq => pieceAt(sq)?.owner == s))"

flow:
  - phase: play
    turns:
      order: { from: "seat(x)", direction: clockwise }
      budget: 1
      actions: [mark]
      onNoLegalAction:
        - endGame: { draw: true, reason: board full }

actions:
  mark:
    choose:
      - at: { from: "board.cells()", where: "pieceAt(at) == null" }
    effect:
      - create: { type: mark, props: { owner: self }, into: at }
      - if:
          cond: "wins(self)"
          then: [ { endGame: { winner: self, reason: three in a row } } ]

end:
  scoring: { win: 1, draw: 0.5, loss: 0 }
  rating: elo
Arbitas

Write your own game in BGDL

Start free and describe your game's rules, or book a demo and we'll walk through your rulebook with you.