POLYMORPHISM AND DESIGN PATTERNS Polymorphism and Design Patterns Topics that must have been completed before starting Due Date 1: 1. Relationships and Inheritance (all the module) 2. STL (all the...

1 answer below »
This is a C++ assignment


POLYMORPHISM AND DESIGN PATTERNS Polymorphism and Design Patterns Topics that must have been completed before starting Due Date 1: 1. Relationships and Inheritance (all the module) 2. STL (all the module) 3. Object-Oriented Programming: Advanced object uses Topics that must have been completed before starting Due Date 2: 1. Error handling with exceptions (all the module) 2. Design patterns: Observer 3. Design patterns: Decorator Learning objectives: • Relationships, Inheritance, and Polymorphism in C++ • STL vectors • Decorator and Observer design patterns Question 2 For this assignment, you are not allowed to use the array (i.e., []) forms of newand delete. Further, the CXXFLAGS variable in your Makefile must include the flag -Werror=vla. This means that you must use vectors instead of variable-length arrays. In this question you will be building a version of Conway’s Game of Life. The game of life is not really a “game”, but rather an automaton that simulates the “life” and “death” of cells as they progresses between “generations” according to a set of rules. In the game of life cells in a grid are either “alive” or “dead”, a given state of the grid is called a “generation”. Then to progress to the next generation a set of rules for which cells will become alive or dead the next generation are followed and the next generation is computed. In Conway’s Game of Life the rules are: 1. Any cell with fewer than two living neighbours dies (due to underpopulation). 2. Any cell with more than three living neighbours dies (due to overpopulation). 3. Any dead cell with exactly three live neighbours becomes a live cell (due to reproduction). However, in our version of the Game Of Life not every cell in the grid must follow the exact same rules. Instead rules can be added onto a cell at run time, and we will have some extra rules we can add to cells as well. Our rules will function as follows: rule 1 applies to all cells, the rest have to be added on to cells at run time. That is, all cells in the grid follow the underpopulation rule, all other rules are applied individually to cells. However, for convenience your program will also provide the option to default all the cells to one type (and then additional rules can be added onto cells individually) 1) Only rule that applies to ALL cells: Any dead cell with exactly three live neighbours becomes a live cell, unless another rule says it would die. 2) Any cell with fewer than two living neighbours dies. 3) Any cell with more than three living neighbours dies. 4) A periodic cell - this rule takes an additional integer parameter N denoting the period. When applied to the cell every N generations it will flip its state (if it is dead on the Nth generation it will become alive, if it is alive it will become dead). 5) A friendly cell - this rule takes an additional integer parameter N denoting how many friends this cell likes to have. If this cell is surrounded by exactly N neighbours then it will become (or stay) alive into the next generation. The order the rules are added onto a cell matter. For example the “Friendly Cell” rule can directly conflict with the rules about overpopulation and underpopulation. In the case of a conflict, whichever rule was more recently added takes precedence. Since the rules can be added at runtime to change the behaviour of the cells, you are required to implement this using the Decorator pattern. The test harness a4q2.cc simply instantiates a LifeGame object with a given width and height which are pased in as command line arguments, and calls the play() method on it. You must implement this class, as well as any other classes you need to properly design your solution to match the behaviour required by the spec/sample executable. The play() method does the job of reading input from the user. As such a skeleton for the method has been given, which you will need to fill in to make it work with your implementation of the game. The comments in the skeleton explain the command structure for the game. However, the commands are also listed below. • update command — if the user inputs the character u as a command this should update your Game of Life to the next generation. That is, each cell in the game should become either dead or alive based on the rules applied to that cell. • on command — if the user inputs the character o as a command then two integers x and y are read in and the the cell located at (x, y) on the grid is turned on. The upper left corner of the grid represents (0,0) and the x coordinates get larger as you move right while the y coordinates get larger as you move down. That is, the coordinate (2, 5) represents the cell in the third column (x coordinate of 2), and the sixth row (y coordinate of 5). • Rule two command — if the user inputs the character l as a command then two integers x and y are read in and the cell located at (x, y) on the grid has rule two applied to it (it is decorated with a rule two object). • Rule three command — if the user inputs the character a as a command then two integers x and y are read in and the cell located at (x, y) on the grid as rule three applied to it (it is decorated with a rule three object). • Rule four command — if the user inputs the character t as a command then three integers x, y, and N are read in and the cell located at (x, y) on the grid as rule four parameterized by N applied to it (it is decorated with a rule four object). • Rule five command — if the user inputs the character f as a command then three integers x, y, and N are read in and the cell located at (x, y) on the grid as rule five parameterized by N applied to it (it is decorated with a rule five object). • default command — if the user inputs the character d as a command then you must read in characters until anything other than a l, a, p, or f is read in (or an integer only if it immediately follows an f). Then, all cells in the grid must be set to off and have the rules read in applied to them. That is, if the user inputes dla*, then all cells in the game are set to dead and now follow rules 1, 2, and 3. Similarly, if the user inputs daf7-, then all cells in the game are set to dead and now follow rules 1, 3, and 5 (parameterized with 7). • print command — if the user inputs the character p as a command then the grid will be printed out as a 2D grid of characters. Living cells should be printed as the character “X”, while dead cells should be printed as the period character “.”. • quit command — if the user inputs the character q as a command then the function will end. Important: As the point of this assignment is to use the Decorator pattern, if your solution is found in handmarking to not employ the decorator pattern your correctness marks will be reduced to a maximum of 50%. Hints: Create a Cell abstract base class and have Decorator extend from that. As per the decorator pattern create a BaseCell class which implements the basic behaviour (and also stores the variables you need). Include a Grid class that stores your grid of Cells. Note: Your program should be well documented and employ proper programming style. It should not leak memory. Markers will be checking for these things. Due on Due Date 2: Submit your solution in the file a4q2b.zip. You must include a Makefile, such that issuing the command make will build your program. The executable should be called a4q2. Decorator - CS 246 - Spring 2021 cmoroie Sticky Note cmoroie Rectangle a4q2 a4q2.cc #include #include #include using namespace std; int main(int argc, char *argv[]) { if (argc != 3) { cout < "usage:="" "="">< argv[0]="">< "="" width="" height"="">< endl;="" return="" 1;="" }="" istringstream="" w{argv[1]};="" istringstream="" h{argv[2]};="" int="" width,="" height;="" w="">> width; h >> height; LifeGame g{width, height}; g.play(); } gospers.args 40 30 gospers.in dal* o 26 1 o 24 2 o 26 2 o 14 3 o 15 3 o 22 3 o 23 3 o 36 3 o 37 3 o 13 4 o 17 4 o 22 4 o 23 4 o 36 4 o 37 4 o 2 5 o 3 5 o 12 5 o 18 5 o 22 5 o 23 5 o 2 6 o 3 6 o 12 6 o 16 6 o 18 6 o 19 6 o 24 6 o 26 6 o 12 7 o 18 7 o 26 7 o 13 8 o 17 8 o 14 9 o 15 9 p up up up up up up up up up up up up up up up up up gospers.out ........................................ ..........................X............. ........................X.X............. ..............XX......XX............XX.. .............X...X....XX............XX.. ..XX........X..
Answered 2 days AfterJul 11, 2021

Answer To: POLYMORPHISM AND DESIGN PATTERNS Polymorphism and Design Patterns Topics that must have been...

Swapnil answered on Jul 12 2021
146 Votes
87833/constants.h
#ifndef CONSTANTS_H
#define CONSTANTS_H
#include
#include
const int SCREEN_WIDTH = 1100;
const int SCREEN_HEIGHT = 850;
const int SIZE_OF_ARRAY = 100;
const int CELL_LENGTH = 8;
const int CEW = 4;
const int BUTTON_X = (SIZE_OF_ARRAY+1)*CELL_LENGTH;
#endif
87833/game.cpp
#include "game.h"
Game::Game()
{
window.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Game of Life!", sf::Style::Titlebar | sf::Style::Close);
}
void Game::run()
{
std::cout<<"Start Game of life \n\n";
loadExistedPattern();
while (window.isOpen())
{
processEvents();
update();
render();
}
}
void Game::loadExistedPattern()
{
system.Load();
}
void Game::update()
{
system.Update();
}
void Game::render()
{
system.Render(window);
}
void Game::processEvents()
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
system.Save();
window.close();
std::cout<<"End of Game"< break;
case sf::Event::MouseButtonPressed:
mouseX = event.mouseButton.x;
mouseY = event.mouseButton.y;
cellX = mouseX / 8;
cellY = mouseY / 8;
system.ClickOnScreen(cellX, cellY);
if(system.GameStatusIsPause())
{
std::cout<<"Game is stopped \n";
}
else
{
std::cout<<"Game is running \n";
}
std::cout << "Mouse: (" << mouseX << ", " << mouseY << ") \n";
if(cellX < 100 && cellY < 100)
{
std::cout << "Cell: (" << cellX << ", " << cellY << ") \n\n";
}
else
{
std::cout << "Cell: Out of cells \n\n";
}
break;
default:
break;
}
}
}
87833/game.h
#ifndef GAME_H
#define GAME_H
#include
class Game
{
public:
Game();
void run();
void processEvents();
void update();
void render();
void loadExistedPattern();
private:
int mouseX;
int mouseY;
int cellX;
int cellY;
sf::RenderWindow window;
System system;
};
#endif
87833/gui.cpp
#include "gui.h"
void Gui::SetupWindow(sf::RenderWindow &window)
{
CellsSetup();
ButtonsSetup();
PatternsSetup();
Draw(window);
window.display();
}
void Gui::CellsSetup()
{
sf::Vector2f SIZE_CELL(CELL_LENGTH, CELL_LENGTH);
cells.setSize(SIZE_CELL);
cells.setOutlineColor(sf::Color::Blue);
cells.setOutlineThickness(0.6);
}
void Gui::ButtonsSetup()
{
font.loadFromFile("arial.ttf");
Button_1.setFont(font);
Button_2.setFont(font);
Button_3.setFont(font);
Button_4.setFont(font);
Button_5.setFont(font);
Button_6.setFont(font);
Button_7.setFont(font);
Button_8.setFont(font);
Button_1.setString("START");
Button_2.setString("STOP");
Button_3.setString("ADD CELLS");
Button_4.setString("CLEAN WORLD");
Button_5.setString("LOAD WORLD");
Button_6.setString("SAVE WORLD");
Button_7.setString("SAVE PATTERN");
Button_8.setString("LOAD PATTERN");
Button_1.setCharacterSize(CEW * CELL_LENGTH);
Button_2.setCharacterSize(CEW * CELL_LENGTH);
Button_3.setCharacterSize(CEW * CELL_LENGTH);
Button_4.setCharacterSize(CEW * CELL_LENGTH);
Button_5.setCharacterSize(CEW * CELL_LENGTH);
Button_6.setCharacterSize(CEW * CELL_LENGTH);
Button_7.setCharacterSize(CEW * CELL_LENGTH);
Button_8.setCharacterSize(CEW * CELL_LENGTH);
Button_1.setColor(sf::Color::Green);
Button_2.setColor(sf::Color::Green);
Button_3.setColor(sf::Color::Green);
Button_4.setColor(sf::Color::Green);
Button_5.setColor(sf::Color::Green);
Button_6.setColor(sf::Color::Green);
Button_7.setColor(sf::Color::Red);
Button_8.setColor(sf::Color::Red);
Button_1.setPosition(BUTTON_X, 0);
Button_2.setPosition(BUTTON_X, CEW*CELL_LENGTH);
Button_3.setPosition(BUTTON_X, 2*CEW*CELL_LENGTH);
Button_4.setPosition(BUTTON_X, 3*CEW*CELL_LENGTH);
Button_5.setPosition(BUTTON_X, 4*CEW*CELL_LENGTH);
Button_6.setPosition(BUTTON_X, 5*CEW*CELL_LENGTH);
Button_7.setPosition(BUTTON_X, 6*CEW*CELL_LENGTH);
Button_8.setPosition(BUTTON_X, 10*CEW*CELL_LENGTH);
}
void Gui::PatternsSetup()
{
sf::Vector2f SIZE_PATTERN(3*CELL_LENGTH, 3*CELL_LENGTH);
patternSave1.setSize(SIZE_PATTERN);
patternSave2.setSize(SIZE_PATTERN);
patternSave3.setSize(SIZE_PATTERN);
patternSave4.setSize(SIZE_PATTERN);
patternSave5.setSize(SIZE_PATTERN);
patternSave6.setSize(SIZE_PATTERN);
patternSave7.setSize(SIZE_PATTERN);
patternSave8.setSize(SIZE_PATTERN);
patternSave9.setSize(SIZE_PATTERN);
patternLoad1.setSize(SIZE_PATTERN);
patternLoad2.setSize(SIZE_PATTERN);
patternLoad3.setSize(SIZE_PATTERN);
patternLoad4.setSize(SIZE_PATTERN);
patternLoad5.setSize(SIZE_PATTERN);
patternLoad6.setSize(SIZE_PATTERN);
patternLoad7.setSize(SIZE_PATTERN);
patternLoad8.setSize(SIZE_PATTERN);
patternLoad9.setSize(SIZE_PATTERN);
patternSave1.setFillColor(sf::Color::Green);
patternSave2.setFillColor(sf::Color::Green);
patternSave3.setFillColor(sf::Color::Green);
patternSave4.setFillColor(sf::Color::Green);
patternSave5.setFillColor(sf::Color::Green);
patternSave6.setFillColor(sf::Color::Green);
patternSave7.setFillColor(sf::Color::Green);
patternSave8.setFillColor(sf::Color::Green);
patternSave9.setFillColor(sf::Color::Green);
patternLoad1.setFillColor(sf::Color::Green);
patternLoad2.setFillColor(sf::Color::Green);
patternLoad3.setFillColor(sf::Color::Green);
patternLoad4.setFillColor(sf::Color::Green);
patternLoad5.setFillColor(sf::Color::Green);
patternLoad6.setFillColor(sf::Color::Green);
patternLoad7.setFillColor(sf::Color::Green);
patternLoad8.setFillColor(sf::Color::Green);
patternLoad9.setFillColor(sf::Color::Green);
patternSave1.setPosition(BUTTON_X, 7*CEW*CELL_LENGTH);
patternSave2.setPosition(BUTTON_X+4*CELL_LENGTH, 7*CEW*CELL_LENGTH);
patternSave3.setPosition(BUTTON_X+8*CELL_LENGTH, 7*CEW*CELL_LENGTH);
patternSave4.setPosition(BUTTON_X, 8*CEW*CELL_LENGTH);
patternSave5.setPosition(BUTTON_X+4*CELL_LENGTH, 8*CEW*CELL_LENGTH);
patternSave6.setPosition(BUTTON_X+8*CELL_LENGTH, 8*CEW*CELL_LENGTH);
patternSave7.setPosition(BUTTON_X, 9*CEW*CELL_LENGTH);
patternSave8.setPosition(BUTTON_X+4*CELL_LENGTH, 9*CEW*CELL_LENGTH);
patternSave9.setPosition(BUTTON_X+8*CELL_LENGTH, 9*CEW*CELL_LENGTH);
patternLoad1.setPosition(BUTTON_X, 11*CEW*CELL_LENGTH);
patternLoad2.setPosition(BUTTON_X+4*CELL_LENGTH, 11*CEW*CELL_LENGTH);
patternLoad3.setPosition(BUTTON_X+8*CELL_LENGTH, 11*CEW*CELL_LENGTH);
patternLoad4.setPosition(BUTTON_X, 12*CEW*CELL_LENGTH);
patternLoad5.setPosition(BUTTON_X+4*CELL_LENGTH, 12*CEW*CELL_LENGTH);
patternLoad6.setPosition(BUTTON_X+8*CELL_LENGTH, 12*CEW*CELL_LENGTH);
patternLoad7.setPosition(BUTTON_X, 13*CEW*CELL_LENGTH);
patternLoad8.setPosition(BUTTON_X+4*CELL_LENGTH, 13*CEW*CELL_LENGTH);
patternLoad9.setPosition(BUTTON_X+8*CELL_LENGTH, 13*CEW*CELL_LENGTH);
}
void Gui::Draw(sf::RenderWindow& window)
{
window.draw(patternSave1);
window.draw(patternSave2);
window.draw(patternSave3);
window.draw(patternSave4);
window.draw(patternSave5);
window.draw(patternSave6);
window.draw(patternSave7);
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here