-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_of_life.lua
131 lines (109 loc) · 3.54 KB
/
game_of_life.lua
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
-- Copyright (c) 2019 Claus Jørgensen
-- Conway's Game of Life
-- https:#en.wikipedia.org/wiki/Conway%27s_Game_of_Life
GameOfLife = { map = {} }
function GameOfLife:new(seed)
local object = {}
setmetatable(object, self)
self.__index = self
self.map = seed
return object
end
function GameOfLife:transition()
local newMap = {}
for x = 1, #self.map do
for y = 1, #self.map[x] do
local neighbors = self:neighbors(x, y)
local numAlive = 0
for n = 0, #neighbors do
if neighbors[n] == 1 then
numAlive = numAlive + 1
end
end
if newMap[x] == nil then
newMap[x] = {}
end
if newMap[x][y] == nil then
newMap[x][y] = self.map[x][y]
end
if numAlive < 2 then
newMap[x][y] = 0 -- death by under population
elseif numAlive > 3 then
newMap[x][y] = 0 -- death by over population
elseif self.map[x][y] == 0 and numAlive == 3 then
newMap[x][y] = 1 -- alive by reproduction
end
end
end
self.map = newMap
end
function GameOfLife:__tostring()
local output = ""
for x = 1, #self.map do
for y = 1, #self.map[x] do
if self.map[x][y] == 1 then
output = output .. "+ "
else
output = output .. " "
end
end
output = output .. "\n"
end
return output
end
function GameOfLife:neighbors(x, y)
local neighbors = {}
if self.map[x - 1] and self.map[x - 1][y] then
table.insert(neighbors, self.map[x - 1][y]) -- left
end
if self.map[x + 1] and self.map[x + 1][y] then
table.insert(neighbors, self.map[x + 1][y]) -- right
end
if self.map[x][y - 1] then
table.insert(neighbors, self.map[x][y - 1]) -- top
end
if self.map[x][y + 1] then
table.insert(neighbors, self.map[x][y + 1]) -- bottom
end
if self.map[x - 1] and self.map[x - 1][y - 1] then
table.insert(neighbors, self.map[x - 1][y - 1]) -- top-left
end
if self.map[x - 1] and self.map[x - 1][y + 1] then
table.insert(neighbors, self.map[x - 1][y + 1]) -- bottom-left
end
if self.map[x + 1] and self.map[x + 1][y + 1] then
table.insert(neighbors, self.map[x + 1][y + 1]) -- top-right
end
if self.map[x + 1] and self.map[x + 1][y - 1] then
table.insert(neighbors, self.map[x + 1][y - 1]) -- bottom-right
end
return neighbors
end
local 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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