Rebol3 Code Examplex


Deal cards for FreeCell

Distribute cards into the standard FreeCell layout.

Rebol [
    title: "Rosetta code: Deal cards for FreeCell"
    file:  %Deal_cards_for_FreeCell.r3
    url:   https://rosettacode.org/wiki/Deal_cards_for_FreeCell
]

deal-FreeCell: function/with [
    "Deal a FreeCell game by number"
    num [integer!]
][
    deck: copy ordered-deck
    state: num ; seed the MSLCG
    print ["^/Game:" as-green num]
    len: length? deck
    while [len > 0] [
        ;; pick random remaining card and swap to end
        choice: rng % len + 1
        swap (at deck choice) (at deck len)
        prin [" " take/last deck]
        -- len
        if 4 == mod len 8 [prin "^/"]
    ]
    print ""
][
    ordered-deck: []
    foreach r "A23456789TJQK" [foreach s "♣♦♥♠" [ append ordered-deck ajoin [r s] ] ]
    ;; Microsoft linear congruential generator
    state: 0
    rng: does [
        state: (state * 214013 + 2531011) & 0#7fffffff
        state >> 16
    ]
]

deal-FreeCell 1
deal-FreeCell 617
deal-FreeCell 1 + random 32000

Output:


Game: 1
  J♦  2♦  9♥  J♣  5♦  7♥  7♣  5♥
  K♦  K♣  9♠  5♠  A♦  Q♣  K♥  3♥
  2♠  K♠  9♦  Q♦  J♠  A♠  A♥  3♣
  4♣  5♣  T♠  Q♥  4♥  A♣  4♦  7♠
  3♠  T♦  4♠  T♥  8♥  2♣  J♥  7♦
  6♦  8♠  8♦  Q♠  6♣  3♦  8♣  T♣
  6♠  9♣  2♥  6♥

Game: 617
  7♦  A♦  5♣  3♠  5♠  8♣  2♦  A♥
  T♦  7♠  Q♦  A♣  6♦  8♥  A♠  K♥
  T♥  Q♣  3♥  9♦  6♠  8♦  3♦  T♣
  K♦  5♥  9♠  3♣  8♠  7♥  4♦  J♠
  4♣  Q♠  9♣  9♥  7♣  6♥  2♣  2♠
  4♠  T♠  2♥  5♦  J♣  6♣  J♥  Q♥
  J♦  K♠  K♣  4♥

Game: 9519
  7♠  3♦  5♣  3♠  K♥  8♥  T♣  K♦
  J♥  4♠  8♣  Q♦  4♣  J♦  Q♠  5♥
  7♦  8♠  6♥  9♣  5♦  K♣  2♠  K♠
  7♣  8♦  Q♥  7♥  3♣  6♣  3♥  2♣
  9♥  4♦  A♦  4♥  T♥  6♠  A♥  J♠
  A♣  2♦  T♦  9♠  T♠  2♥  6♦  5♠
  Q♣  9♦  J♣  A♠