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.



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

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

Instance Attribute Details

#heightObject

Returns the value of attribute height.



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

def height
  @height
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#stepObject (readonly)

Returns the value of attribute step.



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

def step
  @step
end

#widthObject

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#alive_cells(x, y) ⇒ Object



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

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



123
124
125
126
127
# File 'lib/kaki-lifegame.rb', line 123

def back
  return if @store.empty?
  @field = @store.pop
  @step -= 1
end

#change_window_size(size, f = true) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/kaki-lifegame.rb', line 129

def change_window_size(size, f = true)
  return false if @size == size
  @size = size
  @width, @height = (@size == :small) ? [70, 50] : [85, 60]
  clear if f
  @step = @stored_step = 0
  @reserved = copy
  @store = []
  true
end

#clearObject



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

def clear
  @field = new_field
end

#copyObject



62
63
64
65
66
67
68
# File 'lib/kaki-lifegame.rb', line 62

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

#countObject



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

def count
  @step += 1
end

#each_cellObject



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

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



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

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

#load(file_name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kaki-lifegame.rb', line 88

def load(file_name)
  size = nil
  open(file_name, "r") do |io|
    m = /^Size : \((.+)\)$/.match(io.gets.chomp)
    unless m
      puts "読み込みエラーです"
      break
    end
    @field = []
    size = m[1].to_sym
    @step = io.gets.chomp.scan(/\d+/)[0].to_i
    io.each_line do |line|
      @field << line.chomp.split(",").map(&:to_i)
    end
  end
  return false unless size
  change_window_size(size, false)
end

#new_fieldObject



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

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

#nextObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kaki-lifegame.rb', line 30

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



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

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

#renewal?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#reset_cell(x, y) ⇒ Object



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

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

#restoreObject



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

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

#save(file_name) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/kaki-lifegame.rb', line 107

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



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

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

#step_resetObject



119
120
121
# File 'lib/kaki-lifegame.rb', line 119

def step_reset
  @step = 0
end