class LifeGame::Field

Constants

Large
MG

Attributes

height[RW]
size[R]
step[R]
width[RW]

Public Class Methods

new() click to toggle source
# File lib/kaki-lifegame.rb, line 8
def initialize
  @width, @height = Small
  @size = :small
  clear
  @step = @stored_step = 0
  @reserved = copy
  @store = []
end

Public Instance Methods

alive_cells(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 47
def alive_cells(x, y)
  get_cell(x - 1, y - 1) + get_cell(x, y - 1) + get_cell(x + 1, y - 1) +
    get_cell(x - 1, y) + get_cell(x + 1, y) +
    get_cell(x - 1, y + 1) + get_cell(x, y + 1) + get_cell(x + 1, y + 1)
end
back() click to toggle source
# File lib/kaki-lifegame.rb, line 120
def back
  return if @store.empty?
  @field = @store.pop
  @step -= 1
end
change_window_size(size, f = true) click to toggle source
# File lib/kaki-lifegame.rb, line 126
def change_window_size(size, f = true)
  return false if @size == size
  @size = size
  @width, @height = (@size == :small) ? Small : Large
  if f
    field_convert(@size)
  else
    @step = @stored_step = 0
  end
  @reserved = copy
  @store = []
  true
end
clear() click to toggle source
# File lib/kaki-lifegame.rb, line 85
def clear
  @field = new_field
end
copy() click to toggle source
# File lib/kaki-lifegame.rb, line 63
def copy
  copied = new_field
  each_cell do |x, y|
    copied[y + MG][x + MG] = get_cell(x, y)
  end
  copied
end
count() click to toggle source
# File lib/kaki-lifegame.rb, line 112
def count
  @step += 1
end
each_cell() { |x| ... } click to toggle source
# File lib/kaki-lifegame.rb, line 53
def each_cell
  (@height + (MG - 1) * 2).times do |y|
    (@width + (MG - 1) * 2).times {|x| yield(x - MG + 1, y - MG + 1)}
  end
end
field_convert(size) click to toggle source
# File lib/kaki-lifegame.rb, line 140
def field_convert(size)
  mg_w = (Large[0] - Small[0]) / 2
  mg_h = (Large[1] - Small[1]) / 2
  a = - MG + 1
  converted_field = new_field
  
  (Small[1] + (MG - 1) * 2).times do |y|
    (Small[0] + (MG - 1) * 2).times do |x|
      if size == :small
        converted_field[y + 1][x + 1] = 1 if get_cell(x + a + mg_w, y + a + mg_h) == 1
      else
        converted_field[y + 1 + mg_h][x + 1 + mg_w] = 1 if get_cell(x + a, y + a) == 1
      end
    end
  end
  
  @field = converted_field
end
get_cell(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 27
def get_cell(x, y)
  @field[y + MG][x + MG]
end
load(file_name) click to toggle source
# File lib/kaki-lifegame.rb, line 89
def load(file_name)
  size = nil
  
  open(file_name, "r") do |io|
    size = /^Size : \((.+)\)$/.match(io.gets.chomp)[1].to_sym
    @field = []
    @step = io.gets.chomp.scan(/\d+/)[0].to_i
    io.each_line do |line|
      @field << line.chomp.split(",").map(&:to_i)
    end
  end
  
  change_window_size(size, false)
end
new_field() click to toggle source
# File lib/kaki-lifegame.rb, line 59
def new_field
  Array.new(@height + MG * 2) {Array.new(@width + MG * 2, 0)}
end
next() click to toggle source
# File lib/kaki-lifegame.rb, line 31
def next
  @before = copy
  @store << @before
  @store.shift if @store.size > 20
  nxf = new_field
  each_cell do |x, y|
    n = alive_cells(x, y)
    if get_cell(x, y).zero?
      nxf[y + MG][x + MG] = 1 if n == 3
    else
      nxf[y + MG][x + MG] = 1 if n == 2 or n == 3
    end
  end
  @field = nxf
end
preserve() click to toggle source
# File lib/kaki-lifegame.rb, line 75
def preserve
  @reserved = copy
  @stored_step = @step
end
renewal?(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 71
def renewal?(x, y)
  @before[y + MG][x + MG] != get_cell(x, y)
end
reset_cell(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 23
def reset_cell(x, y)
  @field[y + MG][x + MG] = 0
end
restore() click to toggle source
# File lib/kaki-lifegame.rb, line 80
def restore
  @field = @reserved
  @step = @stored_step
end
save(file_name) click to toggle source
# File lib/kaki-lifegame.rb, line 104
def save(file_name)
  open(file_name, "w") do |io|
    io.puts "Size : (#{@size.to_s})"
    io.puts "Step : #{@step}"
    @field.each {|x| io.puts x.map(&:to_s).join(",")}
  end
end
set_cell(x, y) click to toggle source
# File lib/kaki-lifegame.rb, line 19
def set_cell(x, y)
  @field[y + MG][x + MG] = 1
end
step_reset() click to toggle source
# File lib/kaki-lifegame.rb, line 116
def step_reset
  @step = 0
end