This project explores the functional programming aspects of Haskell and Go by implementing Conway's Game of Life in both languages.
-
Haskell Implementation:
-
Go Implementation:
- Build with Go and Ebitenengine V2 for UI elements
The each project is divided into the same three modules:
- const.go, Const.hs: Defines constants used across the application, such as grid size, row and column count, and other configurations.
- gameOfLife.go, GameOfLife.hs: Contains the game logic for computing the next state of the grid based on the previous grid and the rules of Conway's Game of Life.
- main.go, main.hs: Handles the user interface and user input.
- Each GameEngine provides similar methods for the gameflow.
- The main method is used to setup the initial GameState and the necessary components for the UI
- The update method is used to get the new GameState from the gamelogic module.
- The draw method is used to translate the GameState to the UI.
- Key and Mouse presses are handled differently: In Haskell the method handleKeys is used for both. In Go the gameengine provides methods (ebiten.IsKeyPressed(ebiten.KeySpace), ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft)) for checking. These are used in the update method.
- Rules:
- Dead -> Alive: 3 neighbors
- Dead -> Dead: more or less that 3 neighbors
- Alive -> Alive: 2 or 3 neighbors
- Alive -> Dead: less that 2 or more that 3 neighbors
- Extra rule: On the grid edges the neighbors are always dead to provide a natural border
- Flow
- gameStep method gets the current alive neighbors as x and y coordinates at parameters and returns a list of the coordinates of the next generation.
- in gameStep the neighbors are counted from (x-1 | y-1) to (x+1 | y+1) and based on the rules the next generation is calculated
- Flowdiagram
- Ensure GHC and Cabal is installed
- Build the project:
cabal build
- Run the project:
cabal run
- Usage:
- Left click to place or delete a cell
- Space bar to resume or pause the simulation
- There is a known bug for windows on "cabal run":
This happens because GLUT is not correctly configured by Cabal and Gloss. A easy solution for Windows this is to place the "glut32.dll" found in dll\glut32.dll in C:\Windows\System32
haskell: user error (unknown GLUT entry glutInit)
- Run tests (only the GameOfLife.hs file is unit tested)
cabal test
- Ensure Go is installed
- Run the project:
go run .
- Build and run the project
go build
.\haskell-vs-go.exe
- Usage:
- Left click to place or delete a cell
- Space bar to resume or pause the simulation
- Run tests (only the gameOfLife.go file is unit tested)
go test -v