yeeeeeeeeeeeee

sub do;dzo

Nov 5th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. -- Dynamic neighbor-influenced Game of Life
  2. math.randomseed(os.time())
  3.  
  4. local width, height = 20, 10
  5. local aliveChar = "O"
  6. local deadChar = "o"
  7. local maxRandomInfluence = 0.3 -- max extra random chance
  8.  
  9. -- Initialize grid randomly
  10. local grid = {}
  11. for y = 1, height do
  12. grid[y] = {}
  13. for x = 1, width do
  14. grid[y][x] = math.random() > 0.5 and 1 or 0
  15. end
  16. end
  17.  
  18. -- Count alive neighbors
  19. local function countNeighbors(x, y)
  20. local count = 0
  21. for dy = -1,1 do
  22. for dx = -1,1 do
  23. if not (dx == 0 and dy == 0) then
  24. local nx, ny = x + dx, y + dy
  25. if nx >= 1 and nx <= width and ny >= 1 and ny <= height then
  26. count = count + grid[ny][nx]
  27. end
  28. end
  29. end
  30. end
  31. return count
  32. end
  33.  
  34. -- Update grid
  35. local function updateGrid()
  36. local newGrid = {}
  37. for y = 1, height do
  38. newGrid[y] = {}
  39. for x = 1, width do
  40. local neighbors = countNeighbors(x, y)
  41. local current = grid[y][x]
  42.  
  43. -- Base probability influenced by neighbors
  44. local neighborInfluence = neighbors / 8 -- 0 to 1
  45. local randomChance = math.random()
  46.  
  47. -- Decision influenced by neighbors
  48. local nextState
  49. if randomChance < neighborInfluence then
  50. nextState = 1 -- more neighbors -> more likely alive
  51. elseif randomChance < neighborInfluence + maxRandomInfluence then
  52. nextState = 1 - current -- small chance to flip randomly
  53. else
  54. nextState = 0 -- otherwise dead
  55. end
  56.  
  57. newGrid[y][x] = nextState
  58. end
  59. end
  60. grid = newGrid
  61. end
  62.  
  63. -- Draw grid
  64. local function drawGrid()
  65. term.clear()
  66. term.setCursorPos(1,1)
  67. for y = 1, height do
  68. local line = ""
  69. for x = 1, width do
  70. line = line .. (grid[y][x] == 1 and aliveChar or deadChar)
  71. end
  72. print(line)
  73. end
  74. end
  75.  
  76. -- Main loop
  77. while true do
  78. drawGrid()
  79. updateGrid()
  80. sleep(0.2)
  81. end
  82.  
Advertisement
Add Comment
Please, Sign In to add comment