-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCards.hs
More file actions
75 lines (58 loc) · 1.9 KB
/
Cards.hs
File metadata and controls
75 lines (58 loc) · 1.9 KB
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
-- Definitionen der Karten --
module Cards where
data Color = Diamonds | Hearts | Spades | Clubs
deriving (Eq, Ord, Enum, Bounded)
data Value = Two | Three | Four | Five | Six | Seven | Eight | Nine
| Ten | Jack | Queen | King | Ace
deriving (Ord, Eq, Enum, Bounded)
data Card = Card (Color, Value)
deriving (Show, Bounded)
instance Show Color where
show Diamonds = " \ESC[47;38;5;160m♦\ESC[0m "
show Hearts = " \ESC[47;38;5;160m♥\ESC[0m "
show Spades = " \ESC[47;38;5;16m♠\ESC[0m "
show Clubs = " \ESC[47;38;5;16m♣\ESC[0m "
{--Karten werden nicht fancy angezeigt
instance Show Color where
show Diamonds = "♦"
show Hearts = "♥"
show Spades = "♠"
show Clubs = "♣"
--}
instance Eq Card where
(Card (c1,v1)) == (Card (c2,v2)) = v1 == v2
instance Ord Card where
compare (Card (c1,v1)) (Card (c2,v2)) = compare v1 v2
-- Quelle: http://stackoverflow.com/questions/5684049/is-there-some-way-to-define-an-enum-in-haskell-that-wraps-around
-- | a `pred` that wraps
-- damit Ace auch der Vorgaenger von Two ist
predB :: (Bounded a, Enum a, Eq a) => a -> a
predB en | en == minBound = maxBound
| otherwise = pred en
instance Show Value where
show Two = "2"
show Three = "3"
show Four = "4"
show Five = "5"
show Six = "6"
show Seven = "7"
show Eight = "8"
show Nine = "9"
show Ten = "10"
show Jack = "J"
show Queen = "Q"
show King = "K"
show Ace = "A"
--Liste mit allen Karten-Werten (Zwei bis Ass)
values :: [Value]
values = [minBound .. maxBound]
-- Liste mit allen 4 Farben
colors :: [Color]
colors = [minBound .. maxBound]
-- Liste mit allen Karten eines Decks (sortiert)
cards :: [Card]
cards = [Card (c,v) | c <- colors, v <- values]
getColor :: Card -> Color
getColor (Card (c,v)) = c
getValue :: Card -> Value
getValue (Card (c,v)) = v