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

Conways Game of Life: Visible array size[40][20]: Code for Game Function Below:

ID: 3662158 • Letter: C

Question

Conways Game of Life:

Visible array size[40][20]: Code for Game Function Below: Question: How do I make a glider with five points dissapear of the edge of the array? I have expanded the array outside the visible array thinking this would work. But the glider seems to keep bouncing of the bounds in the printout.

void Game::gamerules()
{
   int livecount = 0;
   int sum = 0;
   int colum, row;

   do
   {
       sum = 0;
       for (int r = 0; r < 45; r++)
       {
           for (int c = 0; c < 25; c++)
           {
               game_t[r][c] = game[r][c];
               livecount = 0;

               row = r;
               colum = c;

               sum += game[r][c]; //keeps track of if cells are all alive or dead  

               if (( r < 40) || (c < 20))

               if (game[row][colum] == 1) livecount++;
               if (game[row - 1][colum - 1] == 1) livecount++;
               if (game[row - 1][colum] == 1) livecount++;
               if (game[row - 1][colum + 1] == 1) livecount++;
               if (game[row][colum - 1] == 1) livecount++;
               if (game[row][colum + 1] == 1) livecount++;
               if (game[row + 1][colum - 1] == 1) livecount++;
               if (game[row + 1][colum] == 1) livecount++;
               if (game[row + 1][colum + 1] == 1) livecount++;

               if (livecount <= 2)
                   game_t[r][c] = 0;
               if ((livecount == 3)||(livecount == 4))
                   game_t[r][c] = 1;
               if (livecount >= 5)
                   game_t[r][c] = 0;
           }

       }
       copy(game, game_t);
       printout(game);
   }
   while (sum);
}
void Game::oscillator()
{
   Game user;
   int ypos, ypos2, ypos3, xpos, xpos2, xpos3;

   std::cout << "What is the starting three row positions? ";
   std::cin >> ypos;
   std::cout << "Enter next Row ";
   std::cin >> ypos2;
   std::cout << "Enter next Row ";
   std::cin >> ypos3;

   std::cout << "What is the starting three column positions? ";
   std::cin >> xpos;
   std::cout << "Enter next Column ";
   std::cin >> xpos2,
   std::cout << "Enter next Column ";
   std::cin >> xpos3;
  
   game[ypos][xpos] = 1;
   game[ypos2][xpos2] = 1;
   game[ypos3][xpos3] = 1;
  
   gamerules();

}
void Game::glider()
{
   int start_r;
   int start_c;

   std::cout << "Enter in the starting row ";
   std::cin >> start_r;
   std::cout << "Enter in the starting colum ";
   std::cin >> start_c;
       {
           game[start_r][start_c] = 1;
           game[start_r+1][start_c+1] = 1;
           game[start_r+2][start_c+1] = 1;
           game[start_r+2][start_c] = 1;
           game[start_r+2][start_c-1] = 1;
       }

   gamerules();
}
void Game::copy(int game[][30], int game_t[][30])
{
   for (int r = 0; r < 39; r++)
   {
       for (int c = 0; c < 19; c++)
       {
           game[r][c] = game_t[r][c];
       }
   }
}
void Game::printout(int game[][30])
{
   for (int r = 0; r < 45; r++)
   {
       for (int c = 0; c < 24; c++)
       {
           if (game[r][c] == 1)
               Sleep(25);
           if (game[r][c] == 1)
               std::cout <<'X';
           else
               Sleep(5);
           std::cout << ".";
       }
   }
}

Explanation / Answer

#include <iostream>

#include<unistd.h>

using namespace std;

class Game

{

unsigned int microseconds;

public:

void gamerules();

void oscillator();

void glider();

void copy(int game[][30], int game_t[][30]);

void printout(int game[][30]);

void sleepcp(int milliseconds);

private:

int game[][30];

int game_t[][30];

};

void Game::gamerules()

{

int livecount = 0;

int sum = 0;

int colum, row;

do

{

sum = 0;

for (int r = 0; r < 45; r++)

{

for (int c = 0; c < 25; c++)

{

game_t[r][c] = game[r][c];

livecount = 0;

row = r;

colum = c;

sum += game[r][c]; //keeps track of if cells are all alive or dead

if (( r < 40) || (c < 20))

if (game[row][colum] == 1) livecount++;

if (game[row - 1][colum - 1] == 1) livecount++;

if (game[row - 1][colum] == 1) livecount++;

if (game[row - 1][colum + 1] == 1) livecount++;

if (game[row][colum - 1] == 1) livecount++;

if (game[row][colum + 1] == 1) livecount++;

if (game[row + 1][colum - 1] == 1) livecount++;

if (game[row + 1][colum] == 1) livecount++;

if (game[row + 1][colum + 1] == 1) livecount++;

if (livecount <= 2)

game_t[r][c] = 0;

if ((livecount == 3)||(livecount == 4))

game_t[r][c] = 1;

if (livecount >= 5)

game_t[r][c] = 0;

}

}

copy(game, game_t);

printout(game);

}

while (sum);

}

void Game::oscillator()

{

Game user;

int ypos, ypos2, ypos3, xpos, xpos2, xpos3;

std::cout << "What is the starting three row positions? ";

std::cin >> ypos;

std::cout << "Enter next Row ";

std::cin >> ypos2;

std::cout << "Enter next Row ";

std::cin >> ypos3;

std::cout << "What is the starting three column positions? ";

std::cin >> xpos;

std::cout << "Enter next Column ";

std::cin >> xpos2,

std::cout << "Enter next Column ";

std::cin >> xpos3;

  

game[ypos][xpos] = 1;

game[ypos2][xpos2] = 1;

game[ypos3][xpos3] = 1;

  

gamerules();

}

void Game::glider()

{

int start_r;

int start_c;

std::cout << "Enter in the starting row ";

std::cin >> start_r;

std::cout << "Enter in the starting colum ";

std::cin >> start_c;

{

game[start_r][start_c] = 1;

game[start_r+1][start_c+1] = 1;

game[start_r+2][start_c+1] = 1;

game[start_r+2][start_c] = 1;

game[start_r+2][start_c-1] = 1;

}

gamerules();

}

void Game::copy(int game[][30], int game_t[][30])

{

for (int r = 0; r < 39; r++)

{

for (int c = 0; c < 19; c++)

{

game[r][c] = game_t[r][c];

}

}

}

void Game: sleepcp(int milliseconds)

{

#ifdef WIN32

Sleep(milliseconds);

#else

usleep(milliseconds * 1000);

#endif // win32

}

void Game:printout(int game[][30])

{

for (int r = 0; r < 45; r++)

{

for (int c = 0; c < 24; c++)

{

if (game[r][c] == 1)

sleepcp(25);

if (game[r][c] == 1)

std::cout <<'X';

else

sleepcp(5);

std::cout << ".";

}

}

}