Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. :- %say
  2. |= [[* eny=@uv *] [n-players=@ud n-cards=@ud ~] ~]
  3. :- %noun
  4. =<
  5. ^- (list (list [@tas @tas]))
  6. =/ deck-size=@ud 52
  7. =/ draw-size=@ud (mul n-players n-cards)
  8. ?: (gth draw-size deck-size)
  9. ~| "Input error: Too many players or cards per player!" !!
  10. =/ deck=(list [@tas @tas]) (create-deck deck-size)
  11. =/ draw-ids=(list @ud) (get-draw-ids [deck-size draw-size])
  12. (deal-cards [deck draw-ids n-players n-cards])
  13. |%
  14. ++ create-deck
  15. |= deck-size=@ud
  16. =/ deck=(list [@tas @tas]) ~
  17. =/ i=@ud 0
  18. |-
  19. ^- (list [@tas @tas])
  20. ?: =(i deck-size)
  21. deck
  22. =/ card=[@tas @tas] (create-card i)
  23. $(deck [card deck], i +(i))
  24. ++ create-card
  25. |= i=@ud
  26. ^- [@tas @tas]
  27. :: i/13: quotient is the suit index, remainder is the rank index.
  28. =/ suit=@tas (snag (div i 13) suits)
  29. =/ rank=@tas (snag (mod i 13) ranks)
  30. [rank suit]
  31. ++ suits
  32. `(list @tas)`~[%'Spd' %'Hrt' %'Dia' %'Clb']
  33. ++ ranks
  34. `(list @tas)`~[%'A' %'2' %'3' %'4' %'5' %'6' %'7' %'8' %'9' %'T' %'J' %'Q' %'K']
  35. ::
  36. :: Prepares a list of draw-ids (list @ud) determined by draw-size.
  37. :: draw-ids are position index of cards in the deck.
  38. ++ get-draw-ids
  39. |= [deck-size=@ud draw-size=@ud]
  40. :: draw-ids is initialised to ~
  41. =+ [i=0 rng=~(. og eny) draw-ids=*(list @ud)]
  42. |-
  43. ^- (list @ud)
  44. :: ~& [%get-draw-ids draw-ids] :: debug
  45. ?: =(i draw-size)
  46. (flop draw-ids)
  47. =^ id rng (rads:rng deck-size)
  48. :: after each card id obtained, decrement deck-size for next iteration
  49. $(i +(i), deck-size (dec deck-size), draw-ids [id draw-ids])
  50. ::
  51. :: Deal cards to a list of n-players each to have a list of n-cards.
  52. :: Arguments:
  53. :: - the deck of cards in (list [@tas @tas])
  54. :: - draw-ids: the list of position index of cards to be selected from deck.
  55. :: - n-players: number of players
  56. :: - n-cards: number of cards per player
  57. :: Returns the dealt list of list of cards, where each card
  58. :: is represented by [@tas @tas], eg [%K %Spd].
  59. ++ deal-cards
  60. |= [deck=(list [@tas @tas]) draw-ids=(list @ud) n-players=@ud n-cards=@ud]
  61. =/ i=@ud 0
  62. =/ dealt-cards=(list (list [@tas @tas])) ~
  63. |-
  64. ^- (list (list [@tas @tas]))
  65. :: ~& [%deal-cards dealt-cards] :: debug
  66. ?: =(i n-players)
  67. dealt-cards
  68. ::
  69. :: the 2nd variable deck now contains the remaining deck after
  70. :: one-player-cards are removed from the deck.
  71. =^ one-player-cards deck (deal-player [deck draw-ids n-cards])
  72. %= $
  73. i +(i)
  74. :: remove the 1st n-cards ids from draw-ids and iterate
  75. draw-ids (slag n-cards draw-ids)
  76. dealt-cards [one-player-cards dealt-cards]
  77. ==
  78. ::
  79. :: Deal the cards to one player.
  80. :: Arguments:
  81. :: - the deck of cards in (list [@tas @tas])
  82. :: - draw-ids: the list of position index of cards to be selected from deck.
  83. :: - Number of cards to deal to the player.
  84. :: Returns a cell:
  85. :: - Head contains the list of cards dealt to the player, where each card
  86. :: is represented by [@tas @tas], eg [%K %Spd].
  87. :: - Tail contains the remaining deck after dealing cards to that player.
  88. ++ deal-player
  89. |= [deck=(list [@tas @tas]) draw-ids=(list @ud) n-cards=@ud]
  90. =/ j=@ud 0
  91. =/ one-player-cards=(list [@tas @tas]) ~
  92. |-
  93. ^- [(list [@tas @tas]) (list [@tas @tas])]
  94. ?: =(j n-cards)
  95. :: ~& [%deal-p one-player-cards] :: debug
  96. [one-player-cards deck]
  97. =/ id=@ud ?~(draw-ids !! i.draw-ids)
  98. :: 2nd var deck now contains the remaining deck after getting the card
  99. =^ card deck (get-card [deck id])
  100. :: ~& [%deal-p deck] :: debug
  101. %= $
  102. j +(j)
  103. draw-ids ?~(draw-ids !! t.draw-ids)
  104. one-player-cards [card one-player-cards]
  105. ==
  106. ::
  107. :: Get card from deck at position index id.
  108. :: Returns a cell:
  109. :: - Head contains the card represented by [@tas @tas], eg [%K %Spd].
  110. :: - Tail contains the remaining deck after removing the card from deck.
  111. ++ get-card
  112. |= [deck=(list [@tas @tas]) id=@ud]
  113. ^- [[@tas @tas] (list [@tas @tas])]
  114. [(snag id deck) (oust [id 1] deck)]
  115. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement