-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_of_life.rb
116 lines (106 loc) · 3.43 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
# Copyright (c) 2019 Claus Jørgensen
# Conway's Game of Life
# https:#en.wikipedia.org/wiki/Conway%27s_Game_of_Life
class GameOfLife
# Constructs a Game of Life
# +seed+:: The initial game seed (two-dimensional array).
def initialize(seed = [[]])
@map = seed
end
# Transitions the game once
def transition
newMap = []
for x in [email protected] do
for y in 0...@map[x].length do
if !newMap[x]
newMap[x] = []
end
if !newMap[x][y]
newMap[x][y] = @map[x][y]
end
numAlive = neighbors(x, y).reduce(0) { |a, c| c == 1 ? a + 1 : a }
if numAlive < 2
newMap[x][y] = 0 # death by under population
elsif numAlive > 3
newMap[x][y] = 0 # death by over population
elsif @map[x][y] == 0 and numAlive == 3
newMap[x][y] = 1 # alive by reproduction
end
end
end
@map = newMap
end
# Gets a string representation of the game's current state.
def to_s
output = ""
for x in [email protected] do
for y in 0...@map[x].length do
if @map[x][y] == 1
output += "+ "
else
output += " "
end
end
output += "\n"
end
return output
end
private
# Gets the cell neighbors
def neighbors(x, y)
neighbors = []
if @map[x - 1] && @map[x - 1][y]
neighbors.push(@map[x - 1][y]) # left
end
if @map[x + 1] && @map[x + 1][y]
neighbors.push(@map[x + 1][y]) # right
end
if @map[x][y - 1]
neighbors.push(@map[x][y - 1]) # top
end
if @map[x][y + 1]
neighbors.push(@map[x][y + 1]) # bottom
end
if @map[x - 1] && @map[x - 1][y - 1]
neighbors.push(@map[x - 1][y - 1]) # top-left
end
if @map[x - 1] && @map[x - 1][y + 1]
neighbors.push(@map[x - 1][y + 1]) # bottom-left
end
if @map[x + 1] && @map[x + 1][y + 1]
neighbors.push(@map[x + 1][y + 1]) # top-right
end
if @map[x + 1] && @map[x + 1][y - 1]
neighbors.push(@map[x + 1][y - 1]) # bottom-right
end
return neighbors
end
end
pentadecathlon = [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 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