Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --]] Game of life, by Redxone(Lewisk3) [[--
- -- Rules:
- -- if less than 2, die
- -- if more then 3, die
- -- if has 2 or 3, live
- -- if more then 3, die
- -- if has exactly 3 and is dead, live
- os.loadAPI("grid")
- ------------------------------------
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clearLine()
- print("Creating cell universe...")
- local w, h = term.getSize()
- local verse = grid.create(w,h)
- local isPaused = false
- local running = true
- local generation = 0
- local speed = 0.2
- local genTimer = os.startTimer(speed)
- function newCell(alive)
- local cell = {isAlive=true}
- if(not alive)then cell.isAlive = false end
- return cell
- end
- --- Fill universe with dead cells ---
- verse:fill(newCell(false))
- -------------------------------------
- function drawCell(x,y)
- if(x <= w and y <= h and y >= 2)then
- term.setCursorPos(x,y)
- if(verse:get(x,y).isAlive==true)then
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.black)
- write(' ')
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.lightGray)
- write('L')
- end
- end
- end
- function life()
- for x = 1, verse:getHeight() do
- for y = 1, verse:getWidth() do
- local friends = 0
- if(verse:get(y-1,x-1).isAlive == true)then friends = friends + 1 end
- if(verse:get(y,x-1).isAlive == true)then friends = friends + 1 end
- if(verse:get(y-1,x).isAlive == true)then friends = friends + 1 end
- if(verse:get(y,x+1).isAlive == true)then friends = friends + 1 end
- if(verse:get(y+1,x).isAlive == true)then friends = friends + 1 end
- if(verse:get(y+1,x+1).isAlive == true)then friends = friends + 1 end
- if(verse:get(y+1,x-1).isAlive == true)then friends = friends + 1 end
- if(verse:get(y-1,x+1).isAlive == true)then friends = friends + 1 end
- -- Die conditions
- if(verse:get(y,x).isAlive and friends < 2)then
- verse:get(y,x).isAlive = false
- elseif(friends > 3 and verse:get(x,y).isAlive)then
- verse:get(y,x).isAlive = false
- end
- -- Live conditions
- if(verse:get(y,x).isAlive and (friends == 2 or friends == 3))then
- verse:get(y,x).isAlive = true
- end
- if(not verse:get(y,x).isAlive and (friends == 3))then
- verse:get(y,x).isAlive = true
- end
- end
- end
- end
- function infoBar()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clearLine()
- write(" Paused: " .. tostring(isPaused) .. " Generation: " .. generation .. " Speed: " .. speed)
- end
- verse:loop(drawCell)
- while running do
- term.current().setVisible(false)
- infoBar()
- ev = {os.pullEvent()}
- if(ev[1] == 'key' and ev[2] == keys.space)then
- isPaused = not isPaused
- end
- if(ev[1] == 'timer' and ev[2] == genTimer)then
- if(not isPaused)then life() verse:loop(drawCell) generation = generation + 1 end
- genTimer = os.startTimer(speed)
- elseif(isPaused)then
- if(ev[1] == "mouse_click")then
- if(ev[2] == 1)then verse:set(ev[3],ev[4],newCell(true)) end
- if(ev[2] == 2)then verse:set(ev[3],ev[4],newCell(false)) end
- drawCell(ev[3],ev[4])
- end
- end
- term.current().setVisible(true)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement