Why is my output shifted after I listened for input?

35 Views Asked by At

I figured out that this isn't problem with my terminal, because it does happen in vscode terminal as well. (I'm complete new to c and I don't really know where the problem is)

0000010000000000000000000
                     0000000000000000000000000
 0000000000000000000000000
                          0000000000000000000000000
      0000000000000000000000000
                               0000000000000000000000000
           0000000000000000000000000
                                    0000000000000000000000000
                0000000000000000000000000
                                        0000000000000000000000000

It is two dimensional array of zeros and one one

This is my code until I run into that infinite loop it shifts but until that it works normally.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>

int main() {
int rows, colls;
rows = 10;
colls = 25;
int dysplay[rows][colls];
int grid[rows][colls];
int marked[rows*colls];
int x_axis, y_axis;
x_axis = 0;
y_axis = 0;
srand(time(0));

//setting grid of mines and 
for (size_t i = 0; i < rows; i++)
{
    for (size_t j = 0; j < colls; j++)
    {
        //get random nuber betwene 1 and 0
        int num = rand() % 2;
        //give the coordinates value of random number
        grid[i][j] = num;
        dysplay[i][j] = 0; 
        //print numbers
        printf("%d", num);
    }
    printf("\n");
}

while (1)
{
    initscr();              // Initialize ncurses
    cbreak();              // Disable line buffering and character echoing
    keypad(stdscr, TRUE);  // Enable keypad function keys (e.g., function keys, arrow keys)
    noecho();              // Disable character echoing
    refresh();             // Refresh the screen

    char con = getch();      // Wait for a keypress
    if (con == 'd')
    {
        x_axis = x_axis + 1;
        dysplay[y_axis][x_axis] = 1;
        dysplay[y_axis][x_axis-1] = 0;
    }
    system("clear");
    for (size_t i = 0; i < rows; i++)
    {
        for (size_t j = 0; j < colls; j++)
        {
            printf("%d", dysplay[i][j]);
        }
        printf("\n");
    }
}
return 0;

}

I compile it like this

gcc ./main.c -o ./main -Wall -Wextra -g3 -lncurses
0

There are 0 best solutions below