Skip to content

Instantly share code, notes, and snippets.

@vickychijwani
Last active November 29, 2016 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vickychijwani/f919102b0ec73e8f1bd559e804051b71 to your computer and use it in GitHub Desktop.
Save vickychijwani/f919102b0ec73e8f1bd559e804051b71 to your computer and use it in GitHub Desktop.
Conway's Game of Life in Eve

Conway's Game of Life

Data

commit
  [#ticker]

  [#cell x: 1, y: 1, alive: false]
  [#cell x: 2, y: 1, alive: false]
  [#cell x: 3, y: 1, alive: false]

  [#cell x: 1, y: 2, alive: true]
  [#cell x: 2, y: 2, alive: true]
  [#cell x: 3, y: 2, alive: true]

  [#cell x: 1, y: 3, alive: false]
  [#cell x: 2, y: 3, alive: false]
  [#cell x: 3, y: 3, alive: false]

Grid

commit @browser
  [#svg #grid, viewBox: "0 0 100 100", width: 200, height: 200, sort: 0]
  [#div #ticker]

Draw cells

search @session @browser
  grid = [#grid]
  cell = [#cell, x, y]
  color = if cell.alive = true then "#000" else "#fff"

bind @browser
  grid.children += [#rect, x: 1+(x * 10), y: 1+(10 * y), width: 9, height: 9, fill: color]

Tick

search
  [#time seconds]
  ticker = [#ticker not(ready)]

commit
  ticker += #ready

Update the game on the next tick.

Rules:

  • Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  • Any live cell with two or three live neighbours lives on to the next generation.
  • Any live cell with more than three live neighbours dies, as if by over-population.
  • Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
search @session @browser
  ticker = [#ticker #ready]

  // find out how many alive neighbours each cell has
  cell = [#cell]
  neighbour = [#cell]
  neighbour.x >= cell.x - 1
  neighbour.x <= cell.x + 1
  neighbour.y >= cell.y - 1
  neighbour.y <= cell.y + 1
  neighbour.alive = true
  neighbour != cell
  alive-neighbours = count[given: neighbour, per: cell]

  // game rules
  next-alive = if alive-neighbours < 2 then false
    else if alive-neighbours > 3 then false
    else if alive-neighbours = 2 then cell.alive
    else if alive-neighbours = 3 then true

commit
  ticker -= #ready
  cell.alive := next-alive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment