Skip to content

Commit

Permalink
First pass on apply_rules() function
Browse files Browse the repository at this point in the history
  • Loading branch information
afarinetti committed Nov 19, 2024
1 parent d774cc7 commit 97df2a8
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions src/conway_sim.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import cell
import gleam/list
import gleam/option
import grid
import operation

pub type ConwaySim {
ConwaySim(grid: grid.Grid, generation: Int)
Expand Down Expand Up @@ -41,14 +44,77 @@ pub fn neighbor_count(this: ConwaySim, row: Int, col: Int) -> Int {
// filter out neighbors that are not alive
neighbors
|> list.filter(fn(pos) { is_valid_position(this, pos) })
|> list.filter(fn(pos) {
let #(r, c) = pos
grid.is_cell_alive(this.grid, r, c)
})
|> list.filter(fn(pos) { grid.is_cell_alive(this.grid, pos.0, pos.1) })
|> list.length
}

fn is_valid_position(this: ConwaySim, pos: #(Int, Int)) -> Bool {
let #(row, col) = pos
row >= 0 && row < this.grid.num_rows && col >= 0 && col < this.grid.num_cols
}

fn apply_rules(
this: ConwaySim,
row: Int,
col: Int,
) -> option.Option(operation.Operation) {
let neighbor_count = neighbor_count(this, row, col)
let alive = grid.is_cell_alive(this.grid, row, col)

// case alive {
// // RULES FOR LIVE CELLS //////////////////////////////////////////
// True -> {
// case neighbor_count {
// // rule 1: any live cell with fewer than two live neighbors dies,
// // as if caused by under-population.
// c if c < 2 -> option.Some(operation.new(row, col, cell.Dead))
//
// // rule 2: any live cell with two or three live neighbors lives on
// // to the next generation.
// c if c <= 3 -> option.None
//
// // rule 3: any live cell with more than three neighbors dies, as if
// // caused by overcrowding.
// _ -> option.Some(operation.new(row, col, cell.Dead))
// }
// }
// // RULES FOR DEAD CELLS //////////////////////////////////////////
// False -> {
// case neighbor_count {
// // rule 4: any dead cell with exactly three live neighbors becomes
// // a live cell, as if by reproduction.
// c if c == 3 -> option.Some(operation.new(row, col, cell.Alive))
//
// // otherwise
// _ -> option.None
// }
// }
// }

case alive, neighbor_count {
// RULES FOR LIVE CELLS ////////////////////////////////////////////////////
// rule 1: any live cell with fewer than two live neighbors dies,
// as if caused by under-population.
True, c if c < 2 -> option.Some(operation.new(row, col, cell.Dead))

// rule 2: any live cell with two or three live neighbors lives on
// to the next generation.
True, c if c <= 3 -> option.None

// rule 3: any live cell with more than three neighbors dies, as if
// caused by overcrowding.
True, _ -> option.Some(operation.new(row, col, cell.Dead))

// RULES FOR DEAD CELLS ////////////////////////////////////////////////////
// rule 4: any dead cell with exactly three live neighbors becomes
// a live cell, as if by reproduction.
False, c if c == 3 -> option.Some(operation.new(row, col, cell.Alive))

// otherwise
False, _ -> option.None
}
}

pub fn step(this: ConwaySim) -> ConwaySim {
todo
}

0 comments on commit 97df2a8

Please sign in to comment.