Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You're to write a C++ program that randomly fills the entire screen with a character, like an asterisk, with certain foreground/background colors. After you've filled

You're to write a C++ program that randomly fills the entire screen with a character, like an asterisk, with certain foreground/background colors. After you've filled the entire screen with the character, you'll shift foreground/background colors and do it again and again, shifting foreground/background colors for a hypnotic effect! The program will do this endlessly in an infinite loop, until the user either stops it with a Ctrl+c keystroke, or simply forgets to breathe and expires.

Youll need to research how to generate random numbers using the rand function; If you're able to generate a series of random numbers, those numbers can be used to derive random row/column coordinates, which means you'd be able to draw a character to random positions on the screen until it's eventually filled up. We also considered how you could keep track of which row/column positions had already been used so that you don't keep writing characters to the same locations, by using a 2D array of bools.

cmatrix.h -- This header file contains the declaration for the CMatrix class. The purpose of this class is to fill the screen with a character at random locations. If you look at the class declaration, you'll see that it only has two data members: one to store the total number of rows, and another to store the total number of columns. These data members are initialized by the class constructor, which receives those values in the parameter list. Once the CMatrix object has been initialized, it's ready to have its CMatrix::Fill member function called. This function takes as input a character to draw, and a sleep interval to control the speed with which they're drawn. A loop is entered to draw the character to random locations on the screen, until all available locations have been used and the screen is completely filled. (Hmm... how will it keep track of available screen locations? How will it know when the screen has been completely filled?)

cscreen.h -- This header file contains the declaration for the CScreen class. This class has two data members -- one that stores the character to display, and another which stores the sleep interval that can be used as an argument to the usleep function. These data members will get their values from an external file called config.dat, which is read by the class constructor. The constructor will also be responsible for performing any initialization needed by the curses library, which can be handled easily enough by calling the InitCurses member function, which can set up the initialization and colors. The CScreen class only has one other member function, Scatter, which is responsible for the random drawing of characters. As it changes the foreground/background colors, it will use a local CMatrix object to fill the screen with the display character.

main.cpp -- Obviously, this is the main module that drives the whole program. It creates an instance of the CScreen class, passing the name of the configuration file to its constructor. Then calls that object's Scatter function, which takes it from there to randomly fill the screen.

config.dat -- This is the configuration file that the program reads before it begins. If you open it up with a text editor, you'll see that it contains two items: the character to display, and the sleep interval to use when the usleep function is called. Look in the main module and you'll see the name of this file is passed to the CScreen constructor; if you look in the private section of that class, you'll see how the class stores the items in config.dat in its private data members.

scatter -- This is a sample executable, so run the program and see what it does. Try modifying the items in config.dat and see how your changes affect the behavior of the program.

BTW, the only files you should modify are the cmatrix.cpp and cscreen.cpp files, the header files and the main modules are already in a finished state.

The first stage won't require any code. All you have to do is write the comments for each non-inlined function you see in the .cpp files. This will give you a chance to sketch out the algorithmic steps without having to worry about any code, that will come later. It will also give you something to follow once you do start writing code. It's okay to experiment with some throw-away code so you can get more familiar with the curses functions, but as far as what you turn in for the first stage, keep it to just your comments added to the empty functions, no code yet. The second stage will be the coded implementation

MAIN.CPP

#include "cscreen.h" // ==== main ================================================================== // // ============================================================================ int main() { // create a screen object CScreen screen("config.dat"); // have the object fill the screen at random locations with colored // characters screen.Scatter(); return 0; } // end of "main"

CSCREEN.CPP #include cstdlib #include ctime #include fstream #include ncurses.h #include unistd.h using namespace std; #include "cscreen.h" #include "cmatrix.h" // ==== CScreen::CScreen ====================================================== // // This is the constructor for the CScreen class. It uses the parameter to open // the configuration file and fetch the display character and sleep interval to // initialize the object. If values cannot be read from the input file, default // values are used. Then the random number generator is seeded with the current // system time, and the curses library is initialized before returning. // // Input: // fname [IN] -- a cstring containing the name of the configuration // file // // ============================================================================ CScreen::CScreen(const char fname[]) { ??? } // end of "CScreen::CScreen" // ==== CScreen::InitCurses =================================================== // // This function is responsible for initializing the curses library. It also // establishes the foreground and background colors for all of the color pair // structures. // // Input: // Nothing // // Output: // Nothing // // ============================================================================ void CScreen::InitCurses() { ??? } // end of "CScreen::InitCurses" // ==== CScreen::Scatter ====================================================== // // This function contains an infinite loop that draws characters to random // locations on the screen. Inside the loop, a COLOR_PAIR is activated, then a // local CMatrix object is used to fill the screen with the display character. // After the screen has been filled, the loop pauses for about a half-second, // before doing it all over again and again, until the user presses Ctrl+c, // which terminates the loop so that the function can return to the caller. // // Input: // Nothing // // Output: // Nothing // // ============================================================================ void CScreen::Scatter() { ??? } // end of "CScreen::Scatter"

CMATRIX.CPP

#include cstdlib #include ctime #include fstream #include ncurses.h #include unistd.h using namespace std; #include "cmatrix.h"

// ==== CMatrix::CMatrix ====================================================== // // This is the CMatrix constructor, it just uses the parameters to initialize // the CMatrix data members so that the screen dimensions are known to the // matrix ADT. // // Input: // numRows [IN] -- the number of rows in the current display // // numCols [IN] -- the number of columns in the current display // // ============================================================================

CMatrix::CMatrix(int numRows, int numCols) { ???

} // end of "CMatrix::CMatrix"

// ==== CMatrix::CMatrix ====================================================== // // This function is responsible for filling the screen at random locations // with the character parameter, and pausing the specified number of micro- // seconds beween the drawing of each character. This drawing of characters // continues until the screen is completely filled, at which point the // function returns to the caller. // // NOTE: It is assumed that the caller has set the foreground and background // colors before making this call. // // Input: // dispChar [IN] -- the character to use to fill the screen // // msecs [IN] -- the number of microseconds to pause between the // drawing of each character // // Outpu: // Nothing // // ============================================================================

void CMatrix::Fill(char dispChar, int msecs) { ???

} // end of "CMatrix::Fill"

CMATRIX.h

// ============================================================================ // File: cmatrix.h // ============================================================================ // This header file contains the declaration for the CMatrix class. The class // is used by the CScreen class to help fill the screen display with characters // at random localtions. // ============================================================================

#ifndef CMATRIX_H #define CMATRIX_H

#include #include using namespace std;

// class declaration class CMatrix { public: // constructor CMatrix(int numRows, int numCols);

// member functions void Fill(char dispChar, int msecs);

private: int m_numRows; int m_numCols; };

#endif // CMATRIX_H

CSCREEN.h

// ============================================================================ // File: cscreen.h // ============================================================================ // This is the header file for the CScreen class. // ============================================================================

#ifndef CSCREEN_H #define CSCREEN_H

#include #include using namespace std;

// defined constants const char DEFAULT_DISPCHAR = '*'; const int DEFAULT_SLEEP = 200; const int LAST_COLOR_PAIR_INDEX = 7;

// class declaration class CScreen { public: // constructor CScreen(const char fname[]);

// member functions void InitCurses(); void Scatter();

private: char m_dispChar; int m_sleep; };

#endif // CSCREEN_H

CONFIG.dat

+

2000000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions

Question

How would you choose among widely different estimates of R(Re)?

Answered: 1 week ago