# Hearts (four players), a conformance game (issue #33) and the first with hidden information: hands only their owner
# sees, a hidden shuffled deck, and a three-card pass made by everyone at once.
# Rules: each hand, all 52 cards are dealt, 13 each. Before play each player passes three cards: to the left, then the
# right, then across, then no pass, repeating. The holder of the two of clubs leads it to the first trick; players must
# follow the led suit if they can, and the highest card of the led suit takes the trick and leads the next. Hearts can't
# be led until a heart has been played, unless the leader holds only hearts. Each heart taken scores a point and the
# queen of spades thirteen; a player who takes all 26 points "shoots the moon" and each other player scores 26 instead.
# The game ends when a player reaches 100 points, and the lowest score wins. Not modelled: the rule against points on
# the first trick.
dsl: "0.4"

game:
  id: hearts
  name: Hearts
  players: { min: 4, max: 4 }
  seats:
    topology: ring
    names: [north, east, south, west]

enums:
  suit: [clubs, diamonds, spades, hearts]

components:
  card:
    props:
      suit: suit
      rank: "int = 2"                # 2 to 14, the ace high
      playedBy: "seat? = null"

zones:
  deck:  { kind: stack, of: card, visibility: hidden }
  hand:  { kind: set, of: card, perSeat: true, visibility: owner }
  trick: { kind: list, of: card, visibility: public }
  taken: { kind: set, of: card, perSeat: true, visibility: public }

state:
  global:
    hand: "int = 0"                  # hands played so far, which sets the passing direction
    tricks: "int = 0"                # tricks played this hand
    leader: "seat? = null"
    heartsBroken: "bool = false"
  seat:
    score: "int = 0"

functions:
  passing:
    params: {}
    returns: bool
    expr: "global.hand % 4 != 3"
  passTarget:
    params: { s: seat }
    returns: seat
    expr: "global.hand % 4 == 0 ? leftOf(s) : global.hand % 4 == 1 ? rightOf(s) : leftOf(leftOf(s))"
  isTwoOfClubs:
    params: { c: card }
    returns: bool
    expr: "c.suit == suit.clubs && c.rank == 2"
  ledSuit:
    params: {}
    returns: "suit?"
    expr: "trick.isEmpty() ? null : trick.first().suit"
  # Whether a seat may play a card now: the two of clubs to open the hand; no heart lead before hearts are broken
  # (unless the hand is all hearts); otherwise follow the led suit if possible.
  canPlay:
    params: { c: card, s: seat }
    returns: bool
    expr: >
      trick.isEmpty()
        ? (global.tricks == 0 ? isTwoOfClubs(c) : c.suit != suit.hearts || global.heartsBroken || hand[s].all(x => x.suit == suit.hearts))
        : c.suit == ledSuit() || !hand[s].any(x => x.suit == ledSuit())
  trickWinner:
    params: {}
    returns: seat
    expr: >
      let best = trick.where(c => c.suit == ledSuit()).max(c => c.rank);
      trick.first(c => c.suit == ledSuit() && c.rank == best).playedBy
  points:
    params: { c: card }
    returns: int
    expr: "c.suit == suit.hearts ? 1 : c.suit == suit.spades && c.rank == 12 ? 13 : 0"
  handPoints:
    params: { s: seat }
    returns: int
    expr: "taken[s].sum(c => points(c))"

setup:
  - forEach:
      in: "[suit.clubs, suit.diamonds, suit.spades, suit.hearts]"
      as: s
      do:
        - forEach: { in: "range(2, 15)", as: r, do: [ { create: { type: card, props: { suit: s, rank: r }, into: deck } } ] }

flow:
  - loop:
      until: "allSeats().any(s => s.score >= 100)"
      steps:
        - phase: deal
          do:
            - forEach:
                in: "allSeats()"
                as: s
                do:
                  - moveAll: { from: "taken[s]", to: deck }
            - shuffle: deck
            - forEach: { in: "allSeats()", as: s, do: [ { deal: { from: deck, to: "hand[s]", count: 13 } } ] }
            - set: { target: global.tricks, value: 0 }
            - set: { target: global.heartsBroken, value: false }
        - phase: pass
          simultaneous: { seats: "passing() ? allSeats() : []", action: passCards, mode: snapshot, reveal: never }
        - phase: findLeader
          do:
            - set: { target: global.leader, value: "allSeats().first(s => hand[s].any(c => isTwoOfClubs(c)))" }
        - loop:
            until: "global.tricks == 13"
            steps:
              - phase: trick
                turns:
                  order: { from: "global.leader", direction: clockwise }
                  budget: 1
                  actions: [play]
                  until: "trick.count() == 4"
              - phase: takeTrick
                do:
                  - let: { name: winner, value: "trickWinner()" }
                  - moveAll: { from: trick, to: "taken[winner]" }
                  - set: { target: global.leader, value: winner }
                  - set: { target: global.tricks, value: "global.tricks + 1" }
        - phase: score
          do:
            - let: { name: moon, value: "allSeats().firstOrNull(s => handPoints(s) == 26)" }
            - forEach:
                in: "allSeats()"
                as: s
                do:
                  - set: { target: s.score, value: "s.score + (moon == null ? handPoints(s) : s == moon ? 0 : 26)" }
            - set: { target: global.hand, value: "global.hand + 1" }
  - phase: end
    do:
      - endGame: { winner: "allSeats().orderBy(s => s.score).first()", reason: lowest score }

actions:
  passCards:
    choose:
      - cards: { from: "hand[self]", count: 3 }
    effect:
      - move: { what: cards, to: "hand[passTarget(self)]" }
  play:
    choose:
      - card: { from: "hand[self]", where: "canPlay(card, self)" }
    effect:
      - set: { target: card.playedBy, value: self }
      - move: { what: card, to: trick }
      - if:
          cond: "card.suit == suit.hearts"
          then: [ { set: { target: global.heartsBroken, value: true } } ]

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