Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module AlphabetCake where
- -- Angel Canal (716205)
- -- Jorge Pinilla (715750)
- -- maxW maxH (Letter, StartX, StartY, EndX, EndY)
- type CakeElement = (Char, Int, Int, Int, Int)
- data Cake = CakeTuple [CakeElement] Int Int deriving Show
- data SplitMode = HOR | VERT deriving (Enum,Eq)
- --
- -- CONSTRUCTOR
- --
- bakeCake :: Int -> Int -> Cake
- bakeCake width height = CakeTuple [] width height
- --
- -- GETTERS
- --
- getLetter :: CakeElement -> Char
- getLetter (l,_,_,_,_) = l
- getStartPos :: CakeElement -> (Int, Int)
- getStartPos (_,x,y,_,_) = (x,y)
- getStartX :: CakeElement -> Int
- getStartX (_,x,_,_,_) = x
- getStartY :: CakeElement -> Int
- getStartY (_,_,y,_,_) = y
- getEndX :: CakeElement -> Int
- getEndX (_,_,_,x,_) = x
- getEndY :: CakeElement -> Int
- getEndY (_,_,_,_,y) = y
- --
- -- INSERTS
- --
- duplicatedLetter :: [CakeElement] -> Char -> Bool
- duplicatedLetter [] c = False
- duplicatedLetter (x:xs) c
- | (getLetter x) == c = True
- | otherwise = duplicatedLetter xs c
- addLetter :: Cake -> Char -> Int -> Int -> Cake
- addLetter (CakeTuple cake w h) c x y
- | x < 0 = error "Letter placed outside left bound!"
- | x >= w = error "Letter placed outside right bound!"
- | y < 0 = error "Letter placed outside top bound!"
- | y >= h = error "Letter placed outside bottom bound!"
- | (duplicatedLetter cake c) = error "Tried to place a duplicated letter!"
- | otherwise = CakeTuple ([(c, x, y, x+1, y+1)] ++ cake) w h
- splitCake :: Cake -> Cake
- splitCake (CakeTuple cake w h) = (CakeTuple (splitHor 0 0 w h cake) w h)
- splitHor :: Int -> Int -> Int -> Int -> [CakeElement] -> [CakeElement]
- splitHor x0 y0 xn yn [] = error "We reached an empty chunk"
- splitHor x0 y0 xn yn [c] = [(getLetter c, x0, y0, xn, yn)]
- splitHor x0 y0 xn yn l@(x:xs)
- | null (snd cakeTuple) = (splitVert x0 y0 xn yn l)
- | otherwise = (splitVert x0 y0 xn ((getStartY x) + 1) ([x] ++ (fst cakeTuple))) ++ (splitVert x0 ((getStartY x) + 2) xn yn (snd cakeTuple))
- where
- cakeTuple = splitChunks x0 y0 xn ((getStartY x) + 1) xs HOR
- splitVert :: Int -> Int -> Int -> Int -> [CakeElement] -> [CakeElement]
- splitVert x0 y0 xn yn [] = error "We reached an empty chunk"
- splitVert x0 y0 xn yn [c] = [(getLetter c, x0, y0, xn, yn)]
- splitVert x0 y0 xn yn l@(x:xs)
- | null (snd cakeTuple) = (splitHor x0 y0 xn yn l)
- | otherwise = (splitHor x0 y0 ((getStartX x) + 1) yn ([x] ++ (fst cakeTuple))) ++ (splitHor ((getStartX x) + 2) y0 xn yn (snd cakeTuple))
- where
- cakeTuple = splitChunks x0 y0 ((getStartX x) + 1) yn xs VERT
- splitChunks :: Int -> Int -> Int -> Int -> [CakeElement] -> SplitMode -> ([CakeElement], [CakeElement])
- splitChunks x0 y0 xn yn [] mode = error "We reached an empty chunk in splitting chunks"
- splitChunks x0 y0 xn yn [c] mode
- | ((getStartX c) < xn) && (mode == VERT) = ([c], [])
- | ((getStartY c) < yn) || (mode == HOR) = ([c], [])
- | otherwise = ([], [c])
- splitChunks x0 y0 xn yn (x:xs) mode = (fst l ++ fst l2, snd l ++ snd l2)
- where
- l = splitChunks x0 y0 xn yn [x] mode
- l2 = splitChunks x0 y0 xn yn xs mode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement