Advertisement
NickM13

[ComputerCraft Monitor] Game of Life

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