Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Replace the arrays or vectors in the game of life with binary search trees below is the code that need modification. #include #include using namespace

Replace the arrays or vectors in the game of life with binary search trees

below is the code that need modification.

#include #include using namespace std; const int too_lonely_count = 1; const int too_crowded_count = 4; const int births_count = 3; const int n_neighbors = 8; const int steps_cols[] = {-1, 1, 1,1,0,-1,-1, 0}; const int steps_rows[] = {-1,-1,-1,1,1, 0,-1,-1}; int n_rows = 0; int n_cols = 0; bool **cells = NULL;; void create(void); void clear(void); bool goodCell(int col, int row); int countPopulation(void); void destroy(void); int countNeighbors(int col, int row); bool nextGeneration(int col, int row); void show(void); int main() { n_rows = 8; n_cols = 8; create(); cells[1][1] = true; cells[n_cols][n_rows] = true; cells[n_cols][1] = true; cells[1][n_rows] = true; cells[4][4] = true; cout << countPopulation() << endl; clear(); cells[4][4] = true; cout << countNeighbors(4,4) << endl; cells[4][5] = true; cout << countNeighbors(4,4) << endl; cells[5][5] = true; cout << countNeighbors(4,4) << endl; show(); clear(); cells[3][2] = true; cells[3][3] = true; cells[3][4] = true; cout << countPopulation() << endl; cells[2][3] = nextGeneration(2,3); cout << endl; show(); cout << countPopulation() << endl; cells[4][3] = true; cells[3][3] = nextGeneration(3,3); cout << countPopulation() << endl; show(); system("pause"); destroy(); return 0; } bool nextGeneration(int col, int row) { bool alive = false; if(goodCell(col,row)) { int neighbor_count = countNeighbors(col,row); if(cells[col][row] == true) alive = too_lonely_count < neighbor_count && neighbor_count < too_crowded_count; else alive = neighbor_count == births_count; } return alive; } void show(void) { for(int row = 1; row <= n_rows; row++) { for(int col = 1; col <= n_cols; col++) if(cells[col][row] == true) cout << 'X'; else cout << ' '; cout << endl; } } int countNeighbors(int col, int row) { int neighbor_count = 0; if(goodCell(col, row)) { for(int neighbor_index = 1; neighbor_index <= n_neighbors; neighbor_index++) { int neighbor_col = col + steps_cols[neighbor_index]; int neighbor_row = row + steps_rows[neighbor_index]; if(goodCell(neighbor_col, neighbor_row) == true && cells[neighbor_col][neighbor_row] == true) neighbor_count++; } } return neighbor_count; } void destroy(void) { for(int col = 1; col <= n_cols; col++) { delete [] cells[col]; cells[col] = NULL; } delete cells; cells = NULL; } void clear(void) { for(int col = 1; col <= n_cols; col++) for(int row = 1; row <= n_rows; row++) cells[col][row] = false; } int countPopulation(void) { int population_count = 0; for(int col = 1; col <= n_cols; col++) for(int row = 1; row <= n_rows; row++) if(cells[col][row] == true) population_count++; return population_count; } bool goodCell(int col, int row) { return 1 <= col && 1 <= row && col <= n_cols && row <= n_rows; } void create(void) { cells = new bool*[n_cols + 1]; for(int col = 1; col <= n_cols; col++) cells[col] = new bool[n_rows + 1]; clear(); } 

The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

Any live cell with less than two live neighbors dies, as if caused by under-population.

Any live cell with two or three live neighbors lives on to the next generation.

Any live cell with more than three live neighbors dies, as if by overcrowding.

Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seedbirths and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.

applying the above rules simultaneously is easy. First, assign the cells of the original 2D array to a new 2D array. Second, look at the original array and follow the rules but apply any changes to the new array. Third, after looking at all the cells in the original array, assign the cells of the new array to the original array. Fourth, and last, discard the new array.

Write a function that counts a cells neighbors. The function has 2 parameters the target cells row and column. The function returns an integer.

Write a function that determines a cells fate does the cell die, does the cell live to the next generation, does the cell die as if by overcrowding, does the cell, necessarily a dead cell, become a life cell. The function has 2 parameters - the target cells row and column. The function returns true if the cell lives and false if it dies.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic6 Database Programming

Authors: John W. Fronckowiak, David J. Helda

1st Edition

ISBN: 0764532545, 978-0764532542

More Books

Students also viewed these Databases questions

Question

Discuss consumer-driven health plans.

Answered: 1 week ago

Question

5. Our efficiency focus eliminates free time for fresh thinking.

Answered: 1 week ago