Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dimensions = {}
- dimensions["width"] = 36
- dimensions["height"] = 20
- local field = {}
- for x = 1, dimensions["width"] do
- field[x] = {}
- for y = 1, dimensions["height"] do
- field[x][y] = 0
- end
- end
- field[1][2] = 1
- field[2][2] = 1
- field[2][3] = 1
- field[3][3] = 1
- function printField()
- for y = 1, dimensions["height"] do
- for x = 1, dimensions["width"] do
- if field[x][y] == 0 then
- write("-")
- else
- write("X")
- end
- end
- print()
- end
- end
- function getCorrectCoords(x, y)
- if (x > dimensions["height"]) then
- x = 1
- elseif x < 1 then
- x = dimensions["width"]
- end
- if (y > dimensions["height"]) then
- y = 1
- elseif y < 1 then
- y = dimensions["height"]
- end
- res = {x, y}
- return res
- end
- function countNeighbors(x, y)
- local neighbors = 0
- for why = 1, 3 do
- for ex = 1, 3 do
- local coords = getCorrectCoords((x-2)+ex, (y-2)+why)
- if field[coords[1]][coords[2]] == 1 then
- neighbors = neighbors + 1
- end
- end
- end
- if field[x][y] == 1 then
- neighbors = neighbors - 1
- end
- return neighbors
- end
- function updateField()
- -- declares the field which will hold the new cell values
- local newfield = {} -- the problem persists even when newfield is a global variable
- -- fills newfield with 0's
- for x = 1, dimensions["width"] do
- newfield[x] = {}
- for y = 1, dimensions["height"] do
- newfield[x][y] = 0
- end
- end
- print(newfield[1][1]) -- this line executes, and throws no errors at all
- -- this block goes through cells, counts neighbors, and determines next state of cell
- for x = 1, dimensions["width"] do
- print(newfield[1][1]) -- past this point all newfield references throw errors
- for y = 1, dimensions["height"] do
- print(newfield[1][1]) -- this line executes, but as soon as it finishes it throws an error
- if (field[x][y] == 0) and (countNeighbors(x, y) == 3) then
- newfield[x][y] = 1
- elseif (field[x][y] == 1) and ((countNeighbors(x, y) == 3) or (countNeighbors(x, y) == 2)) then
- newfield[x][y] = 1
- else
- newfield = 0
- end
- end
- end
- field = newfield
- end
- while true do
- sleep(0.5)
- term.clear()
- term.setCursorPos(1,1)
- updateField()
- printField()
- end
Advertisement
Add Comment
Please, Sign In to add comment