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

c++, bad at english, need fully comments, ty Cell::Cell() { state = 0; face = \'

ID: 3849193 • Letter: C

Question

c++, bad at english, need fully comments, ty

   Cell::Cell() {
       state = 0;
       face = '-';
   }

   Cell::Cell(bool intialstate) {

       face = intialstate;
   }

   Cell::~Cell() {

   }

   bool Cell::getState() const {
       return state;
   }

   void Cell::setState(bool newState) {
       state = newState;
       if (state == 0)
           face = dead;
       else
           face = alive;
   }

   char Cell::getFace() const {
       return face;
   }
   GameOfLife::GameOfLife(size_t myBoardSize) throw (std::bad_alloc)
   {
       boardSize = myBoardSize;
       if (boardSize ==0) {
           throw std::bad_alloc();
       }
   }

   GameOfLife::GameOfLife(const GameOfLife& game) { //copy constructor
       GameOfLife newGame;

       newGame.currentLife = new CellPtr[boardSize];//dynamically allocate newGame's current life array

       for (int i = 0; i < boardSize; ++i)
           newGame.currentLife[i] = new Cell[boardSize];

       for (int i = 0; i < boardSize; i++) //make newGame currentLife equal to game
       {
           for (int j = 0; j < boardSize; j++)
           {
               newGame.currentLife[i][j].setState(game.currentLife[i][j].getState());
           }
       }

   }
   GameOfLife::~GameOfLife() {//destructor

       delete[] nextLife;
       delete[] currentLife;
   }

   void GameOfLife::seedBoard(string fileName) throw (FileIOException)
   {
       char input;
       bool inputState = false;
       ifstream infile;
       infile.open(fileName);
       if (infile.fail()) {
               throw 0;
           }

       currentLife = new CellPtr[boardSize];

       for (int i = 0; i < boardSize; ++i)

           currentLife[i] = new Cell[boardSize];

           for (int i = 0; i < boardSize; i++)
           {
               for (int j = 0; j < boardSize; j++)
               {
                   infile >> input;

                   if (input == '-')
                   {
                       inputState = false;
                   }
                   else if (input == 'o')
                   {
                       inputState = true;
                   }

                   currentLife[i][j].setState(inputState);
               }
           }
       infile.close();
   }

   void GameOfLife::seedBoard(size_t seeds) {

       srand(time(NULL));

       currentLife = new CellPtr[boardSize];

       for (int i = 0; i < boardSize; ++i)

       currentLife[i] = new Cell[boardSize];

       size_t count = 0;
       while (count < seeds)
       {
           int randNumber1 = rand() % (26) + 2;
           int randNumber2 = rand() % (26) + 2;
           count++;

           currentLife[randNumber1][randNumber2].setState(true);
       }


   }
   size_t GameOfLife::getBoardSize() const {

       return boardSize;

   }

Explanation / Answer

//Cell constructor initialize the state and face fields of a Cell class to defaults value
Cell::Cell() {
state = 0;
face = '-';
}
//Cell parameterized constructor take the value of face as an argument and update face value
Cell::Cell(bool intialstate) {
face = intialstate;
}
//Destructor
Cell::~Cell() {
}
//Getter method for state which returns the state value
bool Cell::getState() const {
return state;
}
//Setter method for state which set the state to newState which passed as argument
void Cell::setState(bool newState) {
state = newState;
//Check the value of state if it is 0 then face will be dead
if (state == 0)
face = dead;
else
face = alive;
}
//getter method for face -returns face
char Cell::getFace() const {
return face;
}
//GameOfLife parameterized constructor which trows bad_alloc exception
GameOfLife::GameOfLife(size_t myBoardSize) throw (std::bad_alloc)
{
//update the board size
boardSize = myBoardSize;
//If the board size is 0 then throws exception
if (boardSize ==0) {
throw std::bad_alloc();
}
}
// GameOfLife copy constructor
GameOfLife::GameOfLife(const GameOfLife& game) { //copy constructor
GameOfLife newGame;
newGame.currentLife = new CellPtr[boardSize];//dynamically allocate newGame's current life array
for (int i = 0; i < boardSize; ++i)
newGame.currentLife[i] = new Cell[boardSize];
for (int i = 0; i < boardSize; i++) //make newGame currentLife equal to game
{
for (int j = 0; j < boardSize; j++)
{
newGame.currentLife[i][j].setState(game.currentLife[i][j].getState());
}
}
}
//GameOfLife Destructor
GameOfLife::~GameOfLife() {//destructor
delete[] nextLife;
delete[] currentLife;
}
//Method for seedBoard
void GameOfLife::seedBoard(string fileName) throw (FileIOException)
{
char input;
bool inputState = false;
ifstream infile;
infile.open(fileName);
//If file is empty then throws exception
if (infile.fail()) {
throw 0;
}
//Instantiate currentLife array object
currentLife = new CellPtr[boardSize];
//Instantiate all elements in currentLife array
for (int i = 0; i < boardSize; ++i)
currentLife[i] = new Cell[boardSize];
//Read value of each cell from file and update the value of that cell in currentLife array which is 2D array
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
infile >> input; //reading character from input file
if (input == '-') //if input is - then inputState is false
{
inputState = false;
}
else if (input == 'o')
{
inputState = true;
}
//update the state of cell
currentLife[i][j].setState(inputState);
}
}
infile.close();
}
//Updating the value of cells by randomly
//until count is less than seed
void GameOfLife::seedBoard(size_t seeds) {
srand(time(NULL));
currentLife = new CellPtr[boardSize];
for (int i = 0; i < boardSize; ++i)
currentLife[i] = new Cell[boardSize];
size_t count = 0;
while (count < seeds)
{
int randNumber1 = rand() % (26) + 2;
int randNumber2 = rand() % (26) + 2;
count++;
currentLife[randNumber1][randNumber2].setState(true);
}

}
//return the boardSize of GameOfLife class
size_t GameOfLife::getBoardSize() const {
return boardSize;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote