Skip to content

Instantly share code, notes, and snippets.

@netmute netmute/README.md
Last active Jul 28, 2019

Embed
What would you like to do?
Game of Life

Game of Life

An implementation of Conway's Game of Life in 140 characters of Ruby.

Author

Created by Simon Ernst (@sier).

Thanks to @aemkei for feedback and inspiration!

life=->g,s{(0..s*s-1).map{|i|->n{n==3||(g[i]&&n==2)||nil}[[g[i-s-1],g[i-s],g[i-s+1],g[i-1],g[i+1],g[i+s-1],g[i+s],g[i+s+1]].compact.count]}}
# The code with some animation logic for demonstration.
#
life=->g,s{(0..s*s-1).map{|i|->n{n==3||(g[i]&&n==2)||nil}[[g[i-s-1],g[i-s],g[i-s+1],g[i-1],g[i+1],g[i+s-1],g[i+s],g[i+s+1]].compact.count]}}
size = 20
grid = (1..size*size).map { rand(0..1)==1 ? 1 : nil }
while true do
system 'clear'
grid = life[grid, size]
(0..size-1).each do |y|
(0..size-1).each do |x|
print "#{(grid[x+(y*size)] ? 'O' : '.')}"
end
puts
end
sleep 0.1
end
# Expanded version for better readability.
#
life = lambda do |grid, size|
(0..size*size-1).map do |i|
lambda do |neighbours|
neighbours == 3 || ( grid[i] && neighbours == 2 )|| nil
end.call (
[
grid[i-size-1], grid[i-size], grid[i-size+1],
grid[i-1], grid[i+1],
grid[i+size-1], grid[i+size], grid[i+size+1]
].compact.count
)
end
end
# Small rewrite of the original code to support independent x and y values.
# Doesn't fit in 140 chars anymore, though.
#
life = lambda do |grid, x, y|
(0..x*y-1).map do |i|
lambda do |neighbours|
neighbours == 3 || ( grid[i] && neighbours == 2 )|| nil
end.call (
[
grid[i-x-1], grid[i-x], grid[i-x+1],
grid[i-1], grid[i+1],
grid[i+x-1], grid[i+x], grid[i+x+1]
].compact.count
)
end
end
x = 80
y = 20
grid = (1..x*y).map { rand(0..1)==1 ? 1 : nil }
while true do
system 'clear'
grid = life[grid, x, y]
(0..y-1).each do |yi|
(0..x-1).each do |xi|
print "#{(grid[xi+(yi*x)] ? 'O' : '.')}"
end
puts
end
sleep 0.1
end
@maettig

This comment has been minimized.

Copy link

maettig commented Feb 9, 2012

Just for the sake of completeness, here is Conway's Game of Life in 140byt.es JavaScript.

@Neurogami

This comment has been minimized.

Copy link

Neurogami commented Feb 11, 2012

Interesting, but the expanded version gives an error:

`rand': can't convert Range into Integer (TypeError)
@DNNX

This comment has been minimized.

Copy link

DNNX commented Feb 11, 2012

rand(a..b) does not work by default in 1.8. you should use 1.9 or replace rand(range) call with Random.new.rand(range)

@netmute

This comment has been minimized.

Copy link
Owner Author

netmute commented Feb 11, 2012

@Neurogami Directly passing ranges to rand only works in Ruby 1.9.3.
Try Random.new.rand instead.

@jablan

This comment has been minimized.

@Neurogami

This comment has been minimized.

Copy link

Neurogami commented Feb 11, 2012

Ah, thanks. I had tried it 1.9.2

@pjlsergeant

This comment has been minimized.

Copy link

pjlsergeant commented Feb 11, 2012

I put together a cute little Perl version: https://gist.github.com/1803656

@Roman2K

This comment has been minimized.

Copy link

Roman2K commented Feb 11, 2012

Reminds me of an experiment I made years ago, world:

$ git clone https://github.com/Roman2K/_world world
$ cd world
$ ruby lib/world.rb
@xem

This comment has been minimized.

Copy link

xem commented Sep 13, 2013

My attempt, in JavaScript: 130 bytes
http://xem.github.io/miniGameOfLife

@coreyward

This comment has been minimized.

Copy link

coreyward commented Sep 16, 2013

Here's an even shorter version:

require 'open-uri'
eval open('http://bit.ly/16brOlw').read

👅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.