SlideShare a Scribd company logo
1 of 12
Download to read offline
#include <iostream>
#include <string>
#include <cstdlib>
#include <chrono>
#include <thread>
#ifdef _MSC_VER // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES
#include <windows.h>
#else // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES
// not Microsoft Visual C++, so assume UNIX interface
#include <iostream>
#include <cstring>
#include <cstdlib>
#endif // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES
///////////////////////////////////////////////////////////////////////////
// Global Constants
///////////////////////////////////////////////////////////////////////////
const int CANVAS_ROW = 24;
const int CANVAS_COL = 24;
const int SQUARE_SIZE = 2;
const int PENT_SIZE = 3;
const int BLINK4_SIZE = 9;
const int SLIDER_SIZE = 3;
const int MAX_STEPS = 30;
const char ALIVE = 'X';
const char DEAD = '.';
///////////////////////////////////////////////////////////////////////////
// Type Definitions
///////////////////////////////////////////////////////////////////////////
class World;
class Simulation;
class Life {
public:
int getCol() const {
return m_col;
}
int getRow() const {
return m_row;
}
int getHeight() const {
return m_height;
}
int getWidth() const {
return m_width;
}
char getFigure(int r, int c) const {
return m_pattern[r][c];
}
void inWorld(World *w) {
m_world = w;
}
void inSimulation(Simulation *s) {
m_simulation = s;
}
bool areWeInASimulation() {
return m_simulation != nullptr;
}
protected:
int m_col;
int m_row;
int m_height;
int m_width;
char **m_pattern;
World *m_world;
Simulation *m_simulation;
};
class Pent : public Life {
public:
Pent(int r, int c) {
m_col = c;
m_row = r;
m_height = PENT_SIZE;
m_width = PENT_SIZE;
//Allocate space for figure
m_pattern = new char*[m_height];
for (int i = 0; i < m_height; i++) {
m_pattern[i] = new char[m_width];
}
//Initialize figure
for (int i = 0; i < m_height; i++) {
for (int j = 0; j < m_width; j++) {
m_pattern[i][j] = ALIVE;
}
}
m_pattern[0][0] = DEAD;
m_pattern[0][2] = DEAD;
m_pattern[2][1] = DEAD;
m_pattern[2][2] = DEAD;
}
~Pent() {
for (int i = 0; i < m_height; i++) {
delete[] m_pattern[i];
}
delete[] m_pattern;
}
};
class Square : public Life {
public:
Square(int r, int c) {
m_col = c;
m_row = r;
m_height = SQUARE_SIZE;
m_width = SQUARE_SIZE;
//Allocate space for figure
m_pattern = new char*[m_height];
for (int i = 0; i < m_height; i++) {
m_pattern[i] = new char[m_width];
}
//Initialize figure
for (int i = 0; i < m_height; i++) {
for (int j = 0; j < m_width; j++) {
m_pattern[i][j] = ALIVE;
}
}
}
~Square() {
for (int i = 0; i < m_height; i++) {
delete[] m_pattern[i];
}
delete[] m_pattern;
}
};
class Slider : public Life {
public:
Slider(int r, int c) {
m_col = c;
m_row = r;
m_height = SLIDER_SIZE;
m_width = SLIDER_SIZE;
// Allocate space for figure
m_pattern = new char*[m_height];
for (int i = 0; i < m_height; i++) {
m_pattern[i] = new char[m_width];
}
// Initialize figure
for (int i = 0; i < m_height; i++) {
for (int j = 0; j < m_width; j++) {
m_pattern[i][j] = DEAD;
}
}
m_pattern[0][1] = ALIVE;
m_pattern[1][2] = ALIVE;
m_pattern[2][0] = ALIVE;
m_pattern[2][1] = ALIVE;
m_pattern[2][2] = ALIVE;
}
~Slider() {
for (int i = 0; i < m_height; i++) {
delete[] m_pattern[i];
}
delete[] m_pattern;
}
};
class Blink4 : public Life {
public:
Blink4(int r, int c) {
m_col = c;
m_row = r;
m_height = BLINK4_SIZE;
m_width = BLINK4_SIZE;
//Allocate space for figure
m_pattern = new char*[m_height];
for (int i = 0; i < m_height; i++) {
m_pattern[i] = new char[m_width];
}
//Initialize figure
for (int i = 0; i < m_height; i++) {
for (int j = 0; j < m_width; j++) {
m_pattern[i][j] = DEAD;
}
}
m_pattern[0][4] = ALIVE;
m_pattern[1][4] = ALIVE;
m_pattern[2][4] = ALIVE;
m_pattern[6][4] = ALIVE;
m_pattern[7][4] = ALIVE;
m_pattern[8][4] = ALIVE;
m_pattern[4][0] = ALIVE;
m_pattern[4][1] = ALIVE;
m_pattern[4][2] = ALIVE;
m_pattern[4][6] = ALIVE;
m_pattern[4][7] = ALIVE;
m_pattern[4][8] = ALIVE;
}
~Blink4() {
for (int i = 0; i < m_height; i++) {
delete[] m_pattern[i];
}
delete[] m_pattern;
}
};
class World {
public:
World() {
m_toggle = true;
m_grid_1 = new char*[CANVAS_ROW];
m_grid_2 = new char*[CANVAS_ROW];
for (char i = 0; i < CANVAS_ROW; i++) {
m_grid_1[i] = new char[CANVAS_COL];
m_grid_2[i] = new char[CANVAS_COL];
}
for (char i = 0; i < CANVAS_ROW; i++) {
for (char j = 0; j < CANVAS_COL; j++) {
m_grid_1[i][j] = DEAD;
}
}
}
~World() {
for (char i = 0; i < CANVAS_ROW; i++) {
delete[] m_grid_1[i];
delete[] m_grid_2[i];
}
delete[] m_grid_1;
delete[] m_grid_2;
}
void render() const {
clearScreen();
if (m_toggle) {
for (char i = 0; i < CANVAS_ROW; i++) {
for (char j = 0; j < CANVAS_COL; j++) {
std::cout << m_grid_1[i][j];
}
std::cout << std::endl;
}
}
else {
for (char i = 0; i < CANVAS_ROW; i++) {
for (char j = 0; j < CANVAS_COL; j++) {
std::cout << m_grid_2[i][j];
}
std::cout << std::endl;
}
}
for (char i = 0; i < CANVAS_COL; i++) {
std::cout << '=';
}
std::cout << std::endl;
}
void computeNextState() {
if (m_toggle) {
for (char i = 0; i < CANVAS_ROW; i++) {
for (char j = 0; j < CANVAS_COL; j++) {
m_grid_2[i][j] =
nextState(m_grid_1[i][j], i, j, m_toggle);
}
}
m_toggle = !m_toggle;
}
else {
for (char i = 0; i < CANVAS_ROW; i++) {
for (char j = 0; j < CANVAS_COL; j++) {
m_grid_1[i][j] =
nextState(m_grid_2[i][j], i, j, m_toggle);
}
}
m_toggle = !m_toggle;
}
}
bool initState(Life *life) {
if (life == nullptr) {
std::cout << "Cannot add nullptr life" << std::endl;
return false;
}
for (char i = life->getRow(); i - life->getRow() < life->getHeight(); i++) {
for (char j = life->getCol(); j - life->getCol() < life->getWidth(); j++) {
if (i < CANVAS_ROW && j < CANVAS_COL) {
m_grid_1[i][j] =
life->getFigure(i - life->getRow(), j - life->getCol());
}
}
}
return true;
}
private:
void clearScreen() const {
#ifdef _MSC_VER // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConsole, &csbi);
DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
COORD upperLeft = { 0, 0 };
DWORD dwCharsWritten;
FillConsoleOutputCharacter(hConsole, TCHAR(' '), dwConSize, upperLeft,
&dwCharsWritten);
SetConsoleCursorPosition(hConsole, upperLeft);
#else // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES
static const char* term = getenv("TERM");// will just write a newline in an Xcode output window
if (term == nullptr || strcmp(term, "dumb") == 0)
std::cout << std::endl;
else {
static const char* ESC_SEQ = "x1B["; // ANSI Terminal esc seq: ESC [
std::cout << ESC_SEQ << "2J" << ESC_SEQ << "H" << std::flush;
}
#endif // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES
}
char nextState(char state, char row, char col, bool toggle) const {
int yCoord = row;
int xCoord = col;
char neighbors = 0;
if (toggle) {
for (char i = yCoord - 1; i <= yCoord + 1; i++) {
for (char j = xCoord - 1; j <= xCoord + 1; j++) {
if (i == yCoord && j == xCoord) {
continue;
}
if (i > -1 && i < CANVAS_ROW && j > -1 && j < CANVAS_COL) {
if (m_grid_1[i][j] == ALIVE) {
neighbors++;
}
}
}
}
}
else {
for (char i = yCoord - 1; i <= yCoord + 1; i++) {
for (char j = xCoord - 1; j <= xCoord + 1; j++) {
if (i == yCoord && j == xCoord) {
continue;
}
if (i > -1 && i < CANVAS_ROW && j > -1 && j < CANVAS_COL) {
if (m_grid_2[i][j] == ALIVE) {
neighbors++;
}
}
}
}
}
if (state == ALIVE) {
return (neighbors > 1 && neighbors < 4) ? ALIVE : DEAD;
}
else {
return (neighbors == 3) ? ALIVE : DEAD;
}
}
// The rules of Conway's Simulation of Life requires each cell to
// examine it's neighbors. So, we have to check the entire world
// first before we change it. We can use two copies of the world,
// one to check the current state (current day) then another to generate
// the next state(for the next day). We can toggle between them each
// step, to avoid having to copy between worlds each step (day) of the game.
char **m_grid_1;
char **m_grid_2;
bool m_toggle;
};
class Simulation {
public:
Simulation(Life **life, int numLife) {
watchme = 4;
m_steps = 0;
m_automate = false;
m_world = new World();
if (life != nullptr) {
for (int i = 0; i < numLife; i++) {
if (life[i] != nullptr) {
bool success = m_world->initState(life[i]);
if (!success) {
std::cout << "Failed to add life to the world" << std::endl;
}
}
}
}
}
~Simulation() {
delete m_world;
}
void simulate() {
while (true) {
m_world->render();
if (!m_automate) {
std::cout << "command (<space> to step, <a> to automate, <q> to quit): ";
std::string action;
std::getline(std::cin, action);
switch (action[0])
{
default:
std::cout << 'a' << std::endl; // beep
continue;
case 'q':
std::cout << "Quitting Game." << std::endl;
return;
case 's':
continue;
case ' ':
break;
case 'a':
m_automate = true;
break;
}
}
else {
if (m_steps >= MAX_STEPS) {
std::cout << "Reached max steps, quitting." << std::endl;
return;
}
delay(300);
}
m_steps++;
m_world->computeNextState();
}
}
void report() {
std::string msg = "Hello World!"; // Replace Hello World with your answer.
std::cout << msg << std::endl;
}
int two(int u) {
return u >> 1;
}
int three(int x) {
if (x % 2 == 0)
return 1;
else
return 2;
}
void one(int t) {
int i = 7;
int k = watchme;
while (--i > t) {
if (i == 0)
k = 7;
else if (i == 1)
k = 9;
else if (i == 4)
k = 8;
else if (i == 5)
k = 5;
else
k = two(watchme);
watchme = k;
}
k = three(watchme);
watchme = k;
}
private:
void delay(int ms) const {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
int watchme;
World * m_world;
int m_steps;
bool m_automate;
};
int main() {
const int numLife = 4;
Life **population = new Life*[numLife];
population[0] = new Slider(8, 4);
population[1] = new Pent(18, 14);
population[2] = new Blink4(3, 13);
population[3] = new Square(14, 10);
// Create the game
Simulation s(population, numLife);
// Debugger Exercise
s.one('k' - 'h');
// Run the game
s.simulate();
// Report
s.report();
// Clean up
delete population[0];
delete population[1];
delete population[2];
delete population[3];
delete[] population;
}
Part 1: Organize the code (85%) : Following what we discussed on multiple file etiquette take the
single source file provided, and divide it into appropriate header files and implementation files, one
pair of files for each class. When complete the main function will be the only function in main. cpp.
Make sure each file #includes the headers it needs. Each header file must have include guards.
Only include header files when definitions are actually needed; declare objects in situations when
the compiler only requires to "know" about the objects existence but not its definition. Now what
about the global constants? Place them in their own header file named constants. h. Note that
there are two versions of clearScreen depending on platform, do not break up the preprocessor
directives; keep both versions. The first thing you should do is make sure that the single source
compiles as is. Play around with it to get comfortable the program. The program implements
Conway's Game of Life: https://conwaylife.com/wiki/Conway's Game of Life This is a simple
simulation where cells are either alive or dead based simple rules based on under population or
overcrowding. For those who are feeling adventurous look for some Life patterns and try to
implement them. Part 2: Analysis (15%): You should notice that the class Simulation has the
function report does not do anything useful other than printing "Hello World!" to the console.
Replace "Hello World!" with a brief discussion addressing the questions below. This may require
stepping through the code to get an overview of execution, hand tracing is an option but using the
debugger with break points is much more efficient.Any code you submit must compile in VS2019,
even if incomplete; any project that does not compile will receive a zero score.

More Related Content

Similar to include ltiostreamgt include ltstringgt include .pdf

为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?勇浩 赖
 
Write a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfWrite a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfmckenziecast21211
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for SpeedYung-Yu Chen
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerAndrey Karpov
 
Graphics programs
Graphics programsGraphics programs
Graphics programsNAVYA RAO
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency명신 김
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Aman Deep
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Aman Deep
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docxPiersRCoThomsonw
 
Im trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfIm trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfeyeonsecuritysystems
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docxannetnash8266
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureanishgoel
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfcalderoncasto9163
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codePVS-Studio LLC
 
Moony li pacsec-1.8
Moony li pacsec-1.8Moony li pacsec-1.8
Moony li pacsec-1.8PacSecJP
 

Similar to include ltiostreamgt include ltstringgt include .pdf (20)

为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
Write a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdfWrite a C++ program which generates and displays a random walk acros.pdf
Write a C++ program which generates and displays a random walk acros.pdf
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
Graphics programs
Graphics programsGraphics programs
Graphics programs
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output) Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)Practical Class 12th (c++programs+sql queries and output)
Practical Class 12th (c++programs+sql queries and output)
 
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
#include -stdio-h- #include -stdlib-h- #include -stdbool-h- #include - (1).docx
 
Rkf
RkfRkf
Rkf
 
Im trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdfIm trying again -Okay, Im in need of some help - this is the c.pdf
Im trying again -Okay, Im in need of some help - this is the c.pdf
 
write the TODO part of the program.docx
write the TODO part of the program.docxwrite the TODO part of the program.docx
write the TODO part of the program.docx
 
Ch4
Ch4Ch4
Ch4
 
ARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lectureARM 7 LPC 2148 lecture
ARM 7 LPC 2148 lecture
 
Vcs16
Vcs16Vcs16
Vcs16
 
This is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdfThis is the Java code i have for a Battleship project i am working o.pdf
This is the Java code i have for a Battleship project i am working o.pdf
 
Graphical representation of Stack
Graphical representation of StackGraphical representation of Stack
Graphical representation of Stack
 
A scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ codeA scrupulous code review - 15 bugs in C++ code
A scrupulous code review - 15 bugs in C++ code
 
Moony li pacsec-1.8
Moony li pacsec-1.8Moony li pacsec-1.8
Moony li pacsec-1.8
 

More from contact32

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfcontact32
 
From the following list of accounts calculate the quick rati.pdf
From the following list of accounts calculate the quick rati.pdfFrom the following list of accounts calculate the quick rati.pdf
From the following list of accounts calculate the quick rati.pdfcontact32
 
Errors are defined as An injury caused by medical managemen.pdf
Errors are defined as An injury caused by medical managemen.pdfErrors are defined as An injury caused by medical managemen.pdf
Errors are defined as An injury caused by medical managemen.pdfcontact32
 
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdf
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdfESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdf
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdfcontact32
 
Deliverables Your deliverable in this assignment is to d.pdf
Deliverables Your deliverable in this assignment is to  d.pdfDeliverables Your deliverable in this assignment is to  d.pdf
Deliverables Your deliverable in this assignment is to d.pdfcontact32
 
Diana and Ryan Workman were married on January 1 of last yea.pdf
Diana and Ryan Workman were married on January 1 of last yea.pdfDiana and Ryan Workman were married on January 1 of last yea.pdf
Diana and Ryan Workman were married on January 1 of last yea.pdfcontact32
 
6 15 points Let Ntt0 be a Poisson process with rate .pdf
6 15 points Let Ntt0 be a Poisson process with rate .pdf6 15 points Let Ntt0 be a Poisson process with rate .pdf
6 15 points Let Ntt0 be a Poisson process with rate .pdfcontact32
 
Bario Corporation ha proporcionado los siguientes datos sobr.pdf
Bario Corporation ha proporcionado los siguientes datos sobr.pdfBario Corporation ha proporcionado los siguientes datos sobr.pdf
Bario Corporation ha proporcionado los siguientes datos sobr.pdfcontact32
 
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdf
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdfCaratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdf
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdfcontact32
 
A function annotation also known as a type hint serves to .pdf
A function annotation also known as a type hint serves to .pdfA function annotation also known as a type hint serves to .pdf
A function annotation also known as a type hint serves to .pdfcontact32
 
Beckenworth had the following purchases and sales during the.pdf
Beckenworth had the following purchases and sales during the.pdfBeckenworth had the following purchases and sales during the.pdf
Beckenworth had the following purchases and sales during the.pdfcontact32
 
Part 1 True or False 16 marks Write True or false for th.pdf
Part 1 True or False 16 marks Write True or false for th.pdfPart 1 True or False 16 marks Write True or false for th.pdf
Part 1 True or False 16 marks Write True or false for th.pdfcontact32
 
Which of the following is the correct sequence for the germi.pdf
Which of the following is the correct sequence for the germi.pdfWhich of the following is the correct sequence for the germi.pdf
Which of the following is the correct sequence for the germi.pdfcontact32
 
Oriole Corporation issues 340000 of bonds for 343400 a.pdf
Oriole Corporation issues 340000 of bonds for 343400 a.pdfOriole Corporation issues 340000 of bonds for 343400 a.pdf
Oriole Corporation issues 340000 of bonds for 343400 a.pdfcontact32
 
You and a team of diverse scientists and astronauts have bee.pdf
You and a team of diverse scientists and astronauts have bee.pdfYou and a team of diverse scientists and astronauts have bee.pdf
You and a team of diverse scientists and astronauts have bee.pdfcontact32
 
You are working to your full scope as a Certificate IV gradu.pdf
You are working to your full scope as a Certificate IV gradu.pdfYou are working to your full scope as a Certificate IV gradu.pdf
You are working to your full scope as a Certificate IV gradu.pdfcontact32
 
Teresa has a deck of 10 cards numbered 1 through 10 She is.pdf
Teresa has a deck of 10 cards numbered 1 through 10  She is.pdfTeresa has a deck of 10 cards numbered 1 through 10  She is.pdf
Teresa has a deck of 10 cards numbered 1 through 10 She is.pdfcontact32
 
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdf
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdfTore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdf
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdfcontact32
 
What gets said if I run the script below The variable globa.pdf
What gets said if I run the script below The variable globa.pdfWhat gets said if I run the script below The variable globa.pdf
What gets said if I run the script below The variable globa.pdfcontact32
 
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdfcontact32
 

More from contact32 (20)

Help to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdfHelp to implement delete_node get_succ get_pred walk and.pdf
Help to implement delete_node get_succ get_pred walk and.pdf
 
From the following list of accounts calculate the quick rati.pdf
From the following list of accounts calculate the quick rati.pdfFrom the following list of accounts calculate the quick rati.pdf
From the following list of accounts calculate the quick rati.pdf
 
Errors are defined as An injury caused by medical managemen.pdf
Errors are defined as An injury caused by medical managemen.pdfErrors are defined as An injury caused by medical managemen.pdf
Errors are defined as An injury caused by medical managemen.pdf
 
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdf
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdfESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdf
ESTUDIO DE CASO SNCLAVALIN GROUP INC Por Steven L McShan.pdf
 
Deliverables Your deliverable in this assignment is to d.pdf
Deliverables Your deliverable in this assignment is to  d.pdfDeliverables Your deliverable in this assignment is to  d.pdf
Deliverables Your deliverable in this assignment is to d.pdf
 
Diana and Ryan Workman were married on January 1 of last yea.pdf
Diana and Ryan Workman were married on January 1 of last yea.pdfDiana and Ryan Workman were married on January 1 of last yea.pdf
Diana and Ryan Workman were married on January 1 of last yea.pdf
 
6 15 points Let Ntt0 be a Poisson process with rate .pdf
6 15 points Let Ntt0 be a Poisson process with rate .pdf6 15 points Let Ntt0 be a Poisson process with rate .pdf
6 15 points Let Ntt0 be a Poisson process with rate .pdf
 
Bario Corporation ha proporcionado los siguientes datos sobr.pdf
Bario Corporation ha proporcionado los siguientes datos sobr.pdfBario Corporation ha proporcionado los siguientes datos sobr.pdf
Bario Corporation ha proporcionado los siguientes datos sobr.pdf
 
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdf
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdfCaratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdf
Caratinin 2 it Yoe may alos amune the itadaiand joypertin o.pdf
 
A function annotation also known as a type hint serves to .pdf
A function annotation also known as a type hint serves to .pdfA function annotation also known as a type hint serves to .pdf
A function annotation also known as a type hint serves to .pdf
 
Beckenworth had the following purchases and sales during the.pdf
Beckenworth had the following purchases and sales during the.pdfBeckenworth had the following purchases and sales during the.pdf
Beckenworth had the following purchases and sales during the.pdf
 
Part 1 True or False 16 marks Write True or false for th.pdf
Part 1 True or False 16 marks Write True or false for th.pdfPart 1 True or False 16 marks Write True or false for th.pdf
Part 1 True or False 16 marks Write True or false for th.pdf
 
Which of the following is the correct sequence for the germi.pdf
Which of the following is the correct sequence for the germi.pdfWhich of the following is the correct sequence for the germi.pdf
Which of the following is the correct sequence for the germi.pdf
 
Oriole Corporation issues 340000 of bonds for 343400 a.pdf
Oriole Corporation issues 340000 of bonds for 343400 a.pdfOriole Corporation issues 340000 of bonds for 343400 a.pdf
Oriole Corporation issues 340000 of bonds for 343400 a.pdf
 
You and a team of diverse scientists and astronauts have bee.pdf
You and a team of diverse scientists and astronauts have bee.pdfYou and a team of diverse scientists and astronauts have bee.pdf
You and a team of diverse scientists and astronauts have bee.pdf
 
You are working to your full scope as a Certificate IV gradu.pdf
You are working to your full scope as a Certificate IV gradu.pdfYou are working to your full scope as a Certificate IV gradu.pdf
You are working to your full scope as a Certificate IV gradu.pdf
 
Teresa has a deck of 10 cards numbered 1 through 10 She is.pdf
Teresa has a deck of 10 cards numbered 1 through 10  She is.pdfTeresa has a deck of 10 cards numbered 1 through 10  She is.pdf
Teresa has a deck of 10 cards numbered 1 through 10 She is.pdf
 
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdf
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdfTore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdf
Tore ne to the irrit Blued fequiredb Eupeoce that Chajs.pdf
 
What gets said if I run the script below The variable globa.pdf
What gets said if I run the script below The variable globa.pdfWhat gets said if I run the script below The variable globa.pdf
What gets said if I run the script below The variable globa.pdf
 
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf
21 Ribozom antlamas ve dahili ribozom balangcnn ortak nokta.pdf
 

Recently uploaded

How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Recently uploaded (20)

YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

include ltiostreamgt include ltstringgt include .pdf

  • 1. #include <iostream> #include <string> #include <cstdlib> #include <chrono> #include <thread> #ifdef _MSC_VER // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES #include <windows.h> #else // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES // not Microsoft Visual C++, so assume UNIX interface #include <iostream> #include <cstring> #include <cstdlib> #endif // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES /////////////////////////////////////////////////////////////////////////// // Global Constants /////////////////////////////////////////////////////////////////////////// const int CANVAS_ROW = 24; const int CANVAS_COL = 24; const int SQUARE_SIZE = 2; const int PENT_SIZE = 3; const int BLINK4_SIZE = 9; const int SLIDER_SIZE = 3; const int MAX_STEPS = 30; const char ALIVE = 'X'; const char DEAD = '.'; /////////////////////////////////////////////////////////////////////////// // Type Definitions /////////////////////////////////////////////////////////////////////////// class World; class Simulation; class Life { public: int getCol() const { return m_col; } int getRow() const { return m_row; } int getHeight() const { return m_height; } int getWidth() const {
  • 2. return m_width; } char getFigure(int r, int c) const { return m_pattern[r][c]; } void inWorld(World *w) { m_world = w; } void inSimulation(Simulation *s) { m_simulation = s; } bool areWeInASimulation() { return m_simulation != nullptr; } protected: int m_col; int m_row; int m_height; int m_width; char **m_pattern; World *m_world; Simulation *m_simulation; }; class Pent : public Life { public: Pent(int r, int c) { m_col = c; m_row = r; m_height = PENT_SIZE; m_width = PENT_SIZE; //Allocate space for figure m_pattern = new char*[m_height]; for (int i = 0; i < m_height; i++) { m_pattern[i] = new char[m_width]; } //Initialize figure for (int i = 0; i < m_height; i++) { for (int j = 0; j < m_width; j++) { m_pattern[i][j] = ALIVE; } } m_pattern[0][0] = DEAD;
  • 3. m_pattern[0][2] = DEAD; m_pattern[2][1] = DEAD; m_pattern[2][2] = DEAD; } ~Pent() { for (int i = 0; i < m_height; i++) { delete[] m_pattern[i]; } delete[] m_pattern; } }; class Square : public Life { public: Square(int r, int c) { m_col = c; m_row = r; m_height = SQUARE_SIZE; m_width = SQUARE_SIZE; //Allocate space for figure m_pattern = new char*[m_height]; for (int i = 0; i < m_height; i++) { m_pattern[i] = new char[m_width]; } //Initialize figure for (int i = 0; i < m_height; i++) { for (int j = 0; j < m_width; j++) { m_pattern[i][j] = ALIVE; } } } ~Square() { for (int i = 0; i < m_height; i++) { delete[] m_pattern[i]; } delete[] m_pattern; } }; class Slider : public Life { public: Slider(int r, int c) { m_col = c; m_row = r;
  • 4. m_height = SLIDER_SIZE; m_width = SLIDER_SIZE; // Allocate space for figure m_pattern = new char*[m_height]; for (int i = 0; i < m_height; i++) { m_pattern[i] = new char[m_width]; } // Initialize figure for (int i = 0; i < m_height; i++) { for (int j = 0; j < m_width; j++) { m_pattern[i][j] = DEAD; } } m_pattern[0][1] = ALIVE; m_pattern[1][2] = ALIVE; m_pattern[2][0] = ALIVE; m_pattern[2][1] = ALIVE; m_pattern[2][2] = ALIVE; } ~Slider() { for (int i = 0; i < m_height; i++) { delete[] m_pattern[i]; } delete[] m_pattern; } }; class Blink4 : public Life { public: Blink4(int r, int c) { m_col = c; m_row = r; m_height = BLINK4_SIZE; m_width = BLINK4_SIZE; //Allocate space for figure m_pattern = new char*[m_height]; for (int i = 0; i < m_height; i++) { m_pattern[i] = new char[m_width]; } //Initialize figure for (int i = 0; i < m_height; i++) { for (int j = 0; j < m_width; j++) { m_pattern[i][j] = DEAD;
  • 5. } } m_pattern[0][4] = ALIVE; m_pattern[1][4] = ALIVE; m_pattern[2][4] = ALIVE; m_pattern[6][4] = ALIVE; m_pattern[7][4] = ALIVE; m_pattern[8][4] = ALIVE; m_pattern[4][0] = ALIVE; m_pattern[4][1] = ALIVE; m_pattern[4][2] = ALIVE; m_pattern[4][6] = ALIVE; m_pattern[4][7] = ALIVE; m_pattern[4][8] = ALIVE; } ~Blink4() { for (int i = 0; i < m_height; i++) { delete[] m_pattern[i]; } delete[] m_pattern; } }; class World { public: World() { m_toggle = true; m_grid_1 = new char*[CANVAS_ROW]; m_grid_2 = new char*[CANVAS_ROW]; for (char i = 0; i < CANVAS_ROW; i++) { m_grid_1[i] = new char[CANVAS_COL]; m_grid_2[i] = new char[CANVAS_COL]; } for (char i = 0; i < CANVAS_ROW; i++) { for (char j = 0; j < CANVAS_COL; j++) { m_grid_1[i][j] = DEAD; } } } ~World() { for (char i = 0; i < CANVAS_ROW; i++) { delete[] m_grid_1[i]; delete[] m_grid_2[i];
  • 6. } delete[] m_grid_1; delete[] m_grid_2; } void render() const { clearScreen(); if (m_toggle) { for (char i = 0; i < CANVAS_ROW; i++) { for (char j = 0; j < CANVAS_COL; j++) { std::cout << m_grid_1[i][j]; } std::cout << std::endl; } } else { for (char i = 0; i < CANVAS_ROW; i++) { for (char j = 0; j < CANVAS_COL; j++) { std::cout << m_grid_2[i][j]; } std::cout << std::endl; } } for (char i = 0; i < CANVAS_COL; i++) { std::cout << '='; } std::cout << std::endl; } void computeNextState() { if (m_toggle) { for (char i = 0; i < CANVAS_ROW; i++) { for (char j = 0; j < CANVAS_COL; j++) { m_grid_2[i][j] = nextState(m_grid_1[i][j], i, j, m_toggle); } } m_toggle = !m_toggle; } else { for (char i = 0; i < CANVAS_ROW; i++) { for (char j = 0; j < CANVAS_COL; j++) { m_grid_1[i][j] = nextState(m_grid_2[i][j], i, j, m_toggle);
  • 7. } } m_toggle = !m_toggle; } } bool initState(Life *life) { if (life == nullptr) { std::cout << "Cannot add nullptr life" << std::endl; return false; } for (char i = life->getRow(); i - life->getRow() < life->getHeight(); i++) { for (char j = life->getCol(); j - life->getCol() < life->getWidth(); j++) { if (i < CANVAS_ROW && j < CANVAS_COL) { m_grid_1[i][j] = life->getFigure(i - life->getRow(), j - life->getCol()); } } } return true; } private: void clearScreen() const { #ifdef _MSC_VER // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hConsole, &csbi); DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y; COORD upperLeft = { 0, 0 }; DWORD dwCharsWritten; FillConsoleOutputCharacter(hConsole, TCHAR(' '), dwConSize, upperLeft, &dwCharsWritten); SetConsoleCursorPosition(hConsole, upperLeft); #else // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES static const char* term = getenv("TERM");// will just write a newline in an Xcode output window if (term == nullptr || strcmp(term, "dumb") == 0) std::cout << std::endl; else { static const char* ESC_SEQ = "x1B["; // ANSI Terminal esc seq: ESC [ std::cout << ESC_SEQ << "2J" << ESC_SEQ << "H" << std::flush; } #endif // DO NOT BREAK APART THE PREPROCESSOR DIRECTIVES }
  • 8. char nextState(char state, char row, char col, bool toggle) const { int yCoord = row; int xCoord = col; char neighbors = 0; if (toggle) { for (char i = yCoord - 1; i <= yCoord + 1; i++) { for (char j = xCoord - 1; j <= xCoord + 1; j++) { if (i == yCoord && j == xCoord) { continue; } if (i > -1 && i < CANVAS_ROW && j > -1 && j < CANVAS_COL) { if (m_grid_1[i][j] == ALIVE) { neighbors++; } } } } } else { for (char i = yCoord - 1; i <= yCoord + 1; i++) { for (char j = xCoord - 1; j <= xCoord + 1; j++) { if (i == yCoord && j == xCoord) { continue; } if (i > -1 && i < CANVAS_ROW && j > -1 && j < CANVAS_COL) { if (m_grid_2[i][j] == ALIVE) { neighbors++; } } } } } if (state == ALIVE) { return (neighbors > 1 && neighbors < 4) ? ALIVE : DEAD; } else { return (neighbors == 3) ? ALIVE : DEAD; } } // The rules of Conway's Simulation of Life requires each cell to // examine it's neighbors. So, we have to check the entire world // first before we change it. We can use two copies of the world,
  • 9. // one to check the current state (current day) then another to generate // the next state(for the next day). We can toggle between them each // step, to avoid having to copy between worlds each step (day) of the game. char **m_grid_1; char **m_grid_2; bool m_toggle; }; class Simulation { public: Simulation(Life **life, int numLife) { watchme = 4; m_steps = 0; m_automate = false; m_world = new World(); if (life != nullptr) { for (int i = 0; i < numLife; i++) { if (life[i] != nullptr) { bool success = m_world->initState(life[i]); if (!success) { std::cout << "Failed to add life to the world" << std::endl; } } } } } ~Simulation() { delete m_world; } void simulate() { while (true) { m_world->render(); if (!m_automate) { std::cout << "command (<space> to step, <a> to automate, <q> to quit): "; std::string action; std::getline(std::cin, action); switch (action[0]) { default: std::cout << 'a' << std::endl; // beep continue; case 'q': std::cout << "Quitting Game." << std::endl;
  • 10. return; case 's': continue; case ' ': break; case 'a': m_automate = true; break; } } else { if (m_steps >= MAX_STEPS) { std::cout << "Reached max steps, quitting." << std::endl; return; } delay(300); } m_steps++; m_world->computeNextState(); } } void report() { std::string msg = "Hello World!"; // Replace Hello World with your answer. std::cout << msg << std::endl; } int two(int u) { return u >> 1; } int three(int x) { if (x % 2 == 0) return 1; else return 2; } void one(int t) { int i = 7; int k = watchme; while (--i > t) { if (i == 0) k = 7; else if (i == 1) k = 9;
  • 11. else if (i == 4) k = 8; else if (i == 5) k = 5; else k = two(watchme); watchme = k; } k = three(watchme); watchme = k; } private: void delay(int ms) const { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); } int watchme; World * m_world; int m_steps; bool m_automate; }; int main() { const int numLife = 4; Life **population = new Life*[numLife]; population[0] = new Slider(8, 4); population[1] = new Pent(18, 14); population[2] = new Blink4(3, 13); population[3] = new Square(14, 10); // Create the game Simulation s(population, numLife); // Debugger Exercise s.one('k' - 'h'); // Run the game s.simulate(); // Report s.report(); // Clean up delete population[0]; delete population[1]; delete population[2]; delete population[3]; delete[] population; }
  • 12. Part 1: Organize the code (85%) : Following what we discussed on multiple file etiquette take the single source file provided, and divide it into appropriate header files and implementation files, one pair of files for each class. When complete the main function will be the only function in main. cpp. Make sure each file #includes the headers it needs. Each header file must have include guards. Only include header files when definitions are actually needed; declare objects in situations when the compiler only requires to "know" about the objects existence but not its definition. Now what about the global constants? Place them in their own header file named constants. h. Note that there are two versions of clearScreen depending on platform, do not break up the preprocessor directives; keep both versions. The first thing you should do is make sure that the single source compiles as is. Play around with it to get comfortable the program. The program implements Conway's Game of Life: https://conwaylife.com/wiki/Conway's Game of Life This is a simple simulation where cells are either alive or dead based simple rules based on under population or overcrowding. For those who are feeling adventurous look for some Life patterns and try to implement them. Part 2: Analysis (15%): You should notice that the class Simulation has the function report does not do anything useful other than printing "Hello World!" to the console. Replace "Hello World!" with a brief discussion addressing the questions below. This may require stepping through the code to get an overview of execution, hand tracing is an option but using the debugger with break points is much more efficient.Any code you submit must compile in VS2019, even if incomplete; any project that does not compile will receive a zero score.