-1

I was planning to make a snake game on CMD by using arrays in C language, i've coded the parts of board creating and the game, it wasnt done totally yet, but i have to create also the snake and the food in the table.

I was trying to debug the program, and i've found out that rand() function doesnt work the second time i use my function, also it crashes when i try to call those functions more than once. I couldn't solve why.

int main()
{

int row,column;
create_snake(row,column,2);
create_snake(row,column,3);
}
void create_snake(int row,int column,int x){

srand(time(NULL));

row=rand()%25;
column=rand()%64;
}

here is the full code (not fully done yet but it crashes)

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

int board[25][64];

void create_snake(int,int,int);


int main()
{

int row,column;

for(int i=0;i<25;i++){   //creating board
        for(int a=0;a<64;a++){
              if(i == 0 || i == 24){
            board[i][a]=1;
            }else if(a == 0 || a==63){
            board[i][a]=1;
            }else{
                board[i][a]=0;
            }
        }
    }

create_snake(row,column,2); //creating snake



/*for(int i=0;i<25;i++){
        for(int a=0;a<64;a++){
        printf("%d",board[i][a]);
}
printf("\n");
}
*/
create_snake(row,column,3); //creating food

}



void create_snake(int row,int column,int x){

srand(time(NULL));

row=rand()%25;
column=rand()%64;

printf("%d   %d",row,column);
printf("\n");
/*if(board[row][column]==1){
  // create_snake(row,column,x);

}else if(board[row][column]==0){
    board[row][column]=x;

}else{
    //create_snake(row,column,x);
}
*/
}
v
  • 1
    you should call srand() exactly once in your application. time(NULL) may return the same number. for both calls. so you reset rand() to the same seed, resulting in the same number – Raildex Dec 31 '21 at 10:10
  • Put `srand(time(NULL));` first in `main()` and never call it again. – Ted Lyngmo Dec 31 '21 at 10:14
  • thanks, my recursive functions also crash the program the second time i call the function with the int 3, any clue why is that happening? – user9560064 Dec 31 '21 at 10:15
  • 1
    Do you have control over the recursion depth? How deep has it recursed when crashing? Do you need recursion at all? Can you show it in a [mre]? – Ted Lyngmo Dec 31 '21 at 10:17
  • i dont have control over recursion depth and i guess i may handle it with a while loop instead of recursion, though recursion seemed easier (program was checking array numbers and if they were different than 0, i called recursive to use rand again and again) yeah let me give a minimal reproducible example, gimme min – user9560064 Dec 31 '21 at 10:19
  • @TedLyngmo ``` void function(int row, int x){ row=rand()%25; if(board[row]==0){ board[row]=x; }else{ function(row,x) } } ``` – user9560064 Dec 31 '21 at 10:22
  • 1
    Put the code in the question. A [mre] means that I can copy the code and compile it (without having to add to or modify it) and get the same error as you get. – Ted Lyngmo Dec 31 '21 at 10:26
  • 1
    ah got it, but nevermind, rand() works fine right now after leaving only one srand, and also recursions doesnt crash now, so its solved, thanks to all of you – user9560064 Dec 31 '21 at 10:29
  • ˙create_snake` doesn't return any values. – stark Dec 31 '21 at 12:29

1 Answers1

1

Calling srand() multiple times can cause issues, depending on what you want to achieve. More details here. When using rand(), I personally learned it this way:

rand() % (25 + 1 - 0) + 0;
rand() % (64 + 1 - 0) + 0;

In your function declaration above the main function, write out the variable names, don't just type int.

pion
  • 34
  • 5
  • why does writing only int cause a problem? i've changed it and program doesnt crash anymore, but i guess if i dont write the name of the variable, the function expects a literaly integer number instead of variable? like 3? – user9560064 Dec 31 '21 at 11:19
  • pion, `rand() % (25 + 1 - 0) + 0;` is off-by-one for `int board[25][64];` That makes 26 different `row`. `row=rand()%25;` is fine. – chux - Reinstate Monica Dec 31 '21 at 11:19