Advertisement
Cassimus

life game

Apr 2nd, 2025
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. const int SIZE = 20;
  5. const string CELL_SIGN = "X";
  6.  
  7. void display_board(int board[SIZE][SIZE])
  8. {
  9. for (int row = 0; row < SIZE; row++)
  10. {
  11. for (int col = 0; col < SIZE; col++)
  12. {
  13. if (board[row][col] == 0)
  14. {
  15. cout << " ";
  16. }
  17. else
  18. {
  19. cout << CELL_SIGN;
  20. }
  21. }
  22. cout << endl;
  23. }
  24. }
  25.  
  26. int count_neighbours(int board[SIZE][SIZE], int row, int col)
  27. {
  28. int neighbours = 0;
  29.  
  30. int up, down, left, right, vertical_center, horizontal_center;
  31.  
  32. if (row == 0)
  33. {
  34. up = SIZE-1;
  35. vertical_center = row;
  36. down = row + 1;
  37. }
  38. else if (row == SIZE - 1)
  39. {
  40. up = row - 1;
  41. vertical_center = row;
  42. down = 0;
  43. }
  44. else
  45. {
  46. up = row - 1;
  47. vertical_center = row;
  48. down = row + 1;
  49. }
  50.  
  51. if(col == 0)
  52. {
  53. left = SIZE-1;
  54. horizontal_center = col;
  55. right = col + 1;
  56. }
  57. else if (col == SIZE-1)
  58. {
  59. left = col-1;
  60. horizontal_center = col;
  61. right = 0;
  62. }
  63. else
  64. {
  65. left = col-1;
  66. horizontal_center = col;
  67. right = col + 1;
  68. }
  69.  
  70. neighbours += board[up][left];
  71. neighbours += board[up][horizontal_center];
  72. neighbours += board[up][right];
  73. neighbours += board[vertical_center][left];
  74. neighbours += board[vertical_center][right];
  75. neighbours += board[down][left];
  76. neighbours += board[down][horizontal_center];
  77. neighbours += board[down][right];
  78.  
  79. return neighbours;
  80. }
  81.  
  82. void generation(int board[SIZE][SIZE])
  83. {
  84. int new_board[SIZE][SIZE] = {0};
  85.  
  86. for (int row = 0; row < SIZE; row++)
  87. {
  88. for (int col = 0; col < SIZE; col++)
  89. {
  90. int neighbours= count_neighbours(board, row, col);
  91.  
  92. if (board[row][col] == 1 && ( neighbours < 2 || neighbours > 3))
  93. {
  94. new_board[row][col] = 0;
  95. }
  96. else if (board[row][col] == 1)
  97. {
  98. new_board[row][col] = 1;
  99. }
  100. else if (board[row][col] == 0 && neighbours == 3)
  101. {
  102. new_board [row][col] = 1;
  103. }
  104.  
  105. }
  106. }
  107.  
  108. for (int row = 0; row < SIZE; row++)
  109. {
  110. for (int col = 0; col < SIZE; col++)
  111. {
  112. board[row][col] = new_board[row][col];
  113. }
  114. }
  115.  
  116. }
  117.  
  118. int main()
  119. {
  120. int board[SIZE][SIZE] = {0};
  121.  
  122. board[9][8] = 1;
  123. board[9][10] = 1;
  124. board[10][10] = 1;
  125. board[10][9] = 1;
  126. board[11][9] = 1;
  127.  
  128.  
  129. display_board(board);
  130. generation(board);
  131. display_board(board);
  132.  
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement