Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to code this question but am not really sure as to how if anyone could offer any insight on how to possibly code this let me know.

2. game_of_life
What is The Game of life?
Not to be confused with Hasbro's The Game of Life, this problem refers to British mathematician John Conway's famous cellular automation. The term 'cellular automation' refers to a space (in this case a grid) of cells, which continually transform themselves to create new patterns and states, based on the states of neighboring cells, by following a discrete set of mathematical rules. The simulation models cells living, dying, and multiplying over multiple generations.

The Rules
Any live cell with fewer than two live neighbors dies (as if by underpopulation).
Any live cell with more than three live neighbors dies (as if by overpopulation/overcrowding).
Any live cell with two or three live neighbors lives, unchanged, to the next generation.
Any dead cell with exactly three live neighbors will come to life (as if by reanimation or birth).
Neighbors refer to the eight cells adjacent to any given cell in a grid, with the exception of border cells.

The Assignment
For this assignment, we are asking you to implement Conway's Game of Life using a two-dimensional array as a grid in which to store the cells. Live cells are denoted as * characters, dead cells are denoted by the . character.

Input
As input, you will be given a line containing the number of rows, number of columns, and number of generations to simulate in the form m n g followed by m lines containing the initial state of the grid. 3 <= m,n <= 20 and 0 <= g <= 50

For example, a 4 x 5 grid on which 7 generations of life should be simulated, would be given as follows:

4 5 7
. * * . .
. * . . .
* * * . *
. . . . *
You should store this initial grid state in a multidimensional array as discussed in class.

Your Task
The initial state (generation 0) stored in a grid.

. * * . .
. * . . .
* * * . *
. . . . *
You should then compute the next generation (generation 1) by applying the rules to the previous generation grid (generation 0 in this case).

The state of the grid after computing generation 1 should be as follows:

. * * . .
. . . * .
* * * * .
. * . * .
Important Note: "[Generation n] is created by applying the [...] rules simultaneously to every cell in [generation n-1] — births and deaths happen simultaneously" (Wikipedia). This means that when applying rules to n-1 to compute/create n, you should at no point change values in n-1, only observe them. Only after n has been seeded should you change n-1.

Repeat the above process g times until your grid is in its final state. This means that if you receive a g = 0, your initial state and final state would be the same.

Output
Your output should be the final state of the grid after g generations printed to std::cout in the format above.

Note: Your printed grid should contain no trailing whitespace at the end of each row.

What I have tried:

I am not sure as to where to start coding this problem so any help would be appreciated.
Posted
Updated 14-Nov-19 21:25pm
Comments
Richard MacCutchan 15-Nov-19 3:15am    
Much the same issue as your sliding puzzle question.

Quote:
I am trying to code this question but am not really sure as to how

There is an easy way to get that knowledge, just give it a try and see which part works and which one don't, then elaborate. This is trial and error learning.
As programmer, your job is to create solutions to your problems.

Advice: Learn one or more analyze methods, E.W. Djikstra/N. Wirth Stepwize Refinement/top-Down method is a good start.
Structured Programming.pdf[^]
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]

When you have a big problem, split it in a few smaller/easier problems, once each little problem is solved, the big one is solved too.
- Reading input will probably be the first step.
- Playing Game of Life in middle.
- giving result the last step.

So far, you show no work and didn't stated a question.
 
Share this answer
 
v2
Start by breaking down problem into a series of steps that need to be done. First you need to figure out your storage scheme. Since you were given size constraints it can be either static or dynamic. What you discussed in class is probably your best option. After that, you need to write logic to accept input that defines the game. The next step is perform the logic of the generations of the simulation. Lastly, display the state of the simulation.

Since this is your work I am not going to write any code for you. If you do each of these steps you should be able to get this program written. You can always ask for help but we are here to help you with your code, not to write it for you.

I will give you one hint. Since the game represents cells containing one of two characters, I would make each line of cells as an array of characters. Each cell will have a period or an asterisk. I don't know what you have learned so far so I can't describe how you should store the data.
 
Share this answer
 
Comments
Patrice T 15-Nov-19 11:24am    
+5
To get you started, you need to understand that all programming assignments come down to the following steps:

1. Reading input information
2. Setting up and initializing some data structures
3. Process your data
4. Present the results
Sometimes steps 3 and 4 might influence how you define and set up your data structures. But for a first draft, that's typically not required.

There are typically some additional steps that need to be performed in between, or steps that get repeated, but these four steps are pretty much always there.

Therefore you should check your assignments and find out
- what is there to read
- what data structures do you need
- what should you do with that data
- what are you supposed to display as output

Thankfully, your assignment is already structured to provide this information very clearly:

Start by looking at the 'The Assignment' and 'Input' paragraphs. These give you enough info to define data structures for your program.

Under 'The Task' you find the description what to do. Write a function that takes the input data structures and processes them as described here.

The 'Output' section offers clear instructions what to display at the end. I don't think that should be a problem after solving the previous steps.
 
Share this answer
 
Comments
Patrice T 15-Nov-19 11:22am    
I would swap 1 and 2.
+5
Stefan_Lang 20-Nov-19 3:12am    
Yes, maybe. Thanks for the vote.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900