0

Task is to create a flood-it game in a 20x60 game, starting in the upper left corner. Numbers are used instead of colours for ease. So, the code seems to be working fine up to one point. But after a certain amount of rounds, player plays and then it just crashes ("...stopped working") message. Here is the code:

#include <stdio.h>
#include <stdlib.h>
int k=0,l=0;
void BoardMaking(int A[20][60]){
    int i,j;

    srand(time(0));
    for(i=0;i<20;i++){
        for(j=0;j<60;j++){
            A[i][j]=rand()%5 +1;    
        }
    }

}

void Print_Board(int A[20][60]){
    int i,j;
    for(i=0;i<20;i++){
        for(j=0;j<60;j++){
            printf("%d",A[i][j]);

        }
        printf("\n");
    }   

}

int Player(){
    int x;
    printf("\ngive a number between 1-5\n");
    scanf("%d",&x);  

    return x;
}

void Change(int A[20][60],int y,int x){
    A[k][l]=x;
    if(A[k][l+1]==y){
        A[k][l+1]=x;
        l++;
        Change(A,y,x);
    }
    if(A[k][l-1]==y){
        A[k][l-1]=x;
        l--;
        Change(A,y,x);
    }
    if(A[k-1][l]==y){
        A[k-1][l]=x;
        k--;
        Change(A,y,x);
    }
    if(A[k+1][l]==y){
        A[k+1][l]=x;
        k++;
        Change(A,y,x);
    }

    k=0;l=0;
}

int main(){
    int y,x;

    int A[20][60];
    BoardMaking(A);
    while(1){

        Print_Board(A);
        x=Player();
        if (x==A[0][0]){
            printf("give another number \n");
        }
        else if (x!=A[0][0])
        {
        y=A[0][0];
        Change(A,y,x);  
        }


    }

    return 0;
}
Konzo
  • 37
  • 7

1 Answers1

1

There is no bound checking in your Change function.

When k = 0 and l = 0 this condition (on line 50) if(A[k-1][l]==y){ accesses A[-1][0] which is outside of allocated memory.

This is how you can check for illegal values of k and l. (I'm not completely sure your code works properly but it does not crash anymore with following changes.)

void Change(int A[20][60],int y,int x){ 
    A[k][l]=x;
    if(l < 59 && A[k][l+1]==y){
        A[k][l+1]=x;
        l++;
        Change(A,y,x);
    }   
    if(l > 0 && A[k][l-1]==y){
        A[k][l-1]=x;
        l--;
        Change(A,y,x);
    }   
    if(k > 0 && A[k-1][l]==y){
        A[k-1][l]=x;
        k--;
        Change(A,y,x);
    }    
    if(k < 19 && A[k+1][l]==y){
        A[k+1][l]=x;
        k++;
        Change(A,y,x);
    }   

    k=0;l=0;
}

Thanks, @RuudHelderman for suggesting bound checking for right and bottom border.

curusarn
  • 403
  • 4
  • 11
  • @curusam that seems to be it! thanks man:) so two if conditions for k!=0 and l != 0 for their cases will do it i guess – Konzo May 19 '18 at 09:16
  • @Konzo I have compiled your code with debugging flag like this `gcc -g floodgame.c`. I have used Valgrind (memory checking tool) to find the problem. – curusarn May 19 '18 at 09:21
  • Valgrind quick tutorial: http://valgrind.org/docs/manual/quick-start.html – curusarn May 19 '18 at 09:21
  • @curusam I did it just like the code above now, seems to be working all fine now. thanks for the valgrind tut, gonna be checking it out – Konzo May 19 '18 at 09:22
  • 1
    @Konzo This only covers the left and top border; you need similar bounds checking at the right and bottom border. An alternative approach is to expand the board with a border prefilled with a dummy color that does not match any of the game colors. – Ruud Helderman May 19 '18 at 09:26
  • 1
    @Konzo I have updated the answer with bound checking for right and bottom border. – curusarn May 19 '18 at 09:59