Skip to content

Commit

Permalink
Finished game of life
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoLR10 committed May 27, 2022
1 parent ac5b37f commit 12a919b
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 15 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import Display
import Types

main :: IO ()
main = print "Hello World!"
main = playGame gliderGrid
4 changes: 4 additions & 0 deletions h-vita.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ library
exposed-modules:
Display
GameOfLife
Games
Types
other-modules:
Paths_h_vita
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
, gloss
, matrix
default-language: Haskell2010

Expand All @@ -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
Expand All @@ -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
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ description: Please see the README on GitHub at <https://github.com/gith
dependencies:
- base >= 4.7 && < 5
- matrix
- gloss

library:
source-dirs: src
Expand Down
33 changes: 32 additions & 1 deletion src/Display.hs
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
module Display where
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
3 changes: 0 additions & 3 deletions src/GameOfLife.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 18 additions & 3 deletions src/Games.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]]
,[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]]
15 changes: 8 additions & 7 deletions src/Types.hs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 12a919b

Please sign in to comment.