How can I make it so that this code generates the board without any of the console "flashing" occuring?

I had to remove a room so that I could fit it in the content limit, so just don't try to enter room 5. w, a, s, d to navigate the board.

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <iostream>
#include <string>
#include <conio.h>
#include <stdlib.h>

using namespace std;

void printFunc(string room[12][12]){
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";                     // So that user only sees one instance
                                                                        // of the game board at a time.


    for (int i = 0; i < 12; i++){
        for (int j = 0; j < 12; j++){
            cout << room[i][j] << " ";
        }
        cout << endl;
    }
}


void room1Func(char move, int &r, int &c, int &currRoom){
    int validMove = 1;
    string x = "x";
    string p = " ";
    string room1gen[12][12] =
    {
    {x,x,x,x,x,x,x,x,x,x,x,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,"|",},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,x,x,"_",x,x,x,x,x,x,x,x,},
    };

    if (move == 'd' && room1gen[r][c + 1] == "|"){                      // check for movement to room 2
        validMove = 0;
        currRoom = 2;
        r = 3;
        c = 1;
    }

    if (move == 's' && room1gen[r + 1][c] == "_"){                      // check for movement to room 3
        validMove = 0;
        currRoom = 3;
        r = 1;
        c = 3;
    }

    if (move == 'w' && room1gen[r - 1][c] != x && validMove == 1) {     // movement and checking for walls
        r--;
    }
    else if (move == 'a' && room1gen[r][c - 1] != x && validMove == 1) {
        c--;
    }
    else if (move == 's' && room1gen[r + 1][c] != x && validMove == 1) {
        r++;
    }
    else if (move == 'd' && room1gen[r][c + 1] != x && validMove == 1) {
        c++;
    }
    else if (move == ' '){                                              // for movement between rooms
    }
    else if (validMove == 0) {
    }
    else {
        cout << "ERROR: Invalid move." << endl;
        validMove = 0;
    }

    if (validMove == 1){
        room1gen[r][c] = "0";
        printFunc(room1gen);
    }
}


