Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --]] Game of life, by Redxone(Lewisk3), Special thanks to: MultMine [[--
- -- 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()
- local w, h = term.getSize()
- -- check for updates --
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos((w/2) - 11, 4)
- write("Checking for updates...")
- if(http)then
- term.setCursorPos((w/2) - 1, (h/2) - 1)
- for i = 1, 5 do
- term.setCursorPos((w/2) - 1, (h/2) - 1)
- write(":")
- sleep(0.2)
- term.setCursorPos((w/2) - 1, (h/2) - 1)
- write("/")
- sleep(0.2)
- term.setCursorPos((w/2) - 1, (h/2) - 1)
- write("-")
- sleep(0.2)
- term.setCursorPos((w/2) - 1, (h/2) - 1)
- write("\\")
- end
- local gameupdate = http.get("http://pastebin.com/raw/R3HfhA4x")
- sleep(0.2)
- if(gameupdate ~= nil)then
- gameupdate = gameupdate.readAll()
- f = fs.open(shell.getRunningProgram(),"r")
- _contents = f.readAll()
- f.close()
- if(_contents ~= gameupdate)then
- term.setCursorPos((w/2) - 11, 4)
- write("Update found, updating...")
- f = fs.open(shell.resolve(shell.getRunningProgram()),"w")
- f.write(gameupdate)
- f.close()
- sleep(0.5)
- term.setCursorPos((w/2) - 11, 4)
- term.clearLine()
- write(" Restarting game...")
- sleep(0.5)
- return shell.run(shell.getRunningProgram())
- end
- else
- term.setCursorPos((w/2) - 11, 4)
- write("Failed. launching game...")
- sleep(0.2)
- end
- end
- term.clear()
- term.setCursorPos((w/2) - 12, (h/2) - 1)
- print("Creating cell universe...")
- local verse = grid.create(82,50)
- local ngen = grid.create(82,50)
- sleep(0.2)
- local isPaused = false
- local running = true
- local generation = 0
- local speed = 0.2
- local xoff, yoff = 0, 0
- 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))
- --ngen:fill(newCell(false))
- --------------------------------------
- function drawCell(x,y)
- if(x <= w and y <= 50 and y >= 2)then
- term.setCursorPos(x,y)
- if(verse:get(x+xoff,y+yoff).isAlive==true)then
- if(isPaused)then term.setBackgroundColor(colors.yellow) else term.setBackgroundColor(colors.green) end
- term.setTextColor(colors.black)
- write(' ')
- else
- if(isPaused)then term.setBackgroundColor(colors.gray) else term.setBackgroundColor(colors.black) end
- term.setTextColor(colors.lightGray)
- if(isPaused)then write('L') else write (' ') end
- end
- end
- end
- function updateview()
- for x = 1, verse:getHeight() do
- for y = 1, verse:getWidth() do
- drawCell(y,x)
- end
- end
- end
- function life()
- for x = 1, verse:getHeight() do
- for y = 1, verse:getWidth() do
- local friends = 0
- drawCell(y,x)
- for _y = -1, 1 do
- for _x = -1, 1 do
- if _y ~= 0 or _x ~= 0 then
- if(verse:get(y+_y,x+_x).isAlive == true) then friends = friends + 1 end
- end
- end
- end
- -- Die conditions
- if(verse:get(y,x).isAlive and friends < 2)then
- ngen:set(y,x,newCell(false))
- elseif(friends > 3 and verse:get(y,x).isAlive)then
- ngen:set(y,x,newCell(false))
- end
- -- Live conditions
- if(verse:get(y,x).isAlive and (friends == 2 or friends == 3))then
- ngen:set(y,x,newCell(true))
- end
- if(verse:get(y,x).isAlive ~= true and (friends == 3))then
- ngen:set(y,x,newCell(true))
- end
- end
- end
- verse:replace(ngen:getRaw())
- ngen = grid.create(82,50)
- end
- function infoBar()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clearLine()
- local printgeneration = generation
- if generation > 999 then printgeneration = 999 end
- write(" Editing:" .. tostring(isPaused) .. " Generation: " .. printgeneration .. "," .. speed .. " X:" .. xoff .. ",Y:" .. yoff)
- term.setCursorPos(w,1)
- term.setBackgroundColor(colors.red)
- write('X')
- term.setCursorPos(w-1,1)
- term.setBackgroundColor(colors.lightGray)
- write('C')
- end
- function endscr()
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.clearLine()
- write("Thank you for playing Lewisk3's Game Of Life! Credits: John Conway, Multmine")
- term.setCursorPos(w,1)
- term.setBackgroundColor(colors.brown)
- write('X')
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,3)
- end
- verse:loop(drawCell)
- while running do
- term.current().setVisible(false)
- infoBar()
- ev = {os.pullEvent()}
- if(ev[1] == 'key')then
- if(ev[2] == keys.space)then isPaused = not isPaused end
- if(ev[2] == keys.w and yoff > 0)then yoff = yoff - 1 end
- if(ev[2] == keys.a and xoff > 0)then xoff = xoff - 1 end
- if(ev[2] == keys.s and yoff < verse:getHeight()-h)then yoff = yoff + 1 end
- if(ev[2] == keys.d and xoff < verse:getWidth()-w)then xoff = xoff + 1 end
- if(ev[2] == keys.up and speed > 0.01)then speed = speed - 0.01 end
- if(ev[2] == keys.down)then speed = speed + 0.01 end
- updateview()
- end
- if(ev[1] == 'timer' and ev[2] == genTimer)then
- if(not isPaused)then life() generation = generation + 1 end
- genTimer = os.startTimer(speed)
- elseif(ev[1] == "mouse_click" or ev[1] == "mouse_drag")then
- if(ev[3] == w and ev[4] == 1)then running = false endscr() end
- if(ev[3] == w-1 and ev[4] == 1)then verse = grid.create(verse:getWidth(),verse:getHeight()) verse:fill({}) ngen = grid.create(ngen:getWidth(),ngen:getHeight()) updateview() end
- if(isPaused)then
- if(ev[2] == 1)then verse:set(ev[3]+xoff,ev[4]+yoff,newCell(true)) end
- if(ev[2] == 2)then verse:set(ev[3]+xoff,ev[4]+yoff,{}) end
- drawCell(ev[3],ev[4])
- end
- end
- term.current().setVisible(true)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement