Build tic-tac-toe
This page builds tic-tac-toe from an empty file, one section at a time, then extends it into a match where the first player to win two rounds wins. To follow along, make a new game in the portal with Start blank, open its Rules tab, and replace what’s there as you go. The checker will list problems until the file is complete.
1. The game and its seats
Section titled “1. The game and its seats”Every file starts with the language version and the game section: an id (lower case, digits and hyphens), a name,
how many players it takes, and the seats.
dsl: "1.1"
game: id: tic-tac-toe name: Tic-tac-toe players: { min: 2, max: 2 } seats: topology: ring names: [x, o] # seat 0 is X, who moves first2. The board and the marks
Section titled “2. The board and the marks”The board is a 3 by 3 grid. Its labels name the squares the way players read them: columns a to c, rows 1 to
3.
boards: board: kind: grid size: [3, 3] labels: { x: "abc", y: "123" }The only component is a mark, which remembers whose it is:
components: mark: props: owner: seatMarks go on the board’s squares. A cell zone gives every square its own place, and capacity: 1 means a square holds
one mark at most. Everyone can see the board, so it’s public.
zones: cell: { kind: cell, board: board, of: mark, capacity: 1, visibility: public }3. The move
Section titled “3. The move”A player’s one move is to mark an empty square. The action’s choice step offers every square on the board
(board.cells()) where there’s no mark yet (pieceAt(at) == null), and its effect creates a mark owned by the player
(self) on the chosen square:
actions: mark: choose: - at: { from: "board.cells()", where: "pieceAt(at) == null", label: Choose an empty square } effect: - create: { type: mark, props: { owner: self }, into: at }4. Taking turns
Section titled “4. Taking turns”The flow has one phase of turns. X goes first, play passes clockwise, and each turn is one mark:
flow: - phase: play turns: order: { from: "seat(x)", direction: clockwise } budget: 1 actions: [mark] onNoLegalAction: - endGame: { draw: true, reason: board full }onNoLegalAction runs when the player whose turn it is has no legal move. In tic-tac-toe that only happens when the
board is full, so it ends the game as a draw.
5. Winning
Section titled “5. Winning”To know when someone has won, the rules need the eight lines on the board. A function computes them once, as lists of squares: three rows, three columns and two diagonals. A second function says whether a seat owns every square of some line:
functions: # The eight lines: three rows, three columns 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))"pieceAt(sq)?.owner is the owner of the mark on a square, or null if the square is empty, so an empty square never
counts. Now the move can end the game. After placing the mark, the action checks whether the player has a line:
actions: mark: choose: - at: { from: "board.cells()", where: "pieceAt(at) == null", label: Choose an empty square } effect: - create: { type: mark, props: { owner: self }, into: at } - if: cond: "wins(self)" then: [{ endGame: { winner: self, reason: three in a row } }]The whole file
Section titled “The whole file”Put together, that’s the complete game. It passes the checker and plays to the end:
dsl: "1.1"
game: id: tic-tac-toe name: Tic-tac-toe players: { min: 2, max: 2 } seats: topology: ring names: [x, o] # seat 0 is 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 rows, three columns 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", label: Choose an empty square } effect: - create: { type: mark, props: { owner: self }, into: at } - if: cond: "wins(self)" then: [{ endGame: { winner: self, reason: three in a row } }]Try it from the setup wizard’s Try it step, or the Playtest tab, against the search bot.
Extend it: a match of several rounds
Section titled “Extend it: a match of several rounds”One game of tic-tac-toe is short. This version plays rounds until someone has won two, and the players take turns to go first. It needs four changes.
A constant and some state. roundsToWin is a named constant, so the number is written once. The rules now keep
track of who starts the round and whether it’s over, and each seat counts the rounds it has won:
consts: roundsToWin: 2
state: global: starter: "seat? = null" # who moves first this round roundOver: "bool = false" seat: rounds: "int = 0" # rounds wonSetup. setup runs once, before the flow starts. X starts the first round:
setup: - set: { target: global.starter, value: "seat(x)" }A flow that loops. A loop runs its steps until its until holds. Each pass plays a round, a phase of turns that
lasts until global.roundOver, then clears the board, resets the flag, and hands the first move to the other player.
When the loop ends, a last phase ends the game:
flow: - loop: until: "allSeats().any(s => s.rounds >= roundsToWin)" steps: - phase: play turns: order: { from: "global.starter", direction: clockwise } budget: 1 actions: [mark] until: "global.roundOver" - phase: nextRound do: - removeFromGame: "allSeats().selectMany(s => pieces(s))" - set: { target: global.roundOver, value: false } - set: { target: global.starter, value: "opponent(global.starter)" } - phase: end do: - endGame: { winner: "allSeats().first(s => s.rounds >= roundsToWin)", reason: won the match }A move that ends a round, not the game. Winning a round now adds to the player’s count and ends the round. A full
board with no winner ends the round too, as a drawn round. The move checks this itself rather than leaving it to
onNoLegalAction, which runs when a player is stuck but doesn’t end the phase:
functions: # ... lines and wins as before ... boardFull: params: {} returns: bool expr: "board.cells().all(sq => pieceAt(sq) != null)"
actions: mark: choose: - at: { from: "board.cells()", where: "pieceAt(at) == null", label: Choose an empty square } effect: - create: { type: mark, props: { owner: self }, into: at } - if: cond: "wins(self)" then: - add: { target: self.rounds, value: 1 } - set: { target: global.roundOver, value: true } else: - if: cond: "boardFull()" then: [{ set: { target: global.roundOver, value: true } }]Two players who never make a mistake draw every round, so this match could go on for as long as they keep drawing. A real game would add a limit, such as a round count after which the match is a draw.
The whole match
Section titled “The whole match”dsl: "1.1"
game: id: tic-tac-toe-match name: Tic-tac-toe match players: { min: 2, max: 2 } seats: topology: ring names: [x, o]
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 }
consts: roundsToWin: 2
state: global: starter: "seat? = null" # who moves first this round roundOver: "bool = false" seat: rounds: "int = 0" # rounds won
functions: 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))" boardFull: params: {} returns: bool expr: "board.cells().all(sq => pieceAt(sq) != null)"
setup: - set: { target: global.starter, value: "seat(x)" }
flow: - loop: until: "allSeats().any(s => s.rounds >= roundsToWin)" steps: - phase: play turns: order: { from: "global.starter", direction: clockwise } budget: 1 actions: [mark] until: "global.roundOver" - phase: nextRound do: - removeFromGame: "allSeats().selectMany(s => pieces(s))" - set: { target: global.roundOver, value: false } - set: { target: global.starter, value: "opponent(global.starter)" } - phase: end do: - endGame: { winner: "allSeats().first(s => s.rounds >= roundsToWin)", reason: won the match }
actions: mark: choose: - at: { from: "board.cells()", where: "pieceAt(at) == null", label: Choose an empty square } effect: - create: { type: mark, props: { owner: self }, into: at } - if: cond: "wins(self)" then: - add: { target: self.rounds, value: 1 } - set: { target: global.roundOver, value: true } else: - if: cond: "boardFull()" then: [{ set: { target: global.roundOver, value: true } }]Where next
Section titled “Where next”- Let players choose how many rounds win the match, or play the misère variant where three in a row loses: Variants, options and expansions.
- Make the bots play it better: The bot heuristic.