summaryrefslogtreecommitdiff
path: root/ws2015/ffp/blaetter/11/GameUnits.hs
diff options
context:
space:
mode:
Diffstat (limited to 'ws2015/ffp/blaetter/11/GameUnits.hs')
-rw-r--r--ws2015/ffp/blaetter/11/GameUnits.hs76
1 files changed, 76 insertions, 0 deletions
diff --git a/ws2015/ffp/blaetter/11/GameUnits.hs b/ws2015/ffp/blaetter/11/GameUnits.hs
new file mode 100644
index 0000000..a47614e
--- /dev/null
+++ b/ws2015/ffp/blaetter/11/GameUnits.hs
@@ -0,0 +1,76 @@
1-- Fortgeschrittene Funktionale Programmierung,
2-- LMU, TCS, Wintersemester 2015/16
3-- Steffen Jost, Alexander Isenko
4--
5-- Übungsblatt 11. 13.01.2016
6--
7-- Teilaufgabe
8-- A11-4b TemplateHaskell
9
10{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
11
12module GameUnits where
13
14
15import Data.Char
16import System.Random
17import Language.Haskell.TH
18
19uncapitalize :: String -> String
20uncapitalize (c:s) = (toLower c):s
21uncapitalize [] = []
22
23---------------------------
24-- Datatype Declarations --
25---------------------------
26
27data Unit = Unit {str, hit :: Int, flyer :: Bool, name :: String }
28 deriving (Eq, Show)
29type Units = [Unit]
30
31unit :: Unit -- default Value
32unit = Unit {str=undefined, hit=2, flyer=False, name=undefined }
33
34takeHit :: Unit -> Unit
35takeHit monster = monster { hit = hit monster - 1 }
36
37isDead :: Unit -> Bool
38isDead Unit { hit=n } = n <= 0
39
40----------------------------------
41-- Some Stock Units in the game --
42----------------------------------
43stockUnitList :: [Unit]
44stockUnitList =
45 [ unit{ name = "Scout", str= 5 }
46 , unit{ name = "Crow", str= 5, hit=1, flyer=True }
47 , unit{ name = "Orc", str=20, hit=1 }
48 , unit{ name = "Dwarf", str=15, hit=2 }
49 , unit{ name = "Elf", str=30, hit=1 }
50 , unit{ name = "Giant", str=20, hit=4 }
51 , unit{ name = "Knight",str=35, hit=2 }
52 , unit{ name = "Dragon",str=55, hit=3, flyer=True }
53 ]
54
55
56battle :: Unit -> Unit -> IO Unit
57battle att def = do
58 attRoll <- randomRIO (0,99)
59 defRoll <- randomRIO (0,99)
60 case (attRoll < str att, defRoll < str def) of
61 (True, False) -> check att $ takeHit def
62 (False,True ) -> check (takeHit att) def
63 _other -> battle att def -- reroll
64 where
65 check a d
66 | isDead a = return d
67 | isDead d = return a
68 | otherwise = battle a d
69
70
71
72stockUnitShortcuts :: Q [Dec]
73-- declares a constants of type Unit for each unit on the
74stockUnitShortcuts = undefined -- !!! TODO !!!
75
76 \ No newline at end of file