-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetAssets.hs
More file actions
56 lines (45 loc) · 1.91 KB
/
SetAssets.hs
File metadata and controls
56 lines (45 loc) · 1.91 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
module SetAssets
( Card
, Cards
, Game(..)
, newDeck
, isSet
, anySets
, sets
, cardnum
) where
import Data.List
data Color = Red | Purple | Green deriving (Eq, Ord, Show, Read, Bounded, Enum)
data Shape = Squiggle | Diamond | Circle deriving (Eq, Ord, Show, Read, Bounded, Enum)
data Number = One | Two | Three deriving (Eq, Ord, Show, Read, Bounded, Enum)
data Shade = Fill | Hatch | Empty deriving (Eq, Ord, Show, Read, Bounded, Enum)
data Card = Card Number Shade Color Shape deriving (Eq, Ord, Show, Read)
type Cards = ([Card], [Card])
data Game = Game {
players :: [(String, Int)],
deck :: Cards,
started :: Bool
} deriving Show
newDeck :: [Card]
newDeck = [ Card number shade color shape | shade <- [Fill .. Empty]
,shape <- [Squiggle .. Circle]
,color <- [Red .. Green]
,number <- [One .. Three]]
isSet :: [Card] -> Bool
isSet [] = False
isSet cards = (sameOrDiff numbers) && (sameOrDiff shades) && (sameOrDiff colors) && (sameOrDiff shapes)
where numbers = map (\(Card num _ _ _ ) -> num) cards
shades = map (\(Card _ shade _ _ ) -> shade) cards
colors = map (\(Card _ _ color _ ) -> color) cards
shapes = map (\(Card _ _ _ shape ) -> shape) cards
sameOrDiff lst = or [(length $ nub lst) == 1, (length $ nub lst) == (length lst) ]
sets :: [Card] -> [[Card]]
sets cards = filter isSet $ combinations 3 cards
anySets :: [Card] -> Bool
anySets cards = (length $ sets cards) > 0
cardnum :: Card -> Int
cardnum c = let Just x = findIndex (\dc -> dc==c) newDeck in x+1
--interal helper functions, not exported.
combinations :: Int -> [a] -> [[a]]
combinations 0 _ = [ [] ]
combinations n xs = [ y:ys | y:xs' <- tails xs, ys <- combinations (n-1) xs']