-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.rb
64 lines (51 loc) · 1.31 KB
/
Game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require("singleton.rb")
require("./Cluster.rb")
require("./Cell.rb")
class Game
include Singleton
attr_accessor :cell_matrix
attr_accessor :clusters
def start(matrix_size = 5)
@cell_matrix = Hash.new
@clusters = []
@cycle_born_clusters = []
@cycle_dead_clusters = []
matrix_side = (1..matrix_size).to_a
matrix_keys = matrix_side.product(matrix_side)
matrix_keys.collect do |index|
cell = Cell.new(10)
@cell_matrix[index] = cell
end
matrix_keys.collect do |index|
x, y = index
cell = @cell_matrix[index]
[-1,0,1].product([-1,0,1]).collect do |displacement|
dx, dy = displacement
if (dx == 0 or dy == 0) and dx != dy
adjacent_index = [x+dx,y+dy]
if @cell_matrix.key?(adjacent_index)
cell.add_adjacent(@cell_matrix[adjacent_index])
end
end
end
end
end
def register_cluster(cluster)
@cycle_born_clusters.push(cluster)
end
def unregister_cluster(cluster)
@cycle_dead_clusters.push(cluster)
end
def cycle
@clusters.each do |cluster|
cluster.cycle
end
puts @clusters.size
@clusters = @clusters.concat(@cycle_born_clusters)
@cycle_dead_clusters.each do |cluster|
@clusters.delete(cluster)
end
@cycle_born_clusters = []
@cycle_dead_clusters = []
end
end