Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import FRP.Helm
  2. import qualified FRP.Helm.Keyboard as Keyboard
  3. import qualified FRP.Helm.Window as Window
  4.  
  5. boardWidth = 7
  6. boardHeight = 6
  7.  
  8. --type Board [[Int]]
  9. {- 6x7 circles
  10. 0 - empty
  11. 1 - red
  12. 2 - yellow
  13. -}
  14. data State = State { currentColor :: Int,
  15. aimPos :: (Int, Int),
  16. board :: [[Int]] }
  17.  
  18. step :: (Int, Int) -> State -> State
  19. step (dx, dy) (State { currentColor = currentColor, aimPos = (aimx, aimy), board = board }) =
  20. State { currentColor = currentColor,
  21. aimPos = (mod (aimx + dx) boardWidth, mod (aimy + dy) boardHeight),
  22. board = board}
  23.  
  24. stateColor :: Int -> Color
  25. stateColor 0 = white
  26. stateColor 1 = red
  27. stateColor 2 = yellow
  28.  
  29. coordToForm :: Int -> Int -> (Form -> Form)
  30. coordToForm x y = move ((fromIntegral x) * 90 - 300, (fromIntegral y) * 90 - 250)
  31.  
  32. stateToForm :: Int -> Int -> Int -> Form
  33. stateToForm x y color = coordToForm x y $ filled (stateColor color) $ circle 40
  34.  
  35. aimToForm :: (Int, Int) -> Int -> Form
  36. aimToForm (x, y) color = coordToForm x y $ outlined (dashed (stateColor color)) $ square 64
  37.  
  38. stateToRenderlist :: (Int, Int) -> State -> [Form]
  39. stateToRenderlist (w,h) (State { currentColor = currentColor, aimPos = aimPos, board = board }) =
  40. concat (map (\(row, y) -> map (\(colour, x) -> stateToForm x y colour) row) enumBoard)
  41. where
  42. enumBoard = zip (map (\x -> zip x [0..]) board) [0..]
  43.  
  44. render :: (Int, Int) -> State -> Element
  45. render (w, h) (State { currentColor = currentColor, aimPos = aimPos, board = board }) =
  46. centeredCollage w h ((stateToRenderlist (w,h) (State { currentColor = currentColor, aimPos = aimPos, board = board }))
  47. ++[aimToForm aimPos currentColor])
  48.  
  49. main :: IO ()
  50. main = run defaultConfig $ render <~ Window.dimensions ~~ stepper
  51. where
  52. state = State { currentColor = 1,
  53. aimPos = (2, 2),
  54. board = [[0,0,0,0,0,0,0],
  55. [0,0,0,0,0,0,0],
  56. [0,0,0,0,0,0,0],
  57. [0,0,0,0,0,0,0],
  58. [0,0,0,2,0,0,0],
  59. [0,1,0,1,0,2,0]]}
  60. stepper = foldp step state Keyboard.arrows--Keyboard.Number1Key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement