fork download
  1. module Main ( main ) where
  2. main :: IO ()
  3. main = pure ()
  4.  
  5. {-
  6. Input:
  7. Prelude>pp(ticktack (8,8) [(1,1),(8,8),(2,2),(3,3),(4,2),(3,2)])
  8. ( width, height x o x o x o )
  9.  
  10. Expected output:
  11. ----------
  12. | o|
  13. | |
  14. | |
  15. | |
  16. | |
  17. | o |
  18. | xox |
  19. |x |
  20. ----------
  21.  
  22.  
  23. My output:
  24. My output:
  25.  
  26.  x
  27.   xox
  28.   o
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35. Position (8,8) doesn't show on play field. Tic tacs are showed upside down.
  36. -}
  37.  
  38.  
  39. type Pic = [String]
  40.  
  41. pp :: Pic -> IO ()
  42. pp x = putStr (concat (map (++"\n") x))
  43.  
  44. ticktack :: (Int, Int) -> [(Int, Int)] -> Pic
  45. ticktack _[] = [""]
  46. ticktack (x, y) tulp = if (x <= 0 || y <= 0) then [""]
  47. else split (x, y) (board (mark (x, y) (fill (x, y) tulp)))
  48. where
  49. split :: (Int, Int) -> String -> [String]
  50. split (lenx, leny) str = [ [ str!! (j * lenx + i) | i <- [0..(lenx - 1)] ] | j <- [0..(leny - 1)] ]
  51.  
  52. board :: [Int] -> String
  53. board [] = ""
  54. board (x:xs) | x == 0 = " " ++ board xs
  55. | x == 1 = "x" ++ board xs
  56. | otherwise = "o" ++ board xs
  57.  
  58. mark :: (Int, Int) -> [Int] -> [Int]
  59. mark (posx, posy) a = [ if ((order a x) < 0) then 0
  60. else (if (mod (order a x) 2 == 0) then 1
  61. else 2)
  62. | x <- [0..(posx * posy - 1)] ]
  63. where
  64. order :: [Int] -> Int -> Int
  65. order [] _ = - 10 -- -10 is a walkout, remains undefined positions for tic/tac blank, is limited to 10-1 pairs inside of tulpa
  66. order (x:xs) a = if (x == a) then 0
  67. else 1 + order xs a
  68.  
  69. fill :: (Int, Int) -> [(Int, Int)] -> [Int]
  70. fill _ [] = []
  71. fill (posx, posy) (x:xs) = ((posx * snd x) + fst x) : fill (posx, posy) xs
Success #stdin #stdout 0s 4328KB
stdin
pp(ticktack (8,8) [(1,1),(8,8),(2,2),(3,3),(4,2),(3,2)])
stdout
Standard output is empty