Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Program The mathematician John Horton Conway invented the “Game of Life.” Th

ID: 3760721 • Letter: C

Question

C++ Program

The mathematician John Horton Conway invented the “Game of Life.” Though not a “game” in any traditional sense, it provides interesting behavior that is specified with only a few rules. This project asks you to write a program that allows you to specify an initial configuration. The program follows the rules of LIFE to show the continuing behavior of the configuration.

LIFE is an organism that lives in a discrete, two-dimensional world. While this world is actually unlimited, we don’t have that luxury, so we restrict the array to 80 characters wide by 22 character positions high. If you have access to a larger screen, by all means use it.

This world is an array with each cell capable of holding one LIFE cell. Generations mark the passing of time. Each generation brings births and deaths to the LIFE community. The births and deaths follow the following set of rules:

1) WE define each cell to have eight neighbor cells. The neighbors of a cell are the cells directly above, below, to the right, to the left, diagonally above to the right and left, and diagonally below to the right and left.

2) If an occupied cell has zero or one neighbors, it dies of loneliness. If an occupied cell has more than three neighbors, it dies of overcrowding.

3) If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell to replace the empty cell.

4) Births and deaths are instantaneous and occur at the changes of generation. A cell dying for whatever reason may help cause birth, but a newborn cell cannot resurrect a cell that is dying, nor will a cell’s death prevent the death of another, say, by reducing the local population.

Notes: Some configurations grow from relatively small starting configurations. Others move across the region. It is recommended that for text output you use a rectangular array of char with 80 columns and 22 rows to store the LIFE world’s successive generations. Use an asterisk * to indicate a living cell, and use a blank to indicate an empty (or dead) cell. If you have a screen with more rows than that, by all means make use of the whole screen.

Examples:

   ***

  

   becomes

   *

   *

   *

   then becomes

  

   ***

  

   again, and so on.

Suggestions: Look for stable configurations. That is, look for communities that repeat patterns continually. The number of configurations in the repetition is called the period. There are configurations that are fixed, which continue without change. A possible project is to find such configurations.

Hints: Define a void function named generation that takes the array we call world, an 80-column by 22-row array of char, which contains the initial configuration. The function scans the array and modifies the cells, marking the cells with births and deaths in accord with the rules listed earlier. This involves examining each cell in turn, either killing the cell, letting it live, or, if the cell is empty, deciding whether a cell should be born. There should be a function display that accepts the array world and displays the array on the screen. Some sort of time delay is appropriate between calls to generation and display. To do this, your program should generate and display the next generation when you press return. you are at liberty to automate this, but automation is not necessary for the program.

Here is a prime example of how the program should perform: <http://www.4shared.com/web/preview/pdf/Rji71zmfce>

- In the picture it shows a U shape with dimensions: height = 3 and width = 3 in astericks. This is saved in the array world as a char value (char world[][] = {the 22 by 80 filled with the 0's and 1's/astericks}), along with the surrounding zeros in the two dimensional space whose boundaries are 22 rows by 80 columns. In the program, when we type "n" and hit enter, it should show the second iteration/generation shown in the picture.

Here is the given pseudo code in top-down design: <http://www.4shared.com/web/preview/pdf/QeFKxUMnce>

- With this code, we break the whole problem down into smaller portions, or code blocks.

I've been stuck on this code for over two days straight and can't seem to get it to work. I've looked up many codes in Chegg and Google and most randomize their program, whereas mine starts off as a fixed U shape. Any help/tips is appreciated.

Explanation / Answer

#define WIDTH 10
#define HEIGHT 4
#define QUIT 'q'
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <stdlib.h>
#include "World.h"
using std::cout;
using std::cerr;
using std::cin;
using std::endl;
using std::logic_error;
enum DrawSymbols { ALIVE = '*', DEAD = '.' };
void display_generation( char world[WIDTH][HEIGHT], size_t &gen );
void advance_generation( char world[WIDTH][HEIGHT] );
bool confirm_continue( );
int main ( int argc, char *argv[] )
{
cout << "~~~ The Game of Life ~~~" << endl;
try
{
std::ostringstream errmsg;
if ( argc <= 1 )
{
errmsg << "Error: No input file argument provided.";
throw logic_error( errmsg.str() );
}
else
{
std::ifstream infile;
infile.open(argv[1]);
if ( infile.fail( ) )
{
errmsg << "Error: Could not open input file '" << errmsg << "'";
throw logic_error( errmsg.str() );
}
cout << "opened input file '" << argv[1] << "'." << endl;
std::string nextline;
size_t width(0), height(0);
while ( getline(infile, nextline) )
{
height++;
if (height > 1 && width != nextline.length())
{
errmsg << "Parse error: line " << height-1 << " is " << width <<" cols wide, but line " << height << " is " << nextline.length() << " cols wide.";
throw logic_error( errmsg.str() );
}
width = nextline.length();
for (size_t i = 0; i < width; i++)
switch( nextline[i] )
{
case ALIVE:
break;
case DEAD:
break;
default:
errmsg << "Parse error, line " << height << ", col: " << i+1 <<" invalid char '" << nextline[i] << "'";
throw logic_error( errmsg.str() );
}
}
cerr << "height " << height << ", width " << width << endl;
infile.close( );
}
}
catch (logic_error e)
{
cerr << e.what( ) << endl;
exit(1);
}
char world[WIDTH][HEIGHT];
for (int x = 0; x < WIDTH; x++)
for (int y = 0; y < HEIGHT; y++)
world[x][y] = DEAD;
world[1][1] = ALIVE;
world[1][2] = ALIVE;
world[1][3] = ALIVE;
cout << "Press enter to pass a generation. '" << QUIT << "' key quits." << endl;
size_t gen = 0;
do
{
display_generation( world, gen );
advance_generation( world );
}
while ( confirm_continue( ) );
cout << "Stopped after generation " << gen-1 << endl;
return 0;
}
bool confirm_continue( )
{
bool retval(true);
char symbol;
do
{
cin.get( symbol );
if ( tolower(symbol) == QUIT )
{
retval = false;
break;
}
}
while ( symbol != ' ' );
return retval;
}
void display_generation( char world[WIDTH][HEIGHT], size_t &gen )
{
cout << "Generation " << gen << endl;
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
cout << world[x][y];
cout << endl;
}
gen++;
}
void advance_generation( char world[WIDTH][HEIGHT] )
{
char next[WIDTH][HEIGHT];
cout << endl;
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++)
{
int l = (x-1 <= 0) ? x-1 : WIDTH-1;
int r = (x+1 < WIDTH) ? x+1 : 0;
int d = (y-1 <= 0) ? y-1 : HEIGHT-1;
int u = (y+1 < HEIGHT) ? y+1 : 0;
int neighbors = 0;
if (world[l][u] == ALIVE) neighbors++;
if (world[x][u] == ALIVE) neighbors++;
if (world[r][u] == ALIVE) neighbors++;
if (world[l][y] == ALIVE) neighbors++;
//if (world[x][y] == ALIVE) neighbors++;
if (world[r][y] == ALIVE) neighbors++;
if (world[l][d] == ALIVE) neighbors++;
if (world[x][d] == ALIVE) neighbors++;
if (world[r][d] == ALIVE) neighbors++;
cout << neighbors;
if (x == WIDTH-1) cout << endl;
next[x][y] = world[x][y];
if (next[x][y] == ALIVE)
{
if (neighbors < 2 || neighbors > 3) next[x][y] = DEAD;
}
else
{
if (neighbors == 3) next[x][y] = ALIVE;
}
}
cout << endl;
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++)
world[x][y] = next[x][y];
return;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote