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
13
14
15
# File 'lib/kaki-lifegame.rb', line 8

def initialize
  @width, @height = Small
  @size = :small
  clear
  @step = @stored_step = 0
  @reserved = copy
  @store = []
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



17
18
19
# File 'lib/kaki-lifegame.rb', line 17

def height
  @height
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#stepObject (readonly)

Returns the value of attribute step.



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

def step
  @step
end

#widthObject

Returns the value of attribute width.



17
18
19
# File 'lib/kaki-lifegame.rb', line 17

def width
  @width
end

Instance Method Details

#alive_cells(x, y) ⇒ Object



47
48
49
50
51
# 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

#backObject



120
121
122
123
124
# 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) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# 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

#clearObject



85
86
87
# File 'lib/kaki-lifegame.rb', line 85

def clear
  @field = new_field
end

#copyObject



63
64
65
66
67
68
69
# 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

#countObject



112
113
114
# File 'lib/kaki-lifegame.rb', line 112

def count
  @step += 1
end

#each_cellObject



53
54
55
56
57
# 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) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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) ⇒ Object



27
28
29
# File 'lib/kaki-lifegame.rb', line 27

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

#load(file_name) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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_fieldObject



59
60
61
# File 'lib/kaki-lifegame.rb', line 59

def new_field
  Array.new(@height + MG * 2) {Array.new(@width + MG * 2, 0)}
end

#nextObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

#preserveObject



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

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

#renewal?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
# 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) ⇒ Object



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

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

#restoreObject



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

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

#save(file_name) ⇒ Object



104
105
106
107
108
109
110
# 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) ⇒ Object



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

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

#step_resetObject



116
117
118
# File 'lib/kaki-lifegame.rb', line 116

def step_reset
  @step = 0
end