康威的生命游戏:为什么模式行为不正确?



我正试图在Lua中创建Conway的"生命游戏"的实现。

游戏场是一个二维圆桌,有100行47列(共4700个单元格(,其中一个单元格的值可以是1(有效(或0(无效(。

问题是,当我试图创建一个闪烁器,并制作一个由3个相邻的活细胞组成的模式时,两侧细胞都会在下一代中死亡,而其他细胞则不会存活。我也曾试图创建一个滑翔机,但在下一代中,它的顶部中间单元格和底部左侧单元格都死了,然后模式变得静止。

当我尝试调试时,所有变量似乎都有正确的值,所以我认为问题出在函数nextGen()的值分配中,从表fieldnfield,反之亦然。我可能做错了什么?

-- Game window's width and height. 
-- Rows 48, 49, and 50 are used for control tips and program information
local width = 100
local height = 50
-- Current generation field
local field = {}
-- Next generation field
local nfield = {}
-- UI Color codes
local black = 0x000000
local white = 0xFFFFFF
-- Field clearing/generation
local function clearField()
-- Making the field array circular (code from https://stackoverflow.com/a/63169007/12696005)
setmetatable(field, {
__index = function(t,i)
local index = i%width
index = index == 0 and width or index
return t[index] end
})
--
for gx=1,width do
field[gx] = {}
nfield[gx] = {}
-- Making the field array circular pt.2
setmetatable(field[gx], {
__index = function(t,i)
local index = i%height-3
index = index == 0 and height-3 or index
return t[index] end
})
--
for gy=1,height-3 do
field[gx][gy] = 0
end
end
end
--Field redraw
local function drawField(data)
for x=1, width do
for y=1,height-3 do
if data[x][y]==1 then
-- Alive cell
display.setBackgroundColor(white)
display.setForegroundColor(black)
else
-- Dead cell
display.setBackgroundColor(black)
display.setForegroundColor(white)
end
-- Drawing the cell
display.fillRectangle(x, y, 1, 1, " ")
end
end
end
--- Calculating next generation field
local function nextGen()
-- Going through all the table
for x=1,width do
for y=1,height-3 do
local livingCells = 0
-- Calculation of living cells around x:y
for i=x-1,x+1 do
for j=y-1,y+1 do
livingCells = livingCells + field[i][j]
end
end
-- Substracting the x:y cell from the overall amount
livingCells = livingCells - field[x][y]
-- Cell spawns
if field[x][y]==0 and livivngCells==3 then
nfield[x][y] = 1
-- Cell dies
elseif field[x][y]==1 and (livingCells < 2 or livingCells > 3) then
nfield[x][y] = 0
-- Remains the same
else
nfield[x][y] = field[x][y]
end
end
end
end
-- Game loop
clearField()
while true do
local lastEvent = {events.listenFilter(filter)}
-- Cell creation/deletion
if lastEvent[1] == "mouseClicked" and lastEvent[5] == "lmb" then
--State invertion and cell redrawing
if field[lastEvent[3]][lastEvent[4]]==1 then
field[lastEvent[3]][lastEvent[4]] = 0
display.setBackgroundColor(black)
display.setForegroundColor(white)
else
field[lastEvent[3]][lastEvent[4]] = 1
display.setBackgroundColor(white)
display.setForegroundColor(black)
end
display.fillRectangle(lastEvent[3], lastEvent[4], 1, 1, " ")
elseif lastEvent[1] == "key_pressed" then
-- Previous generation
if lastEvent[4] == keyboard.lbracket then
drawField(field)
-- Next generation
elseif lastEvent[4] == keyboard.rbracket then
display.setBackgroundColor(blue)
display.fillRectangle(1, height-2, width, 1, " ")
nextGen()
drawField(nfield)
for i=1,width do
for j=1,height-3 do
field[i][j] = nfield[i][j]
end
end
elseif lastEvent[4] == keyboard.backslash then
-- more code --
end
end
end

变量displaykeyboardevents是库。

发现唯一的问题是if field[x][y]==0 and livivngCells==3 then行中的livingCells拼写错误。

最新更新