Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type Result = [String]
  2. pp :: Result -> IO ()
  3. pp x = putStr (concat (map (++"\n") x))
  4.  
  5. ticktack :: (Int,Int) -> [(Int,Int)] -> Result
  6. ticktack (rows, cols) xs = reverse (createAllResultLines (updateTableWithEveryPosition xs False (createCornersInTable (createFirstTableLayout (rows,cols)) (rows+1) (cols+1))) 0)
  7.  
  8. -- creates all result lines - all lines of final output; essentially just parses list of points to readable format
  9. createAllResultLines :: [(Char, Int, Int)] -> Int -> Result
  10. createAllResultLines [] _ = []
  11. createAllResultLines xs currentRow = [createResultLine xs currentRow] ++ createAllResultLines [(a,b,c) | (a,b,c) <- xs, b /= currentRow] (currentRow + 1)
  12.  
  13. -- creates one result - one line of final output; essentially just parses list of points to readable format
  14. createResultLine :: [(Char, Int, Int)] -> Int -> String
  15. createResultLine xs currentRow = [a | (a,b,c) <- filter (\(a,b,c) -> b == currentRow) xs]
  16.  
  17. updateTableWithEveryPosition :: [(Int, Int)] -> Bool -> [(Char, Int, Int)] -> [(Char, Int, Int)]
  18. updateTableWithEveryPosition [] _ xs = xs
  19. updateTableWithEveryPosition ((row,col):positions) even table = updateTableWithEveryPosition positions (not even) (updateTableWithOnePosition (col, row) even table)
  20.  
  21. updateTableWithOnePosition :: (Int, Int) -> Bool -> [(Char, Int, Int)] -> [(Char, Int, Int)]
  22. updateTableWithOnePosition _ _ [] = []
  23. updateTableWithOnePosition (row,col) even ((a,b,c):xs) = [changeOnePositionInTable (row,col) (not even) (a,b,c)] ++ updateTableWithOnePosition (row,col) even xs
  24.  
  25. changeOnePositionInTable :: (Int, Int) -> Bool -> (Char, Int, Int) -> (Char, Int, Int)
  26. changeOnePositionInTable (row, col) even (a,b,c)
  27.         | (b == row && c == col && even) = ('x', b, c)
  28.         | (b == row && c == col && not even) = ('o', b ,c)
  29.         | otherwise = (a,b,c)
  30.  
  31. createFirstTableLayout :: (Int, Int) -> [(Char, Int, Int)]
  32. createFirstTableLayout (rows, cols) = [(' ', x, y)  | x <- [0..rows + 1], y <- [0..cols + 1]]
  33.  
  34. createCornersInTable :: [(Char, Int, Int)] -> Int -> Int -> [(Char, Int, Int)]
  35. createCornersInTable [] _ _= []
  36. createCornersInTable ((a,b,c):xs) maxRow maxCol= [createOneCornerLetter (a,b,c) maxRow maxCol] ++ createCornersInTable xs maxRow maxCol
  37.  
  38. createOneCornerLetter :: (Char, Int, Int) -> Int -> Int -> (Char, Int, Int)
  39. createOneCornerLetter (a,b,c) maxRow maxCol
  40.     | (b == 0 || b == maxRow) = ('-', b, c)
  41.     | (c == 0 || c == maxCol) = ('|', b ,c)
  42.     | otherwise = (a,b,c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement