Advertisement
Guest User

Untitled

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