Skip to content

Instantly share code, notes, and snippets.

@iamvery
Last active August 29, 2015 14:01
Show Gist options
  • Save iamvery/90047a29a68e00e07cfc to your computer and use it in GitHub Desktop.
Save iamvery/90047a29a68e00e07cfc to your computer and use it in GitHub Desktop.
Conway's Game of Life, written in io

Conway's Game of Life, written in io

Run the game

io driver.io

Run the specs

io cell_spec.io
io listext_spec.io
doFile("listext.io")
Cell := Object clone
Cell x := nil
Cell y := nil
Cell new := method(x, y,
self clone init(x,y)
)
Cell init := method(x, y,
self x = x
self y = y
self
)
Cell neighbors := method(
list(
Cell new(self x - 1, self y - 1),
Cell new(self x - 1, self y ),
Cell new(self x - 1, self y + 1),
Cell new(self x , self y - 1),
Cell new(self x , self y + 1),
Cell new(self x + 1, self y - 1),
Cell new(self x + 1, self y ),
Cell new(self x + 1, self y + 1)
)
)
Cell living_neighbors := method(grid,
neighbors map(neighbor,
if(grid containsEqual(neighbor), 1, 0)
) reduce(+)
)
Cell living := method(grid,
grid containsEqual(self)
)
Cell birth := method(grid,
living(grid) not and living_neighbors(grid) == 3
)
Cell die := method(grid,
living(grid) and (living_neighbors(grid) < 2 or living_neighbors(grid) > 3)
)
Cell == = method(other,
x == other x and y == other y
)
doFile("spec.io")
cell := nil
before = block(
cell = Cell new(0,0)
)
it("has 8 neighbors", block(
expect(cell neighbors size) to(equal(8))
))
it("has living neighbors", block(
grid := list(Cell new(1,0))
expect(cell living_neighbors(grid)) to(equal(1))
))
it("is living", block(
grid := list(Cell new(0,0))
expect(cell living(grid)) to(equal(true))
))
it("births when dead cell has 3 living neighbors", block(
cell = Cell new(0,1)
grid := list(
Cell new(1,0),
Cell new(1,1),
Cell new(1,2)
)
expect(cell birth(grid)) to(equal(true))
))
it("dies when less than 2 living neighbors", block(
grid := list(cell)
expect(cell die(grid)) to(equal(true))
))
it("dies when more than 3 living neighbors", block(
cell = Cell new(0,1)
grid := list(
cell,
Cell new(0,0),
Cell new(1,0),
Cell new(1,1),
Cell new(1,2)
)
expect(cell die(grid)) to(equal(true))
))
it("is equal to another cell with the same coords", block(
other_cell := Cell new(0,0)
expect(other_cell) to(equal(cell))
))
render := method(grid,
System system("clear")
size := 10
for(i, 0, size,
for(j, 0, size,
alive := grid containsEqual(Cell new(i,j))
if(alive,
"0" print,
"." print
)
)
"" println
)
)
blinker := list(
Cell new(0,1),
Cell new(1,1),
Cell new(2,1)
)
glider := list(
Cell new(1,0),
Cell new(2,1),
Cell new(0,2),
Cell new(1,2),
Cell new(2,2)
)
grid := glider
loop(
render(grid)
new_grid := grid clone
process := method(cell,
if(cell birth(grid),
new_grid push(cell)
)
if(cell die(grid),
new_grid remove(cell)
)
)
grid foreach(cell,
process(cell)
cell neighbors foreach(neighbor,
process(neighbor)
)
)
grid = new_grid
wait(1)
)
EqualityMatcher := Object clone
EqualityMatcher subject := nil
EqualityMatcher init := method(subject,
self subject = subject
self
)
EqualityMatcher match := method(match,
subject == match
)
Failure := Exception clone
Expectation := Object clone
Expectation subject := nil
Expectation init := method(subject,
self subject = subject
self
)
Expectation to := method(matcher,
if(matcher match(subject),
"." print,
Failure raise
)
)
List containsEqual := method(obj,
self map(v, v == obj) reduce(or)
)
doFile("listext.io")
doFile("spec.io")
it("contains equal but not same object", block(
o1 := Object clone
o2 := Object clone
expect(o1 == o2) to(equal(false))
o1 == = method(other, true)
o2 == = method(other, true)
expect(o1 == o2) to(equal(true))
list := list(o1)
expect(list containsEqual(o2)) to(equal(true))
))
before := nil
it := method(description, body,
before ?call
e := try(
body call
)
e catch(Failure,
"" println
"Failure:" println
description println
) pass
)
expect := method(subject,
Expectation clone init(subject)
)
equal := method(match,
EqualityMatcher clone init(match)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment