-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life.rb
142 lines (127 loc) · 3.7 KB
/
game_of_life.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
class Game
attr_accessor :world, :seeds
def initialize(world=World.new, seeds=[])
@world = world
@seeds = seeds
seeds.each do |seed|
world.cell_grid[seed[0]][seed[1]].alive = true
end
end
def tick!
next_round_dead = []
next_round_alive = []
@world.cells.each do |cell|
count = self.world.live_neighbors_around_cell(cell).count
if count < 2 && cell.alive?
next_round_dead << cell
end
if (count == 2 || count == 3) && cell.alive?
next_round_alive << cell
end
if count> 3 && cell.alive?
next_round_dead << cell
end
if count == 3 && cell.dead?
next_round_alive << cell
end
end
next_round_alive.each do |cell|
cell.birth!
end
next_round_dead.each do |cell|
cell.die!
end
end
end
class World
attr_accessor :rows, :cols, :cell_grid, :cells
def initialize(rows=3, cols=3)
@rows = rows
@cols = cols
@cells = []
@cell_grid = Array.new(rows) do |row|
Array.new(cols) do |col|
Cell.new(col, row)
end
end
@cell_grid.each do |row|
row.each do |element|
if element.is_a?(Cell)
cells << element
end
end
end
end
def live_neighbors_around_cell(cell)
live_neighbors = []
#neighbor to the north
if cell.y > 0
candidate = self.cell_grid[cell.y-1][cell.x]
live_neighbors << candidate if candidate.alive?
end
#neighbor to the north east
if cell.y>0 && cell.x < (@cols - 1)
candidate = self.cell_grid[cell.y-1][cell.x+1]
live_neighbors << candidate if candidate.alive?
end
#neighbor to the east
if cell.x < (@cols - 1)
candidate = self.cell_grid[cell.y][cell.x+1]
live_neighbors << candidate if candidate.alive?
end
#neighbor to the south east
if cell.y < (@rows- 1) && cell.x < ( @cols - 1)
candidate = self.cell_grid[cell.y + 1][cell.x + 1]
live_neighbors << candidate if candidate.alive?
end
#neighbor to the south
if cell.y < (@rows- 1)
candidate = self.cell_grid[cell.y + 1][cell.x]
live_neighbors << candidate if candidate.alive?
end
#neighbor to the south west
if cell.y < (@rows- 1) && cell.x > 0
candidate = self.cell_grid[cell.y + 1][cell.x - 1]
live_neighbors << candidate if candidate.alive?
end
#neighbor to the west
if cell.x > 0
candidate = self.cell_grid[cell.y][cell.x - 1]
live_neighbors << candidate if candidate.alive?
end
#neighbor to the north west
if cell.x > 0 && cell.y > 0
candidate = self.cell_grid[cell.y - 1][cell.x - 1]
live_neighbors << candidate if candidate.alive?
end
live_neighbors
end
def live_cells
cells.select { |cell| cell.alive }
end
def randomly_populate
cells.each do |cell|
cell.alive = [true, false].sample
end
end
end
class Cell
attr_accessor :alive, :x, :y
def initialize(x=0, y=0)
@alive = false
@x = x
@y = y
end
def alive?
@alive
end
def dead?
!@alive
end
def die!
@alive = false
end
def birth!
@alive = true
end
end