Conway's Game of Life as a React web app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.ts 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. export const generateNewGrid = (grid: boolean[][]) => {
  2. return [...grid].map((row, y) => {
  3. const newRow = [...row].map((cell, x) => {
  4. const isAlive = cell;
  5. const livingNeighbors = countLivingNeighbors(grid, x, y);
  6. if (isAlive) {
  7. if (livingNeighbors === 2 || livingNeighbors === 3) {
  8. return true;
  9. } else {
  10. return false;
  11. }
  12. } else {
  13. if (livingNeighbors === 3) {
  14. return true;
  15. }
  16. }
  17. return isAlive;
  18. });
  19. return newRow;
  20. });
  21. };
  22. export const countLivingNeighbors = (
  23. grid: boolean[][],
  24. x: number,
  25. y: number,
  26. ): number => {
  27. return getNeighbors(grid, x, y).filter((el) => el).length;
  28. };
  29. export const getNeighbors = (
  30. grid: boolean[][],
  31. x: number,
  32. y: number,
  33. ): boolean[] => {
  34. const neighbors = [];
  35. const height = grid.length;
  36. const width = grid[0].length;
  37. const isNotOnTopRow = y > 0;
  38. const isNotOnFarLeftRow = x > 0;
  39. const isNotOnBottomRow = y < height - 1;
  40. const isNotOnFarRightRow = x < width - 1;
  41. if (isNotOnTopRow) {
  42. neighbors.push(grid[y - 1][x]); // N
  43. if (isNotOnFarLeftRow) neighbors.push(grid[y - 1][x - 1]); // NW
  44. if (isNotOnFarRightRow) neighbors.push(grid[y - 1][x + 1]); // NE
  45. }
  46. if (isNotOnFarLeftRow) neighbors.push(grid[y][x - 1]); // W
  47. if (isNotOnFarRightRow) neighbors.push(grid[y][x + 1]); // E
  48. if (isNotOnBottomRow) {
  49. neighbors.push(grid[y + 1][x]); // S
  50. if (isNotOnTopRow) neighbors.push(grid[y + 1][x - 1]); // SW
  51. if (isNotOnFarRightRow) neighbors.push(grid[y + 1][x + 1]); // SE
  52. }
  53. return neighbors;
  54. };
  55. export const updateGridDimensions = (
  56. oldDimensions: {
  57. width: number;
  58. height: number;
  59. },
  60. newDimensions: { width?: number; height?: number },
  61. grid: boolean[][],
  62. ): boolean[][] => {
  63. const width = newDimensions.width ?? oldDimensions.width;
  64. const height = newDimensions.height ?? oldDimensions.height;
  65. grid = [...grid];
  66. if (height > oldDimensions.height) {
  67. while (grid.length < height) {
  68. grid.push(Array(width).fill(false));
  69. }
  70. } else if (height < oldDimensions.height) {
  71. while (grid.length > height) {
  72. if (grid.length <= 1) break;
  73. grid.pop();
  74. }
  75. }
  76. if (width > oldDimensions.width) {
  77. grid = grid.map((row) => {
  78. while (row.length < width) {
  79. row.push(false);
  80. }
  81. return row;
  82. });
  83. } else if (width < oldDimensions.width) {
  84. grid = grid.map((row) => {
  85. while (row.length > width) {
  86. if (row.length <= 1) break;
  87. row.pop();
  88. }
  89. return row;
  90. });
  91. }
  92. return grid;
  93. };