Game of Life

Page 1 of 1

2 Replies - 1079 Views - Last Post: 11 December 2009 - 08:11 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=144629&amp;s=a5f734a0265aa3acf45cc27434c76ee4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 kira_trinity

Reputation: 0
• Posts: 6
• Joined: 09-December 09

Game of Life

Posted 10 December 2009 - 12:36 AM

I just need some help with next generations of this project, Instructions:

The game of life is a computer simulation of the life and death events of a population of organisms. This program will determine the life, death, and survival of bacteria from one generation to the next, assuming the starting grid of bacteria is generation zero (0). Each cell has a total of up to 8 neighbors, including the 4 immediately adjacent cells and the 4 diagonal cells. The rules for the creation of each cell in the next generation are as follows:

1. If the cell is currently empty:
1. If the cell has exactly three living neighbors, it will come to life in the next generation.
2. If the cell has any other number of living neighbors, it will remain empty.
2. If the cell is currently living:
1. If the cell has one or zero living neighbors, it will die of loneliness in the next generation.
2. If the cell has four or more living neighbors, it will die of overcrowding in the next generation.
3. If the cell has two or three neighbors, it will remain living.
3. All births and deaths occur simultaneously (make sure you don't get this one wrong!).

Here is what I got so far:
```#include <iostream>
#include <fstream>
#include <iomanip>
#include <cassert>
using namespace std;

const int SIZE = 20;

int main()
{
bool life[SIZE][SIZE];
int row, col;

//initialize array
for (int row = 0; row < SIZE; row++)
{
for (int col = 0; col < SIZE; col++)
{
life[row][col] = false;
}
}
//print contents of array after initialization
cout << "  01234567890123456789" << endl;
for (int row = 0; row < SIZE; row++)
{
cout << setw(2) << row;
for (int col = 0; col < SIZE; col++)
{
cout<<life[row][col];
}
cout << endl;
}
ifstream infile("lifedata.txt");
assert(infile);
infile >> row >> col;
while (infile)
{
life[row][col] = true;
infile >> row >> col;
}
infile.close();

//print contents of two-dimensional array

cout << "  01234567890123456789" << endl;
for (int row = 0; row < SIZE; row++)
{
cout << setw(2) << row;
for (int col = 0; col < SIZE; col++)
{
if (life[row][col])
{
cout << "*";
}
else
{
cout << " ";
}
}
cout << endl;
}
system ("PAUSE");
return 0;
}
```

Is This A Good Question/Topic? 0

Replies To: Game of Life

#2 NickDMax

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

Re: Game of Life

Posted 10 December 2009 - 09:43 AM

life is not really a terribly hard program to write (unless you want to optimize it for deep experimentation and need to work with 1000x1000 grids with hundreds of thousands of generations.)

IT will help if you think about functional decomposition... i.e. break things up into functions to help you out.

For example, if you are given a square (x,y) you need to check what the values of the squars are for:
(x-1,y-1), (x-1,y), (x-1, y+1),
(x,y-1), (x, y+1),
(x+1,y-1), (x+1,y), (x+1, y+1)

since the rules deal with "how many" values there are, I generally did a function like say int localSum(int row, int col, char *grid) which just added a 1 or 0 depending upon the cell entry. You can then test that with the conditions.

Though you will have to keep track of grid boundaries and boundary conditions.

#3 kira_trinity

Reputation: 0
• Posts: 6
• Joined: 09-December 09

Re: Game of Life

Posted 11 December 2009 - 08:11 PM

NickDMax, on 10 Dec, 2009 - 08:43 AM, said:

life is not really a terribly hard program to write (unless you want to optimize it for deep experimentation and need to work with 1000x1000 grids with hundreds of thousands of generations.)

IT will help if you think about functional decomposition... i.e. break things up into functions to help you out.

For example, if you are given a square (x,y) you need to check what the values of the squars are for:
(x-1,y-1), (x-1,y), (x-1, y+1),
(x,y-1), (x, y+1),
(x+1,y-1), (x+1,y), (x+1, y+1)

since the rules deal with "how many" values there are, I generally did a function like say int localSum(int row, int col, char *grid) which just added a 1 or 0 depending upon the cell entry. You can then test that with the conditions.