Advertisement
NickM13

[ComputerCraft] Game of Life

May 29th, 2015
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.68 KB | None | 0 0
  1. --Game of Life programmed by Nick Mead(NickM13)
  2. --A ComputerCraft program
  3. --v0.0.1 Pastebin Code: yxfaBGdK
  4. --v0.0.2 Pastebin Code: KLswNb9f
  5.  
  6. local exitGame = false
  7. local playing = false
  8. local dimension = {}
  9. local screen = {}
  10. local cell = {}
  11. local newCell = {}
  12. local livingCells = 0
  13. local mouse = {}
  14. local gps = 2 -- Generations per second
  15. local skipping = false
  16.  
  17. local function wait(t)
  18.     os.sleep(t)
  19. end
  20.  
  21. local function resetCells()
  22.     gps = 2
  23.     livingCells = 0
  24.     for i=1, dimension.width, 1 do
  25.         for j=1, dimension.height, 1 do
  26.             cell[i][j] = 0
  27.         end
  28.     end
  29. end
  30.  
  31. local function centeredText(msg, y)
  32.     local width = term.getSize()
  33.     term.setCursorPos(math.ceil(width - string.len(msg))/2+1, y)
  34.     term.write(msg)
  35. end
  36.  
  37. local function writeText(msg, x, y)
  38.     term.setCursorPos(x, y)
  39.     term.write(msg)
  40. end
  41.  
  42. local function drawRect(color, x0, y0, x1, y1, outline)
  43.     term.setBackgroundColor(color)
  44.     for x=0, x1-x0, 1 do
  45.         for y=0, y1-y0, 1 do
  46.             if outline then
  47.                 if (x == 0 or x == x1-x0 or y == 0 or y == y1-y0) then
  48.                     term.setCursorPos(x+x0, y+y0)
  49.                     term.write(" ")
  50.                 end
  51.             else
  52.                 term.setCursorPos(x+x0, y+y0)
  53.                 term.write(" ")
  54.             end
  55.         end
  56.     end
  57. end
  58.  
  59. local function keyInput()
  60.     local event, key = os.pullEvent('char')
  61.     if key == ' ' then
  62.         playing = not playing
  63.     elseif key == 't' then
  64.         exitGame = true
  65.     elseif key == 'c' then
  66.         resetCells()
  67.     elseif key == 'a' then
  68.         gps = gps - 1
  69.         if gps < 1 then gps = 1 end
  70.     elseif key == 'd' then
  71.         gps = gps + 1
  72.         if gps > 20 then gps = 20 end
  73.     elseif key == 's' then
  74.         skipping = true
  75.     end
  76. end
  77.  
  78. local function mouseClickInput()
  79.     local event, b, x, y = os.pullEvent('mouse_click')
  80.     if b == 1 then mouse.left = true mouse.lclick = true end
  81.     if b == 2 then mouse.right = true end
  82.     mouse.x = x
  83.     mouse.y = y
  84. end
  85.  
  86. local function mouseDragInput()
  87.     local event, b, x, y = os.pullEvent('mouse_drag')
  88.     if b == 1 then mouse.left = true end
  89.     if b == 2 then mouse.right = true end
  90.     mouse.x = x
  91.     mouse.y = y
  92. end
  93.  
  94. local function input()
  95.     parallel.waitForAny(keyInput, mouseClickInput, mouseDragInput)
  96. end
  97.  
  98. local function update()
  99.     if mouse.left then
  100.         if (mouse.x < dimension.width) then
  101.             if not playing then if cell[mouse.x][mouse.y] == 0 then cell[mouse.x][mouse.y] = 1 livingCells = livingCells + 1 end end
  102.         else
  103.             if mouse.lclick then
  104.                 if (mouse.y >= 5 and mouse.y <= 7) then playing = not playing end
  105.                 if (mouse.x == screen.width and mouse.y == screen.height) then
  106.                     playing = false
  107.                     resetCells()
  108.                 end
  109.                 if (mouse.x == dimension.width + 1 and mouse.y == screen.height) then exitGame = true end
  110.                 if mouse.x == dimension.width + 1 then if mouse.y == 2 or mouse.y == 3 then gps = gps - 1 end end
  111.                 if mouse.x == dimension.width + 10 then if mouse.y == 2 or mouse.y == 3 then gps = gps + 1 end end
  112.                 if (mouse.x > dimension.width + 1 and mouse.y == 9) then skipping = true end
  113.                 if gps < 1 then gps = 1 end
  114.                 if gps > 20 then gps = 20 end
  115.             end
  116.         end
  117.         mouse.left = false
  118.         mouse.lclick = false
  119.     end
  120.     if mouse.right then
  121.         if (mouse.x < dimension.width) then
  122.             if not playing then if cell[mouse.x][mouse.y] == 1 then cell[mouse.x][mouse.y] = 0 livingCells = livingCells - 1 end end
  123.         else
  124.            
  125.         end
  126.         mouse.right = false
  127.     end
  128.     if playing or skipping then
  129.         local alive = 0
  130.         livingCells = 0
  131.         for i=1, dimension.width, 1 do
  132.             for j=1, dimension.height, 1 do
  133.                 newCell[i][j] = cell[i][j]
  134.             end
  135.         end
  136.         for i=1, dimension.width, 1 do
  137.             for j=1, dimension.height, 1 do
  138.                 if (i == 1 and j > 1 and j < dimension.height) then alive = cell[i][j-1] + cell[i+1][j] + cell[i][j+1] + cell[i+1][j-1] + cell[i+1][j+1]
  139.                 elseif (i == dimension.width and j > 1 and j < dimension.height) then alive = cell[i-1][j] + cell[i][j-1] + cell[i][j+1] + cell[i-1][j-1] + cell[i-1][j+1]
  140.                 elseif (j == 1 and i > 1 and i < dimension.width) then alive = cell[i-1][j] + cell[i+1][j] + cell[i][j+1] + cell[i-1][j+1] + cell[i+1][j+1]
  141.                 elseif (j == dimension.height and i > 1 and i < dimension.width) then alive = cell[i-1][j] + cell[i][j-1] + cell[i+1][j] + cell[i-1][j-1] + cell[i+1][j-1]
  142.                 elseif (i == 1 and j == 1) then alive = cell[i+1][j] + cell[i][j+1] + cell[i+1][j+1]
  143.                 elseif (i == 1 and j == dimension.height) then alive = cell[i][j-1] + cell[i+1][j] + cell[i+1][j-1]
  144.                 elseif (i == dimension.width and j == 1) then alive = cell[i-1][j] + cell[i][j+1] + cell[i-1][j+1]
  145.                 elseif (i == dimension.width and j == dimension.height) then alive = cell[i-1][j] + cell[i][j-1] + cell[i-1][j-1]
  146.                 else alive = cell[i-1][j] + cell[i][j-1] + cell[i+1][j] + cell[i][j+1] + cell[i-1][j-1] + cell[i-1][j+1] + cell[i+1][j-1] + cell[i+1][j+1]
  147.                 end
  148.                 if cell[i][j] == 1 then
  149.                     if alive < 2 then
  150.                         newCell[i][j] = 0
  151.                     elseif alive > 3 then
  152.                         newCell[i][j] = 0
  153.                     end
  154.                 else
  155.                     if alive == 3 then
  156.                         newCell[i][j] = 1
  157.                     end
  158.                 end
  159.                 livingCells = livingCells + newCell[i][j]
  160.             end
  161.         end
  162.         for i=1, dimension.width, 1 do
  163.             for j=1, dimension.height, 1 do
  164.                 cell[i][j] = newCell[i][j]
  165.             end
  166.         end
  167.         skipping = false
  168.     end
  169. end
  170.  
  171. local function render()
  172.     for i=1, dimension.width, 1 do
  173.         for j=1, dimension.height, 1 do
  174.             term.setCursorPos(i, j)
  175.             if cell[i][j] == 1 then
  176.                 term.setBackgroundColor(colors.white)
  177.             else
  178.                 term.setBackgroundColor(colors.black)
  179.             end
  180.             term.write(" ")
  181.         end
  182.     end
  183.    
  184.     drawRect(colors.black, dimension.width + 1, 2, screen.width, 3, false)
  185.     writeText("Gen/s", dimension.width + 4, 2)
  186.     writeText(gps, dimension.width + 5, 3)
  187.     term.setBackgroundColor(colors.lightGray)
  188.     writeText("/", dimension.width + 1, 2)
  189.     writeText("\\", dimension.width + 1, 3)
  190.     writeText("\\", dimension.width + 10, 2)
  191.     writeText("/", dimension.width + 10, 3)
  192.    
  193.     if playing then
  194.         drawRect(colors.green, dimension.width + 1, 5, dimension.width + 10, 7, false)
  195.     else
  196.         drawRect(colors.red, dimension.width + 1, 5, dimension.width + 10, 7, false)
  197.     end
  198.    
  199.     drawRect(colors.lightGray, dimension.width + 1, 9, dimension.width + 10, 9, false)
  200.     writeText("Skip Frame", dimension.width + 1, 9)
  201.    
  202.     drawRect(colors.black, dimension.width + 1, dimension.height - 3, dimension.width + 10, dimension.height - 2, false)
  203.     writeText("Cells:"..livingCells, dimension.width + 2, dimension.height - 2)
  204.    
  205.     term.setBackgroundColor(colors.black)
  206.     writeText("C", screen.width, screen.height)
  207.     writeText("T", dimension.width + 1, screen.height)
  208.    
  209.     term.setBackgroundColor(colors.black)
  210.     term.setCursorPos(1, 19)
  211. end
  212.  
  213. local function loop()
  214.     update()
  215.     render()
  216.     wait(1 / gps)
  217. end
  218.  
  219. local function gameLoop()
  220.     while not exitGame do
  221.         parallel.waitForAny(input, loop)
  222.     end
  223.     term.clear()
  224.     term.setCursorPos(1, 1)
  225.     term.write("Terminated.")
  226.     term.setCursorPos(1, 2)
  227. end
  228.  
  229. local function intro()
  230.     term.setBackgroundColor(colors.gray)
  231.     term.setTextColor(colors.white)
  232.     term.clear()
  233.     for i=1, 10, 1 do
  234.         centeredText("Game of Life", 10 - i)
  235.         centeredText("Programmed by NickM13", 10 + i)
  236.         writeText("v 0.0.2", 44, 18)
  237.         drawRect(colors.orange, 1, 1, screen.width, screen.height, true)
  238.         wait(1.0/i)
  239.         term.setBackgroundColor(colors.gray)
  240.         term.setTextColor(colors.white)
  241.         term.clear()
  242.     end
  243.     term.setCursorPos(1, 19)
  244.     wait(1)
  245. end
  246.  
  247. local function main()
  248.     screen.width, screen.height = term.getSize()
  249.     dimension.height = screen.height
  250.     dimension.width = screen.width - 10
  251.     for i=1, dimension.width, 1 do
  252.         cell[i] = {}
  253.         newCell[i] = {}
  254.     end
  255.     for i=1, dimension.width, 1 do
  256.         for j=1, dimension.height, 1 do
  257.             cell[i][j] = 0
  258.             newCell[i][j] = 0
  259.         end
  260.     end
  261.     cell[6][5] = 1
  262.     cell[7][6] = 1
  263.     cell[7][7] = 1
  264.     cell[6][7] = 1
  265.     cell[5][7] = 1
  266.     livingCells = 5
  267.     intro()
  268.     term.setBackgroundColor(colors.gray)
  269.     term.clear()
  270.     gameLoop()
  271. end
  272.  
  273. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement