class GameOfLifeGto::Game
Attributes
board[R]
Public Class Methods
new(board)
click to toggle source
# File lib/game_of_life_gto.rb, line 55 def initialize(board) @board = board @temp_board = Marshal::load(Marshal.dump(board)) @temp_board.reset end
Public Instance Methods
play(loop_size)
click to toggle source
# File lib/game_of_life_gto.rb, line 61 def play(loop_size) loop_size.times do thick @board = Marshal::load(Marshal.dump(@temp_board)) @board.display @temp_board.reset sleep 0.5 system('clear') end end
thick()
click to toggle source
# File lib/game_of_life_gto.rb, line 72 def thick @board.array.each_with_index do |row, y| row.each_with_index do |cell, x| neighborhood = get_neighborhood(x, y) if cell.is_alive? if deserves_to_die?(neighborhood) @temp_board.array[y][x].dies else @temp_board.array[y][x].id = 1 end else if deserves_to_revive?(neighborhood) @temp_board.array[y][x].resuscitates else @temp_board.array[y][x].id = 0 end end end end end
Private Instance Methods
deserves_to_die?(neighborhood)
click to toggle source
# File lib/game_of_life_gto.rb, line 125 def deserves_to_die?(neighborhood) points = 0 neighborhood.each { |cell| points += 1 if cell.is_alive? } return true if points < 2 || points > 3 return false if points == 2 || points == 3 end
deserves_to_revive?(neighborhood)
click to toggle source
# File lib/game_of_life_gto.rb, line 132 def deserves_to_revive?(neighborhood) points = 0 neighborhood.each { |cell| points += 1 if cell.is_alive? } return true if points == 3 return false end
get_neighborhood(x, y)
click to toggle source
# File lib/game_of_life_gto.rb, line 94 def get_neighborhood(x, y) neighborhood = Array.new coordinates = { x: x - 1, y: y - 1 } 3.times do 3.times do if outter_coordinates?(coordinates) neighborhood << Cell.new(0) else if coordinates[:x] == x && coordinates[:y] == y coordinates[:x] += 1 next end neighborhood.push(board.array[coordinates[:y]][coordinates[:x]]) end coordinates[:x] += 1 end coordinates[:x] = x -1 coordinates[:y] += 1 end return neighborhood end
outter_coordinates?(coordinates)
click to toggle source
# File lib/game_of_life_gto.rb, line 117 def outter_coordinates?(coordinates) if coordinates[:x] < 0 || coordinates[:x] > board.width - 1 || coordinates[:y] < 0 || coordinates[:y] > board.height - 1 return true end return false end