Created
March 22, 2012 06:01
-
-
Save rondale-sc/2156522 to your computer and use it in GitHub Desktop.
life-in-a-shade-of-ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # If cell is alive then it continues to stay alive if it has | |
| # 2 or 3 alive neighbors. If the cell is dead then it comes | |
| # back to life only if it has exactly 3 alive neighbors. | |
| @alive ? [2,3].include?(alive_neighbors) : alive_neighbors == 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Cell | |
| attr_accessor :y, :x, :alive, :future_alive | |
| def initialize(x,y,alive=false) | |
| @x = x | |
| @y = y | |
| @alive = alive | |
| end | |
| def is_alive?(sum) | |
| self.future_alive = if @alive | |
| [2,3].include?(sum) | |
| else | |
| sum == 3 | |
| end | |
| end | |
| def set_next_generation | |
| self.alive = self.future_alive | |
| self.future_alive = nil | |
| end | |
| def to_s; @alive ? ' O ' : " " end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Array#new accepts a block....radical! | |
| @cells = Array.new(@height) { |y| Array.new(@width) { |x| Cell.new(x,y) }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Game | |
| def initialize(w,h,steps) | |
| @width,@height,@steps = w,h,steps | |
| @cells = Array.new(@height) { |y| Array.new(@width) { |x| Cell.new(x,y) }} | |
| @neighbors = [[-1, 0],[1, 0],[-1, 1],[0, 1],[1, 1],[-1, -1],[0, -1], [1, -1]] | |
| end | |
| def play | |
| (1..@steps).each_with_index do |i| | |
| system('clear') | |
| puts self.to_s | |
| step | |
| sleep 0.1 | |
| end | |
| end | |
| def step | |
| @cells.reverse.each do |row| | |
| row.each do |cell| | |
| cell.is_alive? alive_neighbors(cell.x, cell.y) | |
| end | |
| end | |
| @cells.each {|r| r.each {|c| c.set_next_generation }} | |
| end | |
| def alive_neighbors(x,y) | |
| @neighbors.inject(0) do |sum, (neighbor_x, neighbor_y)| | |
| sum += 1 if @cells[y + neighbor_y][x + neighbor_x].alive; sum | |
| end | |
| end | |
| def to_s | |
| @cells.reverse.map { |row| row.join }.join("\n") | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Pattern | |
| def initialize(x, y, pattern=nil) | |
| @origin_x, @origin_y = origin_x, origin_y | |
| @default_patterns = {:acorn => [[-3,0],[-2,0],[-2,2],[0,1],[1,0],[2,0],[3,0]]} | |
| @pattern = @default_patterns[pattern].nil? ? @default_patterns[:acorn] : @default_patterns[pattern] | |
| end | |
| def set_cells(cells) | |
| @pattern.each do |(x,y)| | |
| unless cells[@origin_x + x].nil? || cells[@origin_x + x][@origin_y + y].nil? | |
| cells[@origin_y + y][@origin_x + x].alive = true; | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Pattern.new(somepoint_x, somepoint_y, :acorn).set_cells(@cells) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| :glider_gun => [[0,0],[-1,0],[-1,1],[-1,-1],[-2,2],[-2,-2],[-3,0], | |
| [-4,3],[-5,3],[-4,-3],[-5,-3],[-6, 2],[-6,-2], | |
| [-7,1],[-7,0],[-7,-1],[-16,0],[-17,0],[-16,1], | |
| [-17,1],[3,3],[3,2],[3,1],[4,3],[4,2],[4,1],[5,4], | |
| [5,0],[7,4],[7,5],[7,0],[7,-1],[17,2],[17,3],[18,2], | |
| [18,3]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment