Game of Life (Abstract Class Version) ((((( if i am satisfied with the work i wi
ID: 638898 • Letter: G
Question
Game of Life (Abstract Class Version) ((((( if i am satisfied with the work i will provide another link for another 1500 points .....total of 3000 points))))))
In this assignment, you will define two member functions of class Square: bool getLiveInfo() and void update() as pure virtual functions by the "=0" syntax. As a result, class Square will become an abstract class. As we studied in class, no concrete objects can be created through an abstract class. That means that class Square will only serve as a parent class of class Cell. Class Square, however, can be used as a pointer or reference data type of child class objects as "Square * _squares[ROWS][COLS]" in class Grid. In addition, those two pure virtual member functions must be overridden by child class.
In the Game of Life programming assignment, multiple classes are used. Some classes use others as types of their members. Normally, referenced classes in a class should be declared before the referencing class. Sometimes, this cannot be done because multiple classes are referenced each other in an interleaved manner. A good solution would be pre-declarations of classes as used in this assignment.
You are required to use the class declarations (abstract version) given below to re-implement the Game of Life simulation. Once your program works correctly, you should be able to reproduce the simulation results provided below. A client program (main() function) is the same as the last version.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed
Explanation / Answer
// grid.h
//Predeclaration
//Class Coordinate is represented by a pair of x and y coordinates
public class Coordinate
{
//Constructors
public Coordinate()
{
_x = 0;
_y = 0;
}
public Coordinate(int x, int y)
{
_x = x;
_y = y;
}
public void dispose()
{
}
public final int getX()
{
return _x;
}
public final int getY()
{
return _y;
}
public final void setX(int anX)
{
_x = anX;
}
public final void setY(int aY)
{
_y = aY;
}
private int _x;
private int _y;
}
//Class Square serves as an abstract class from which class Cell inherits.
//No concrete objects of Square can be created because of the nature of its abstract
public abstract class Square
{
Square(Grid aGrid, Coordinate aCoord, sbyte aImage);
public void dispose();
sbyte getImage();
Grid getGrid();
void setImage(sbyte NamelessParameter);
Coordinate getCoordinate();
//This function will be overwritten by getLiveInfo() in a child class (Cell)
public abstract boolean getLiveInfo(); //Pure virtual function
void print();
//This function will be overwritten by update() in a child class (Cell)
public abstract void update(); //Pure virtual function
private Grid _grid; //The grid to which this square belongs
private Coordinate _coord; //The coordinate of this square on the grid
private byte _image; //Printing form of this object. For example: live cell: O, dead live: -
}
//Child class
public class Cell extends Square
{
//Constructor of Grid. Initializer will be used to initialize the parent part
Cell(Grid aGrid, Coordinate aCoord, sbyte aImage, int f);
boolean getLiveInfo();
void setLiveInfo(boolean NamelessParameter);
//Count how many live cells among the eight neighbors using the rules
int countLiveNeighbors();
void resetLifeSpan();
//If a cell continues to be live during next generation, _lifeSpan will increment by one
void incrementLiveSpan();
//Based on the number of live neighbor cells, update the state of each cell
/ void update();
private int _lifeSpan; //The number of generations this cell has lived
private boolean _toBeLive; //True if this cell will be live during next generation
}
public class Grid
{
public void dispose();
void initialize();
void run();
void display();
void getStats();
Square getSquare(int x, int y);
void addCells();
private Square[][] _squares = new Square[DefineConstants.ROWS][DefineConstants.COLS]; //A 2D array of pointers to squares (parent class of cells)
}
final class DefineConstants
{
public static final int ROWS = 20;
public static final int COLS = 20;
public static final int NUM_CELLS = 40;
public static final char LIVE_IMAGE = 'O';
public static final char DEAD_IMAGE = '-';
public static final int NUM_RUNS = 10;
public static final int NUM_DISPLAY = 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.