# Tablut, the Sámi form of hnefatafl ("Viking chess"), a conformance game written by hand from Linnaeus's 1732 account
# (Lachesis Lapponica, 1811; the source text is with AI Ingestion's test data). It is the reference the showcase AI
# import (D-236) is compared against. Linnaeus leaves much open, so these rules follow the commonly played
# reconstruction; each choice the source doesn't settle is marked "reading:".
# Rules: the Swedes (the king and eight men) start in the centre, the sixteen Muscovites in four groups at the edges.
# Every piece moves any distance along a rank or file without jumping. A man between two enemies on a line, by the
# enemy's move, is captured. The king escapes, and the Swedes win, by reaching any edge square. The king is captured,
# and the Muscovites win, when surrounded on all four sides, or on the three other sides when he stands beside the
# throne. A side with no legal move loses.
dsl: "1.0"

game:
  id: tablut
  name: Tablut
  players: { min: 2, max: 2 }
  seats:
    topology: ring
    names: [swedes, muscovites]   # reading: the Swedes move first; Linnaeus only says the sides take turns

boards:
  board:
    kind: grid
    size: [9, 9]                  # reading: the source's figure isn't in the text; 9x9 is the board the figure shows
    labels: { x: "abcdefghi", y: "123456789" }

components:
  piece:
    props:
      owner: seat
      king: "bool = false"

data:
  startPosition:
    of: piece
    rows:
      - { owner: swedes, king: true, at: [e5] }
      - { owner: swedes, at: [e3, e4, e6, e7, c5, d5, f5, g5] }
      - { owner: muscovites, at: [a4, a5, a6, b5, i4, i5, i6, h5, d1, e1, f1, e2, d9, e9, f9, e8] }

zones:
  square:   { kind: cell, board: board, of: piece, capacity: 1, visibility: public }
  captured: { kind: set, of: piece, perSeat: true, visibility: public }

consts:
  lines: "[(x: 1, y: 0), (x: -1, y: 0), (x: 0, y: 1), (x: 0, y: -1)]"

functions:
  # The throne (the king's castle, konakis): no other piece may enter it. Reading: nor pass through it.
  throne:
    params: {}
    returns: square
    expr: "square(5, 5)"
  onEdge:
    params: { sq: square }
    returns: bool
    expr: "sq.file == 1 || sq.file == 9 || sq.rank == 1 || sq.rank == 9"
  # The empty squares a piece can reach along one line, stopping at the first piece, the edge, or (for a man) the throne.
  ray:
    params: { p: piece, at: square, d: "(x: int, y: int)" }
    returns: "list<square>"
    expr: >
      let next = offset(at, d.x, d.y);
      next == null || pieceAt(next) != null || (next == throne() && !p.king) ? [] : [next].concat(ray(p, next, d))
  reach:
    params: { p: piece }
    returns: "list<square>"
    expr: "lines.selectMany(d => ray(p, p.at, d))"
  # The men a move to a square captures: an enemy man on a line from it with one of the mover's pieces beyond him.
  # Reading: the king takes part in captures; the empty throne doesn't.
  captures:
    params: { s: seat, to: square }
    returns: "list<square>"
    expr: >
      lines.select(d => (near: offset(to, d.x, d.y), far: offset(to, d.x * 2, d.y * 2)))
        .where(l => l.near != null && l.far != null && pieceAt(l.near)?.owner == opponent(s) && !pieceAt(l.near).king && pieceAt(l.far)?.owner == s)
        .select(l => l.near)
  theKing:
    params: {}
    returns: piece
    expr: "pieces(seat(swedes)).where(p => p.king).first()"
  # Whether a Muscovite arriving on `to` takes the king: he must arrive beside him, and every square beside the king must
  # hold a Muscovite or be the throne, so all four, or the three that aren't the throne when he stands beside it
  # (Linnaeus's laws 10 and 11; reading: away from the throne, all four as well). An edge is never a side that closes
  # him in. As with men, the capture is the closing move's (D-241): a king who steps into an enclosure isn't taken until
  # a Muscovite arrives beside him.
  kingTaken:
    params: { to: square }
    returns: bool
    expr: >
      let around = lines.select(d => offset(theKing().at, d.x, d.y));
      around.any(sq => sq == to) && around.all(sq => sq != null && (sq == throne() || pieceAt(sq)?.owner == seat(muscovites)))

setup:
  - createFrom: { table: startPosition, into: square, positionColumn: at }

flow:
  - phase: play
    turns:
      order: { from: "seat(swedes)", direction: clockwise }
      budget: 1
      actions: [move]
      onNoLegalAction:
        - endGame: { winner: "opponent(activeSeat)", reason: no legal move }

actions:
  move:
    choose:
      - piece: { from: "pieces(self)", where: "reach(piece).any()" }
      - to: { from: "reach(piece)" }
    effect:
      - forEach: { in: "captures(self, to)", as: sq, do: [ { move: { what: "pieceAt(sq)", to: "captured[self]" } } ] }
      - move: { what: piece, to: to }
      - if:
          cond: "piece.king && onEdge(piece.at)"
          then: [ { endGame: { winner: self, reason: the king escaped } } ]
      - if:
          cond: "self == seat(muscovites) && kingTaken(to)"
          then: [ { endGame: { winner: self, reason: the king was taken } } ]

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