Program Conway\'s Game of Life using a N times N array. The starting state of th
ID: 3782224 • Letter: P
Question
Program Conway's Game of Life using a N times N array. The starting state of the board will be determined by one of the initialization subroutines. Leave the borders of the board empty and do not bother to update the borders of the board. A #define for N. Two arrays, one for the current board state and the other for the next board state. Store the state of each cell as an int8, dead = 0 and alive = 1. A subroutine that takes as input the current board array, a row array index, a column array index, and return the dead or alive status of that cell in the next generation. Initialize the next board array to 0. A subroutine that initializes an array with a Block (first still life) pattern near the middle of the board. A subroutine that initializes an array with a Period-2 Blinker near the middle of the board. A subroutine that initializes an array with a glider near the middle of the board. No global variables. Show that the current and next board arrays are the same at the end of the board update when the array is initialized to a Block. Do this by examining the array values for the two rows occupied by the Block. Show that the current and next board arrays show proper operation of a period-2 Blinker at the end of the board update when the array is initialized with a period-2 Blinker.Explanation / Answer
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
const int row ='8';
const int col ='8';
const int BOARD_row();
const int BOARD_col();
const char live = '1';
const char dead = '0';
char NewBoard[row][col];
char quit;
bool MakeArray(string filename, char board[][col]);
void GameBoard(char board[][col]);
void SwitchBoard (char board[][col]);
void NextState(char board[][col]);
char board [row][col];
string filename; //Name of the file
cout<<"Enter the filename: ";
cin>>filename;
//cout<<endl;
if (MakeArray(filename, board))
{
cout << "File not found. ";
cin>>quit; //stops from quiting
return 1;
}
cout<<endl;
for (int l = 0; l<10; l++)
{
NextState(board);
GameBoard(board);
SwitchBoard(board);
}
char q;
cin >> q;
return 0;
}
bool MakeArray(string filename, char board[][col])
{
ifstream myfile;
myfile.open (filename.c_str());
if (!myfile) return true;
for (int r=0; r<row; r++)
{
for (int c=0; c<col; c++)
{
myfile>>board[r][c];
}
}
myfile.close();
return false;
}
void GameBoard (char board[][col])
{
//cout<<endl;
for (int r=1; r<=row-2; r++)
{
for (int c=1; c<=col-2; c++)
{
cout<<board[r][c];
}
cout<<endl;
}
cout<<endl;
}
void NextState (char board[][col])
{
for (int r=0; r<row; r++)
{
for (int c=0; c<col; c++)
{
int liveCnt=0;
if (board[r-1][c-1]==live)
{
liveCnt++;
}
if (board[r-1][c]==live)
{
liveCnt++;
}
if (board[r-1][c+1]==live)
{
liveCnt++;
}
if (board[r][c-1]==live)
{
liveCnt++;
}
if (board[r][c+1]==live)
{
liveCnt++;
}
if (board[r+1][c-1]==live)
{
liveCnt++;
}
if (board[r+1][c+1]==live)
{
liveCnt++;
}
if (board[r+1][c]==live)
{
liveCnt++;
}
NewBoard[r][c] = dead;
if (board[r][c] == live && liveCnt < 2)
{
NewBoard[r][c]=dead;
}
else if (board[r][c]==live && (liveCnt==2 || liveCnt==3))
{
NewBoard[r][c]=live;
}
else if (board[r][c]==live && liveCnt>3 )
{
NewBoard[r][c]=dead;
}
else if (board[r][c]==dead && liveCnt==3)
{
NewBoard[r][c]=live;
}
}
}
}
void SwitchBoard(char board[][col])
{
for (int r=0; r<row; r++)
{
for (int c=0; c<col; c++)
{
board[r][c]=NewBoard[r][c];
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.