-
Notifications
You must be signed in to change notification settings - Fork 0
/
card_games.clj
63 lines (49 loc) · 1.61 KB
/
card_games.clj
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
(ns card-games)
(defn rounds
"Takes the current round number and returns
a `list` with that round and the _next two_."
[n]
(list n (+ 1 n) (+ 2 n)))
(defn concat-rounds
"Takes two lists and returns a single `list`
consisting of all the rounds in the first `list`,
followed by all the rounds in the second `list`"
[l1 l2]
(concat l1 l2))
(defn contains-round?
"Takes a list of rounds played and a round number.
Returns `true` if the round is in the list, `false` if not."
[l n]
(boolean (some #(= n %) l)))
(defn card-average
"Returns the average value of a hand"
[hand]
(float (/ (apply + hand) (count hand))))
(defn approx-average?
"Returns `true` if average is equal to either one of:
- Take the average of the _first_ and _last_ number in the hand.
- Using the median (middle card) of the hand."
[hand]
(let [ave (card-average hand)
fi_ave (-> (+ (first hand) (last hand))
(/ 2))
median (->> (quot (count hand) 2)
(nth hand))]
(or (== ave fi_ave)
(== ave median))))
(defn average-even-odd?
"Returns true if the average of the cards at even indexes
is the same as the average of the cards at odd indexes."
[hand]
(let [even-avg (-> (take-nth 2 hand)
(card-average))
odd-avg (-> (take-nth 2 (rest hand))
(card-average))]
(= even-avg odd-avg)))
(defn maybe-double-last
"If the last card is a Jack (11), doubles its value
before returning the hand."
[hand]
(if (= 11 (last hand))
(concat (drop-last hand) '(22))
hand))