Guest User

Untitled

a guest
Dec 13th, 2015
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. local game_state = {
  2. size = 10,
  3. field = {
  4. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  5. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  6. {0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
  7. {0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
  8. {0, 1, 1, 1, 0, 0, 0, 0, 0, 0},
  9. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  10. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  11. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  12. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  13. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  14. },
  15. time = 0
  16. }
  17.  
  18. function game_state:next()
  19. local neighbours = function(i, j)
  20. local left = (i > 1) and (i - 1) or self.size
  21. local right = (i < self.size) and i + 1 or 1
  22. local up = (j > 1) and j - 1 or self.size
  23. local down = (j < self.size) and j + 1 or 1
  24. local field = self.field
  25. return {
  26. field[left][up], field[i][up], field[right][up],
  27. field[left][j], field[right][j],
  28. field[left][down], field[i][down], field[right][down]
  29. }
  30. end
  31.  
  32. local sum = function(arr)
  33. local s = 0
  34. for i, v in ipairs(arr) do
  35. s = s + v
  36. end
  37. return s
  38. end
  39.  
  40. local temp = {}
  41. for i = 1, self.size do
  42. temp[i] = {}
  43. for j = 1, self.size do
  44. local n = sum(neighbours(i, j))
  45. if self.field[i][j] == 1 then
  46. temp[i][j] = (n < 2 or 3 < n) and 0 or 1
  47. else
  48. temp[i][j] = (n == 3) and 1 or 0
  49. end
  50. end
  51. end
  52. self.field = temp
  53. return temp
  54. end
  55.  
  56. local g = love.graphics
  57. local dimension = 800
  58. local update_interval = 0.15
  59.  
  60. function love.load()
  61. love.window.setMode(dimension, dimension)
  62. end
  63.  
  64. function love.draw()
  65. local cell_size = dimension / game_state.size
  66. for i = 1, game_state.size do
  67. for j = 1, game_state.size do
  68. if game_state.field[i][j] == 1 then
  69. g.rectangle("fill",
  70. cell_size * (j - 1), cell_size * (i - 1),
  71. cell_size, cell_size)
  72. end
  73. end
  74. end
  75. end
  76.  
  77. function love.update(dt)
  78. game_state.time = game_state.time + dt
  79. if game_state.time > update_interval then
  80. game_state:next()
  81. game_state.time = 0
  82. end
  83. end
Add Comment
Please, Sign In to add comment