Advertisement
Redxone

CC - Game Of Life

Feb 20th, 2016
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.44 KB | None | 0 0
  1. --]] Game of life, by Redxone(Lewisk3), Special thanks to: MultMine [[--
  2. -- Rules:
  3. -- if less than 2, die
  4. -- if more then 3, die
  5. -- if has 2 or 3, live
  6. -- if more then 3, die
  7. -- if has exactly 3 and is dead, live
  8. os.loadAPI("grid")
  9. ------------------------------------
  10. term.setBackgroundColor(colors.black)
  11. term.clear()
  12. term.setCursorPos(1,1)
  13. term.setBackgroundColor(colors.gray)
  14. term.setTextColor(colors.white)
  15. term.clearLine()
  16. local w, h = term.getSize()
  17. -- check for updates --
  18. term.setBackgroundColor(colors.gray)
  19. term.setTextColor(colors.white)
  20. term.clear()
  21. term.setCursorPos((w/2) - 11, 4)
  22. write("Checking for updates...")
  23. if(http)then
  24.         term.setCursorPos((w/2) - 1, (h/2) - 1)
  25.         for i = 1, 5 do
  26.             term.setCursorPos((w/2) - 1, (h/2) - 1)
  27.             write(":")
  28.             sleep(0.2)
  29.             term.setCursorPos((w/2) - 1, (h/2) - 1)
  30.             write("/")
  31.             sleep(0.2)
  32.             term.setCursorPos((w/2) - 1, (h/2) - 1)
  33.             write("-")
  34.             sleep(0.2)
  35.             term.setCursorPos((w/2) - 1, (h/2) - 1)
  36.             write("\\")
  37.         end
  38.     local gameupdate = http.get("http://pastebin.com/raw/R3HfhA4x")
  39.     sleep(0.2)
  40.     if(gameupdate ~= nil)then
  41.         gameupdate = gameupdate.readAll()
  42.         f = fs.open(shell.getRunningProgram(),"r")
  43.         _contents = f.readAll()
  44.         f.close()
  45.         if(_contents ~= gameupdate)then
  46.             term.setCursorPos((w/2) - 11, 4)
  47.             write("Update found, updating...")
  48.             f = fs.open(shell.resolve(shell.getRunningProgram()),"w")
  49.             f.write(gameupdate)
  50.             f.close()
  51.             sleep(0.5)
  52.             term.setCursorPos((w/2) - 11, 4)
  53.             term.clearLine()
  54.             write(" Restarting game...")
  55.             sleep(0.5)
  56.             return shell.run(shell.getRunningProgram())
  57.         end
  58.     else
  59.         term.setCursorPos((w/2) - 11, 4)
  60.         write("Failed. launching game...")
  61.         sleep(0.2)
  62.     end
  63. end
  64. term.clear()
  65. term.setCursorPos((w/2) - 12, (h/2) - 1)
  66. print("Creating cell universe...")
  67. local verse = grid.create(82,50)
  68. local ngen = grid.create(82,50)
  69. sleep(0.2)
  70. local isPaused = false
  71. local running = true
  72. local generation = 0
  73. local speed = 0.2
  74. local xoff, yoff = 0, 0
  75. local genTimer = os.startTimer(speed)
  76. function newCell(alive)
  77.     local cell = {isAlive=true}
  78.     if(not alive)then cell.isAlive = false end
  79.     return cell
  80. end
  81. --- Fill universe with dead cells ---
  82. --verse:fill(newCell(false))
  83. --ngen:fill(newCell(false))
  84. --------------------------------------
  85. function drawCell(x,y)
  86.     if(x <= w and y <= 50 and y >= 2)then
  87.         term.setCursorPos(x,y)
  88.         if(verse:get(x+xoff,y+yoff).isAlive==true)then
  89.             if(isPaused)then term.setBackgroundColor(colors.yellow) else term.setBackgroundColor(colors.green) end
  90.             term.setTextColor(colors.black)
  91.             write(' ')
  92.         else
  93.              if(isPaused)then term.setBackgroundColor(colors.gray) else term.setBackgroundColor(colors.black) end
  94.              term.setTextColor(colors.lightGray)
  95.              if(isPaused)then write('L') else write (' ') end
  96.         end
  97.     end
  98. end
  99. function updateview()
  100.     for x = 1, verse:getHeight() do
  101.         for y = 1, verse:getWidth() do
  102.             drawCell(y,x)
  103.         end
  104.     end
  105. end
  106. function life()
  107.     for x = 1, verse:getHeight() do
  108.         for y = 1, verse:getWidth() do
  109.             local friends = 0
  110.             drawCell(y,x)
  111.             for _y = -1, 1 do
  112.              for _x = -1, 1 do
  113.               if _y ~= 0 or _x ~= 0 then
  114.                if(verse:get(y+_y,x+_x).isAlive == true) then friends = friends + 1 end
  115.               end
  116.              end
  117.             end
  118.              -- Die conditions
  119.             if(verse:get(y,x).isAlive and friends < 2)then
  120.                 ngen:set(y,x,newCell(false))
  121.             elseif(friends > 3 and verse:get(y,x).isAlive)then
  122.                 ngen:set(y,x,newCell(false))
  123.             end
  124.                 -- Live conditions
  125.             if(verse:get(y,x).isAlive and (friends == 2 or friends == 3))then
  126.                 ngen:set(y,x,newCell(true))
  127.             end
  128.             if(verse:get(y,x).isAlive ~= true and (friends == 3))then
  129.                 ngen:set(y,x,newCell(true))
  130.             end
  131.         end
  132.     end
  133.   verse:replace(ngen:getRaw())
  134.   ngen = grid.create(82,50)
  135. end
  136. function infoBar()
  137.     term.setCursorPos(1,1)
  138.     term.setBackgroundColor(colors.gray)
  139.     term.setTextColor(colors.white)
  140.     term.clearLine()
  141.     local printgeneration = generation
  142.     if generation > 999 then printgeneration = 999 end
  143.     write(" Editing:" .. tostring(isPaused) .. " Generation: " .. printgeneration .. "," .. speed .. " X:" .. xoff .. ",Y:" .. yoff)
  144.     term.setCursorPos(w,1)
  145.     term.setBackgroundColor(colors.red)
  146.     write('X')
  147.     term.setCursorPos(w-1,1)
  148.     term.setBackgroundColor(colors.lightGray)
  149.     write('C')
  150. end
  151. function endscr()
  152.     term.setBackgroundColor(colors.black)
  153.     term.clear()
  154.     term.setCursorPos(1,1)
  155.     term.setBackgroundColor(colors.gray)
  156.     term.setTextColor(colors.white)
  157.     term.clearLine()
  158.     write("Thank you for playing Lewisk3's Game Of Life! Credits: John Conway, Multmine")
  159.     term.setCursorPos(w,1)
  160.     term.setBackgroundColor(colors.brown)
  161.     write('X')
  162.     term.setBackgroundColor(colors.black)
  163.     term.setCursorPos(1,3)
  164.  
  165. end
  166.  
  167. verse:loop(drawCell)
  168. while running do
  169.     term.current().setVisible(false)
  170.     infoBar()
  171.     ev = {os.pullEvent()}
  172.     if(ev[1] == 'key')then
  173.         if(ev[2] == keys.space)then isPaused = not isPaused end
  174.         if(ev[2] == keys.w and yoff > 0)then yoff = yoff - 1 end
  175.         if(ev[2] == keys.a and xoff > 0)then xoff = xoff - 1 end
  176.         if(ev[2] == keys.s and yoff < verse:getHeight()-h)then yoff = yoff + 1 end
  177.         if(ev[2] == keys.d and xoff < verse:getWidth()-w)then xoff = xoff + 1 end
  178.         if(ev[2] == keys.up and speed > 0.01)then speed = speed - 0.01 end  
  179.         if(ev[2] == keys.down)then speed = speed + 0.01 end
  180.         updateview()
  181.     end
  182.     if(ev[1] == 'timer' and ev[2] == genTimer)then
  183.         if(not isPaused)then life() generation = generation + 1 end
  184.         genTimer = os.startTimer(speed)
  185.     elseif(ev[1] == "mouse_click" or ev[1] == "mouse_drag")then
  186.             if(ev[3] == w and ev[4] == 1)then running = false endscr() end
  187.             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
  188.         if(isPaused)then  
  189.             if(ev[2] == 1)then verse:set(ev[3]+xoff,ev[4]+yoff,newCell(true)) end
  190.             if(ev[2] == 2)then verse:set(ev[3]+xoff,ev[4]+yoff,{}) end
  191.             drawCell(ev[3],ev[4])
  192.         end
  193.     end
  194.     term.current().setVisible(true)
  195. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement