Skip to content

Instantly share code, notes, and snippets.

@luisguzman02
Forked from netmute/README.md
Created March 28, 2014 22:50
Show Gist options
  • Select an option

  • Save luisguzman02/9844613 to your computer and use it in GitHub Desktop.

Select an option

Save luisguzman02/9844613 to your computer and use it in GitHub Desktop.

Revisions

  1. @netmute netmute revised this gist Sep 19, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion experimentation.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Small rewrite of the original code to support independent x and y values.
    # Doesn't fit in 140 chars anymore thought.
    # Doesn't fit in 140 chars anymore, though.
    #
    life = lambda do |grid, x, y|
    (0..x*y-1).map do |i|
  2. Simon Ernst revised this gist Feb 9, 2012. 3 changed files with 6 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion conway.rb
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    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]}}
    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]}}
    4 changes: 3 additions & 1 deletion demonstration.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # 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
    @@ -13,4 +15,4 @@
    puts
    end
    sleep 0.1
    end
    end
    2 changes: 2 additions & 0 deletions expanded.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Expanded version for better readability.
    #
    life = lambda do |grid, size|
    (0..size*size-1).map do |i|
    lambda do |neighbours|
  3. Simon Ernst revised this gist Feb 9, 2012. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions experimentation.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    # Small rewrite of the original code to support independent x and y values.
    # Doesn't fit in 140 chars anymore thought.
    #
    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
  4. Simon Ernst revised this gist Feb 9, 2012. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions expanded.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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
  5. Simon Ernst revised this gist Feb 7, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,4 @@ Author

    Created by Simon Ernst ([@sier](http://twitter.com/sier)).

    (Thanks to ([@aemkei](http://twitter.com/aemkei)) for feedback and inspiration!)
    Thanks to [@aemkei](http://twitter.com/aemkei) for feedback and inspiration!
  6. Simon Ernst revised this gist Feb 7, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,5 @@ Author
    ------

    Created by Simon Ernst ([@sier](http://twitter.com/sier)).

    (Thanks to ([@aemkei](http://twitter.com/aemkei)) for feedback and inspiration!)
  7. Simon Ernst created this gist Feb 7, 2012.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    Game of Life
    ============

    An implementation of Conway's [Game of Life](http://en.wikipedia.org/wiki/Conway's_Game_of_Life) in 140 characters of Ruby.

    Author
    ------

    Created by Simon Ernst ([@sier](http://twitter.com/sier)).
    (Thanks to ([@aemkei](http://twitter.com/aemkei)) for feedback and inspiration!)
    1 change: 1 addition & 0 deletions conway.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    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]}}
    16 changes: 16 additions & 0 deletions demonstration.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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