-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> "◻" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.