Skip to content

Commit

Permalink
updating documentation for cell
Browse files Browse the repository at this point in the history
  • Loading branch information
jooaf committed Aug 10, 2024
1 parent 74f24d4 commit 2447ad5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/conway/cell.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,35 @@ import gleam/function
import gleam/list
import prng/random

/// This represents if the current Cell is alive or Dead
pub type Life {
Alive
Dead
}

/// Custom Type that contains the alive and dead counts of a cell's neighbors.
pub type AliveDeadCounts {
AliveDeadCounts(alive: Int, dead: Int)
}

/// A single cell. A cell can either be either be a Cell or an InvalidCell.
/// A Cell contains its' poistion on the board, the positions of its' neighbors, and its' life state.
pub type Cell {
Cell(pos: Int, neighbors: List(Int), life: Life)
InvalidCell
}

/// Wrapper type for an Int pair.
pub type Coordinate =
#(Int, Int)

/// Helper type that is used to store coordinate for a Moore's Neighborhood.
/// For more information, please look at this wiki: https://en.wikipedia.org/wiki/Moore_neighborhood .
pub type PotentialNeighborPoint {
PotentialNeighborPoint(dx: Int, dy: Int, nx: Int, ny: Int)
}

/// Get a Cell's position on the board.
pub fn get_pos(cell: Cell) -> Int {
// if suddenly an invalid state occurs
case cell {
Expand All @@ -33,6 +41,7 @@ pub fn get_pos(cell: Cell) -> Int {
}
}

/// Get a cell's neighbors.
pub fn to_int_neighbors(cell: Cell) -> List(Int) {
// if suddenly an invalid state occurs
case cell {
Expand All @@ -42,6 +51,7 @@ pub fn to_int_neighbors(cell: Cell) -> List(Int) {
}
}

/// Check if a cell is a Cell and is not an InvalidCell.
pub fn valid(cell: Cell) -> Result(Cell, Nil) {
case cell {
Cell(p, n, l) -> Ok(Cell(p, n, l))
Expand Down Expand Up @@ -71,11 +81,13 @@ fn tail_rec_alive_dead_counts(
}
}

/// Given a Cell's neighbors, calculate the number of neighboring cells that are alive or dead.
pub fn alive_dead_counts(neighbors: List(Cell)) -> AliveDeadCounts {
let #(alive, dead) = tail_rec_alive_dead_counts(neighbors, 0, 0)
AliveDeadCounts(alive, dead)
}

/// Get a cell's current life state.
pub fn get_life_state_from_cell(cell: Cell) -> Life {
case cell {
Cell(_, _, l) -> l
Expand Down Expand Up @@ -145,6 +157,8 @@ fn moore_neighborhood(x: Int, y: Int, width: Int) -> List(Int) {
|> list.map(fn(p) { p.ny * width + p.nx })
}

/// Create a new cell by identifying its' neighbors by position
/// as well as randomly setting its Life state.
pub fn new_cell(width: Int, pos: Int) -> Cell {
let life_state =
random.int(0, 1)
Expand Down
2 changes: 2 additions & 0 deletions src/conway_life.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub fn execute(command: String, args: List(String)) -> Result(String, String) {
}

pub fn clear_output() -> Nil {
// note i am using nushell here. if you find that there are issues
// please uncomment the io.print command above Nil
io.print(execute("nu", ["-c", "clear"]) |> result.unwrap(""))
// for speed purposes i will run the string output that is generated
// from running the clear shell command via elixir
Expand Down

0 comments on commit 2447ad5

Please sign in to comment.