# Checkers (English draughts), a conformance game (issue #32): forced captures and multi-jump chains. Its move counts
# from the start position are published (perft), so move generation can be checked against them.
# Rules: men move one square diagonally forward and kings in any diagonal direction; a capture jumps an adjacent
# opposing piece to the empty square beyond; capturing is compulsory, and a capturing piece must keep jumping while it
# can, but any capture sequence may be chosen; a man reaching the far row is crowned and its move ends; a player with no
# legal move loses. Black moves first. Draws by agreement or repetition aren't modelled yet.
dsl: "0.4"

game:
  id: checkers
  name: Checkers
  players: { min: 2, max: 2 }
  seats:
    topology: ring
    names: [black, white]          # seat 0 = black, who moves first, from ranks 1-3

boards:
  board:
    kind: grid
    size: [8, 8]
    labels: { x: "abcdefgh", y: "12345678" }

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

data:
  startPosition:
    of: piece
    rows:
      - { owner: black, at: [a1, c1, e1, g1, b2, d2, f2, h2, a3, c3, e3, g3] }
      - { owner: white, at: [b6, d6, f6, h6, a7, c7, e7, g7, b8, d8, f8, h8] }

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

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

functions:
  forward:
    params: { s: seat }
    returns: int
    expr: "s == seat(black) ? 1 : -1"
  kingRow:
    params: { s: seat }
    returns: int
    expr: "s == seat(black) ? 8 : 1"
  # The directions a piece may move or jump in: forward only for a man.
  directions:
    params: { p: piece }
    returns: "list<(x: int, y: int)>"
    expr: "p.king ? diagonals : diagonals.where(d => d.y == forward(p.owner))"
  # Every capture path from a square: each a list of landing squares. A piece can't jump the same piece twice, and
  # jumped pieces stay on the board until the move ends; a man that reaches the king row stops there.
  jumps:
    params: { p: piece, at: square, taken: "list<square>" }
    returns: "list<list<square>>"
    expr: >
      directions(p).selectMany(d =>
        let over = offset(at, d.x, d.y);
        let land = over == null ? null : offset(over, d.x, d.y);
        land == null || pieceAt(over)?.owner != opponent(p.owner) || taken.contains(over) || (pieceAt(land) != null && land != p.at) ? [] :
        !p.king && land.rank == kingRow(p.owner) ? [[land]] :
        (let more = jumps(p, land, taken.concat([over])); more.isEmpty() ? [[land]] : more.select(r => [land].concat(r))))
  steps:
    params: { p: piece }
    returns: "list<list<square>>"
    expr: "directions(p).select(d => offset(p.at, d.x, d.y)).where(sq => sq != null && pieceAt(sq) == null).select(sq => [sq])"
  mustCapture:
    params: { s: seat }
    returns: bool
    expr: "pieces(s).any(p => jumps(p, p.at, []).any())"
  # A piece's legal moves as paths: captures if the side has any, otherwise single steps.
  movesOf:
    params: { p: piece }
    returns: "list<list<square>>"
    expr: "mustCapture(p.owner) ? jumps(p, p.at, []) : steps(p)"
  # The squares a path jumps over, in order.
  jumped:
    params: { from: square, path: "list<square>" }
    returns: "list<square>"
    expr: >
      range(0, path.count()).select(i => (prev: i == 0 ? from : path[i - 1], next: path[i]))
        .where(s => abs(s.next.file - s.prev.file) == 2)
        .select(s => square((s.prev.file + s.next.file) / 2, (s.prev.rank + s.next.rank) / 2))

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

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

actions:
  move:
    choose:
      - piece: { from: "pieces(self)", where: "movesOf(piece).any()" }
      - path: { from: "movesOf(piece)" }
    effect:
      - forEach: { in: "jumped(piece.at, path)", as: sq, do: [ { move: { what: "pieceAt(sq)", to: "captured[self]" } } ] }
      - move: { what: piece, to: "path.last()" }
      - if:
          cond: "!piece.king && piece.at.rank == kingRow(self)"
          then: [ { set: { target: piece.king, value: true } } ]

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