Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- setfenv(1, _G)
- local DEMO = {}
- DEMO.Author = "CapsAdmin"
- DEMO.Title = "game of life????"
- local board = {{}}
- local res = 2
- function DEMO:OnStart(w, h)
- for x = -res, w+res, res do
- for y = -res, h+res, res do
- board[x] = board[x] or {}
- board[x][y] = board[x][y] or math.random() > 0.5 and 1 or 0
- end
- end
- end
- local function count(board, x,y)
- return
- board[x-res][y-res] +
- board[x][y-res] +
- board[x+res][y-res] +
- board[x+res][y] +
- board[x+res][y+res] +
- board[x][y+res] +
- board[x-res][y+res] +
- board[x-res][y]
- end
- function DEMO:OnDraw(w, h)
- surface.SetDrawColor(255, 255, 255, 255)
- for x = 0, w, res do
- for y = 0, h, res do
- if board[x][y] == 1 then
- local neighbours = count(board, x,y)
- -- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- if neighbours < 2 then
- board[x][y] = 0
- end
- -- Any live cell with two or three live neighbours lives on to the next generation.
- if neighbours == 2 or neighbours == 3 then
- continue
- end
- -- Any live cell with more than three live neighbours dies, as if by overcrowding.
- if neighbours > 3 then
- board[x][y] = 0
- end
- surface.DrawRect(x,y, res, res)
- end
- if board[x][y] == 0 then
- -- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
- if neighbours == 3 then
- board[x][y] = 1
- end
- end
- end
- end
- end
- demoman.AddDemo(DEMO, true)
Advertisement
Add Comment
Please, Sign In to add comment