Home > front end >  Why is my Run Length Encoding algorithm not draw things properly to the console?
Why is my Run Length Encoding algorithm not draw things properly to the console?

Time:10-12

So I have made an RLE implementation to draw characters to the console from a matrix but for some reason, the print_chars(); function seems to be overprinting the ammount of characters that sould be displayed, I am only making 8 values in the matrix bigger than 0 but it is printing like 30. I believe this could be due to allocating too much memory but I dont know. Any help would be apreciated.

code:

#include <stdio.h>
#include <time.h>
#include <windows.h>

#define sizeX 150
#define sizeY 40

int grid[sizeY][sizeX];

void colorChar(int r, int g, int b){
    printf("\x1b[48;2;%d;%d;%dm",r,g,b);
}

void draw_screen(){
 int y, x;
 for(y=0;y<sizeY;y  ){
  for(x=0;x<sizeX;x  ){
    if(grid[y][x] == 0){
        colorChar(0,0,0);
    printf(" ");
   }
   else{
    colorChar(255,255,255);
    printf(" ");
   }
  }
  printf("\n");
 }
}

int main(void) {
 int x, y;
 float frameTime;
 grid[19][19] = 1;
 grid[2][8] = 1;
 grid[15][12] = 1;
 grid[6][3] = 1;
 grid[13][16] = 99;
 grid[17][3] = 1;
 grid[6][6] = 1;
 grid[9][3] = 1;

 clock_t start = clock();
 draw_screen();
 clock_t stop = clock();
 frameTime = (float)(stop - start) / CLOCKS_PER_SEC;
 printf("Frame: %f\n", frameTime);
}

CodePudding user response:

I replaced your calls to print_chars with debugging printfs:

void draw_screen(){
 int y, x;
 for(y=0;y<=sizeY;y  ){
  for(x=0;x<=sizeX;x  ){
   if(grid[y][x] == 0){
    counter0  ;
    if(grid[y][x 1] != 0 || x 1 >= sizeX){
     printf("grid[%d][%d] == 0; grid[%d][%d 1] != 0 || %d 1 >= %d; ", y, x, y, x, x, sizeX);
     printf("x=%d; y=%d; counter0=%d; counter1=%d\n", x, y, counter0, counter1);
     //print_chars(counter0, ' ', "BLACK");
     counter0 = 0;
    }
   }
   else{
    counter1  ;
    if(grid[y][x 1] != 1 || x 1 >= sizeX){
     printf("!(grid[%d][%d] == 0); grid[%d][%d 1] != 1 || %d 1 >= %d; ", y, x, y, x, x, sizeX);
     printf("x=%d; y=%d; counter0=%d; counter1=%d\n", x, y, counter0, counter1);
     //print_chars(counter1, ' ', "WHITE");
     counter1 = 0;
    }
   }
  }
  //printf("\n");
 }
}

This outputs:

grid[0][87] == 0; grid[0][87 1] != 0 || 87 1 >= 150; x=87; y=0; counter0=88; counter1=0
!(grid[0][88] == 0); grid[0][88 1] != 1 || 88 1 >= 150; x=88; y=0; counter0=0; counter1=1
grid[0][149] == 0; grid[0][149 1] != 0 || 149 1 >= 150; x=149; y=0; counter0=61; counter1=0
grid[0][150] == 0; grid[0][150 1] != 0 || 150 1 >= 150; x=150; y=0; counter0=1; counter1=0
grid[1][47] == 0; grid[1][47 1] != 0 || 47 1 >= 150; x=47; y=1; counter0=48; counter1=0
!(grid[1][48] == 0); grid[1][48 1] != 1 || 48 1 >= 150; x=48; y=1; counter0=0; counter1=1
grid[1][149] == 0; grid[1][149 1] != 0 || 149 1 >= 150; x=149; y=1; counter0=101; counter1=0
grid[1][150] == 0; grid[1][150 1] != 0 || 150 1 >= 150; x=150; y=1; counter0=1; counter1=0
grid[2][7] == 0; grid[2][7 1] != 0 || 7 1 >= 150; x=7; y=2; counter0=8; counter1=0
!(grid[2][8] == 0); grid[2][8 1] != 1 || 8 1 >= 150; x=8; y=2; counter0=0; counter1=1
grid[2][149] == 0; grid[2][149 1] != 0 || 149 1 >= 150; x=149; y=2; counter0=141; counter1=0
grid[2][150] == 0; grid[2][150 1] != 0 || 150 1 >= 150; x=150; y=2; counter0=1; counter1=0
grid[3][122] == 0; grid[3][122 1] != 0 || 122 1 >= 150; x=122; y=3; counter0=123; counter1=0
!(grid[3][123] == 0); grid[3][123 1] != 1 || 123 1 >= 150; x=123; y=3; counter0=0; counter1=1
grid[3][125] == 0; grid[3][125 1] != 0 || 125 1 >= 150; x=125; y=3; counter0=2; counter1=0
!(grid[3][126] == 0); grid[3][126 1] != 1 || 126 1 >= 150; x=126; y=3; counter0=0; counter1=1
grid[3][149] == 0; grid[3][149 1] != 0 || 149 1 >= 150; x=149; y=3; counter0=23; counter1=0
grid[3][150] == 0; grid[3][150 1] != 0 || 150 1 >= 150; x=150; y=3; counter0=1; counter1=0
grid[4][82] == 0; grid[4][82 1] != 0 || 82 1 >= 150; x=82; y=4; counter0=83; counter1=0
!(grid[4][83] == 0); grid[4][83 1] != 1 || 83 1 >= 150; x=83; y=4; counter0=0; counter1=1
grid[4][85] == 0; grid[4][85 1] != 0 || 85 1 >= 150; x=85; y=4; counter0=2; counter1=0
!(grid[4][86] == 0); grid[4][86 1] != 1 || 86 1 >= 150; x=86; y=4; counter0=0; counter1=1
grid[4][149] == 0; grid[4][149 1] != 0 || 149 1 >= 150; x=149; y=4; counter0=63; counter1=0
grid[4][150] == 0; grid[4][150 1] != 0 || 150 1 >= 150; x=150; y=4; counter0=1; counter1=0
grid[5][42] == 0; grid[5][42 1] != 0 || 42 1 >= 150; x=42; y=5; counter0=43; counter1=0
!(grid[5][43] == 0); grid[5][43 1] != 1 || 43 1 >= 150; x=43; y=5; counter0=0; counter1=1
grid[5][45] == 0; grid[5][45 1] != 0 || 45 1 >= 150; x=45; y=5; counter0=2; counter1=0
!(grid[5][46] == 0); grid[5][46 1] != 1 || 46 1 >= 150; x=46; y=5; counter0=0; counter1=1
grid[5][149] == 0; grid[5][149 1] != 0 || 149 1 >= 150; x=149; y=5; counter0=103; counter1=0
grid[5][150] == 0; grid[5][150 1] != 0 || 150 1 >= 150; x=150; y=5; counter0=1; counter1=0
grid[6][2] == 0; grid[6][2 1] != 0 || 2 1 >= 150; x=2; y=6; counter0=3; counter1=0
!(grid[6][3] == 0); grid[6][3 1] != 1 || 3 1 >= 150; x=3; y=6; counter0=0; counter1=1
grid[6][5] == 0; grid[6][5 1] != 0 || 5 1 >= 150; x=5; y=6; counter0=2; counter1=0
!(grid[6][6] == 0); grid[6][6 1] != 1 || 6 1 >= 150; x=6; y=6; counter0=0; counter1=1
grid[6][122] == 0; grid[6][122 1] != 0 || 122 1 >= 150; x=122; y=6; counter0=116; counter1=0
!(grid[6][123] == 0); grid[6][123 1] != 1 || 123 1 >= 150; x=123; y=6; counter0=0; counter1=1
grid[6][149] == 0; grid[6][149 1] != 0 || 149 1 >= 150; x=149; y=6; counter0=26; counter1=0
grid[6][150] == 0; grid[6][150 1] != 0 || 150 1 >= 150; x=150; y=6; counter0=1; counter1=0
grid[7][82] == 0; grid[7][82 1] != 0 || 82 1 >= 150; x=82; y=7; counter0=83; counter1=0
!(grid[7][83] == 0); grid[7][83 1] != 1 || 83 1 >= 150; x=83; y=7; counter0=0; counter1=1
grid[7][149] == 0; grid[7][149 1] != 0 || 149 1 >= 150; x=149; y=7; counter0=66; counter1=0
grid[7][150] == 0; grid[7][150 1] != 0 || 150 1 >= 150; x=150; y=7; counter0=1; counter1=0
grid[8][42] == 0; grid[8][42 1] != 0 || 42 1 >= 150; x=42; y=8; counter0=43; counter1=0
!(grid[8][43] == 0); grid[8][43 1] != 1 || 43 1 >= 150; x=43; y=8; counter0=0; counter1=1
grid[8][149] == 0; grid[8][149 1] != 0 || 149 1 >= 150; x=149; y=8; counter0=106; counter1=0
grid[8][150] == 0; grid[8][150 1] != 0 || 150 1 >= 150; x=150; y=8; counter0=1; counter1=0
grid[9][2] == 0; grid[9][2 1] != 0 || 2 1 >= 150; x=2; y=9; counter0=3; counter1=0
!(grid[9][3] == 0); grid[9][3 1] != 1 || 3 1 >= 150; x=3; y=9; counter0=0; counter1=1
grid[9][149] == 0; grid[9][149 1] != 0 || 149 1 >= 150; x=149; y=9; counter0=146; counter1=0
grid[9][150] == 0; grid[9][150 1] != 0 || 150 1 >= 150; x=150; y=9; counter0=1; counter1=0
grid[10][135] == 0; grid[10][135 1] != 0 || 135 1 >= 150; x=135; y=10; counter0=136; counter1=0
!(grid[10][136] == 0); grid[10][136 1] != 1 || 136 1 >= 150; x=136; y=10; counter0=0; counter1=1
grid[10][149] == 0; grid[10][149 1] != 0 || 149 1 >= 150; x=149; y=10; counter0=13; counter1=0
grid[10][150] == 0; grid[10][150 1] != 0 || 150 1 >= 150; x=150; y=10; counter0=1; counter1=0
grid[11][95] == 0; grid[11][95 1] != 0 || 95 1 >= 150; x=95; y=11; counter0=96; counter1=0
!(grid[11][96] == 0); grid[11][96 1] != 1 || 96 1 >= 150; x=96; y=11; counter0=0; counter1=1
grid[11][149] == 0; grid[11][149 1] != 0 || 149 1 >= 150; x=149; y=11; counter0=53; counter1=0
grid[11][150] == 0; grid[11][150 1] != 0 || 150 1 >= 150; x=150; y=11; counter0=1; counter1=0
grid[12][55] == 0; grid[12][55 1] != 0 || 55 1 >= 150; x=55; y=12; counter0=56; counter1=0
!(grid[12][56] == 0); grid[12][56 1] != 1 || 56 1 >= 150; x=56; y=12; counter0=0; counter1=1
grid[12][131] == 0; grid[12][131 1] != 0 || 131 1 >= 150; x=131; y=12; counter0=75; counter1=0
!(grid[12][132] == 0); grid[12][132 1] != 1 || 132 1 >= 150; x=132; y=12; counter0=0; counter1=1
grid[12][149] == 0; grid[12][149 1] != 0 || 149 1 >= 150; x=149; y=12; counter0=17; counter1=0
grid[12][150] == 0; grid[12][150 1] != 0 || 150 1 >= 150; x=150; y=12; counter0=1; counter1=0
grid[13][15] == 0; grid[13][15 1] != 0 || 15 1 >= 150; x=15; y=13; counter0=16; counter1=0
!(grid[13][16] == 0); grid[13][16 1] != 1 || 16 1 >= 150; x=16; y=13; counter0=0; counter1=1
grid[13][91] == 0; grid[13][91 1] != 0 || 91 1 >= 150; x=91; y=13; counter0=75; counter1=0
!(grid[13][92] == 0); grid[13][92 1] != 1 || 92 1 >= 150; x=92; y=13; counter0=0; counter1=1
grid[13][149] == 0; grid[13][149 1] != 0 || 149 1 >= 150; x=149; y=13; counter0=57; counter1=0
grid[13][150] == 0; grid[13][150 1] != 0 || 150 1 >= 150; x=150; y=13; counter0=1; counter1=0
grid[14][51] == 0; grid[14][51 1] != 0 || 51 1 >= 150; x=51; y=14; counter0=52; counter1=0
!(grid[14][52] == 0); grid[14][52 1] != 1 || 52 1 >= 150; x=52; y=14; counter0=0; counter1=1
grid[14][122] == 0; grid[14][122 1] != 0 || 122 1 >= 150; x=122; y=14; counter0=70; counter1=0
!(grid[14][123] == 0); grid[14][123 1] != 1 || 123 1 >= 150; x=123; y=14; counter0=0; counter1=1
grid[14][149] == 0; grid[14][149 1] != 0 || 149 1 >= 150; x=149; y=14; counter0=26; counter1=0
grid[14][150] == 0; grid[14][150 1] != 0 || 150 1 >= 150; x=150; y=14; counter0=1; counter1=0
grid[15][11] == 0; grid[15][11 1] != 0 || 11 1 >= 150; x=11; y=15; counter0=12; counter1=0
!(grid[15][12] == 0); grid[15][12 1] != 1 || 12 1 >= 150; x=12; y=15; counter0=0; counter1=1
grid[15][82] == 0; grid[15][82 1] != 0 || 82 1 >= 150; x=82; y=15; counter0=70; counter1=0
!(grid[15][83] == 0); grid[15][83 1] != 1 || 83 1 >= 150; x=83; y=15; counter0=0; counter1=1
grid[15][149] == 0; grid[15][149 1] != 0 || 149 1 >= 150; x=149; y=15; counter0=66; counter1=0
grid[15][150] == 0; grid[15][150 1] != 0 || 150 1 >= 150; x=150; y=15; counter0=1; counter1=0
grid[16][42] == 0; grid[16][42 1] != 0 || 42 1 >= 150; x=42; y=16; counter0=43; counter1=0
!(grid[16][43] == 0); grid[16][43 1] != 1 || 43 1 >= 150; x=43; y=16; counter0=0; counter1=1
grid[16][138] == 0; grid[16][138 1] != 0 || 138 1 >= 150; x=138; y=16; counter0=95; counter1=0
!(grid[16][139] == 0); grid[16][139 1] != 1 || 139 1 >= 150; x=139; y=16; counter0=0; counter1=1
grid[16][149] == 0; grid[16][149 1] != 0 || 149 1 >= 150; x=149; y=16; counter0=10; counter1=0
grid[16][150] == 0; grid[16][150 1] != 0 || 150 1 >= 150; x=150; y=16; counter0=1; counter1=0
grid[17][2] == 0; grid[17][2 1] != 0 || 2 1 >= 150; x=2; y=17; counter0=3; counter1=0
!(grid[17][3] == 0); grid[17][3 1] != 1 || 3 1 >= 150; x=3; y=17; counter0=0; counter1=1
grid[17][98] == 0; grid[17][98 1] != 0 || 98 1 >= 150; x=98; y=17; counter0=95; counter1=0
!(grid[17][99] == 0); grid[17][99 1] != 1 || 99 1 >= 150; x=99; y=17; counter0=0; counter1=1
grid[17][149] == 0; grid[17][149 1] != 0 || 149 1 >= 150; x=149; y=17; counter0=50; counter1=0
grid[17][150] == 0; grid[17][150 1] != 0 || 150 1 >= 150; x=150; y=17; counter0=1; counter1=0
grid[18][58] == 0; grid[18][58 1] != 0 || 58 1 >= 150; x=58; y=18; counter0=59; counter1=0
!(grid[18][59] == 0); grid[18][59 1] != 1 || 59 1 >= 150; x=59; y=18; counter0=0; counter1=1
grid[18][149] == 0; grid[18][149 1] != 0 || 149 1 >= 150; x=149; y=18; counter0=90; counter1=0
grid[18][150] == 0; grid[18][150 1] != 0 || 150 1 >= 150; x=150; y=18; counter0=1; counter1=0
grid[19][18] == 0; grid[19][18 1] != 0 || 18 1 >= 150; x=18; y=19; counter0=19; counter1=0
!(grid[19][19] == 0); grid[19][19 1] != 1 || 19 1 >= 150; x=19; y=19; counter0=0; counter1=1
grid[19][149] == 0; grid[19][149 1] != 0 || 149 1 >= 150; x=149; y=19; counter0=130; counter1=0
grid[19][150] == 0; grid[19][150 1] != 0 || 150 1 >= 150; x=150; y=19; counter0=1; counter1=0
grid[20][149] == 0; grid[20][149 1] != 0 || 149 1 >= 150; x=149; y=20; counter0=150; counter1=0
grid[20][150] == 0; grid[20][150 1] != 0 || 150 1 >= 150; x=150; y=20; counter0=1; counter1=0
grid[21][149] == 0; grid[21][149 1] != 0 || 149 1 >= 150; x=149; y=21; counter0=150; counter1=0
grid[21][150] == 0; grid[21][150 1] != 0 || 150 1 >= 150; x=150; y=21; counter0=1; counter1=0
grid[22][149] == 0; grid[22][149 1] != 0 || 149 1 >= 150; x=149; y=22; counter0=150; counter1=0
grid[22][150] == 0; grid[22][150 1] != 0 || 150 1 >= 150; x=150; y=22; counter0=1; counter1=0
grid[23][149] == 0; grid[23][149 1] != 0 || 149 1 >= 150; x=149; y=23; counter0=150; counter1=0
grid[23][150] == 0; grid[23][150 1] != 0 || 150 1 >= 150; x=150; y=23; counter0=1; counter1=0
grid[24][149] == 0; grid[24][149 1] != 0 || 149 1 >= 150; x=149; y=24; counter0=150; counter1=0
grid[24][150] == 0; grid[24][150 1] != 0 || 150 1 >= 150; x=150; y=24; counter0=1; counter1=0
grid[25][149] == 0; grid[25][149 1] != 0 || 149 1 >= 150; x=149; y=25; counter0=150; counter1=0
grid[25][150] == 0; grid[25][150 1] != 0 || 150 1 >= 150; x=150; y=25; counter0=1; counter1=0
grid[26][149] == 0; grid[26][149 1] != 0 || 149 1 >= 150; x=149; y=26; counter0=150; counter1=0
grid[26][150] == 0; grid[26][150 1] != 0 || 150 1 >= 150; x=150; y=26; counter0=1; counter1=0
grid[27][149] == 0; grid[27][149 1] != 0 || 149 1 >= 150; x=149; y=27; counter0=150; counter1=0
grid[27][150] == 0; grid[27][150 1] != 0 || 150 1 >= 150; x=150; y=27; counter0=1; counter1=0
grid[28][149] == 0; grid[28][149 1] != 0 || 149 1 >= 150; x=149; y=28; counter0=150; counter1=0
grid[28][150] == 0; grid[28][150 1] != 0 || 150 1 >= 150; x=150; y=28; counter0=1; counter1=0
grid[29][149] == 0; grid[29][149 1] != 0 || 149 1 >= 150; x=149; y=29; counter0=150; counter1=0
grid[29][150] == 0; grid[29][150 1] != 0 || 150 1 >= 150; x=150; y=29; counter0=1; counter1=0
grid[30][149] == 0; grid[30][149 1] != 0 || 149 1 >= 150; x=149; y=30; counter0=150; counter1=0
grid[30][150] == 0; grid[30][150 1] != 0 || 150 1 >= 150; x=150; y=30; counter0=1; counter1=0
grid[31][149] == 0; grid[31][149 1] != 0 || 149 1 >= 150; x=149; y=31; counter0=150; counter1=0
grid[31][150] == 0; grid[31][150 1] != 0 || 150 1 >= 150; x=150; y=31; counter0=1; counter1=0
grid[32][149] == 0; grid[32][149 1] != 0 || 149 1 >= 150; x=149; y=32; counter0=150; counter1=0
grid[32][150] == 0; grid[32][150 1] != 0 || 150 1 >= 150; x=150; y=32; counter0=1; counter1=0
grid[33][149] == 0; grid[33][149 1] != 0 || 149 1 >= 150; x=149; y=33; counter0=150; counter1=0
grid[33][150] == 0; grid[33][150 1] != 0 || 150 1 >= 150; x=150; y=33; counter0=1; counter1=0
grid[34][149] == 0; grid[34][149 1] != 0 || 149 1 >= 150; x=149; y=34; counter0=150; counter1=0
grid[34][150] == 0; grid[34][150 1] != 0 || 150 1 >= 150; x=150; y=34; counter0=1; counter1=0
grid[35][149] == 0; grid[35][149 1] != 0 || 149 1 >= 150; x=149; y=35; counter0=150; counter1=0
grid[35][150] == 0; grid[35][150 1] != 0 || 150 1 >= 150; x=150; y=35; counter0=1; counter1=0
grid[36][149] == 0; grid[36][149 1] != 0 || 149 1 >= 150; x=149; y=36; counter0=150; counter1=0
grid[36][150] == 0; grid[36][150 1] != 0 || 150 1 >= 150; x=150; y=36; counter0=1; counter1=0
grid[37][149] == 0; grid[37][149 1] != 0 || 149 1 >= 150; x=149; y=37; counter0=150; counter1=0
grid[37][150] == 0; grid[37][150 1] != 0 || 150 1 >= 150; x=150; y=37; counter0=1; counter1=0
grid[38][149] == 0; grid[38][149 1] != 0 || 149 1 >= 150; x=149; y=38; counter0=150; counter1=0
grid[38][150] == 0; grid[38][150 1] != 0 || 150 1 >= 150; x=150; y=38; counter0=1; counter1=0
grid[39][149] == 0; grid[39][149 1] != 0 || 149 1 >= 150; x=149; y=39; counter0=150; counter1=0
grid[39][150] == 0; grid[39][150 1] != 0 || 150 1 >= 150; x=150; y=39; counter0=1; counter1=0
grid[40][149] == 0; grid[40][149 1] != 0 || 149 1 >= 150; x=149; y=40; counter0=150; counter1=0
grid[40][150] == 0; grid[40][150 1] != 0 || 150 1 >= 150; x=150; y=40; counter0=1; counter1=0
Frame: 0.004000

