-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbonus2 (2).hs
100 lines (84 loc) · 2.99 KB
/
bonus2 (2).hs
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
{-
Sırrı Kırımlıoğlu
-}
data Color = Red
| Black
deriving(Eq,Show)
data Suit = Clubs
| Diamonds
| Hearts
| Spades
deriving(Eq,Show)
data Rank = Two -- Num Int doesnt work as card1 = Card Clubs 4 so i changed Num Int with numbers writings
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Jack
| Queen
| King
| Ace
deriving(Eq,Show)
data Card = Card { suit :: Suit, rank :: Rank }
deriving(Eq,Show)
data Move = Draw
| Discard Card
deriving(Eq,Show)
data State = Continues --There are remaining moves and sum of values is not greater than the goal
| Ended -- No more moves or sum of values is greater than the goal
deriving(Eq,Show)
heldcards = []
cardColor :: Card -> Color
cardColor (Card suit _) | suit == Hearts || suit == Diamonds = Red
| otherwise = Black
cardValue :: Card -> Integer
cardValue (Card _ rank) | rank == Jack || rank == Queen || rank == King = 10
| rank == Ace = 11
| rank == Two = 2
| rank == Three = 3
| rank == Four = 4
| rank == Five = 5
| rank == Six = 6
| rank == Seven = 7
| rank == Eight = 8
| rank == Nine = 9
| otherwise = error "Invalid Card"
removeCard :: [Card] -> Card -> [Card]
removeCard [] _ = []
removeCard (c:cs) x | x == c = cs
| otherwise = c : removeCard cs x
allSameColor :: [Card] -> Bool
allSameColor [] = True
allSameColor [c] = True
allSameColor (c:cs) | cardColor c == cardColor (head cs) = allSameColor cs
| otherwise = False
sumCard :: [Card] -> Integer
sumCard [] = 0
sumCard (c:cs) = sumer cs (cardValue c)
where
sumer :: [Card] -> Integer -> Integer
sumer [] sum = sum
sumer (c:cs) sum = sumer cs (sum + cardValue c)
score :: [Card] -> Integer -> Integer
score [] goal = error "No cards"
score cs goal | sumCard cs > goal && allSameColor cs == False = 3 * (sumCard cs - goal)
| sumCard cs > goal && allSameColor cs == True = (3 * (sumCard cs - goal)) `div` 2
| sumCard cs <= goal && allSameColor cs == False = goal - sumCard cs
| otherwise = (goal - sumCard cs) `div` 2
runGame :: [Card] -> [Move] -> Integer
runGame cs [] goal = gameState Ended
runGame cs ms goal = gameState Continues
where
gameState :: State -> Integer
gameState Ended = score heldcards goal
gameState Continues = makemove cs ms heldcards
where
makemove :: [Card] -> [Move] -> [Card] -> State
makemove cs [] heldcards = gameState Ended
makemove [] ms heldcards | m == Draw = gameState Ended
makemove (c:cs) (m:ms) heldcards | sumCard heldcards > goal = gameState Ended
| m == Discard x = removeCard heldcards x && gameState Continues
| m == Draw = c : heldcards && gameState Continues