Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Result = [String]
- pp :: Result -> IO ()
- pp x = putStr (concat (map (++"\n") x))
- ticktack :: (Int,Int) -> [(Int,Int)] -> Result
- ticktack (rows, cols) xs = reverse (createAllResultLines (updateTableWithEveryPosition xs False (createCornersInTable (createFirstTableLayout (rows,cols)) (rows+1) (cols+1))) 0)
- -- creates all result lines - all lines of final output; essentially just parses list of points to readable format
- createAllResultLines :: [(Char, Int, Int)] -> Int -> Result
- createAllResultLines [] _ = []
- createAllResultLines xs currentRow = [createResultLine xs currentRow] ++ createAllResultLines [(a,b,c) | (a,b,c) <- xs, b /= currentRow] (currentRow + 1)
- -- creates one result - one line of final output; essentially just parses list of points to readable format
- createResultLine :: [(Char, Int, Int)] -> Int -> String
- createResultLine xs currentRow = [a | (a,b,c) <- filter (\(a,b,c) -> b == currentRow) xs]
- updateTableWithEveryPosition :: [(Int, Int)] -> Bool -> [(Char, Int, Int)] -> [(Char, Int, Int)]
- updateTableWithEveryPosition [] _ xs = xs
- updateTableWithEveryPosition ((row,col):positions) even table = updateTableWithEveryPosition positions (not even) (updateTableWithOnePosition (col, row) even table)
- updateTableWithOnePosition :: (Int, Int) -> Bool -> [(Char, Int, Int)] -> [(Char, Int, Int)]
- updateTableWithOnePosition _ _ [] = []
- updateTableWithOnePosition (row,col) even ((a,b,c):xs) = [changeOnePositionInTable (row,col) (not even) (a,b,c)] ++ updateTableWithOnePosition (row,col) even xs
- changeOnePositionInTable :: (Int, Int) -> Bool -> (Char, Int, Int) -> (Char, Int, Int)
- changeOnePositionInTable (row, col) even (a,b,c)
- | (b == row && c == col && even) = ('x', b, c)
- | (b == row && c == col && not even) = ('o', b ,c)
- | otherwise = (a,b,c)
- createFirstTableLayout :: (Int, Int) -> [(Char, Int, Int)]
- createFirstTableLayout (rows, cols) = [(' ', x, y) | x <- [0..rows + 1], y <- [0..cols + 1]]
- createCornersInTable :: [(Char, Int, Int)] -> Int -> Int -> [(Char, Int, Int)]
- createCornersInTable [] _ _= []
- createCornersInTable ((a,b,c):xs) maxRow maxCol= [createOneCornerLetter (a,b,c) maxRow maxCol] ++ createCornersInTable xs maxRow maxCol
- createOneCornerLetter :: (Char, Int, Int) -> Int -> Int -> (Char, Int, Int)
- createOneCornerLetter (a,b,c) maxRow maxCol
- | (b == 0 || b == maxRow) = ('-', b, c)
- | (c == 0 || c == maxCol) = ('|', b ,c)
- | otherwise = (a,b,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement