Thread: strange behavior of arrays in loop

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    strange behavior of arrays in loop

    I am solving interesting yet easy program.

    the program works almost fine but it has strange bug that occur and im lost cuz i have no idea why it happens
    I have mine field that spread mines , o is for empty space , " . " is for space where cannot be placed mine and ! is where "miner is" , he spread bombs in this direction

    o o o o
    o . ! o
    o o o o

    he would spred mines around him if is possible so in this case it would spread like this

    o o ! o
    o . x !
    o o ! o i put the x on the original place of miner , the spreading continue till every place where mine can reach is mined. But i do not know when and why somewhere it code it just breaks and the "miner" does not spread mine in the direction he should.

    First in my code i find the position of miner/s i store their x and y position in
    indexX[];
    indexY[];
    this arrays have same length of items , so i have variable index which incerement every time something should be added.
    then i loop throught arrays and if i find match ( ! ) i check if surrounding can be mined if yes add it there , everything works but somehow sometimes it just broke and i do not know why Here is paste bin [C] #include <stdio.h> #include <stdlib.h> int main() { int days=0; i - Pastebin.com as you can see in 3rd and 5th mine field , it spreaded not how ist supposed to be , how can i fix it?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int days=0;
        int i;
        int j;
        int indexX[15];
        int indexY[15];
        int index=0;
        int x=4;
        int y=4;
        int pocet_nakazenych=0;
        char nakaza[4][4]={{'o','o','o','o'},{'o','.','o','o'},{'o','.','.','o'},{'!','o','.','o'}};
        int tmp=0;
        int changes=0;
        int ass=0;
        for(i=0;i<x;i++){
            for(j=0;j<y;j++){
                if(nakaza[i][j]=='!'){
                    nakaza[i][j]='x';
                    indexX[index]=i;
                    indexY[index]=j;
                    index++;
                    pocet_nakazenych++;
                }
    
            }
        }
        tmp=index;
        do{
            for(i=0;i<x;i++){
                for(j=0;j<y;j++){
                    if(nakaza[i][j]=='!'){
                        nakaza[i][j]='x';
                    }
    
                }
            }
    
        changes=0;
        for(i=0;i<index;i++){
            if(!(indexX[i]+1>x-1)){
                    if( nakaza[indexX[i]+1][indexY[i]]!='.' &&  nakaza[indexX[i]+1][indexY[i]]!='x' && nakaza[indexX[i]+1][indexY[i]]!='!'){
                        nakaza[indexX[i]+1][indexY[i]]='!';
                        pocet_nakazenych++;
                        indexX[tmp]=indexX[i]+1;
                        indexY[tmp]=indexY[i];
                        printf("Nakaza %d:%d xhore\n",indexX[tmp],indexY[tmp]);
                        printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
                        tmp++;
                        changes++;
    
                    }
            }
            if(!(indexX[i]-1<0)){
                    if( nakaza[indexX[i]-1][indexY[i]]!='.' &&  nakaza[indexX[i]-1][indexY[i]]!='x' &&  nakaza[indexX[i]-1][indexY[i]]!='!'){
                        nakaza[indexX[i]-1][indexY[i]]='!';
                        pocet_nakazenych++;
                        indexX[tmp]=indexX[i]-1;
                        indexY[tmp]=indexY[i];
                        printf("Nakaza %d:%d xdole\n",indexX[tmp],indexY[tmp]);
                        printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
                        tmp++;
                        changes++;
    
                    }
            }
            if(!(indexY[i]+1>y-1)){
                    if( nakaza[indexX[i]][indexY[i]+1]!='.' &&  nakaza[indexX[i]][indexY[i]+1]!='x' &&  nakaza[indexX[i]][indexY[i]+1]!='!'){
                        nakaza[indexX[i]][indexY[i]+1]='!';
                        pocet_nakazenych++;
                        indexX[tmp]=indexX[i];
                        indexY[tmp]=indexY[i]+1;
                        printf("Nakaza %d:%d ydoprava\n",indexX[tmp],indexY[tmp]);
                        printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
                        tmp++;
                        changes++;
    
                    }
            }
            if(!(indexY[i]-1<0)){
                    if( nakaza[indexX[i]][indexY[i]-1]!='.' &&  nakaza[indexX[i]][indexY[i]-1]!='x' &&  nakaza[indexX[i]][indexY[i]-1]!='!'){
                        nakaza[indexX[i]][indexY[i]-1]='!';
    
                        pocet_nakazenych++;
                        indexX[tmp]=indexX[i];
                        indexY[tmp]=indexY[i]-1;
                        printf("Nakaza %d:%d ydolava\n",indexX[tmp],indexY[tmp]);
                         printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
                        tmp++;
                        changes++;
    
                    }
            }
        }
    
        index=tmp;
    
        tmp=0;
        if (changes!=0){
            days++;
    
        }else{
            break;
        }
        for(i=0;i<x;i++){
            for(j=0;j<y;j++){
               printf("%c ",nakaza[i][j]);
            }
            printf("\n");
        }
        printf("days = %d\n",days);
        }
        while(changes);
        for(i=0;i<x;i++){
            for(j=0;j<y;j++){
               printf("%c ",nakaza[i][j]);
            }
            printf("\n");
        }
    
    
        printf("dni = %d\npocet nakazenych = %d",days,pocet_nakazenych);
    
    
    }
    Last edited by Salem; 11-21-2015 at 05:02 PM. Reason: inlined code for posterity

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I wish I understood the rules for this game, but I don't

    Edit: Even though I don't understand the rules I do think that your program can be simplified (or at least made easier to read). Have you learned about functions yet?
    Last edited by Hodor; 11-22-2015 at 01:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help, please - very strange behavior
    By snork in forum C Programming
    Replies: 16
    Last Post: 09-26-2011, 01:36 AM
  2. Strange behavior
    By onako in forum C++ Programming
    Replies: 1
    Last Post: 05-01-2010, 07:00 AM
  3. Replies: 3
    Last Post: 05-01-2010, 06:08 AM
  4. strange std::map behavior
    By manav in forum C++ Programming
    Replies: 63
    Last Post: 04-11-2008, 08:00 AM
  5. strange behavior
    By agarwaga in forum C Programming
    Replies: 1
    Last Post: 10-17-2005, 12:03 PM

Tags for this Thread