diff options
Diffstat (limited to 'ws2015/ffp/blaetter/11/GameUnits.hs')
-rw-r--r-- | ws2015/ffp/blaetter/11/GameUnits.hs | 76 |
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 | |||
12 | module GameUnits where | ||
13 | |||
14 | |||
15 | import Data.Char | ||
16 | import System.Random | ||
17 | import Language.Haskell.TH | ||
18 | |||
19 | uncapitalize :: String -> String | ||
20 | uncapitalize (c:s) = (toLower c):s | ||
21 | uncapitalize [] = [] | ||
22 | |||
23 | --------------------------- | ||
24 | -- Datatype Declarations -- | ||
25 | --------------------------- | ||
26 | |||
27 | data Unit = Unit {str, hit :: Int, flyer :: Bool, name :: String } | ||
28 | deriving (Eq, Show) | ||
29 | type Units = [Unit] | ||
30 | |||
31 | unit :: Unit -- default Value | ||
32 | unit = Unit {str=undefined, hit=2, flyer=False, name=undefined } | ||
33 | |||
34 | takeHit :: Unit -> Unit | ||
35 | takeHit monster = monster { hit = hit monster - 1 } | ||
36 | |||
37 | isDead :: Unit -> Bool | ||
38 | isDead Unit { hit=n } = n <= 0 | ||
39 | |||
40 | ---------------------------------- | ||
41 | -- Some Stock Units in the game -- | ||
42 | ---------------------------------- | ||
43 | stockUnitList :: [Unit] | ||
44 | stockUnitList = | ||
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 | |||
56 | battle :: Unit -> Unit -> IO Unit | ||
57 | battle 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 | |||
72 | stockUnitShortcuts :: Q [Dec] | ||
73 | -- declares a constants of type Unit for each unit on the | ||
74 | stockUnitShortcuts = undefined -- !!! TODO !!! | ||
75 | |||
76 | \ No newline at end of file | ||