Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# OPTIONS_GHC -Wall #-}
- {-# LANGUAGE MultiParamTypeClasses,
- TypeSynonymInstances, FlexibleInstances #-}
- module GAS where
- import ProblemState
- import qualified Data.Map.Strict as M
- {-
- Pozițiile tablei de joc, în formă (linie, coloană), unde ambele coordonate
- pot fi negative.
- -}
- type Position = (Int, Int)
- {-
- Culorile pătratelor și cercurilor.
- -}
- data Color = Red | Blue | Gray
- deriving (Eq, Ord, Show)
- {-
- Orientările pătratelor și săgeților.
- -}
- data Heading = North | South | East | West
- deriving (Eq, Ord)
- instance Show Heading where
- show North = "^"
- show South = "v"
- show East = ">"
- show West = "<"
- {-
- *** TODO ***
- Un obiect de pe tabla de joc: pătrat/ cerc/ săgeată.
- -}
- data Object = Square Color Heading | Circle Color | Arrow Heading
- deriving (Eq, Ord)
- {-
- *** TODO ***
- Reprezetarea textuală a unui obiect.
- -}
- instance Show Object where
- show (Square Red s2) = "R" ++ show s2
- show (Square Blue s2) = "B" ++ show s2
- show (Square Gray s2) = "G" ++ show s2
- show (Circle Red) = "r"
- show (Circle Blue) = "b"
- show (Circle Gray) = "g"
- show (Arrow a) = show a
- showListObject :: [Object] -> String
- showListObject [] = " "
- showListObject [s@(Square _ _)] = show s ++ " "
- showListObject [c@(Circle _)] = " " ++ show c
- showListObject [a@(Arrow _)] = " " ++ show a
- showListObject [s@(Square _ _), c@(Circle _)] = show s ++ show c
- {-
- *** TODO ***
- Un nivel al jocului.
- Recomandăm Data.Map.Strict.
- -}
- data Level = Level (M.Map Position [Object])
- deriving (Eq, Ord)
- {-
- *** TODO ***
- Reprezetarea textuală a unui nivel.
- -}
- getxmin :: Level -> Int
- getxmin (Level l) = M.foldlWithKey (\acc x y -> (min acc (fst x))) 100000 l
- getxmax :: Level -> Int
- getxmax (Level l) = M.foldlWithKey (\acc x y -> (max acc (fst x))) (-100000) l
- getymin :: Level -> Int
- getymin (Level l) = M.foldlWithKey (\acc x y -> (min acc (snd x))) 100000 l
- getymax :: Level -> Int
- getymax (Level l) = M.foldlWithKey (\acc x y -> (max acc (snd x))) (-100000) l
- instance Show Level where
- show (Level l) = map (\x -> map (\y -> (showListObject (M.findWithDefault [] (x, y) l)) yrange)) xrange
- where
- xrange = [(getxmin (Level M)) .. (getxmax (Level M))]
- yrange = [(getymin (Level M)) .. (getymax (Level M))]
- {-
- *** TODO ***
- Nivelul vid, fără obiecte.
- -}
- emptyLevel :: Level
- emptyLevel = Level (M.empty)
- {-
- *** TODO ***
- Adaugă un pătrat cu caracteristicile date la poziția precizată din nivel.
- -}
- checkObject :: Color -> Heading -> [Object] -> Position -> Level -> [Object]
- checkObject c h [(Circle c2)] p (Level l) = (Square c h):[Circle c2]
- checkObject c h [] p (Level l) = [(Square c h)]
- checkObject c h [(Arrow h2)] p (Level l) = [(Square c h2)]
- addSquare :: Color -> Heading -> Position -> Level -> Level
- --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)
- addSquare c h p (Level l) = Level (M.insert p (checkObject c h (M.findWithDefault [] p l) p (Level l)) l)
- {-
- *** TODO ***
- Adaugă un cerc cu caracteristicile date la poziția precizată din nivel.
- -}
- addCircle :: Color -> Position -> Level -> Level
- --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)
- addCircle c p (Level l) = Level (M.insert p [(Circle c)] l)
- {-
- *** TODO ***
- Adaugă o săgeată cu caracteristicile date la poziția precizată din nivel.
- -}
- addArrow :: Heading -> Position -> Level -> Level
- addArrow h p (Level l) = Level (M.insert p [(Arrow h)] l)
- {-
- *** TODO ***
- Mută pătratul de la poziția precizată din nivel. Dacă la poziția respectivă
- nu se găsește un pătrat, întoarce direct parametrul.
- -}
- move :: Position -- Poziția
- -> Level -- Nivelul inițial
- -> Level -- Nivelul final
- move = undefined
- --move p (Level l) = Level ()
- {-
- *** TODO ***
- Instanțiați clasa `ProblemState` pentru jocul nostru.
- -}
- instance ProblemState Level Position where
- successors = undefined
- isGoal = undefined
- -- Doar petru BONUS
- -- heuristic =
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement