Skip to content

LucZZ/haskell-vs-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

haskell-vs-go: A Functional Perspective on Conway's Game of Life

This project explores the functional programming aspects of Haskell and Go by implementing Conway's Game of Life in both languages.

Project Overview

  • Haskell Implementation:

    • Built with Cabal for build and package management and Gloss for UI elements
  • Go Implementation:

Program Structure

Overview

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.

Flow

GUI

  • 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.

GameOfLife logic

  1. 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
  1. 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
  1. Flowdiagram

How to Build and Run

Haskell (src\haskell)

  1. Ensure GHC and Cabal is installed
  2. Build the project:
    cabal build
  3. Run the project:
    cabal run
  4. Usage:
    • Left click to place or delete a cell
    • Space bar to resume or pause the simulation
  5. There is a known bug for windows on "cabal run":
    haskell: user error (unknown GLUT entry glutInit)
    
    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
  6. Run tests (only the GameOfLife.hs file is unit tested)
    cabal test

Go (src\go)

  1. Ensure Go is installed
  2. Run the project:
go run .
  1. Build and run the project
go build
.\haskell-vs-go.exe
  1. Usage:
    • Left click to place or delete a cell
    • Space bar to resume or pause the simulation
  2. Run tests (only the gameOfLife.go file is unit tested)
go test -v

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published