我试图在卢阿创建康威的“生命游戏”。
游戏字段是一个有100行47列(4700个单元格总数)的二维圆桌,其中一个单元格的值可以是1(活着)或0(死)。
问题是,当我试图创建一个闪光灯,并使一个模式的3个活细胞在彼此之间,两个边细胞死亡在下一代,而没有其他的活的。我也曾尝试过制造滑翔机,但在下一代,它的上、中、左下角的滑翔机都死了,然后模式就静止了。
当我尝试调试时,所有变量似乎都有正确的值,因此我认为问题在于函数nextGen()中的值分配从表field到nfield,反之亦然。我可能做错什么了?
-- 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变量display、keyboard和events是库。
发布于 2020-07-31 22:09:08
结果发现,唯一的问题是livingCells在行if field[x][y]==0 and livivngCells==3 then中拼写错误。
https://stackoverflow.com/questions/63198702
复制相似问题