diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..c1322dc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = false \ No newline at end of file diff --git a/README.md b/README.md index 790e533..10e0cf8 100644 --- a/README.md +++ b/README.md @@ -1 +1,27 @@ # h-vita + +This is the [game of life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) implemented in Haskell. + +## External Dependencies + +- [Data.Matrix](https://hackage.haskell.org/package/matrix-0.3.6.1/docs/Data-Matrix.html) +- [Graphics.Gloss](https://hackage.haskell.org/package/gloss) + +## How to use run the Game + +To execute the game you do: + +`stack run` + +If you want to see a different game (not the glider one), open the REPL and use: + +`playGame OTHER_GAME` + +## Developers + +- EduardoLR10 +- ribeirotomas1904 + +## Dr.Nekoma + +Builded live on [twitch](https://www.twitch.tv/drnekoma) and archived on [youtube](https://www.youtube.com/channel/UCMyzdYsPiBU3xoqaOeahr6Q) \ No newline at end of file diff --git a/app/Main.hs b/app/Main.hs index 3d3b949..a19896b 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -6,4 +6,4 @@ import Display import Types main :: IO () -main = print "Hello World!" +main = playGame gliderGrid diff --git a/h-vita.cabal b/h-vita.cabal index d610091..ad8173b 100644 --- a/h-vita.cabal +++ b/h-vita.cabal @@ -27,6 +27,7 @@ library exposed-modules: Display GameOfLife + Games Types other-modules: Paths_h_vita @@ -34,6 +35,7 @@ library src build-depends: base >=4.7 && <5 + , gloss , matrix default-language: Haskell2010 @@ -46,6 +48,7 @@ executable h-vita-exe ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base >=4.7 && <5 + , gloss , h-vita , matrix default-language: Haskell2010 @@ -60,6 +63,7 @@ test-suite h-vita-test ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base >=4.7 && <5 + , gloss , h-vita , matrix default-language: Haskell2010 diff --git a/package.yaml b/package.yaml index 78b58b6..46bc9fc 100644 --- a/package.yaml +++ b/package.yaml @@ -22,6 +22,7 @@ description: Please see the README on GitHub at = 4.7 && < 5 - matrix +- gloss library: source-dirs: src diff --git a/src/Display.hs b/src/Display.hs index f845d0e..2457647 100644 --- a/src/Display.hs +++ b/src/Display.hs @@ -1 +1,32 @@ -module Display where \ No newline at end of file +module Display where + +import Data.Matrix +import Graphics.Gloss +import Graphics.Gloss.Interface.Pure.Simulate +import GameOfLife +import Games +import Types + +render :: DisplayCellSize -> Grid -> Picture +render (width, height) grid = Pictures $ toList $ mapPos f grid + where + xAxis column width = (fromIntegral column) * width + borderSize * (fromIntegral column) + yAxis row height = (* (-1)) $ (fromIntegral row) * height + borderSize * (fromIntegral row) + borderSize = 2 + f (row, column) cell + | cell == dead = Blank + | otherwise = Translate (xAxis column width) (yAxis row height) + $ rectangleSolid width height + +advanceGame :: ViewPort -> Float -> Grid -> Grid +advanceGame _ _ = advanceGridGeneration + +playGame :: Grid -> IO () +playGame game + = simulate + (InWindow "h-vita" (800, 600) (10, 10)) -- Setting up window for the game + white -- Background color of the board + 2 -- Number of steps per second + game -- Initial state of the game + (render (10.0, 10.0)) -- Function to convert grid to a Picture + advanceGame -- Function to advance generation of the grid diff --git a/src/GameOfLife.hs b/src/GameOfLife.hs index e63d968..ccc826e 100644 --- a/src/GameOfLife.hs +++ b/src/GameOfLife.hs @@ -5,12 +5,9 @@ import Data.Matrix import Data.Maybe import Data.List - getCell :: Grid -> Location -> Maybe Cell getCell grid (row, column) = safeGet row column grid --- getNeighbours - topLeft :: Location -> Location topLeft (i, j) = (i - 1, j - 1) diff --git a/src/Games.hs b/src/Games.hs index 04a5a67..8663443 100644 --- a/src/Games.hs +++ b/src/Games.hs @@ -4,9 +4,9 @@ import Types import Data.Matrix testGrid :: Grid -testGrid = fromLists [ [dead, dead, dead] +testGrid = fromLists [ [alive, dead, dead] ,[dead, alive, dead] - ,[dead, dead, dead]] + ,[dead, dead, alive]] beaconOscillatorGrid :: Grid beaconOscillatorGrid = fromLists [ [dead, dead, dead, dead, dead, dead] @@ -21,4 +21,19 @@ blinkerOscillatorGrid = fromLists [ [dead, dead, dead, dead, dead] ,[dead, dead, alive, dead, dead] ,[dead, dead, alive, dead, dead] ,[dead, dead, alive, dead, dead] - ,[dead, dead, dead, dead, dead]] \ No newline at end of file + ,[dead, dead, dead, dead, dead]] + +gliderGrid :: Grid +gliderGrid = fromLists [ [dead, alive, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, alive, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[alive, alive, alive, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead] + ,[dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead, dead]] \ No newline at end of file diff --git a/src/Types.hs b/src/Types.hs index d715c15..792684a 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -1,10 +1,11 @@ module Types where import Data.Matrix -type Location = (Int, Int) -- Cell's position on the Grid -type NumberOfNeighbours = Int -- The size of the Grid -type Cell = Bool -- Cell -alive = True -dead = False -type Neighbours = [Cell] -- Cell -type Grid = Matrix Cell -- The board +type Location = (Int, Int) -- Cell's position on the Grid +type NumberOfNeighbours = Int -- The number of neighbours for a given cell +type Cell = Bool -- Cell +alive = True -- State for alive cell +dead = False -- State for dead Cell +type Neighbours = [Cell] -- Alias for cell neighbours +type Grid = Matrix Cell -- The game board +type DisplayCellSize = (Float, Float) -- Size of cell for display