:] Awesome title.
I have a program called Life. This program creates an array from the numbers in a .txt file, then it puts it into an array. The cells in the array will have "bacteria" if they are named in the .txt file. The bacteria will be removed if it does not have 2-3 neighbors. The bacteria in an empty cell will be added if it has exactly 3 neighbors.
My program goes through five "generations". Everything works, but 8 cells. I cant find a flaw in my code or theory, so I am looking for help.
Also, is there any better way to print out the grid numbers at the top? (the column rows)
I've tried using setw and set(precision), but the setw will have to many spaces, and the set(precision) only works with "double", but "double" cannot be used in arrays.
Coordinates that are wrong...
row,col
14,12
17,13
17,14
18,12
18,13
18,14
18,15
19,11
19,14
Instructions if comments are not clear enough...
Background:
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). The rules for the creation of the next generation are as follows:
1. Every empty cell with three living neighbors will come to life in the next generation.
2. Any cell with one or zero neighbors will die of loneliness, while any cell with four or more neighbors will die from overcrowding.
3. Any cell with two or three neighbors will live into the next generation.
4. All births and deaths occur simultaneously.
Assignment:
1. Write a program that solves the game of Life. The size of the grid will be a square
20 x 20.
2. The original grid of bacteria will be supplied to your program from a text file.
a. The first line will contain the number (N) of bacteria locations in the file.
b. What follows are N pairs of data, one pair of numbers per line.
c. The first value of each line indicates the row location while the second value on the line indicates the column location.
d. The data file values are given as: 1 <= Row <= 20 and 1 <= Col <= 20.
3. After your program has initialized the grid with generation 0, your program must allow Life to proceed for 5 generations.
4. Display the final results on the screen and determine the following statistical information:
a. The number of living cells in the entire board.
b. The number of living cells in row 10.
c. The number of living cells in column 10.
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <iomanip>
using namespace std;
const int MAX = 21;
void input (int cellGrid[][MAX])
{
ifstream infile; //declare a file variable
int row;
int column;
int number;
infile.open("life100.txt"); //open a file
assert (infile); //make sure file opened
infile >> number; //inserts number of "bacteria"
while(infile>>row>>column) { //inserts bacteria places into array
cellGrid[row][column]=1; //makes it equal one if bacteria is present
}
infile.close(); //closes file
}
void clearLife1(int finalGrid[][MAX]) //clears finalGrid
{
for(int row=1;row<MAX;row++) //goes through each row
{
for(int col=1;col<MAX;col++) //goes through each column
{
finalGrid[row][col]=0; //clears the specific cell
}
}
}
void clearLife2(int cellGrid[][MAX]) //clears cellGrid
{
for(int row=1;row<MAX;row++) //goes through each row
{
for(int col=1;col<MAX;col++) //goes through each column
{
cellGrid[row][col]=0; //clears the specific cell
}
}
}
void cellToFinal (int cellGrid[][MAX], int finalGrid[][MAX]) //changes cellGrid bacteria and moves to finalGrid
{
int neighborCounter;
clearLife1 (finalGrid);
for (int row=1; row<MAX; row++)
{
for (int column=1; column<MAX; column++) //counts up the neighbors
{
neighborCounter = 0;
if (cellGrid [row-1][column] == 1){
neighborCounter++;
}
if (cellGrid [row-1][column+1] == 1){
neighborCounter++;
}
if (cellGrid [row-1][column-1] == 1){
neighborCounter++;
}
if (cellGrid [row][column+1] == 1){
neighborCounter++;
}
if (cellGrid [row][column-1] == 1){
neighborCounter++;
}
if (cellGrid [row+1][column+1] == 1){
neighborCounter++;
}
if (cellGrid [row+1][column] == 1){
neighborCounter++;
}
if (cellGrid [row+1][column-1] == 1){
neighborCounter++;
}
if (cellGrid [row][column] == 1) //if the cell is not empty then...
{
if (neighborCounter == 2 or neighborCounter == 3) //if the cell has two or three neighbors then...
{
finalGrid [row][column] = 1; //the cell lives
}
else if (neighborCounter != 2 and neighborCounter !=3) //if the cell does not have two or three neighbors then...
{
finalGrid [row][column] = 0; //the cell died
}
}
else if (cellGrid [row][column] == 0) //
{
if (neighborCounter == 3)
{
finalGrid [row][column] = 1;
}
}
}
}
}
void finalToCell (int cellGrid[][MAX], int finalGrid[][MAX]) //changes finalGrid bacteria and moves to cellGrid
{
int neighborCounter;
clearLife2 (cellGrid);
for (int row=1; row<MAX; row++)
{
for (int column=1; column<MAX; column++)
{
neighborCounter = 0;
if (finalGrid [row-1][column] == 1){
neighborCounter++;
}
if (finalGrid [row-1][column+1] == 1){
neighborCounter++;
}
if (finalGrid [row-1][column-1] == 1){
neighborCounter++;
}
if (finalGrid [row][column+1] == 1){
neighborCounter++;
}
if (finalGrid [row][column-1] == 1){
neighborCounter++;
}
if (finalGrid [row+1][column+1] == 1){
neighborCounter++;
}
if (finalGrid [row+1][column] == 1){
neighborCounter++;
}
if (finalGrid [row+1][column-1] == 1){
neighborCounter++;
}
if (finalGrid [row][column] == 1)
{
if (neighborCounter == 2 || neighborCounter == 3)
{
cellGrid [row][column] = 1;
}
else if (neighborCounter < 2 || neighborCounter > 3)
{
cellGrid [row][column] = 0;
}
}
else if (finalGrid [row][column] == 0)
{
if (neighborCounter == 3)
{
cellGrid [row][column] = 1;
}
}
}
}
}
void switcher (int cellGrid[][MAX], int finalGrid[][MAX])
{
int asdf = 0;
for (int counter=0; counter<=5; counter++)
{
if (asdf == 0)
{
cellToFinal (cellGrid, finalGrid);
asdf ++;
}
else if (asdf == 1)
{
finalToCell (cellGrid, finalGrid);
asdf --;
}
}
}
void work (int cellGrid[][MAX], int finalGrid[][MAX], int &number, int &rowTen, int &columnTen)
{
switcher (cellGrid, finalGrid);
cout << " 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20" << endl;
for (int row=1; row<MAX; row++)
{
cout <<setw(2)<< row;
for (int column=1; column<MAX; column++)
{
cout << " ";
if (finalGrid[row][column] == 0){
cout << " ";
}
else if (finalGrid[row][column] == 1){
cout << " *";
if (column == 10)
{
columnTen ++;
}
if (row == 10)
{
rowTen ++;
}
number ++;
}
}
cout << endl;
}
}
int main()
{
int number = 0;
int columnTen = 0;
int rowTen = 0;
int cellGrid[MAX][MAX] = {'\0'}; //initial array
int finalGrid[MAX][MAX] = {'\0'}; //final array to print out
input (cellGrid); //inputs items from a .txt file into the array
work (cellGrid, finalGrid, number, columnTen, rowTen); //changes array
cout << "\n______________________________" << endl;
cout << "The number of bacteria is... " << number << ". " << endl;
cout << "The number of bacteria in column ten is..." << columnTen << ". " << endl;
cout << "The number of bacteria in row ten is..." << rowTen << ". " << endl;
cout << endl;
return 0;
}
life.txt
100
1 3
1 7
1 8
1 11
1 12
1 17
2 1
2 5
2 8
3 7
3 13
3 16
3 20
4 1
4 5
4 15
4 17
4 18
5 1
5 9
5 13
5 15
5 16
6 1
6 3
6 5
6 8
6 14
6 18
7 12
7 15
7 16
7 19
8 5
8 11
8 14
8 15
9 7
9 12
9 13
9 15
9 18
9 20
10 7
10 9
10 14
10 15
11 4
11 11
11 20
12 7
12 8
12 10
12 11
12 18
13 1
13 3
13 13
14 1
14 2
14 8
14 9
14 15
14 16
15 8
15 10
15 13
15 14
15 17
15 19
16 1
16 3
16 7
16 9
16 11
16 12
16 17
16 18
17 3
17 4
17 19
18 3
18 8
18 12
18 14
18 20
19 1
19 6
19 7
19 12
19 13
19 14
19 15
20 4
20 5
20 7
20 9
20 12
20 17
20 18