Program 4 – Fireworks! Due: Refer to the Canvas assignment. Background The purpose of this program is to familiarize yourself with pointer and memory buffer operations in C++. In this assignment, you...

1 answer below »
I need a program completed in C++ in CLION.


Program 4 – Fireworks! Due: Refer to the Canvas assignment. Background The purpose of this program is to familiarize yourself with pointer and memory buffer operations in C++. In this assignment, you implement a program that renders fireworks using the ASCII art technique. The technique uses ASCII characters, represented by the char data type in C++, to compose and print images. Consider the following four examples of ASCII firework art “images” as the inputs. 1) .* *. *..* * 2) ^,^,^ ^,^,^,^,^ ^,^,^,^,^ ^,^,^ 3) `o`o` o`o`o`o `o`o` 4) ~@~*~@~ ~*~@~*~@~*~ ~*~@$#$@~*~ ~*~@~*~@~*~ ~@~*~@~ https://en.wikipedia.org/wiki/ASCII_art https://en.wikipedia.org/wiki/ASCII_art The program randomly generates a scene using the fireworks and “renders” the buffer by printing it into the console (std::cout). Here is an example: ^,^,^ ^,^,^,^,^ ^,^,^,^,^ ~@~@~ ^,^,^ ~*~@~*~@~*~ / .* *. ~*~@$#$@~*~ / *..* `o`o` ~*~@~*~@~*~ / * o`o`o`o ~@~@~ / / `o`o` / / / \ / / / \ / / / \ / / / \ / / / \ / / / \ / / / \ / / / \ / / / \ / / / The Task Conceptually, you can think of the rendered scene as a buffer of characters. The buffer is rectangular in shape. It has 20 lines with 60 characters each. The buffer is “rendered” by printing it into the console line by line. The program runs an infinite loop that asks the user to render the fireworks. The figure below serves as an illustration. The program renders the fireworks scene if the user answers ‘yes’ with the ‘y’ character. It exits the loop otherwise. An iteration of the loop randomly generates a fireworks image. The program uses a random number generator (refer to Zybook Chapter 2.19) for each firework art to define its horizontal and vertical positions within the buffer. The position (indicated by the red dots in the figure below) represents the upper left corner of the firework art within the buffer. The firework art characters are copied into the buffer starting at the position. Each firework in the buffer also includes a “trace” to the ground. The trace characters are copied into the buffer after copying the firework art. The trace direction, which can be to the left, right, or straight up, is defined by the relative position of the bottom mid-point of the firework art (the green dot) to the mid-point on the ground (the green arrow). Depending on its direction, the trace is rendered with the ‘\\’, ‘/’, or ‘|’ characters. The tiny dark green dots in the figure below represent the whitespace character to make the buffer visualization in this figure more intuitive. The random fireworks art positions may end up close to the edge of the buffer. In this case, the program renders only the “visible” part that is within the buffer. Here is an example: What to write Your program must have the following structure: 1) Start with allocating the buffer. The buffer must be allocated dynamically (with C++ operator new) as a C++ array of type char. The buffer size is derived from the dimensions of the rectangular rendering area, which is 60x20=1200 characters. 2) The program goes into an infinite loop where it asks the user to print a fireworks scene. 3) If the user answers ‘y’, the program performs the following steps: a. Initializes the buffer with the whitespace character b. Sequentially renders the four firework arts. For each firework art, a random position within the buffer is generated. Note that each firework art must be at least partially visible in the scene – no missing fireworks. After rendering the firework, the program also renders its trace into the buffer. c. Print the buffer into the console and continue to the next iteration of the user loop. 4) Delete the buffer after exiting the user loop. 5) Exit the program. Hints: • One of the most common errors when working with pointers and pointer arithmetics is assigning to memory locations outside the memory buffer. This kind of error produces “undefined run time behavior,” making your program crash or behave unpredictably. Use the assert statement to check at run time that a buffer index is within the buffer limits. • Think about each firework art as a set of horizontal scanlines. The scanlines are copied into the rendering buffer one at a time. Each scanline can be represented by either an array of type char or the std::vector type. • Use the rand() function (refer to Zybook Chapter 2.19) to generate the horizontal and vertical positions of the firework in the buffer. Use the std::array type to store and pass the position to other functions. Implemetation details and grading rubric Please read the implementation details carefully to ensure that you receive full credit for this programming assignment. • The rendering buffer must be implemented as a dynamically allocated array of type char* (10 pts) • The rendering buffer must be deallocated before the program exits (5 pts) • The rendering buffer initialization with the white space must be implemented as a function. (10 pts) • Copying of a firework art into the rendering buffer must be implemented as a function. Introduce a function for
Answered 8 days AfterOct 27, 2021

Answer To: Program 4 – Fireworks! Due: Refer to the Canvas assignment. Background The purpose of this program...

Vaibhav answered on Nov 05 2021
112 Votes
Fireworks.cpp
#include
#include
using namespace std;
/**
* @brief Initialize the empty buffer with whitespace char.
*
* @param buffer

*/
void initializeBuffer(char **buffer) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 60; j++) {
buffer[i][j] = ' ';
}
}
}
void copyTrace(int x, int y, char **buffer) {
char drawChar;
int i, j;
if (y == 30) {
drawChar = '|';
for (i = x, j = y; i < 20 && j < 60; i++) {
buffer[i][j] = drawChar;
}
} else if (y < 30) {
drawChar = '\\';
for (i = x, j = y; i < 20 && j < 60; i++, j++) {
buffer[i][j] = drawChar;
}
} else {
drawChar = '/';
for (i = x, j = y; i < 20 && j < 60; i++, j--) {
buffer[i][j] = drawChar;
}
}
}
void copyFireworkArt1(int startRow, int startCol, char **buffer) {
/* ~@~@~
~*~@~*~@~*~
~*~@$#$@~*~
~*~@~*~@~*~
~@~@~
*/
char pattern[5][11] = {
{' ', ' ', ' ', '~', '@', '~', '@', '~', ' ', ' ', ' '},
{'~', '*', '~', '@', '~', '*', '~', '@', '~', '*', '~'},
{'~', '*', '~', '@', '$', '#', '$', '@', '~', '*', '~'},
{'~', '*', '~', '@', '~', '*', '~', '@', '~', '*', '~'},
{' ', ' ', ' ', '~', '@', '~', '@', '~', ' ', ' ', ' '}};
int i, j;
int k, l;
for (i = startRow, k = 0; i < startRow + 5; i++, k++) {
for (j = startCol, l = 0; j < startCol + 11; j++, l++) {
if (i < 20 && j < 60) {
buffer[i][j] = pattern[k][l];
}
}
}
}
void copyFireworkArt2(int startRow, int startCol, char **buffer) {
/*
.* *.
*..*
* */
char...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here