# Nine Men's Morris, a conformance game (issue #32): a point-and-line board (a graph, not a grid); phases (place, then
# move, then fly with three men); and a mill that triggers a follow-up choice to remove an opposing man.
# Rules: each player places nine men, one per turn, on empty points; then moves a man along a line to an adjacent empty
# point; a player down to three men may move one to any empty point. Three of a player's men on one line form a mill,
# and the player then removes one opposing man that isn't in a mill (any man, if all are). A player with fewer than
# three men after placing, or with no legal move, loses. Draws by repetition aren't modelled.
dsl: "0.4"

game:
  id: nine-mens-morris
  name: Nine Men's Morris
  players: { min: 2, max: 2 }
  seats:
    topology: ring
    names: [white, black]          # seat 0 = white, who moves first

boards:
  board:
    kind: graph
    # The 24 points, named like a 7 by 7 grid, and the lines between them.
    nodes: [a1, d1, g1, b2, d2, f2, c3, d3, e3, a4, b4, c4, e4, f4, g4, c5, d5, e5, b6, d6, f6, a7, d7, g7]
    edges:
      - [a1, d1]
      - [d1, g1]
      - [b2, d2]
      - [d2, f2]
      - [c3, d3]
      - [d3, e3]
      - [a4, b4]
      - [b4, c4]
      - [e4, f4]
      - [f4, g4]
      - [c5, d5]
      - [d5, e5]
      - [b6, d6]
      - [d6, f6]
      - [a7, d7]
      - [d7, g7]
      - [a1, a4]
      - [a4, a7]
      - [b2, b4]
      - [b4, b6]
      - [c3, c4]
      - [c4, c5]
      - [d1, d2]
      - [d2, d3]
      - [d5, d6]
      - [d6, d7]
      - [e3, e4]
      - [e4, e5]
      - [f2, f4]
      - [f4, f6]
      - [g1, g4]
      - [g4, g7]

components:
  man:
    props:
      owner: seat

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

state:
  global: {}
  seat:
    inHand: "int = 9"

consts:
  mills: >
    [ ['a1', 'd1', 'g1'], ['b2', 'd2', 'f2'], ['c3', 'd3', 'e3'], ['a4', 'b4', 'c4'], ['e4', 'f4', 'g4'], ['c5', 'd5', 'e5'],
      ['b6', 'd6', 'f6'], ['a7', 'd7', 'g7'], ['a1', 'a4', 'a7'], ['b2', 'b4', 'b6'], ['c3', 'c4', 'c5'], ['d1', 'd2', 'd3'],
      ['d5', 'd6', 'd7'], ['e3', 'e4', 'e5'], ['f2', 'f4', 'f6'], ['g1', 'g4', 'g7'] ]

functions:
  # The points a seat's men stand on, by name, after moving one from a point (or placing one, from null) to another.
  heldAfter:
    params: { s: seat, from: "node?", to: node }
    returns: "list<string>"
    expr: "pieces(s).select(p => p.at.id).where(id => from == null || id != from.id).concat([to.id])"
  inMill:
    params: { held: "list<string>", id: string }
    returns: bool
    expr: "mills.any(m => m.contains(id) && m.all(x => held.contains(x)))"
  # Whether moving a man from one point (or placing one) onto another forms a mill through that point.
  formsMill:
    params: { s: seat, from: "node?", to: node }
    returns: bool
    expr: "inMill(heldAfter(s, from, to), to.id)"
  # The opposing men that may be removed: those not in a mill, or any if all are.
  removable:
    params: { s: seat }
    returns: "list<man>"
    expr: >
      let theirs = pieces(opponent(s));
      let held = theirs.select(p => p.at.id);
      let free = theirs.where(p => !inMill(held, p.at.id));
      free.isEmpty() ? theirs : free
  emptyPoints:
    params: {}
    returns: "list<node>"
    expr: "board.cells().where(n => pieceAt(n) == null)"
  # Where a man may go: an adjacent empty point, or any empty point once its owner is down to three men.
  destinations:
    params: { p: man }
    returns: "list<node>"
    expr: "pieces(p.owner).count() == 3 ? emptyPoints() : board.adjacent(p.at).where(n => pieceAt(n) == null)"

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

actions:
  place:
    require: "self.inHand > 0"
    choose:
      - to: { from: "emptyPoints()" }
      - remove: { from: "removable(self)", when: "formsMill(self, null, to)" }
    effect:
      - create: { type: man, props: { owner: self }, into: to }
      - set: { target: self.inHand, value: "self.inHand - 1" }
      - if:
          cond: "remove != null"
          then: [ { removeFromGame: remove } ]
  move:
    require: "self.inHand == 0"
    choose:
      - man: { from: "pieces(self)", where: "destinations(man).any()" }
      - to: { from: "destinations(man)" }
      - remove: { from: "removable(self)", when: "formsMill(self, man.at, to)" }
    effect:
      - move: { what: man, to: to }
      - if:
          cond: "remove != null"
          then: [ { removeFromGame: remove } ]

triggers:
  # A player who has placed all nine men and is down to two loses.
  - event: afterAction
    when: "allSeats().any(s => s.inHand == 0 && pieces(s).count() < 3)"
    do:
      - endGame: { winner: "allSeats().first(s => !(s.inHand == 0 && pieces(s).count() < 3))", reason: fewer than three men }

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