Advertisement
Cassimus

gra w zycie

Mar 26th, 2025
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 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. int main()
  83. {
  84.     int board[SIZE][SIZE] = {0};
  85.  
  86.     board[9][8] = 1;
  87.     board[9][10] = 1;
  88.     board[10][10] = 1;
  89.     board[10][9] = 1;
  90.     board[11][9] = 1;
  91.  
  92.     display_board(board);
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement