So Im trying to code snake, and when I try to fix so that the food cannot spawn ontop of the snake the code works as I want it to. But the function wont return the value of the variable "bug" from function "bugCheck" and I cannot figure out why.
int snakeFuncs::food(int randX[2], int randY[2]) {
//declerations
bool bug = false;
do {
//gives variables random values 1-18
randX[0] = rand() % 17 + 2;
randY[0] = rand() % 17 + 2;
bugCheck(randX, randY, bug);
} while (bug == true);
//Places cursor on generated variables and prints food
gotoxy(randX[0], randY[0]);
cout << yellow << "*";
//return necessary variables
return randX[2], randY[2];
}//end of function
//Checks for potential bugs in the game and fixes them
int snakeFuncs::bugCheck(int randX[2], int randY[2], bool bug) {
//sets bool to true if food spawns on snake, loops as many times as snakesize
for (int i = 0; i < snakeSize; i++) {
if (randX[0] == snakeX[i] && randY[0] == snakeY[i]) {
//sets bool to true
bug = true;
}//end of statement
}//end of loop
//returns necessary variables
return bug;
}//end of function
How do you know that the function won't return the bool value? You're not saving or using the return value from bugCheck() so what do you expect to happen?
@imimoises I assumed if I returned bug with a new value it would automaticly get saved in the last function with the new value since I declared it on line 3? If thats not the case, than Im misstaken on how return works in functions. I recently started to use this, Ive only been using global variables, quite new to programing.
int snakeFuncs::food(int randX[2], int randY[2]) {
//declerations
bool bug = false;
do {
//gives variables random values 1-18
randX[0] = rand() % 17 + 2;
randY[0] = rand() % 17 + 2;
bugCheck(randX, randY, bug);
} while (bug == true);
//Places cursor on generated variables and prints food
gotoxy(randX[0], randY[0]);
cout << yellow << "*";
//return necessary variables
return randX[2], randY[2];
}//end of function
//Checks for potential bugs in the game and fixes them
int snakeFuncs::bugCheck(int randX[2], int randY[2], bool bug) {
//sets bool to true if food spawns on snake, loops as many times as snakesize
for (int i = 0; i < snakeSize; i++) {
if (randX[0] == snakeX[i] && randY[0] == snakeY[i]) {
//sets bool to true
bug = true;
}//end of statement
}//end of loop
//returns necessary variables
return bug;
}//end of function
Anyway, I tried it your way, but it still didnt work
//declerations
bool bug = false;
do {
//gives variables random values 1-18
randX[0] = rand() % 17 + 2;
randY[0] = rand() % 17 + 2;
c } while (bug == true);
Since you never change bug to true this loop will never end. Remember the parameter bug in bugCheck() is sent by value, meaning that any changes made in that function are lost when the function returns.
bugCheck() is designed to return an int, yet you never use that return value. I recommend changing the function to the following: bool bugCheck(int randX, int randY);
Then use the return value:
1 2 3
bug = bugCheck(randX, randY);
} while (bug == true);
Also there is no reason to pass bug to the function, just create a bool variable local to the check() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//Checks for potential bugs in the game and fixes them
bool snakeFuncs::bugCheck(int randX[2], int randY[2], bool bug) {
//sets bool to true if food spawns on snake, loops as many times as snakesize
bool bug = false;
for (int i = 0; i < snakeSize; i++) {
if (randX[0] == snakeX[i] && randY[0] == snakeY[i]) {
//sets bool to true
bug = true;
}//end of statement
}//end of loop
//returns necessary variables
return bug;
}//end of function
Also remember you can only return one value from a function using the return statement.