I would suggest that you rewrite your code and solve the problem without recursion - as it is not needed here.
If I understood you right, you a generating tokens at randomized positions and check if the snake (head or tail) is already occupying this location.
Here is how I would try to solve your task
- Generate a random token
- Check if head or tail positions match the tokens position
- if yes, re-generate the token (while-loop?)
- if no, you are finished
hmm another question: are your checks correct? What would you check to see if head and token are at the same position in x/y-ccordinates? At the moment you are checking if either the x-coordinate is the same (head and token are on the same row) or the y-coordinate. But both must match, or? So I would replace the conditional or ||
by a conditional and &&
.