You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.7 KiB

  1. {-# LANGUAGE DeriveGeneric #-}
  2. module SinglePasses(Output(..), Cell(..), passes) where
  3. import Data.Set(Set(..),difference,union,toList,empty)
  4. import Data.Maybe
  5. import Data.Typeable
  6. import GHC.Generics
  7. import Data.Aeson
  8. import Data.Aeson.Types
  9. import qualified Datatypes as DT
  10. import qualified VM
  11. data Cell = Cell { x :: Int, y :: Int}
  12. deriving (Show, Generic)
  13. data Output = Output { width :: Int,
  14. height :: Int,
  15. filled :: [Cell],
  16. touched :: [Cell],
  17. unit :: [Cell],
  18. score :: Int
  19. }
  20. deriving (Show, Generic)
  21. instance FromJSON Cell
  22. instance ToJSON Cell
  23. instance FromJSON Output
  24. instance ToJSON Output
  25. passes :: [(DT.Game, VM.Notes)] -> [Output]
  26. passes gamesnotes = map generateoutput (gameswithtouched gamesnotes)
  27. where
  28. generateoutput ((game,note),touched) = Output { width = DT.width $ DT.board game,
  29. height = DT.height $ DT.board game,
  30. filled = filledcells game,
  31. touched = extractcell touched,
  32. unit = unitcells game,
  33. score = DT.score game
  34. }
  35. filledcells gm = extractcell $ DT.filled $ DT.board gm
  36. unitcells gm = if null $ DT.units gm
  37. then []
  38. else extractunit $ head $ DT.units gm
  39. gameswithtouched :: [(DT.Game,VM.Notes)] -> [((DT.Game,VM.Notes),Set DT.Cell)]
  40. gameswithtouched els = gameswithtouched0 els Data.Set.empty
  41. gameswithtouched0 :: [(DT.Game,VM.Notes)] -> Set DT.Cell -> [((DT.Game,VM.Notes),Set DT.Cell)]
  42. gameswithtouched0 [] acc = []
  43. gameswithtouched0 (x:[]) acc = [(x,acc)]
  44. gameswithtouched0 (x:xs) acc = (x,touched):lastels
  45. where
  46. touched = (difference (difference acc cells) units)
  47. cells = DT.filled $ DT.board $ fst x
  48. units = if null $ DT.units $ fst x
  49. then Data.Set.empty
  50. else DT.members $ head $ DT.units $ fst x
  51. touchedcells tc = extractcell touched
  52. lastels = gameswithtouched0 xs (union touched units)
  53. extractunit :: DT.Unit -> [Cell]
  54. extractunit unit = extractcell $ DT.members unit
  55. extractcell :: Set DT.Cell -> [Cell]
  56. extractcell cell = map (\(x,y) -> Cell x y) (toList cell)