--miscellaneous és típusok import Data.List type Name = String type Health = Integer…
- User
--miscellaneous és típusok import Data.List
type Name = String type Health = Integer type Spell = (Integer -> Integer) type Army = [Unit] type EnemyArmy = Army type Amount = Integer
showState a = show a showMage a = show a eqMage a b = a == b showUnit a = show a showOneVOne a = show a
papi = let tunderpor enemyHP | enemyHP < 8 = 0 | even enemyHP = div (enemyHP * 3) 4 | otherwise = enemyHP - 3 in Master "Papi" 126 tunderpor java = Master "Java" 100 (\x -> x - (mod x 9)) traktor = Master "Traktor" 20 (\x -> div (x + 10) ((mod x 4) + 1)) jani = Master "Jani" 100 (\x -> x - div x 4) skver = Master "Skver" 100 (\x -> div (x+4) 2)
potionMaster = let plx x | x > 85 = x - plx (div x 2) | x == 60 = 31 | x >= 51 = 1 + mod x 30 | otherwise = x - 7 in Master "PotionMaster" 170 plx
--Első feladat (Felkészülés)
data State unittype = Alive unittype | Dead deriving(Eq) data Entity = Golem Health | HaskellElemental Health deriving (Show) data Mage = Master Name Health Spell data Unit = E (State Entity) | M (State Mage) deriving(Eq)
instance Show unittype => Show (State unittype) where show (Alive a) = show a show Dead = "Dead"
instance Show Mage where show (Master name health _) | health >= 5 = name | otherwise = "Wounded " ++ name
instance Eq Mage where (Master name1 health1 _) == (Master name2 health2 _) = (name1 == name2) && (health1 == health2)
instance Show Unit where show (E stateEntity) = case stateEntity of Alive entity -> case entity of Golem health -> "Golem " ++ show health HaskellElemental health -> "HaskellElemental " ++ show health Dead -> "Dead" show (M stateMage) = case stateMage of Alive mage -> showMage mage Dead -> "Dead"
instance Eq Entity where (Golem health1) == (Golem health2) = health1 == health2 (HaskellElemental health1) == (HaskellElemental health2) = health1 == health2 _ == _ = False
--Második feladat (Elesettek) formationFix :: Army -> Army formationFix army = filter isAlive army ++ filter isDead army where isAlive (E (Alive _)) = True isAlive (M (Alive _)) = True isAlive _ = False isDead = not . isAlive
--Harmadik feladat (Vége?) over :: Army -> Bool over = all temp
temp :: Unit -> Bool temp (E Dead) = True temp (M Dead) = True temp _ = False
--Negyedik feladat (Ütközet) fight :: EnemyArmy -> Army -> Army fight [] army = army fight army [] = army fight (mage@(M (Alive (Master _ _ spell))):enemies) allies = let updatedEnemies = applyMageAbility spell enemies in fight updatedEnemies allies fight (E (Alive (HaskellElemental )):hs) (E (Alive (Golem h)):gs) = let damagedGolem = E (Alive (Golem (if h >= 3 then h - 3 else 0))) in damagedGolem : fight hs gs fight (:hs) (_:gs) = fight hs gs
applyMageAbility :: Spell -> EnemyArmy -> EnemyArmy applyMageAbility _ [] = [] applyMageAbility spell (enemy@(E (Alive (Golem h))):enemies) = let updatedEnemy = attackMage enemy spell in updatedEnemy : applyMageAbility spell enemies applyMageAbility spell (enemy@(E (Alive (HaskellElemental h))):enemies) = let updatedEnemy = attackMage enemy spell in updatedEnemy : applyMageAbility spell enemies applyMageAbility _ enemies = enemies
- Assistant
- User
my fight function doesn't work for this testcase: fight [M (Alive potionMaster)] [E (Alive (Golem n)) | n <- [70,70,70,70,70]] == [E (Alive (Golem 11)),E (Alive (Golem 11)),E (Alive (Golem 11)),E (Alive (Golem 11)),E (Alive (Golem 11))] it doesn't apply the damage, so it just gives back [Golem 70,Golem 70,Golem 70,Golem 70,Golem 70]
- Assistant
- User
now please add to the fight function, so it can handle the testcase of:fight [E (Alive (HaskellElemental n))| n <- [50,50,50,50]] [E (Alive (Golem n)) | n <- [70,70,70,70,70]] ==[E (Alive (Golem 67)),E (Alive (Golem 67)),E (Alive (Golem 67)),E (Alive (Golem 67)),E (Alive (Golem 70))] Amikor két sereg összeütközik, az egységek csak a velük szemben lévőket tudják támadni, a Golem-ek egyet, míg a HaskellElementalok hármat sebeznek ellenfelükbe. But everything related to my previous testcase should still be true
- Assistant
- User
gigamegabeadando.hs:111:8: error: [GHC-72516] Parse error in pattern: :es | 111 | fight (:es) as = fight es as
- Assistant
The error message is indicating that there is a problem with the pattern
(:es). In Haskell, the cons operator should be used in a pattern match to destructure a list into its head and tail, but it needs to be used between a head element and the tail variable, not alone.