a one dimensional cellular automata, using comonads
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.

71 lines
1.8KB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. module BrickStuff where
  3. import Automata
  4. import Spaces.Space2
  5. import System.Random
  6. import Brick
  7. import Brick.BChan (newBChan, writeBChan)
  8. import qualified Brick.Widgets.Border as B
  9. import qualified Brick.Widgets.Border.Style as BS
  10. import qualified Brick.Widgets.Center as C
  11. import qualified Graphics.Vty as V
  12. -----------------
  13. -- brick stuff --
  14. -----------------
  15. data Tick = Tick
  16. type Name = ()
  17. -- App definition
  18. app :: Int -> Int -> App (Space2 CellState) Tick Name
  19. app h w = App { appDraw = drawUI h w
  20. , appChooseCursor = neverShowCursor
  21. , appHandleEvent = handleEvent
  22. , appStartEvent = return
  23. , appAttrMap = const theMap
  24. }
  25. -- Handling events
  26. theMap :: AttrMap
  27. theMap = attrMap V.defAttr
  28. [ (rockAttr, V.red `on` V.blue)
  29. , (scissorsAttr, V.green `on` V.red)
  30. , (paperAttr, V.blue `on` V.green)
  31. ]
  32. handleEvent :: (Space2 CellState) -> BrickEvent Name Tick -> EventM Name (Next (Space2 CellState))
  33. handleEvent g (AppEvent Tick) = continue $ step rps g
  34. handleEvent g (VtyEvent (V.EvKey (V.KChar 'q') [])) = halt g
  35. handleEvent g _ = continue g
  36. drawUI :: Int -> Int -> Space2 CellState -> [Widget Name]
  37. drawUI h w g = [ C.center $ drawGrid h w g ]
  38. drawGrid :: Int -> Int -> Space2 CellState -> Widget Name
  39. drawGrid h w g = vBox rows
  40. where
  41. bw = mat2 g
  42. rows = [ hBox $ cellsInRow r | r <- bw ]
  43. cellsInRow y = map drawCell y
  44. drawCell :: CellState -> Widget Name
  45. drawCell Paper = withAttr paperAttr $ str " "
  46. drawCell Scissors = withAttr scissorsAttr $ str " "
  47. drawCell Rock = withAttr rockAttr $ str " "
  48. rockAttr, scissorsAttr, paperAttr :: AttrName
  49. rockAttr = "rockAttr"
  50. paperAttr = "paperAttr"
  51. scissorsAttr = "scissorsAttr"
  52. initGame :: IO (Space2 CellState)
  53. initGame = do
  54. rng <- getStdGen
  55. return $ createRandSpace2 rng