Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have everything except the CMatrix::Fill() function which is at the bottom of the question. I hope someone can help. You're to write a C++

I have everything except the CMatrix::Fill() function which is at the bottom of the question. I hope someone can help.

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.

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 -- This is the main module that drives the whole program. It creates an instance of the CScreenclass, 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.

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.

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.

#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 // Description: // This is the implementation file for the CScreen class. // // ============================================================================

#include #include #include #include #include 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[]) { //use fname extraction operator to assign the values to the private data // members.Then random number generator is seeded for random pairing //this also calls the InitCurses function before returning back to the caller ifstream myFile;

myFile.open(fname); if(myFile.fail()) { cout << "Error with file name... "; }

//get the display character and sleep interval myFile.get(m_dispChar); myFile >> m_sleep; srand(time(NULL)); InitCurses();

} // 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() { //initialize curses library. initscr(), start_color() //set the foreground and background color. //use init_pair() initscr(); start_color(); init_pair(1, COLOR_RED, COLOR_BLUE); init_pair(2, COLOR_BLACK, COLOR_MAGENTA); init_pair(3, COLOR_WHITE, COLOR_BLUE); init_pair(4, COLOR_BLACK, COLOR GREEN); init_pair(5, COLOR_WHITE, COLOR_CYAN); init_pair(6, COLOR_BLACK, COLOR_YELLOW); init_pair(7, COLOR_WHITE, COLOR_BLACK);

} // 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() {

//Will create a matix object passing in LINES and COLS as arguments. attron() //and color_pair() will be used in the infinite loop filling up the matrix //once the space is used to fill a block in the value of that block will be //true. If not, then false. //use usleep function for a pause.

int index; CMatrix matrix(LINES, COLS);

for(index = 0; index<7 ; ++index) { attron(COLOR_PAIR(index));

matrix.fill(m_dispChar, m_sleep); refresh(); usleep(500000); }

} // 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) { m_numRows = numRows;

m_numCols = 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"

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

Students also viewed these Databases questions

Question

Derive expressions for the rates of forward and reverse reactions?

Answered: 1 week ago

Question

Write an expression for half-life and explain it with a diagram.

Answered: 1 week ago

Question

What do you mean by underwriting of shares ?

Answered: 1 week ago

Question

Define "Rights Issue".

Answered: 1 week ago