Class: LifeGame::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/kaki-lifegame.rb

Constant Summary collapse

MG =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeField

Returns a new instance of Field.



8
9
10
11
12
# File 'lib/kaki-lifegame.rb', line 8

def initialize
  clear
  @step = @stored_step = 0
  @reserved = copy
end

Instance Attribute Details

#stepObject (readonly)

Returns the value of attribute step.



13
14
15
# File 'lib/kaki-lifegame.rb', line 13

def step
  @step
end

Instance Method Details

#alive_cells(x, y) ⇒ Object



41
42
43
44
45
# File 'lib/kaki-lifegame.rb', line 41

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

#clearObject



79
80
81
# File 'lib/kaki-lifegame.rb', line 79

def clear
  @field = Array.new(Height + MG * 2) {Array.new(Width + MG * 2, 0)}
end

#copyObject



57
58
59
60
61
62
63
# File 'lib/kaki-lifegame.rb', line 57

def copy
  copied = new_field
  each_cell do |x, y|
    copied[y + MG][x + MG] = get_cell(x, y)
  end
  copied
end

#countObject



100
101
102
# File 'lib/kaki-lifegame.rb', line 100

def count
  @step += 1
end

#each_cellObject



47
48
49
50
51
# File 'lib/kaki-lifegame.rb', line 47

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

#get_cell(x, y) ⇒ Object



23
24
25
# File 'lib/kaki-lifegame.rb', line 23

def get_cell(x, y)
  @field[y + MG][x + MG]
end

#load(file_name) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/kaki-lifegame.rb', line 83

def load(file_name)
  @field = []
  open(file_name, "r") do |io|
    @step = io.gets.chomp.scan(/\d+/)[0].to_i
    io.each_line do |line|
      @field << line.chomp.split(",").map(&:to_i)
    end
  end
end

#new_fieldObject



53
54
55
# File 'lib/kaki-lifegame.rb', line 53

def new_field
  Array.new(Height + MG * 2) {Array.new(Width + MG * 2, 0)}
end

#nextObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kaki-lifegame.rb', line 27

def next
  @before = copy
  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

#preserveObject



69
70
71
72
# File 'lib/kaki-lifegame.rb', line 69

def preserve
  @reserved = copy
  @stored_step = @step
end

#renewal?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/kaki-lifegame.rb', line 65

def renewal?(x, y)
  @before[y + MG][x + MG] != get_cell(x, y)
end

#reset_cell(x, y) ⇒ Object



19
20
21
# File 'lib/kaki-lifegame.rb', line 19

def reset_cell(x, y)
  @field[y + MG][x + MG] = 0
end

#restoreObject



74
75
76
77
# File 'lib/kaki-lifegame.rb', line 74

def restore
  @field = @reserved
  @step = @stored_step
end

#save(file_name) ⇒ Object



93
94
95
96
97
98
# File 'lib/kaki-lifegame.rb', line 93

def save(file_name)
  open(file_name, "w") do |io|
    io.puts "Step : #{@step}"
    @field.each {|x| io.puts x.map(&:to_s).join(",")}
  end
end

#set_cell(x, y) ⇒ Object



15
16
17
# File 'lib/kaki-lifegame.rb', line 15

def set_cell(x, y)
  @field[y + MG][x + MG] = 1
end

#step_resetObject



104
105
106
# File 'lib/kaki-lifegame.rb', line 104

def step_reset
  @step = 0
end