-- Fortgeschrittene Funktionale Programmierung, -- LMU, TCS, Wintersemester 2015/16 -- Steffen Jost, Alexander Isenko -- -- Übungsblatt 11. 13.01.2016 -- -- Teilaufgabe -- A11-4b TemplateHaskell {-# LANGUAGE TemplateHaskell, QuasiQuotes #-} module GameUnits where import Data.Char import System.Random import Language.Haskell.TH uncapitalize :: String -> String uncapitalize (c:s) = (toLower c):s uncapitalize [] = [] --------------------------- -- Datatype Declarations -- --------------------------- data Unit = Unit {str, hit :: Int, flyer :: Bool, name :: String } deriving (Eq, Show) type Units = [Unit] unit :: Unit -- default Value unit = Unit {str=undefined, hit=2, flyer=False, name=undefined } takeHit :: Unit -> Unit takeHit monster = monster { hit = hit monster - 1 } isDead :: Unit -> Bool isDead Unit { hit=n } = n <= 0 ---------------------------------- -- Some Stock Units in the game -- ---------------------------------- stockUnitList :: [Unit] stockUnitList = [ unit{ name = "Scout", str= 5 } , unit{ name = "Crow", str= 5, hit=1, flyer=True } , unit{ name = "Orc", str=20, hit=1 } , unit{ name = "Dwarf", str=15, hit=2 } , unit{ name = "Elf", str=30, hit=1 } , unit{ name = "Giant", str=20, hit=4 } , unit{ name = "Knight",str=35, hit=2 } , unit{ name = "Dragon",str=55, hit=3, flyer=True } ] battle :: Unit -> Unit -> IO Unit battle att def = do attRoll <- randomRIO (0,99) defRoll <- randomRIO (0,99) case (attRoll < str att, defRoll < str def) of (True, False) -> check att $ takeHit def (False,True ) -> check (takeHit att) def _other -> battle att def -- reroll where check a d | isDead a = return d | isDead d = return a | otherwise = battle a d stockUnitShortcuts :: Q [Dec] -- declares a constants of type Unit for each unit on the stockUnitShortcuts = undefined -- !!! TODO !!!