In short, the conditions in your if statements are met this many times, so your function gets called that many times, printing that many strings.

My suggestion would be to rethink and simplify your logic. I can't really give any additional suggestions since I really don't understand what it is you're trying to accomplish. I don't know how many times you are expecting print_chars to be called. If you want it to only be called whenever grid[y][x] > 0, then why not use that as an if condition? The variables counter0, counter1, and counter2 are really confusing to me.

Additional comments:

  • If you never modify the standard output handle via SetStdHandle(), then you can 'cache' the return value of GetStdHandle(STD_OUTPUT_HANDLE) instead of calling the function over and over. HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); or similar.
  • You really shouldn't be comparing strings like that. A raw string like "RED" used in a double-equals comparison 'decays' into a pointer to the string. As @Some Programmer Dude has already said, the only reason this works is because the string literal you call the function with is byte-wise identical to the string literals you compare it against and the compiler is smart enough to resolve both instances to the same (likely read-only) memory addresses. The way you're going about this would, in my opinion, be much better served by an enum color { GREEN, RED, BLUE, WHITE, BLACK, YELLOW }; or something similar.
  • When you do if-else-if chains like if(color == "GREEN") { ... } else if(color == "RED") { ..., you could probably find a way to use a switch-case construct instead. Don't feel pressured to do this though, it's mostly stylistic.
  • When you malloc your buffer, you should do malloc(sizeof(char)*(size 1)). This isn't strictly necessary since sizeof(char) will always be 1 except in very weird circumstances, but I'm mentioning it since you're specifying sizeof(char).
  • If you don't mind upgrading to C99, you can inline the declaration int y, x; into the for loops like for(int y=0; ... and for(int x=0; .... Again, mostly a stylistic choice.
  • Symbols sizeX and sizeY are "macros" and are expanded at compile-time. You should consider declaring grid as int grid[sizeX][sizeY]; to make better use of these macros. Consider also renaming them SIZE_X and SIZE_Y — it seems like a majority of C/C programmers have macros be CAPSLOCKED_WITH_UNDERSCORES to better distinguish them from identifiers.
  • If you do color /? at cmd, it'll show you the hex codes that correspond to the colors in cmd's color palette. You can plug a two-digit hex code (just like the color command!) into SetConsoleTextAttribute like SetConsoleTextAttribute(conout_handle, 0xA0); to get bright green foreground on a black background. Do note also that more modern versions of cmd aren't really limited by a 16-entry color palette anymore. You can use Virtual Terminal escape sequences to specify colors too, and the \e[38;2;r;g;bm and \e[48;2;r;g;bm escapes support arbitrary r,g,b triples.
  • A word of warning: the deeper one gets into the problem of outputting formatted text, the more of a problem mixing C standard library functions and WinApi functions becomes. Especially if Unicode-support comes into the picture. I don't expect you to run into any terrible problems just messing with colors like this but just be aware if you start doing a bunch of nonstandard stuff via the WinApi then your C standard library functions might get unhappy.

CodePudding user response:

I believe I have solved the answer, you can just print the screen in layers, instead of printing everything we use the goTo() function to go wherever the value needs to be printed and just run the algorithm there. This avoids having to print all the 0's.

Code:

#include <stdio.h>
#include <time.h>
#include <windows.h>

#define sizeX 100
#define sizeY 20

int counter;

int grid[sizeY][sizeX];

void goTo(int x,int y){printf("%c[%d;           
  • Related