about summary refs log tree commit diff stats
path: root/life.txt
blob: f0791e47f7d4584b6947b4be1970e027cfe820b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Conway's Game of Life

I like to implement Conway's Game of Life. Here are some notes on my general approach to doing that. 

## Rules

- The universe is an infinate 2d plain of equally sized cells
- The univsrse's state exists as discrete snap-shots of time, 1 snap-shot is a generation
- Every cell has exactly 2 states, live and dead
- A cell must be in exactly 1 state at a time, either living or dead
- A cell's neighbors are considered to be all adjacent cells
- Any cell with less than 2 live neighbors dies
- Any cell with 2 or 3 live neighbors continues to the next generation
- Any cell with more than 3 live neighbor dies
- Any dead cell with 3 neighbors becomes a living cell

## Implementation

- Define the world as a 2d grid, typically an array
- Function to dislay the grid
- Function to count living neighbors
- Function to apply the rules of life to each cell
- Function to bundle everything together, this takes in the initial state as well as the number of generations to run
.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
type timespec {
  tv_sec: int
  tv_nsec: int
}

# TODO: y2038
fn time -> secs/eax: int {
  var t: timespec
  var clock/ebx: int <- copy 0  # CLOCK_MONOTONIC
  var t-addr/ecx: (addr timespec) <- address t
  syscall_clock_gettime
  var t-secs-addr/ecx: (addr int) <- get t-addr, tv_sec
  secs <- copy *t-secs-addr
}

fn ntime -> nsecs/eax: int {
  var t: timespec
  var clock/ebx: int <- copy 0  # CLOCK_MONOTONIC
  var t-addr/ecx: (addr timespec) <- address t
  syscall_clock_gettime
  var t-nsecs-addr/ecx: (addr int) <- get t-addr, tv_nsec
  nsecs <- copy *t-nsecs-addr
}

# nsecs must be less than 999999999 or 0x3b9ac9ff nanoseconds
fn sleep secs: int, nsecs: int {
  var t: timespec
  # initialize t
  var tmp/eax: (addr int) <- get t, tv_sec
  var tmp2/ecx: int <- copy secs
  copy-to *tmp, tmp2
  tmp <- get t, tv_nsec
  tmp2 <- copy nsecs
  copy-to *tmp, tmp2
  # perform the syscall
  var t-addr/ebx: (addr timespec) <- address t
  var rem-addr/ecx: (addr timespec) <- copy 0
  syscall_nanosleep
}