-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypes.hs
More file actions
92 lines (57 loc) · 2.1 KB
/
Copy pathTypes.hs
File metadata and controls
92 lines (57 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE FlexibleInstances #-}
module Types where
import Control.Monad.RWS
import Data.Set as Set
import Data.Map as Map
import Data.List as List
import Data.Maybe as Maybe
data Rank = Rank2|Rank3
deriving (Eq,Ord,Show,Enum,Bounded)
data Direction = North|East|South|West
deriving (Eq,Ord,Show,Enum,Bounded)
oppositeDirection :: Direction -> Direction
oppositeDirection direction = toEnum $ (fromEnum direction + 2) `mod` 4
data Pos = Pos !Int !Int
deriving (Eq,Ord,Show)
type IsClosed = Bool
data Door = Door {isClosed :: IsClosed, doorId:: DoorId}
deriving (Eq,Ord,Show)
newtype DoorId = DoorId Char
deriving (Eq,Ord,Show)
data Props = Wall Direction|HasTrap|HasGoal|HasWarp|StartPosPlayer|StartPosMonster Rank|HasDoor Direction Door|HasSwitch DoorId
deriving (Eq,Ord,Show)
type Field = Set Props
hasDoor :: Direction -> Field -> Maybe Door
hasDoor dir f = listToMaybe[door|(HasDoor d door)<-Set.toList f,d==dir]
hasSwitch :: Field -> Bool
hasSwitch f = []/=[()|(HasSwitch _)<-Set.toList f]
applyDir :: Direction -> Pos -> Pos
applyDir North (Pos x y) = Pos x (y-1)
applyDir East (Pos x y) = Pos (x+1) y
applyDir South (Pos x y) = Pos x (y+1)
applyDir West (Pos x y) = Pos (x-1) y
data MonsterState = WaitRounds !Int | StepsToGo !Int
deriving (Eq,Ord,Show)
getStartPosMonster :: Field -> Maybe Rank
getStartPosMonster f = listToMaybe [r|(StartPosMonster r) <- Set.toList f]
data Situation = Situation
{ monsters :: Map Pos (Rank,MonsterState)
, player :: Pos
, doorStates :: Map DoorId IsClosed
}
deriving (Eq,Ord,Show)
flipSwitch :: DoorId -> Situation -> Situation
flipSwitch d s = s{doorStates=doorStates'}
where
doorStates' = Map.insert d next $ doorStates s
next = not $ doorStates s Map.! d