Question
Hello., I got this assignment wrote some code but I don't know how to write the rest, I added my code at the end, If
Hello.,
I got this assignment wrote some code but I don't know how to write the rest, I added my code at the end, If I could get some help to finish the code it would be great
thank you.
The game of Life was designed in the 1970s by the mathematician J.H. Conway. The game gained popularity after appearing in a Scientific American article, and took the computing world by storm. The game is a simulation that models the life cycle of bacteria, providing entertainment wrapped up in what some would call mathematical elegance. (For some history on the game, as well as the rise of computing in general, I highly recommend the book Hackers, by S. Levy.) The game itself is played on a two-dimensional grid. Each grid location is either empty or occupied by a single cell (X). A locations neighbors are any of the cells in the surrounding eight adjacent locations. (At this point you should be thinking about 2-d arrays.) Rules of the Game: The simulation starts with an initial pattern of cells and computes successive generations according to the following rules: 1. A location that has one or fewer neighbors will be empty in the next generation. If a cell was in that location, it dies of loneliness. (The fate of many a computer scientist) 2. A location with two neighbors remains stable. If there was a cell, theres still a cell. If it was empty, its still empty. 3. A location with three neighbors will contain a cell in the next generation. If it currently has a cell, the cell lives on. If its empty, a new cell is born. 4. A location with four or more neighbors will be empty in the next generation due to overcrowding. 5. The births and deaths that take one generation to the next must all take place simultaneously. When computing a new generation, the births and deaths in that generation can not affect other births and deaths in that generation. Not adhering to this guideline will really mess up your simulation, so be careful. An easy way to get around this is to have 2 versions of the grid. One is for the current generation, and the other is for computing the next generation (based on the current generation) without side effects. When you are done computing the next generation you can either copy it into the current generation grid, or just switch a reference. (In the database community, this little hack is called shadow paging.) Boundary Conditions: One of the gray areas of the program is how to calculate neighbors for cells on the grid boundaries. To make things interesting, at startup your program will ask the user which of the following 2 modes they want to run the simulation in, and calculate accordingly: Classic mode: All locations off the grid are considered to be empty. Doughnut mode: The grid is wrapped around itself horizontally and vertically, resulting in a torus (doughnut) shape. In this mode, any reference off the right-hand-side is wrapped around to the left-most column of the same row and vice-versa. Any reference off the top wraps to the bottom in the same column. Corners are best illustrated with an example: n n n n n n n n In the above, the shaded square has 8 possible neighbors (depending if the cells are occupied), marked with an n. Mirror Mode: References off the grid are bounced back as though the wall were a mirror. This means the reflection of a cell could count as its own neighbor. For a corner cell, if it was occupied, its reflection would count as 3 neighbors1 for the vertical reflection, l for the horizontal reflection, and 1 for the diagonal reflection. As an example of mirror mode: x x x x x x x The x stands for an occupied cell. If the shaded cell were empty, then it would have 4 neighbors. If it were occupied, it would have 5 neighbors, since the neighbor directly above the shaded square is a reflection of the shaded square itself. The Program: When the program starts, you should ask the user if they wish to provide a map file of the world in which the simulation will be carried out, or if they would like a random assignment. If they want a random assignment, prompt for the dimensions of the world. Then prompt for a decimal value (greater than 0 and less than or equal to 1) representing the initial population density of the world. Randomly generate the initial cell configuration using the dimension and density inputs. If the user wants to provide a map file, then prompt for the file path. The file should be a text file with the following configuration: 5 7 ---X--X -X--X-- -----XX X-X-X-- -----XX The first line is height of the grid (number of rows) and the second is the width of the grid (number of columns). The following lines represent rows of the grid. An X stands for an occupied cell. A dash (-) represents an empty cell. Once a generation is calculated you will output it to standard out as a text grid. (Again, a -represents an empty grid and a X represents an occupied grid.) Before each grid, you should output the generation number, starting with 0 for the initial configuration. It is EXTREMELY important that you follow this convention or there will be issues with the automated test scripts I use for grading. In order to make it easy for the user to see what is going on in the program, you should ask the user at the beginning of the program if they want a brief pause between generations (see the system(pause) function), if they want to have to press Enter to display the next generation, or if they want to output everything to a file in which case they should be prompted for a file name. To summarize, when the program is run it should: 1. Ask the user if they wish to start with a random configuration, or specify a flatfile configuration. Prompt appropriately depending on the response. 2. Ask the user what kind of boundary mode to run in. 3. Ask the user if they want a brief pause between generations, if they want to have to press the enter key, or if they want to output to a file. 4. Carry out the simulation 5. If the simulation is infinite, just keep running. But if the world becomes empty or stabilizes, the simulation should halt and ask the user to press enter to exit the program. Programming Strategy: Implementing the game is not an altogether simple task. An elegant solution will take some time, and so it is important for you to begin ASAP. You should know that if you plan out your program ahead of time, the solution can be surprisingly concise. Besides getting experience with arrays, this program also offers practice at program decompositionlearning how to break up your code into manageable modules. With the right decomposition, the program can be much easier to write, debug, and comment. The wrong decomposition can make writing the game a nightmare. Take your time to plan your strategy before you codeit will pay off. (In other words, use an OO approachdont just do everything in main()) Also, make sure to develop and debug incrementally. With a small program, it is often easy to write all your code and then try to debug it. This is unlikely to be the case for this program. Do yourself a favor and test as you go along.
my code:
#include
const int gridsize;
void Display(bool grid[gridsize+1][gridsize+1]){ for(int a = 1; a < gridsize; a++){ for(int b = 1; b < gridsize; b++){ if(grid[a][b] == true){ std::cout << " *"; } else{ std::cout << " "; } if(b == gridsize-1){ std::cout << std::endl; } } } } //This copy's the grid for comparision purposes. void CopyGrid (bool grid[gridsize+1][gridsize+1],bool grid2[gridsize+1][gridsize+1]){ for(int a =0; a < gridsize; a++){ for(int b = 0; b < gridsize; b++){ grid2[a][b] = grid[a][b]; } } } //Calculates Life or Death void liveOrDie(bool grid[gridsize+1][gridsize+1]){ bool grid2[gridsize+1][gridsize+1] = {}; CopyGrid(grid, grid2); for(int a = 1; a < gridsize; a++){ for(int b = 1; b < gridsize; b++){ int life = 0; for(int c = -1; c < 2; c++){ for(int d = -1; d < 2; d++){ if(!(c == 0 && d == 0)){ if(grid2[a+c][b+d]) {++life;} } } } if(life < 2) {grid[a][b] = false;} else if(life == 3){grid[a][b] = true;} else if(life > 3){grid[a][b] = false;} } } }
int main(){
//const int gridsize = 50; bool grid[gridsize+1][gridsize+1] = {};
//Still have to manually enter the starting cells. grid[gridsize/2][gridsize/2] = true; grid[gridsize/2-1][gridsize/2] = true; grid[gridsize/2][gridsize/2+1] = true; grid[gridsize/2][gridsize/2-1] = true; grid[gridsize/2+1][gridsize/2+1] = true;
while (true){ //The following copies our grid.
Display(grid); //This is my display. liveOrDie(grid); //calculate if it lives or dies.
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started