void room2Func(char move, int &r, int &c, int &currRoom){
    int validMove = 1;
    string x = "x";
    string p = " ";
    string room2gen[12][12] =
    {
    {x,x,x,x,"_",x,x,x,x,x,x,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {"|",p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,x,x,x,x,x,x,x,x,x,x,x,},
    };

    if (move == 'a' && room2gen[r][c - 1] == "|"){                      // check for movement to room 1
        validMove = 0;
        currRoom = 1;
        r = 3;
        c = 10;
    }

    if (move == 'w' && room2gen[r - 1][c] == "_"){                      // check for movement to room 4
        validMove = 0;
        currRoom = 4;
        r = 10;
        c = 4;
    }

    if (move == 'w' && room2gen[r - 1][c] != x && validMove == 1) {     // movement and checking for walls
        r--;
    }
    else if (move == 'a' && room2gen[r][c - 1] != x && validMove == 1) {
        c--;
    }
    else if (move == 's' && room2gen[r + 1][c] != x && validMove == 1) {
        r++;
    }
    else if (move == 'd' && room2gen[r][c + 1] != x && validMove == 1) {
        c++;
    }
    else if (move == ' '){                                              // for movement between rooms
    }
    else if (validMove == 0) {
    }
    else {
        cout << "ERROR: Invalid move." << endl;
        validMove = 0;
    }

    if (validMove == 1){
        room2gen[r][c] = "0";
        printFunc(room2gen);
    }
}


void room3Func(char move, int &r, int &c, int &currRoom){
    int validMove = 1;
    string x = "x";
    string p = " ";
    string room3gen[12][12] =
    {
    {x,x,x,"_",x,x,x,x,x,x,x,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,"|",},
    {x,x,x,x,x,x,x,x,x,x,x,x,},
    };

    if (move == 'd' && room3gen[r][c + 1] == "|"){                      // check for movement to room 5
        validMove = 0;
        currRoom = 5;
        r = 10;
        c = 1;
    }

    if (move == 'w' && room3gen[r - 1][c] == "_"){                      // check for movement to room 1
        validMove = 0;
        currRoom = 1;
        r = 10;
        c = 3;
    }

    if (move == 'w' && room3gen[r - 1][c] != x && validMove == 1) {     // movement and checking for walls
        r--;
    }
    else if (move == 'a' && room3gen[r][c - 1] != x && validMove == 1) {
        c--;
    }
    else if (move == 's' && room3gen[r + 1][c] != x && validMove == 1) {
        r++;
    }
    else if (move == 'd' && room3gen[r][c + 1] != x && validMove == 1) {
        c++;
    }
    else if (move == ' '){                                              // for movement between rooms
    }
    else if (validMove == 0) {
    }
    else {
        cout << "ERROR: Invalid move." << endl;
        validMove = 0;
    }

    if (validMove == 1){
        room3gen[r][c] = "0";
        printFunc(room3gen);
    }
}


void room4Func(char move, int &r, int &c, int &currRoom){
    int validMove = 1;
    string x = "x";
    string p = " ";
    string room4gen[12][12] =
    {
    {x,x,x,x,x,x,x,x,x,x,x,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,p,p,p,p,p,p,p,p,p,p,x,},
    {x,x,x,x,"_",x,x,x,x,x,x,x,},
    };

    if (move == 's' && room4gen[r + 1][c] == "_"){                      // check for movement to room 2
        validMove = 0;
        currRoom = 2;
        r = 1;
        c = 4;
    }

    if (move == 'w' && room4gen[r - 1][c] != x && validMove == 1) {     // movement and checking for walls
        r--;
    }
    else if (move == 'a' && room4gen[r][c - 1] != x && validMove == 1) {
        c--;
    }
    else if (move == 's' && room4gen[r + 1][c] != x && validMove == 1) {
        r++;
    }
    else if (move == 'd' && room4gen[r][c + 1] != x && validMove == 1) {
        c++;
    }
    else if (validMove == 0) {
    }
    else if (move == ' '){                                              // for movement between rooms
    }
    else {
        cout << "ERROR: Invalid move." << endl;
        validMove = 0;
    }

    if (validMove == 1){
        room4gen[r][c] = "0";
        printFunc(room4gen);
    }
}


int main() {

    char move = 'w';
    int currRoom = 1;
    int r = 5;
    int c = 4;

    while (move != 'q'){
        switch (currRoom) {
            case 1:
                room1Func(move, r, c, currRoom);
                break;
            case 2:
                room2Func(move, r, c, currRoom);
                break;
            case 3:
                room3Func(move, r, c, currRoom);
                break;
            case 4:
                room4Func(move, r, c, currRoom);
                break;
        }
    move = getch();
    }

    return 0;
}
Last edited on
I would change printfunc() as follows:
8
9
10
11
12
13
14
15
16
17
18
void printFunc(string room[12][12]) {
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";                     // So that user only sees one instance
                                                                        // of the game board at a time.
    for (int i = 0; i < 12; i++) {
        for (int j = 0; j < 12; j++) {
            cout << room[i][j] << " ";
        }
        cout << '\n';
    }
    cout.flush();
}


By writing '\n' at line 15 you avoid flushing the buffer after each line.

BTW, std::string seems like overkill for your room array since your grid really consists of single characters.
Last edited on
I changed line 15 and added the cout.flush(); to 17 but it doesn't seem to have made a difference, there's still a visible split second where the old board is getting pushed upward while the new one generates, the the console flash is still visible. Any way around this?
That's probably the best you're going to get using standard C++ i/O.

My only other suggestion would be to replace line 9 with the following system dependent call:
1
2
 
  system ("CLS");  // Works for Windows.  May be different for other platforms 

The only way to avoid console flash is to use nonstandard toys like gotoxy() to move the cursor around and over-write what was there. It won't flash, but you have to be sure to write spaces where you need to clear values, and you will see the update in place happen. You basically need to know the console position of each character on the screen, and jump to the ones you need to change. Its a big pain to do this.


Thank you jonnin.

Using system("cls") (or any other code to clear the screen) is almost guaranteed to cause flashing.

Use gotoxy() to reposition the cursor at the beginning of your game board, and redraw it —all of it, including spaces.

The very first post in my (Game of Life: Gosper Glider Gun|http://www.cplusplus.com/forum/lounge/75168/) thread is simply code to control the terminal in both Windows and Unix/Linux. Feel free to cut and paste.

In the game proper (next post), notice how the only time I call ClearScreen() is exactly once, at the very beginning of the program.

After that, I use GotoXY() to reposition the cursor for drawing the game board. No flashing!


Finally, be aware that it is entirely possible that your professor will not be using a terminal that this will work on. (It is unlikely, but possible.)

Make sure to check to see if terminal control is possible (like I do on line 107). If it is possible, use the GoHome() function to reposition the cursor at the top-left of the screen. If it is not, print a whole bunch of newlines like you are doing now. That way your program functions no matter what.

Hope this helps.

:O)
Topic archived. No new replies allowed.