Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# OPTIONS_GHC -Wall #-}
  2. {-# LANGUAGE MultiParamTypeClasses,
  3.              TypeSynonymInstances, FlexibleInstances #-}
  4.  
  5. module GAS where
  6.  
  7. import ProblemState
  8.  
  9. import qualified Data.Map.Strict as M
  10.  
  11. {-
  12.     Pozițiile tablei de joc, în formă (linie, coloană), unde ambele coordonate
  13.     pot fi negative.
  14. -}
  15. type Position = (Int, Int)
  16.  
  17. {-
  18.     Culorile pătratelor și cercurilor.
  19. -}
  20. data Color = Red | Blue | Gray
  21.     deriving (Eq, Ord, Show)
  22.  
  23. {-
  24.     Orientările pătratelor și săgeților.
  25. -}
  26. data Heading = North | South | East | West
  27.     deriving (Eq, Ord)
  28.  
  29. instance Show Heading where
  30.     show North = "^"
  31.     show South = "v"
  32.     show East  = ">"
  33.     show West  = "<"
  34.  
  35. {-
  36.     *** TODO ***
  37.  
  38.     Un obiect de pe tabla de joc: pătrat/ cerc/ săgeată.
  39. -}
  40. data Object = Square Color Heading | Circle Color | Arrow Heading
  41.     deriving (Eq, Ord)
  42.  
  43. {-
  44.     *** TODO ***
  45.  
  46.     Reprezetarea textuală a unui obiect.
  47. -}
  48.  
  49. instance Show Object where
  50.     show (Square Red s2) = "R" ++ show s2
  51.     show (Square Blue s2) = "B" ++ show s2
  52.     show (Square Gray s2) = "G" ++ show s2
  53.     show (Circle Red) = "r"
  54.     show (Circle Blue) = "b"
  55.     show (Circle Gray) = "g"
  56.     show (Arrow a) = show a
  57.  
  58. showListObject :: [Object] -> String
  59. showListObject [] = "   "
  60. showListObject [s@(Square _ _)] = show s ++ " "
  61. showListObject [c@(Circle _)] = "  " ++ show c
  62. showListObject [a@(Arrow _)] = "  " ++ show a
  63. showListObject [s@(Square _ _), c@(Circle _)] = show s ++ show c
  64. {-
  65.     *** TODO ***
  66.  
  67.     Un nivel al jocului.
  68.  
  69.     Recomandăm Data.Map.Strict.
  70. -}
  71. data Level = Level (M.Map Position [Object])
  72.     deriving (Eq, Ord)
  73.  
  74. {-
  75.     *** TODO ***
  76.  
  77.     Reprezetarea textuală a unui nivel.
  78. -}
  79.  
  80. getxmin :: Level -> Int
  81. getxmin (Level l) = M.foldlWithKey (\acc x y -> (min acc (fst x))) 100000 l
  82.  
  83. getxmax :: Level -> Int
  84. getxmax (Level l) = M.foldlWithKey (\acc x y -> (max acc (fst x))) (-100000) l
  85.  
  86. getymin :: Level -> Int
  87. getymin (Level l) = M.foldlWithKey (\acc x y -> (min acc (snd x))) 100000 l
  88.  
  89. getymax :: Level -> Int
  90. getymax (Level l) = M.foldlWithKey (\acc x y -> (max acc (snd x))) (-100000) l
  91.  
  92.  
  93. instance Show Level where
  94. show (Level l) = map (\x -> map (\y -> (showListObject (M.findWithDefault [] (x, y) l)) yrange)) xrange
  95.     where
  96.     xrange = [(getxmin (Level M)) .. (getxmax (Level M))]
  97.     yrange = [(getymin (Level M)) .. (getymax (Level M))]
  98.  
  99. {-
  100.     *** TODO ***
  101.  
  102.     Nivelul vid, fără obiecte.
  103. -}
  104. emptyLevel :: Level
  105. emptyLevel = Level (M.empty)
  106.  
  107. {-
  108.     *** TODO ***
  109.  
  110.     Adaugă un pătrat cu caracteristicile date la poziția precizată din nivel.
  111. -}
  112.  
  113. checkObject :: Color -> Heading -> [Object] -> Position -> Level -> [Object]
  114. checkObject c h [(Circle c2)] p (Level l) = (Square c h):[Circle c2]
  115. checkObject c h [] p (Level l) = [(Square c h)]
  116. checkObject c h [(Arrow h2)] p (Level l) = [(Square c h2)]
  117.  
  118.  
  119. addSquare :: Color -> Heading -> Position -> Level -> Level
  120. --addSquare c h p (Level l) = if (M.lookup p l == Nothing) then Level (M.insert p [(Square c h)] l) else if (typeOf (M.findWithDefault [] p l) == Arrow) then (M.insert p [(Square c (M.findWithDefault [] p l))] l) else Level (M.insert p  ((Square c h):(M.findWithDefault [] p l)) l)
  121. addSquare c h p (Level l) = Level (M.insert p (checkObject c h (M.findWithDefault [] p l) p (Level l)) l)
  122.    
  123.  
  124. {-
  125.     *** TODO ***
  126.  
  127.     Adaugă un cerc cu caracteristicile date la poziția precizată din nivel.
  128. -}
  129. addCircle :: Color -> Position -> Level -> Level
  130. --addCircle c p (Level l) = if (M.lookup p l == Nothing) then Level (M.insert p [(Circle c)] l) else Level (M.insert p ((M.findWithDefault [] p l) ++ [(Circle c)]) l)
  131. addCircle c p (Level l) = Level (M.insert p [(Circle c)] l)
  132.  
  133. {-
  134.     *** TODO ***
  135.  
  136.     Adaugă o săgeată cu caracteristicile date la poziția precizată din nivel.
  137. -}
  138. addArrow :: Heading -> Position -> Level -> Level
  139. addArrow h p (Level l) = Level (M.insert p [(Arrow h)] l)
  140.  
  141. {-
  142.     *** TODO ***
  143.  
  144.     Mută pătratul de la poziția precizată din nivel. Dacă la poziția respectivă
  145.     nu se găsește un pătrat, întoarce direct parametrul.
  146. -}
  147. move :: Position  -- Poziția
  148.      -> Level     -- Nivelul inițial
  149.      -> Level     -- Nivelul final
  150. move = undefined
  151. --move p (Level l) = Level ()
  152. {-
  153.     *** TODO ***
  154.  
  155.     Instanțiați clasa `ProblemState` pentru jocul nostru.
  156. -}
  157. instance ProblemState Level Position where
  158.     successors = undefined
  159.  
  160.     isGoal = undefined
  161.  
  162.     -- Doar petru BONUS
  163.     -- heuristic =
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement