Conway's Game of Life [Ruby]

Teme koje se tiču programiranja

Moderator: Urednik

Post Reply

Prijatelj foruma
Prijatelj foruma
offline
User avatar

Posts: 699
Joined: 08 May 2007, 12:39
Location: Banja Luka

Post Napisano: 17 Feb 2012, 00:28


Dvije Ruby implementacije "Igre života" britanskog matematičara Džona Konveja. http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life

1. U 140 karaktera:

Code: Select all

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+s1],g[i+s],g[i+s+1]].compact.count]}}
Sa demonstracijom:

Code: Select all

# 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
Izvor (autor) : https://gist.github.com/1761463?

2. Screencast (48 min). Malo naprednija tema s obzirom da se koristi RSpec.

http://ryanbigg.com/2011/10/screencast-pilot/



Prijatelj foruma
Prijatelj foruma
offline
User avatar

Posts: 699
Joined: 08 May 2007, 12:39
Location: Banja Luka

Post Napisano: 13 Mar 2012, 08:28


Još jedan dobar članak na temu "Game of Life" u Rubiju.

http://www.jonathan-jackson.net/life-in-a-shade-of-ruby


Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest