Guest User

Untitled

a guest
Sep 15th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. const grid = document.getElementById("grid");
  2. const gridSize = 20;
  3. let cells = [];
  4.  
  5. function createGrid() {
  6. for (let y = 0; y < gridSize; y++) {
  7. cells[y] = [];
  8. for (let x = 0; x < gridSize; x++) {
  9. const cell = document.createElement("div");
  10. cell.className = 'cell dead';
  11. cell.addEventListener('click', () => toggleCell(x, y));
  12. grid.appendChild(cell);
  13. cells[y][x] = 'dead';
  14. }
  15. }
  16. }
  17.  
  18. function toggleCell(x, y) {
  19. const cellTypes = ['dead', 'introvert', 'extrovert', 'reproducer', 'killer'];
  20. let currentIndex = cellTypes.indexOf(cells[y][x]);
  21. let nextIndex = (currentIndex + 1) % cellTypes.length;
  22. cells[y][x] = cellTypes[nextIndex];
  23. document.querySelectorAll('.cell')[y * gridSize + x].className = `cell ${cellTypes[nextIndex]}`;
  24. }
  25.  
  26. function nextGeneration() {
  27. const newCells = JSON.parse(JSON.stringify(cells));
  28.  
  29. for (let y = 0; y < gridSize; y++) {
  30. for (let x = 0; x < gridSize; x++) {
  31. const neighbors = getNeighbors(x, y);
  32. const currentType = cells[y][x];
  33. const countType = countNeighborTypes(neighbors);
  34.  
  35. switch (currentType) {
  36. case 'dead':
  37. if (countType.reproducer >= 1) newCells[y][x] = 'dead'; // Dead cells are revived only by reproducers
  38. break;
  39. case 'introvert':
  40. if (countType.introvert + countType.extrovert + countType.reproducer + countType.killer > 3 ||
  41. countType.killer > 0) newCells[y][x] = 'dead';
  42. break;
  43. case 'extrovert':
  44. if (neighbors.length === 0) newCells[y][x] = 'dead';
  45. break;
  46. case 'reproducer':
  47. if (neighbors.length === 0) newCells[y][x] = 'dead';
  48. else newCells[y][x] = 'reproducer'; // Reproducers clone themselves but die if isolated
  49. break;
  50. case 'killer':
  51. if (neighbors.length <= 2 || countType.introvert + countType.extrovert + countType.reproducer + countType.killer >= 3)
  52. newCells[y][x] = 'dead'; // Killers die if overcrowded or insufficient neighbors
  53. break;
  54. }
  55. }
  56. }
  57.  
  58. cells = newCells;
  59. updateGrid();
  60. }
  61.  
  62. function getNeighbors(x, y) {
  63. const neighbors = [];
  64. for (let dx = -1; dx <= 1; dx++) {
  65. for (let dy = -1; dy <= 1; dy++) {
  66. let nx = x + dx, ny = y + dy;
  67. if (dx === 0 && dy === 0) continue;
  68. if (nx >= 0 && ny >= 0 && nx < gridSize && ny < gridSize) {
  69. neighbors.push(cells[ny][nx]);
  70. }
  71. }
  72. }
  73. return neighbors;
  74. }
  75.  
  76. function countNeighborTypes(neighbors) {
  77. let count = { introvert: 0, extrovert: 0, reproducer: 0, killer: 0, dead:0 };
  78. for (const type of neighbors) {
  79. if (count.hasOwnProperty(type)) {
  80. count[type]++;
  81. }
  82. }
  83. return count;
  84. }
  85.  
  86. function updateGrid() {
  87. for (let y = 0; y < gridSize; y++) {
  88. for (let x = 0; x < gridSize; x++) {
  89. const cellIndex = y * gridSize + x;
  90. const cellType = cells[y][x];
  91. document.querySelectorAll('.cell')[cellIndex].className = `cell ${cellType}`;
  92. }
  93. }
  94. }
  95.  
  96. createGrid();
Advertisement
Add Comment
Please, Sign In to add comment