evac/Main.hs

53 lines
1.9 KiB
Haskell
Raw Normal View History

2019-05-15 00:48:51 -04:00
type Group = [MainCharacter]
type NeoTokyo = [Group]
2019-05-14 23:53:15 -04:00
data MainCharacter = Asuka | Shinji | Rei | Misato | Ritsuko | Gendo
2019-05-15 00:48:51 -04:00
deriving (Show, Eq)
2019-05-14 23:53:15 -04:00
2019-05-15 02:14:32 -04:00
-- Each characters default adjacents
2019-05-14 23:53:15 -04:00
defFrens :: MainCharacter -> [MainCharacter]
defFrens Asuka = [Rei, Shinji, Misato]
defFrens Shinji = [Rei, Gendo, Ritsuko, Misato, Asuka]
defFrens Rei = [Gendo, Shinji, Asuka]
defFrens Misato = [Asuka, Shinji, Ritsuko]
defFrens Ritsuko = [Misato, Shinji, Gendo]
defFrens Gendo = [Rei, Shinji, Ritsuko]
2019-05-15 02:14:32 -04:00
-- The group that character x belongs to
2019-05-14 23:53:15 -04:00
charsGroup :: MainCharacter -> NeoTokyo -> Group
2019-05-15 00:48:51 -04:00
charsGroup x nt = head $ filter (\g -> x `elem` g) nt
2019-05-14 23:53:15 -04:00
2019-05-15 02:14:32 -04:00
-- The characters currently grouped with x
2019-05-14 23:53:15 -04:00
curFrens :: MainCharacter -> NeoTokyo -> [MainCharacter]
curFrens x nt = filter (/= x) $ charsGroup x nt
2019-05-15 02:14:32 -04:00
-- Remove duplicates, stabily
2019-05-15 00:48:51 -04:00
uniq :: (Eq a) => [a] -> [a]
2019-05-15 02:14:32 -04:00
uniq x = reverse $ go x []
2019-05-15 00:48:51 -04:00
where
go [] al = al
go (c:cs) al = if (c `elem` al) then (go cs al) else (go cs (c:al))
2019-05-14 23:53:15 -04:00
2019-05-15 02:14:32 -04:00
-- The groups a character is adaject to
2019-05-14 23:53:15 -04:00
adjGroups :: MainCharacter -> NeoTokyo -> [Group]
2019-05-15 00:48:51 -04:00
adjGroups x nt = filter (\g -> any (\x -> x `elem` adjFrens) g) (otherGroups x nt)
2019-05-14 23:53:15 -04:00
where
2019-05-15 02:07:34 -04:00
adjFrens = uniq $ foldr (++) [] (map defFrens $ charsGroup x nt)
2019-05-15 00:48:51 -04:00
2019-05-15 02:14:32 -04:00
-- The groups a character isnt in
2019-05-15 00:48:51 -04:00
otherGroups :: MainCharacter -> NeoTokyo -> [Group]
otherGroups x nt = filter (\g -> not $ x `elem` g) nt
2019-05-14 23:53:15 -04:00
2019-05-15 02:14:32 -04:00
-- All characters a X is adjacent to
2019-05-14 23:53:15 -04:00
getAdj :: MainCharacter -> NeoTokyo -> [MainCharacter]
2019-05-15 00:48:51 -04:00
getAdj x nt = curFrens x nt ++ (foldr (++) [] (adjGroups x nt))
2019-05-14 23:53:15 -04:00
2019-05-15 02:14:32 -04:00
-- Hurt character x
2019-05-14 23:53:15 -04:00
hurt :: MainCharacter -> NeoTokyo -> NeoTokyo
2019-05-15 02:07:34 -04:00
hurt x nt
| curFrens x nt == [] = nt
| otherwise = [x] : (filter (/= x) $ charsGroup x nt) : (otherGroups x nt)
2019-05-14 23:53:15 -04:00
2019-05-15 02:14:32 -04:00
-- Attract X and Y
2019-05-15 00:48:51 -04:00
attract :: MainCharacter -> MainCharacter -> NeoTokyo -> NeoTokyo
attract x y nt = (uniq (charsGroup x nt ++ charsGroup y nt)) : (otherGroups y (otherGroups x nt))