My work for Hoon 101. Will remain private until the class is over.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

259 lines
5.1KB

  1. :: Hoon 201 - Week 1
  2. :: ~bannum-magtus || s@p7.co.nz
  3. ::
  4. :: im very sorry i messed up all my faces and i also dont know
  5. :: how to make gates that make gates so this is messy code :(
  6. :: dont leave your assignments until the last minute kids.
  7. ::
  8. /+ playing-cards
  9. :- %say
  10. |= [[* eny=@uv *] *]
  11. :- %noun
  12. =<
  13. %- rank-hands
  14. %- score-hands
  15. %- sort-hands
  16. %- draw-hands
  17. [%pregame 4]
  18. !:
  19. |%
  20. +$ title
  21. $% %royal-flush
  22. %straight-flush
  23. %four-of-a-kind
  24. %full-house
  25. %flush
  26. %straight
  27. %three-of-a-kind
  28. %two-pair
  29. %pair
  30. %high-card
  31. ==
  32. +$ card darc:playing-cards
  33. +$ suit suit:playing-cards
  34. +$ deck deck:playing-cards
  35. +$ grouped (list (list card))
  36. +$ unsorted (list card)
  37. +$ sorted (list card)
  38. +$ hands [g=grouped s=sorted u=unsorted]
  39. +$ tiebreaker (list [v=@ s=@])
  40. +$ draw-phase-state [h=(list unsorted) d=deck]
  41. +$ sort-phase-state [h=(list hands) d=deck]
  42. +$ score-phase-state [s=(list [r=@ t=title h=hands b=tiebreaker]) d=deck]
  43. +$ rank-phase-state (list [r=@ t=title h=unsorted])
  44. ++ rank-hands
  45. |= [%score st=score-phase-state]
  46. ^- [%ranking rank-phase-state]
  47. =. s.st (sort s.st rank-sort)
  48. [%ranking (flop (rank-display s.st))]
  49. ++ rank-display
  50. |= i=(list [* t=title h=hands *])
  51. ^- (list [r=@ t=title h=unsorted])
  52. =/ c=@ 1
  53. =| l=(list [r=@ t=title h=unsorted])
  54. |-
  55. ?~ i l
  56. =/ s [c t.i.i u.h.i.i]
  57. %= $
  58. c .+(c)
  59. l :-(s l)
  60. i t.i
  61. ==
  62. ++ rank-sort
  63. |= [a=[r=@ * * b=tiebreaker] b=[r=@ * * b=tiebreaker]]
  64. ^- ?
  65. ?. =(r.a r.b)
  66. (gth r.a r.b)
  67. (tb-comp b.a b.b)
  68. ++ tb-comp
  69. |= [a=tiebreaker b=tiebreaker]
  70. ^- ?
  71. ?. =((lent a) (lent b))
  72. !!
  73. |-
  74. ?~ a %.y
  75. ?~ b %.n
  76. ?. =(v.i.a v.i.b)
  77. (gth v.i.a v.i.b)
  78. $(a t.a, b t.b)
  79. ++ draw-hands
  80. |= [%pregame n=@]
  81. =/ d=deck init-deck
  82. =| h=(list unsorted)
  83. ^- [%draw draw-phase-state]
  84. |-
  85. ?~ n [%draw [h d]]
  86. =/ i (draw:playing-cards 5 d)
  87. $(h :-(hand.i h), d rest.i, n (dec n))
  88. ++ sort-hands
  89. |= [%draw st=draw-phase-state]
  90. ^- [%sort sort-phase-state]
  91. [%sort [(turn h.st zip-sorted) d.st]]
  92. ++ card-sort
  93. |= [a=card b=card]
  94. ^- ?
  95. (gth val.a val.b)
  96. ++ sort-hand
  97. |= u=unsorted
  98. ^- sorted
  99. (sort u card-sort)
  100. ++ tuples
  101. |= h=sorted
  102. ^- grouped
  103. =| c=(list card)
  104. =| l=(list (list card))
  105. |-
  106. ?~ h :-(c l)
  107. ?~ c
  108. %= $
  109. c [i.h ~]
  110. h t.h
  111. ==
  112. ?: =(val.i.c val.i.h)
  113. %= $
  114. c :-(i.h c)
  115. h t.h
  116. ==
  117. %= $
  118. c ~
  119. l :-(c l)
  120. ==
  121. ++ zip-sorted
  122. |= u=unsorted
  123. ^- hands
  124. =/ sh (sort-hand u)
  125. =/ gh (tuples sh)
  126. [gh sh u]
  127. ++ score-hands
  128. |= [%sort st=sort-phase-state]
  129. ^- [%score score-phase-state]
  130. [%score (turn h.st con-tb) d.st]
  131. ++ init-deck
  132. (shuffle-deck:playing-cards make-deck:playing-cards eny)
  133. ++ suit-to-num
  134. |= c=darc:playing-cards
  135. ^- @
  136. ?- sut.c
  137. %spades 4
  138. %hearts 3
  139. %diamonds 2
  140. %clubs 1
  141. ==
  142. ++ get-title
  143. |= h=hands
  144. ^- [t=title r=@]
  145. ?: (is-royal-flush h) [%royal-flush 9]
  146. ?: (is-straight-flush h) [%straight-flush 8]
  147. ?: (is-four-of-a-kind h) [%four-of-a-kind 7]
  148. ?: (is-full-house h) [%full-house 6]
  149. ?: (is-flush h) [%flush 5]
  150. ?: (is-straight h) [%straight 4]
  151. ?: (is-three-of-a-kind h) [%three-of-a-kind 3]
  152. ?: (is-two-pair h) [%two-pair 2]
  153. ?: (is-pair h) [%pair 1]
  154. [%high-card 0]
  155. ++ is-flush
  156. |= [* h=sorted *]
  157. ^- ?
  158. =| s=?(~ suit)
  159. |-
  160. ?~ h %.y
  161. ?~ s
  162. $(h t.h, s sut.i.h)
  163. ?. =(s sut.i.h)
  164. %.n
  165. $(h t.h)
  166. ++ is-straight
  167. |= [* h=sorted *]
  168. ^- ?
  169. =| l=?(~ @)
  170. |-
  171. ?~ h %.y
  172. ?~ l
  173. $(h t.h, l val.i.h)
  174. ?. =(.+(l) val.i.h)
  175. %.n
  176. $(l val.i.h, h t.h)
  177. ++ high-card
  178. |= [* h=sorted *]
  179. ^- card
  180. ?~ h !!
  181. i.h
  182. ++ is-royal-flush
  183. |= i=[* h=sorted *]
  184. ^- ?
  185. ?& (is-straight-flush i)
  186. =(13 val:(high-card i))
  187. ==
  188. ++ is-straight-flush
  189. |= i=[* h=sorted *]
  190. ^- ?
  191. ?& (is-straight i)
  192. (is-flush i)
  193. ==
  194. ++ one-of
  195. |= [n=@ [g=grouped * *]]
  196. ^- @
  197. =| t=@
  198. |-
  199. ?~ g t
  200. ?: =(n (lent i.g))
  201. $(g t.g, t .+(t))
  202. $(g t.g)
  203. ++ is-four-of-a-kind
  204. |= i=[g=grouped * *]
  205. ^- ?
  206. =(1 (one-of 4 i))
  207. ++ is-full-house
  208. |= i=[g=grouped * *]
  209. ^- ?
  210. ?& =(1 (one-of 3 i))
  211. =(1 (one-of 2 i))
  212. ==
  213. ++ is-three-of-a-kind
  214. |= i=[g=grouped * *]
  215. ^- ?
  216. =(1 (one-of 3 i))
  217. ++ is-two-pair
  218. |= i=[g=grouped * *]
  219. ^- ?
  220. =(2 (one-of 2 i))
  221. ++ is-pair
  222. |= i=[g=grouped * *]
  223. ^- ?
  224. =(1 (one-of 2 i))
  225. ++ con-tb
  226. |= fh=hands
  227. =/ o=[t=title r=@] (get-title fh)
  228. [r.o t.o fh (get-tiebreaker fh)]
  229. ++ get-tiebreaker
  230. |= [g=grouped * *]
  231. ^- tiebreaker
  232. =. g (sort g tuple-sort)
  233. (turn g tuple-tb)
  234. ++ tuple-tb
  235. |= l=(list card)
  236. ^- [v=@ s=@]
  237. =| sv=@
  238. =| vv=@
  239. |-
  240. ?~ l [vv sv]
  241. =. vv val.i.l
  242. ?. (gth (suit-to-num i.l) sv)
  243. $(l t.l)
  244. $(l t.l, sv (suit-to-num i.l))
  245. ++ tuple-sort
  246. |= [a=(list card) b=(list card)]
  247. ^- ?
  248. =/ la (lent a)
  249. =/ lb (lent b)
  250. ?. =(la lb)
  251. (gth la lb)
  252. ?~ a !!
  253. ?~ b !!
  254. ?: =(val.i.a val.i.b)
  255. (gth (suit-to-num i.a) (suit-to-num i.b))
  256. (gth val.i.a val.i.b)
  257. --