Need help filling screen with random characters.

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.

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.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#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.
// ============================================================================
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...\n";
}

//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.
// ====================================================================

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.
//
// ============================================================================
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.
//
// ============================================================================

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
// 

void CMatrix::Fill(char dispChar, int msecs)
{
???
} // end of "CMatrix::Fill" 


1
2
3
4
void CMatrix::Fill(char dispChar, int msecs)
{
???
} // end of "CMatrix::Fill" 
Last edited on
Hello pbmfs,

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.

This is going to take a little while to sort out, i.e., get to a point that I can read and understand the code. All the ".cpp" code is great, but it would be helpful to have the header files so I could compile and test.

Andy
Hello pbmfs,

As I was looking at the program I came across "config.dat". Looks like something else I need to know about.

Andy
Hi Andy, sorted my code out. I'll see if I can extract config.dat as it's stored server side on putty.
Topic archived. No new replies allowed.