Skip to content

Commit

Permalink
Current progress
Browse files Browse the repository at this point in the history
Not working yet
  • Loading branch information
afarinetti committed Oct 27, 2024
1 parent 0265267 commit 3213ba2
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 6 deletions.
3 changes: 2 additions & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "gameoflife_gleam"
name = "gameoflife"
version = "1.0.0"

description = "Conway's Game of Life implemented in Gleam"
Expand All @@ -7,6 +7,7 @@ repository = { type = "github", user = "afarinetti", repo = "https://github.com/

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
fmglee = ">= 1.0.0 and < 2.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
2 changes: 2 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# You typically do not need to edit this file

packages = [
{ name = "fmglee", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "fmglee", source = "hex", outer_checksum = "8EF1D688382982A134A3F4702F8D5ACF2F3C13F7F3E035893CD25EEABD29B49E" },
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
]

[requirements]
fmglee = { version = ">= 1.0.0 and < 2.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
25 changes: 25 additions & 0 deletions src/cell.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub type Cell {
Alive
Dead
}

pub fn from_bool(state: Bool) -> Cell {
case state {
False -> Dead
True -> Alive
}
}

pub fn to_bool(state: Cell) -> Bool {
case state {
Alive -> True
Dead -> False
}
}

pub fn to_string(cell: Cell) -> String {
case cell {
Alive -> "◼"
Dead -> "◻"
}
}
43 changes: 43 additions & 0 deletions src/conway_sim.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import gleam/io
import grid

pub type ConwaySim {
ConwaySim(grid: grid.Grid, generation: Int)
}

pub fn new(num_rows: Int, num_cols: Int) -> ConwaySim {
ConwaySim(grid.new(num_rows, num_cols), 0)
}

pub fn new_from_grid(grid: grid.Grid) -> ConwaySim {
ConwaySim(grid, 0)
}

pub fn neighbor_count(this: ConwaySim, row: Int, col: Int) -> Int {
// 0 1 2
// 3 X 4
// 5 6 7

let neighbors = []

case row, col {
// check the top left neighbor
row, col if row > 0 && col > 0 -> {
io.debug("top left")
}

// check the top center neighbor
row, _ if row > 0 -> {
io.debug("top center")
}

// check the top right neighbor
row, col if row > 0 && col + 1 < this.grid.num_cols -> {
io.debug("top right")
}

_, _ -> io.debug("todo")
}

0
}
14 changes: 14 additions & 0 deletions src/gameoflife.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import cell
import conway_sim
import grid

pub fn main() {
let grid1 =
grid.new(10, 10)
|> grid.set(0, 0, cell.Alive)
|> grid.set(5, 5, cell.Alive)
|> grid.display

let game = conway_sim.new_from_grid(grid1)
conway_sim.neighbor_count(game, 0, 0)
}
5 changes: 0 additions & 5 deletions src/gameoflife_gleam.gleam

This file was deleted.

58 changes: 58 additions & 0 deletions src/grid.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import cell
import gleam/io
import gleam/iterator
import gleam/set

pub type Grid {
Grid(num_rows: Int, num_cols: Int, grid: set.Set(Int))
}

pub fn new(num_rows: Int, num_cols: Int) -> Grid {
Grid(num_rows, num_cols, set.new())
}

fn cell_to_index(this: Grid, row: Int, col: Int) -> Int {
row * this.num_cols + col
}

pub fn get(this: Grid, row: Int, col: Int) -> cell.Cell {
let index = cell_to_index(this, row, col)
cell.from_bool(set.contains(this.grid, index))
}

pub fn set(this: Grid, row: Int, col: Int, state: cell.Cell) -> Grid {
let index = cell_to_index(this, row, col)
let grid = case state {
cell.Alive -> set.insert(this.grid, index)
cell.Dead -> set.delete(this.grid, index)
}
Grid(num_rows: this.num_rows, num_cols: this.num_cols, grid: grid)
}

pub fn is_cell_alive(this: Grid, row: Int, col: Int) -> Bool {
let index = cell_to_index(this, row, col)
set.contains(this.grid, index)
}

pub fn is_any_cell_alive(this: Grid) -> Bool {
set.size(this.grid) > 0
}

pub fn to_iterator(this: Grid) -> iterator.Iterator(#(Int, cell.Cell)) {
iterator.range(0, { this.num_rows * this.num_cols } - 1)
|> iterator.map(fn(idx) {
#(idx, cell.from_bool(set.contains(this.grid, idx)))
})
}

pub fn display(this: Grid) -> Grid {
to_iterator(this)
|> iterator.each(fn(tuple) {
io.print(cell.to_string(tuple.1))
case tuple.0 {
idx if { idx + 1 } % this.num_cols == 0 -> io.println("")
_ -> Nil
}
})
this
}
18 changes: 18 additions & 0 deletions src/operation.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cell
import fmglee

pub type Operation {
Operation(row: Int, col: Int, state: cell.Cell)
}

pub fn new(row: Int, col: Int, state: cell.Cell) {
Operation(row, col, state)
}

pub fn to_string(op: Operation) -> String {
fmglee.new("Operation[row: %d, col: %d, state: '%s']")
|> fmglee.d(op.row)
|> fmglee.d(op.col)
|> fmglee.s(cell.to_string(op.state))
|> fmglee.build
}
File renamed without changes.

0 comments on commit 3213ba2

Please sign in to comment.