daily pastebin goal
67%
SHARE
TWEET

Untitled

a guest Jul 12th, 2018 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ::
  2. ::  carddeal.hoon
  3. ::
  4. ::  this program deals cards from a randomly shuffled deck.
  5. ::
  6. ::  save as `carddeal.hoon` in the `/gen` directory of your
  7. ::  urbit's pier and run in the dojo:
  8. ::
  9. ::  +carddeal [4 5]
  10. ::
  11. ::
  12. =<
  13. ::
  14. ::  a is the number of hands to be dealt
  15. ::  b is the number of cards per hand
  16. ::
  17. |=  [a=@ b=@]
  18. ::
  19. ::  `cards` is a core with a shuffled deck as state
  20. ::
  21. =/  cards  shuffle:sorted-deck:cards
  22. |-  ^-  (list (list card))
  23. ?:  =(0 a)  ~
  24. ::
  25. ::  `draw` returns a pair of [hand cards]
  26. ::  `hand` is a list of cards, i.e. a dealt hand, and
  27. ::  `cards` is the core with a modified deck
  28. ::  (the cards of `hand` were removed from the deck)
  29. ::
  30. =^  hand  cards  (draw:cards b)
  31. [hand $(a (dec a))]
  32. ::
  33. |%
  34. ::
  35. ::  a `card` is a pair of [value suit]
  36. ::
  37. +=  card  [value suit]
  38. ::
  39. ::  a `value` is either a number or a face value
  40. ::
  41. +=  value  $?  %2  %3  %4
  42.                %5  %6  %7
  43.                %8  %9  %10
  44.                %jack
  45.                %queen
  46.                %king
  47.                %ace
  48.             ==
  49. ::
  50. ::  the card suits are: clubs, hearts, spades,
  51. ::  and diamonds
  52. ::
  53. +=  suit  $?  %clubs
  54.               %hearts
  55.               %spades
  56.               %diamonds
  57.           ==
  58. ::
  59. ::  `cards` is a door with `deck` and `rng` as state
  60. ::  `deck` is a deck of cards
  61. ::  `rng` is a Hoon stdlib core for random # generation
  62. ::  a door is a core with a sample, often multi-arm
  63. ::
  64. ++  cards
  65.   |_  [deck=(list card) rng=_~(. og 123.458)]
  66.   ::
  67.   ::  `this` is, by convention, how a door refers
  68.   ::  to itself
  69.   ::
  70.   ++  this  .
  71.   ::
  72.   ::  `draw` returns two things: (1) a list of cards (i.e. a hand),
  73.   ::  and (2) a modified core for `cards`.  the `deck` in `cards`
  74.   ::  has the dealt cards removed.
  75.   ::
  76.   ++  draw
  77.     |=  a=@
  78.     =|  hand=(list card)
  79.     |-  ^-  [(list card) _this]
  80.     ?:  =(0 a)  [hand this]
  81.     ?~  deck  !!
  82.     %=  $
  83.       hand  [i.deck hand]
  84.       deck  t.deck
  85.       a  (dec a)
  86.     ==
  87.   ::
  88.   ::  `shuffle` returns the `cards` core with a modifed `deck`.
  89.   ::  the cards in the deck have been shuffled randomly.
  90.   ::
  91.   ++  shuffle
  92.     =|  shuffled=(list card)
  93.     =/  len=@  (lent deck)
  94.     |-  ^-  _this
  95.     ?:  =(~ deck)  this(deck shuffled)
  96.     =^  val  rng  (rads:rng len)
  97.     %=  $
  98.       shuffled  [(snag val deck) shuffled]
  99.       deck  (oust [val 1] deck)
  100.       len  (dec len)
  101.     ==
  102.   ::
  103.   ::  `sorted-deck` returns the `cards` core with a modifed
  104.   ::  `deck`.  the full deck of cards is provided, unshuffled.
  105.   ::
  106.   ++  sorted-deck
  107.     %_  this
  108.       deck  =|  sorted=(list card)
  109.             =/  suits=(list suit)
  110.               ~[%spades %diamonds %clubs %hearts]
  111.             |-  ^-  (list card)
  112.             ?~  suits  sorted
  113.             %=  $
  114.               suits  t.suits
  115.               sorted
  116.                 :*  [%2 i.suits]
  117.                     [%3 i.suits]
  118.                     [%4 i.suits]
  119.                     [%5 i.suits]
  120.                     [%6 i.suits]
  121.                     [%7 i.suits]
  122.                     [%8 i.suits]
  123.                     [%9 i.suits]
  124.                     [%10 i.suits]
  125.                     [%jack i.suits]
  126.                     [%queen i.suits]
  127.                     [%king i.suits]
  128.                     [%ace i.suits]
  129.                     sorted  ==
  130.             ==
  131.     ==
  132.   --
  133. --
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top