Home > Software design >  Why is my falling array value disappearing?
Why is my falling array value disappearing?

Time:08-12

I have a 2D array in C and I am trying to make the positive or 1 value fall down till it reaches the bottom of the array but for some reason as soon as I run the code the 1 goes to the left and then down. The way it is meant to work is the current value is turned to 0 and the bottom value turns into a 1, this is repeated and gives the effect of falling straight down, the code for this is in the fallDown() function.

code:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#ifdef __unix__
# include <unistd.h>
#elif defined _WIN32
# include <windows.h>
#define sleep(x) Sleep(1000 * (x))
#endif

int sizeX = 20;
int sizeY = 20;

int grid[20][20] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

int fallDown(int grid[sizeX][sizeY], int x, int y){
    grid[x][y]=0;
 grid[x][y 1]=1;
}

int main(void){
 int neighbour_count[sizeX][sizeY];
    int x,y,iterations;

 for(iterations=0;iterations<500;iterations  ){
  system("cls"); //Clear screen
  for(x=0;x<sizeX;x  ){
    printf("\n");
    for(y=0;y<sizeY;y  ){
     if(grid[x][y]==1){
      printf("@");
             }
    else{
      printf(" ");
    }
         }
  }
  for(y=0;y<sizeY;y  ){
    for(x=0;x<sizeX;x  ){
        if(grid[x][y] == 1){
         fallDown(grid, x, y);
    }
   }
        }
  printf("\n");
  sleep(1);
 }
}

CodePudding user response:

int fallDown(int grid[sizeX][sizeY], int x, int y){
    grid[x][y]=0;
 grid[x][y 1]=1;
}

sizex is number of row and sizeY is for column

So you should change fallDown function like this

int fallDown(int grid[sizeX][sizeY], int x, int y){
      if(x == 19 || grid[x][y] == 0) return;
      grid[x 1][y] = 1;
      grid[x][y] = 0;
}

      

CodePudding user response:

x and y need to be swapped in fallDown.
In the second set of loops in main when a 1 is found, the loop must stop or the next execution of the x loop will fallDown again and again and again... until the loop finishes.
I am using Linux and had to use clear to clear the screen.

#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>

int sizeX = 20;
int sizeY = 20;

int grid[20][20] = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                    {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

void fallDown(int grid[sizeX][sizeY], int x, int y){
    grid[x][y]=0;
      x;
    if ( x == 20) {
        x = 0; // back to top
    }
    grid[x][y]=1;
}

int main(void){
    int x,y,iterations;

    for(iterations=0;iterations<500;iterations  ){
        system("clear"); //Clear screen
        for(x=0;x<sizeX;x  ){
            printf("\n");
            for(y=0;y<sizeY;y  ){
                if(grid[x][y]==1){
                    printf("@");
                }
                else{
                    printf(" ");
                }
            }
        }
        for(y=0;y<sizeY;y  ){
            for(x=0;x<sizeX;x  ){
                if(grid[x][y] == 1){
                    fallDown(grid, x, y);
                    y = sizeY;
                    break;
                }
            }
        }
        printf("\n");
        sleep(1);
    }
}

CodePudding user response:

Another approach is to use terminal escape codes.
(Provided the terminal supports them.)
This wraps the movement so if the @ goes beyond the edge, it appears on the opposite edge. Not sure if this is what is wanted?

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

#define MAXROW 20
#define MAXCOL 20

typedef struct at_s {
    int row;
    int col;
} at_t;

void moveat ( at_t *at, int ud, int rl) {
    // moves up down right left diagonal
    at->row  = ud;
    if ( at->row > MAXROW) {
        at->row = 1;
    }
    if ( at->row < 1) {
        at->row = MAXROW;
    }
    at->col  = rl;
    if ( at->col > MAXCOL) {
        at->col = 1;
    }
    if ( at->col < 1) {
        at->col = MAXCOL;
    }
}

void showat ( at_t at, int show) {
    printf ( "\033[%d;%dH%c", at.row, at.col, show);
    fflush ( stdout);
}

int main ( void) {
    int chgrow = 0;
    int chgcol = 0;
    at_t at = { 4, 12};

    srand ( time ( NULL));
    printf ( "\033[2J");//clear screen

    for ( int moving = 0; moving < 500;   moving) {
        showat ( at, ' ');
        chgrow = rand ( ) % 3; // range of 0 1 or 2
        --chgrow; // range is now -1 0 or 1

        chgcol = rand ( ) % 3;
        --chgcol;
        moveat ( &at, chgrow, chgcol);
        showat ( at, '@');
        printf ( "\033[22;1H\n");
        sleep ( 1);
    }

    printf ( "\033[22;1H\n");

    return 0;
}
  • Related