Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int SIZE = 20;
- const string CELL_SIGN = "X";
- void display_board(int board[SIZE][SIZE])
- {
- for (int row = 0; row < SIZE; row++)
- {
- for (int col = 0; col < SIZE; col++)
- {
- if (board[row][col] == 0)
- {
- cout << " ";
- }
- else
- {
- cout << CELL_SIGN;
- }
- }
- cout << endl;
- }
- }
- int count_neighbours(int board[SIZE][SIZE], int row, int col)
- {
- int neighbours = 0;
- int up, down, left, right, vertical_center, horizontal_center;
- if (row == 0)
- {
- up = SIZE-1;
- vertical_center = row;
- down = row + 1;
- }
- else if (row == SIZE - 1)
- {
- up = row - 1;
- vertical_center = row;
- down = 0;
- }
- else
- {
- up = row - 1;
- vertical_center = row;
- down = row + 1;
- }
- if(col == 0)
- {
- left = SIZE-1;
- horizontal_center = col;
- right = col + 1;
- }
- else if (col == SIZE-1)
- {
- left = col-1;
- horizontal_center = col;
- right = 0;
- }
- else
- {
- left = col-1;
- horizontal_center = col;
- right = col + 1;
- }
- neighbours += board[up][left];
- neighbours += board[up][horizontal_center];
- neighbours += board[up][right];
- neighbours += board[vertical_center][left];
- neighbours += board[vertical_center][right];
- neighbours += board[down][left];
- neighbours += board[down][horizontal_center];
- neighbours += board[down][right];
- return neighbours;
- }
- void generation(int board[SIZE][SIZE])
- {
- int new_board[SIZE][SIZE] = {0};
- for (int row = 0; row < SIZE; row++)
- {
- for (int col = 0; col < SIZE; col++)
- {
- int neighbours= count_neighbours(board, row, col);
- if (board[row][col] == 1 && ( neighbours < 2 || neighbours > 3))
- {
- new_board[row][col] = 0;
- }
- else if (board[row][col] == 1)
- {
- new_board[row][col] = 1;
- }
- else if (board[row][col] == 0 && neighbours == 3)
- {
- new_board [row][col] = 1;
- }
- }
- }
- for (int row = 0; row < SIZE; row++)
- {
- for (int col = 0; col < SIZE; col++)
- {
- board[row][col] = new_board[row][col];
- }
- }
- }
- int main()
- {
- int board[SIZE][SIZE] = {0};
- board[9][8] = 1;
- board[9][10] = 1;
- board[10][10] = 1;
- board[10][9] = 1;
- board[11][9] = 1;
- display_board(board);
- generation(board);
- display_board(board);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement