Project: Scatter

Write a program that does the following: (below is dissected instructions, followed by the code thus far)
Please comment as much as possible, i am really struggling with this one. Any insight/steps as to how you solved the problem before implementing code would be very helpful too.
The only files to modify are cmatrix.cpp and cscreen.cpp. The header files and the main modules are already finished correctly.
1A. randomly fills the entire screen with a character, like an asterisk, with certain foreground/background colors.
1B. 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.
**You’ll need to research how to generate random numbers using the rand function; you can find further detailed notes by reading here (Links to an external site.)Links to an external site..
2A. If you're able to generate a series of random numbers:
2B. 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.
2C. 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 =============================================================
// File: cmatrix.h -- This header file contains the declaration for the CMatrix class.
CM1. The purpose of this class is to fill the screen with a character at random locations.
CM2. If you look at the class declaration, you'll see that it only has two data members:
a- one to store the total number of rows,
b- and another to store the total number of columns.
CM3. These data members are INITIALIZED by the class constructor, which receives those values in the parameter list.
CM4. Once the CMatrix object has been initialized, it's ready to have its CMatrix::Fill member function called.
// ==== CMatrix::Fill =========================================================
CM5. This function takes as input:
a- a character to draw,
b- and a sleep interval to control the speed with which they're drawn.
CM6. 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 =============================================================
// File: cscreen.h -- This header file contains the declaration for the CScreen class.
SC1. This class has two data members --
a. one that stores the character to display,
b. and another which stores the sleep interval that can be used as an argument to the usleep function.
SC2. These data members will get their values from an external file called config.dat, which is read by the class constructor.
SC3. 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.
SC4. The CScreen class only has one other member function
// ==== Scatter ====
SC5. which is responsible for the random drawing of characters.
SC6. As it changes the foreground/background colors, it will use a local CMatrix object to fill the screen with the display character.
// ==== main.cpp ==============================================================
// File: main.cpp -- Obviously, this is the main module that drives the whole program.
M1. It creates an instance of the CScreen class, passing the name of the configuration file to its constructor.
M2. Then calls that object's Scatter function, which takes it from there to randomly fill the screen.
// ==== config.dat ============================================================
// File: config.dat -- This is the configuration file that the program reads before it begins.
CFG1. If you open it up with a text editor, you'll see that it contains two items:
a- the character to display
b- and the sleep interval to use when the usleep function is called.
When using "cat config.dat" below is on stdout:
+
20000
CFG2. Look in the main module and you'll see the name of this file is passed to the CScreen constructor;
CFG3. 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.
------------------------ FILES ------------------------
// ============================================================================
// File: main.cpp
// ============================================================================
#include "cscreen.h"
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;
}
// ============================================================================
// File: cscreen.h
// ============================================================================
#ifndef CSCREEN_H
#define CSCREEN_H
#include <iostream>
#include <fstream>
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
// ============================================================================
// File: 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(const char fname[])
{
// initialize file name
}
void CScreen::InitCurses()
{
// initialize curses library
initscr();
// establish the foreground/background colors
start_color();
}
void CScreen::Scatter()
{
// initizalize curses by calling InitCurses
// ============================================================================
// File: cmatrix.h
// ============================================================================
#ifndef CMATRIX_H
#define CMATRIX_H
#include <iostream>
#include <fstream>
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
// ============================================================================
// File: cmatrix.cpp
// ============================================================================
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <ncurses.h>
#include <unistd.h>
using namespace std;
#include "cmatrix.h"
// ==== CMatrix::CMatrix ======================================================
CMatrix::CMatrix(int numRows, int numCols)
{
// initialize the member variables
numRows = m_numRows;
numCols = m_numCols;
}
// ==== CMatrix::CMatrix ======================================================
void CMatrix::Fill(char dispChar, int msecs)
{
//
}
So, you've just copied and pasted what looks like a homework assignment.

What is it, specifically, that you're having problems with?

Please show us what you've written so far.
Hello DE98,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I do agree with MikeyBoy, but I will say it looks like a good start.

Although I do see some problems that should not be done.

In your header files you have the lines:

1
2
3
#include <iostream>
#include <fstream>
using namespace std;

These include files should not be there. They are better put in the ".cpp" where they are used not header files. Line 3 should not be used at all and never put in a header file as it WILL cause you problems in the future. This is worth reading: http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Otherwise at first look the header files the header file look good and the ".cpp" files look OK.

I do not have the " <ncurses.h>" header file yet so I have a hard time testing the program right now.

Hoope that helps,

Andy
Topic archived. No new replies allowed.