-
Notifications
You must be signed in to change notification settings - Fork 0
/
cup.hs
62 lines (41 loc) · 1.06 KB
/
cup.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
cup f10z message = message f10z
getOz aCup = aCup (\f10z -> f10z)
drink aCup ozDrank =
if ozDiff >= 0
then cup ozDiff
else cup 0
where
f10z = getOz aCup
ozDiff = f10z - ozDrank
isEmptyCup aCup = getOz aCup == 0
-- robot Example
robot (name, attack, hp) message = message (name, attack, hp)
name (n, _, _) = n
attack (_, a, _) = a
hp (_, _, h) = h
-- Getters
getName robot = robot name
getAttack robot = robot attack
getHP robot = robot hp
-- setters
setName aRobot newName = aRobot (\(_, a, h) -> robot (newName, a, h))
setAttack aRobot newAttack = aRobot (\(n, _, h) -> robot (n, newAttack, h))
setHP aRobot newHP = aRobot (\(n, a, _) -> robot (n, a, newHP))
-- print
printRobot aRobot =
aRobot
( \(n, a, h) ->
n
++ " attack: "
++ show a
++ " hp: "
++ show h
)
-- fight
damage aRobot damage = aRobot (\(n, a, h) -> robot (n, a, h - damage))
fight aRobot defender = damage defender attack
where
attack =
if getHP aRobot > 10
then getAttack aRobot
else 0