The game of Life, invented by the mathematician John H. Conway, is intended to m
ID: 3577720 • Letter: T
Question
The game of Life, invented by the mathematician John H. Conway, is intended to model life in a society of organisms. Consider a rectangular array of cells, each of which may contain an organism. If the array is assumed to extend indefinitely in both directions, each cell will have eight neighbors, the eight cells surrounding it.
Birth and deaths occur according to the following rules.
(a)An organism is born in an empty cell that has exactly three neighbors.
(b)An organism dies from isolation if it has fewer than two neighbors.
(c)An organism dies from overcrowding if it has more than three neighbors.
The following display shows the first five generations of a particular configuration of organisms
O
O OOO O OOO
OOO OOO O O O O
O OOO O O O OOO
O O .
Write a program that will start with the first generation above. Your program must generate the next four generations. This can be done with two functions.
Submit your source code in Blackboard.
Read the first generation from a file named LIFE.txt.
Your source code must be well documented.
LIFE.txt is below the outer zeros on the edges should not be included when running the program.
0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
Explanation / Answer
Answer:
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void copy(int array1[52][102], int array2[52][102])
{
for(int j = 0; j < 52; j++)
{
for(int i = 0; i < 102; i++)
array2[j][i] = array1[j][i];
}
}
void life(int array[52][102], char choice)
{
int temp[52][102];
copy(array, temp);
for(int j = 1; j < 51; j++)
{
for(int i = 1; i < 101; i++)
{
if(choice == 'm')
{
int count = 0;
count = array[j-1][i] +
array[j-1][i-1] +
array[j][i-1] +
array[j+1][i-1] +
array[j+1][i] +
array[j+1][i+1] +
array[j][i+1] +
array[j-1][i+1];
if(count < 2 || count > 3)
temp[j][i] = 0;
if(count == 2)
temp[j][i] = array[j][i];
if(count == 3)
temp[j][i] = 1;
}
else if(choice == 'v')
{
int count = 0;
count = array[j-1][i] +
array[j][i-1] +
array[j+1][i] +
array[j][i+1];
if(count < 2 || count > 3)
temp[j][i] = 0;
if(count == 2)
temp[j][i] = array[j][i];
if(count == 3)
temp[j][i] = 1;
}
}
}
copy(temp, array);
}
bool compare(int array1[52][102], int array2[52][102])
{
int count = 0;
for(int j = 0; j < 52; j++)
{
for(int i = 0; i < 102; i++)
{
if(array1[j][i]==array2[j][i])
count++;
}
}
if(count == 52*102)
return true;
else
return false;
}
void print(int array[52][102])
{
for(int j = 1; j < 51; j++)
{
for(int i = 1; i < 101; i++)
{
if(array[j][i] == 1)
cout << '*';
else
cout << ' ';
}
cout << endl;
}
}
int main()
{
int gen0[52][102];
int todo[52][102];
int backup[52][102];
char neighborhood;
char again;
char cont;
bool comparison;
string decoration;
cout << endl << "This program is a C++ implementation of John Conway's Game of Life."
<< endl << "With it, you can simulate how "cells" interact with each other." << endl
<< endl << "There are two types of neighborhoods you can choose, the"
<< endl << "Moore, and the Von Neumann. The Moore neighborhood checks"
<< endl << "all 8 surrounding cells, whereas the Von Neumann checks"
<< endl << "only the 4 cardinal directions: (N, S, E, and W)." << endl
<< endl << "The rules of the "Game of Life" are as follows:" << endl
<< endl << "1. Any live cell with fewer than two live neighbors dies, as if caused by under-population."
<< endl << "2. Any live cell with two or three live neighbors lives on to the next generation."
<< endl << "3. Any live cell with more than three live neighbors dies, as if by overcrowding."
<< endl << "4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction." << endl
<< endl << "The initial configuration (Generation 0) of the board is determined randomly."
<< endl << "Every hundred Generations you will get the option to end or continue the simulation."
<< endl << "If a system becomes "stable" (meaning the system does not change from one"
<< endl << "generation to the next), the simulation will end automatically." << endl << endl;
do
{
do
{
cout << "Which neighborhood would you like to use (m or v): ";
cin >> neighborhood;
}while(neighborhood != 'm' && neighborhood != 'v');
system("clear");
int i = 0;
do
{
srand(time(NULL));
for(int j = 1; j < 51; j++)
{
for (int i = 1; i < 101; i++)
gen0[j][i] = rand() % 2;
}
if(i < 10)
decoration = "#############";
else if(i >= 10 && i < 100)
decoration = "##############";
else if(i >= 100 && i < 1000)
decoration = "###############";
else if(i >= 1000 && i < 10000)
decoration = "################";
else
decoration = "#################";
cout << decoration << endl << "Generation " << i
<< ":" << endl << decoration << endl << endl;
if(i == 0)
copy(gen0, todo);
copy(todo, backup);
print(todo);
life(todo, neighborhood);
i++;
system("sleep .1");
if(i % 100 == 1 && i != 1)
{
cout << endl;
do
{
cout << "Would you like to continue this simulation? (y/n): ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}
comparison = compare(todo, backup);
if(comparison == false)
system("clear");
if(comparison == true)
cout << endl;
}while(comparison == false);
do
{
cout << "Would you like to run another simulation? (y/n): ";
cin >> again;
}while(again != 'y' && again != 'n');
}while(again == 'y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.