-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarddeck.hoon
103 lines (103 loc) · 2.23 KB
/
carddeck.hoon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
:: Debugging mode engaged
::
!:
:: The reason I'm using a say generator, aside from the fact that that's the assignment,
:: is to get entropy from Arvo in order to get randomness.
::
:- %say
:: Let's set up the generator, pull in entropy and accept integers x and y.
:: I want x hands of y cards.
::
|= [[* eny=@uvJ *] [x=@ud y=@ud ~] ~]
:: we're marking the kind of data we're going to produce, a noun
::
:- %noun
:: let's create a trap and evaluate it.
|-
:: kethep typecasts explicitly by label, in this case, to a list of tapes
::
^- (list tape)
:: I want to set up my list, initially empty, and my deck of 52 cards
::
=/ hands=(list tape) [~]
:: my hand, initially an empty tape
::
=/ hand ""
:: and my deck of 52 cards
::
=/ deck=(list cord)
:~ 'A♥'
'K♥'
'Q♥'
'J♥'
'10♥'
'9♥'
'7♥'
'6♥'
'5♥'
'4♥'
'3♥'
'2♥'
'A♣'
'K♣'
'Q♣'
'J♣'
'10♣'
'9♣'
'7♣'
'6♣'
'5♣'
'4♣'
'3♣'
'2♣'
'A♦'
'K♦'
'Q♦'
'J♦'
'10♦'
'9♦'
'7♦'
'6♦'
'5♦'
'4♦'
'3♦'
'2♦'
'A♠'
'K♠'
'Q♠'
'J♠'
'10♠'
'9♠'
'7♠'
'6♠'
'5♠'
'4♠'
'3♠'
'2♠'
==
:: let's barhep this bad larry
::
|-
:: If my user wants zero hands, or hands of zero cards
:: or my list has x hands, which is what was originally asked for, I'm
:: going to return the list
::
?: |(=(x 0) =(y 0) =((lent hands) x))
hands
:: If I have no cards left in the deck, push the current hand into the list and return the list
::
?: =((lent deck) 0) (snoc hands hand)
:: If the current hand is y cards long, recurse, pushing the hand into the list and emptying the hand
::
?: =((lent hand) y)
$(hands (snoc hands hand), hand "")
:: otherwise, we want to take a random integer from the length of the deck
::
=/ rng ~(. og eny)
=/ val (rad:rng (lent deck))
:: let's take the card at that index in the deck
::
=/ card (snag val deck)
:: now, we recurse. We will add the card to the hand, and remove it from the deck
::
$(hand (snoc hand card), deck (oust [val 1] deck))