Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :- %say
- |= [[* eny=@uv *] [n-players=@ud n-cards=@ud ~] ~]
- :- %noun
- =<
- ^- (list (list [@tas @tas]))
- =/ deck-size=@ud 52
- =/ draw-size=@ud (mul n-players n-cards)
- ?: (gth draw-size deck-size)
- ~| "Input error: Too many players or cards per player!" !!
- =/ deck=(list [@tas @tas]) (create-deck deck-size)
- =/ draw-ids=(list @ud) (get-draw-ids [deck-size draw-size])
- (deal-cards [deck draw-ids n-players n-cards])
- |%
- ++ create-deck
- |= deck-size=@ud
- =/ deck=(list [@tas @tas]) ~
- =/ i=@ud 0
- |-
- ^- (list [@tas @tas])
- ?: =(i deck-size)
- deck
- =/ card=[@tas @tas] (create-card i)
- $(deck [card deck], i +(i))
- ++ create-card
- |= i=@ud
- ^- [@tas @tas]
- :: i/13: quotient is the suit index, remainder is the rank index.
- =/ suit=@tas (snag (div i 13) suits)
- =/ rank=@tas (snag (mod i 13) ranks)
- [rank suit]
- ++ suits
- `(list @tas)`~[%'Spd' %'Hrt' %'Dia' %'Clb']
- ++ ranks
- `(list @tas)`~[%'A' %'2' %'3' %'4' %'5' %'6' %'7' %'8' %'9' %'T' %'J' %'Q' %'K']
- ::
- :: Prepares a list of draw-ids (list @ud) determined by draw-size.
- :: draw-ids are position index of cards in the deck.
- ++ get-draw-ids
- |= [deck-size=@ud draw-size=@ud]
- :: draw-ids is initialised to ~
- =+ [i=0 rng=~(. og eny) draw-ids=*(list @ud)]
- |-
- ^- (list @ud)
- :: ~& [%get-draw-ids draw-ids] :: debug
- ?: =(i draw-size)
- (flop draw-ids)
- =^ id rng (rads:rng deck-size)
- :: after each card id obtained, decrement deck-size for next iteration
- $(i +(i), deck-size (dec deck-size), draw-ids [id draw-ids])
- ::
- :: Deal cards to a list of n-players each to have a list of n-cards.
- :: Arguments:
- :: - the deck of cards in (list [@tas @tas])
- :: - draw-ids: the list of position index of cards to be selected from deck.
- :: - n-players: number of players
- :: - n-cards: number of cards per player
- :: Returns the dealt list of list of cards, where each card
- :: is represented by [@tas @tas], eg [%K %Spd].
- ++ deal-cards
- |= [deck=(list [@tas @tas]) draw-ids=(list @ud) n-players=@ud n-cards=@ud]
- =/ i=@ud 0
- =/ dealt-cards=(list (list [@tas @tas])) ~
- |-
- ^- (list (list [@tas @tas]))
- :: ~& [%deal-cards dealt-cards] :: debug
- ?: =(i n-players)
- dealt-cards
- ::
- :: the 2nd variable deck now contains the remaining deck after
- :: one-player-cards are removed from the deck.
- =^ one-player-cards deck (deal-player [deck draw-ids n-cards])
- %= $
- i +(i)
- :: remove the 1st n-cards ids from draw-ids and iterate
- draw-ids (slag n-cards draw-ids)
- dealt-cards [one-player-cards dealt-cards]
- ==
- ::
- :: Deal the cards to one player.
- :: Arguments:
- :: - the deck of cards in (list [@tas @tas])
- :: - draw-ids: the list of position index of cards to be selected from deck.
- :: - Number of cards to deal to the player.
- :: Returns a cell:
- :: - Head contains the list of cards dealt to the player, where each card
- :: is represented by [@tas @tas], eg [%K %Spd].
- :: - Tail contains the remaining deck after dealing cards to that player.
- ++ deal-player
- |= [deck=(list [@tas @tas]) draw-ids=(list @ud) n-cards=@ud]
- =/ j=@ud 0
- =/ one-player-cards=(list [@tas @tas]) ~
- |-
- ^- [(list [@tas @tas]) (list [@tas @tas])]
- ?: =(j n-cards)
- :: ~& [%deal-p one-player-cards] :: debug
- [one-player-cards deck]
- =/ id=@ud ?~(draw-ids !! i.draw-ids)
- :: 2nd var deck now contains the remaining deck after getting the card
- =^ card deck (get-card [deck id])
- :: ~& [%deal-p deck] :: debug
- %= $
- j +(j)
- draw-ids ?~(draw-ids !! t.draw-ids)
- one-player-cards [card one-player-cards]
- ==
- ::
- :: Get card from deck at position index id.
- :: Returns a cell:
- :: - Head contains the card represented by [@tas @tas], eg [%K %Spd].
- :: - Tail contains the remaining deck after removing the card from deck.
- ++ get-card
- |= [deck=(list [@tas @tas]) id=@ud]
- ^- [[@tas @tas] (list [@tas @tas])]
- [(snag id deck) (oust [id 1] deck)]
- --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement