-
Notifications
You must be signed in to change notification settings - Fork 0
/
gosu_gol.rb
116 lines (95 loc) · 2.06 KB
/
gosu_gol.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
class GosuGOL
attr_accessor :original, :next_gen, :n, :alive_cells
def initialize(params, dimension)
@n = dimension
@original = Array.new(@n){ Array.new(@n, 0) }
params.each do |dimen_one_array|
@original[dimen_one_array[0]][dimen_one_array[1]] = 1
end
end
def neighbours
@next_gen = Array.new(@n){ Array.new(@n, 0) }
for i in 0..@n - 1
for j in 0..@n - 1
temp = neighbour_count(i,j)
if temp == 1
@next_gen[i][j] = 0
elsif temp == 2 and @original[i][j] == 1
@next_gen[i][j] = 1
elsif temp == 3 and @original[i][j] == 1
@next_gen[i][j] = 1
elsif temp == 3 and @original[i][j] == 0
@next_gen[i][j] = 1
elsif temp > 3
@next_gen[i][j] = 0
end
end
end
@next_gen
end
def evolve
@original = neighbours
@alive_cells = []
for i in 0..@n - 1
for j in 0..@n - 1
@alive_cells << [i, j] if @original[i][j] == 1
end
end
@alive_cells
end
def boundary?(x, y)
if x - 1 < 0 || y - 1 < 0 || x + 1 > @rows || y + 1 > @cols
return false
end
true
end
def neighbour_count(i, j)
temp = 0
if j+1 >= 0 and j+1 <= @n-1
if @original[i][j+1] == 1
temp+=1
end
end
if j-1 <= @n-1 and j-1 >= 0
if @original[i][j-1] == 1
temp+=1
end
end
if i+1 >= 0and i+1 <= @n-1
if @original[i+1][j] == 1
temp+=1
end
end
if i-1 >= 0 and i-1 <= @n-1
if @original[i-1][j] == 1
temp+=1
end
end
if i-1 >= 0 and i-1 <= @n-1 and j-1 >= 0 and j-1 <= @n-1
if @original[i-1][j-1] == 1
temp+=1
end
end
if i-1 >= 0 and i-1 <= @n-1 and j+1 >= 0 and j+1 <= @n-1
if @original[i-1][j+1] == 1
temp+=1
end
end
if i+1 >= 0 and i+1 <= @n-1 and j-1 >= 0 and j-1 <= @n-1
if @original[i+1][j-1] == 1
temp+=1
end
end
if i+1 >= 0 and i+1 <= @n-1 and j+1 >= 0 and j+1 <= @n-1
if @original[i+1][j+1] == 1
temp+=1
end
end
temp
end
end
# board_detail = GameGOL::FileExtract.new("test.txt")
# board_detail.fetch
# game = GosuGOL.new(board_detail.first_matrix)
# binding.pry
# game.evolve